Commit 88fdcd14 authored by George Hotz's avatar George Hotz

we are running code

parent 8a645640
......@@ -20,6 +20,6 @@ type Account struct {
// TODO: oracle for
// GetProvedAccountBytes(blockNumber, stateRoot, addr) -> rlpAccount
// GetProvedCodeBytes(blockNumber, stateRoot, addr, codehash) -> []byte
// GetProvedCodeBytes(blockNumber, addr, codehash) -> []byte
// GetProvedStorage(blockNumber, stateRoot, addr, root, key) -> uint256
// These functions access the backend oracle, and will assert if the proof is bad
package state
import "github.com/ethereum/go-ethereum/common"
import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/oracle"
)
// TODO: add oracle calls here
// wrapper for the oracle
type Database struct {
BlockNumber *big.Int
}
// ContractCode retrieves a particular contract's code.
func (db *Database) ContractCode(addrHash, codeHash common.Hash) ([]byte, error) {
return nil, nil
func (db *Database) ContractCode(addr common.Address, codeHash common.Hash) ([]byte, error) {
code := oracle.GetProvedCodeBytes(db.BlockNumber, addr, codeHash)
return code, nil
}
// ContractCodeSize retrieves a particular contracts code's size.
func (db *Database) ContractCodeSize(addrHash, codeHash common.Hash) (int, error) {
return 0, nil
func (db *Database) ContractCodeSize(addr common.Address, codeHash common.Hash) (int, error) {
code := oracle.GetProvedCodeBytes(db.BlockNumber, addr, codeHash)
return len(code), nil
}
......@@ -272,7 +272,7 @@ func (s *stateObject) Code(db Database) []byte {
if bytes.Equal(s.CodeHash(), emptyCodeHash) {
return nil
}
code, err := db.ContractCode(s.addrHash, common.BytesToHash(s.CodeHash()))
code, err := db.ContractCode(s.address, common.BytesToHash(s.CodeHash()))
if err != nil {
s.setError(fmt.Errorf("can't load code hash %x: %v", s.CodeHash(), err))
}
......@@ -290,7 +290,7 @@ func (s *stateObject) CodeSize(db Database) int {
if bytes.Equal(s.CodeHash(), emptyCodeHash) {
return 0
}
size, err := db.ContractCodeSize(s.addrHash, common.BytesToHash(s.CodeHash()))
size, err := db.ContractCodeSize(s.address, common.BytesToHash(s.CodeHash()))
if err != nil {
s.setError(fmt.Errorf("can't load code size %x: %v", s.CodeHash(), err))
}
......
......@@ -43,6 +43,8 @@ func NewStateDB(header types.Header) *StateDB {
blockNumber: header.Number,
stateObjects: make(map[common.Address]*stateObject),
stateRoot: header.Root,
db: Database{BlockNumber: header.Number},
accessList: newAccessList(),
}
}
......
......@@ -2,6 +2,7 @@ package oracle
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
......@@ -11,13 +12,14 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
)
type jsonreq struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params [3]interface{} `json:"params"`
Params []interface{} `json:"params"`
Id uint64 `json:"id"`
}
......@@ -27,6 +29,12 @@ type jsonresp struct {
Result AccountResult `json:"result"`
}
type jsonresps struct {
Jsonrpc string `json:"jsonrpc"`
Id uint64 `json:"id"`
Result string `json:"result"`
}
// Result structs for GetProof
type AccountResult struct {
Address common.Address `json:"address"`
......@@ -69,14 +77,13 @@ func GetProvedAccountBytes(blockNumber *big.Int, stateRoot common.Hash, addr com
}
r := jsonreq{Jsonrpc: "2.0", Method: "eth_getProof", Id: 1}
r.Params = make([]interface{}, 3)
r.Params[0] = addr
r.Params[1] = []common.Hash{}
r.Params[2] = fmt.Sprintf("0x%x", blockNumber.Int64()-1)
jsonData, _ := json.Marshal(r)
resp, _ := http.Post(nodeUrl, "application/json", bytes.NewBuffer(jsonData))
defer resp.Body.Close()
jr := jsonresp{}
json.NewDecoder(resp.Body).Decode(&jr)
......@@ -96,3 +103,45 @@ func GetProvedAccountBytes(blockNumber *big.Int, stateRoot common.Hash, addr com
os.WriteFile(cachePath, ret, 0644)
return ret
}
func GetProvedCodeBytes(blockNumber *big.Int, addr common.Address, codehash common.Hash) []byte {
fmt.Println("ORACLE GetProvedCodeBytes:", blockNumber, addr, codehash)
cachePath := fmt.Sprintf("data/code_%s", codehash)
// read cache if we can
{
dat, err := ioutil.ReadFile(cachePath)
if err == nil {
return dat
}
}
r := jsonreq{Jsonrpc: "2.0", Method: "eth_getCode", Id: 1}
r.Params = make([]interface{}, 2)
r.Params[0] = addr
r.Params[1] = fmt.Sprintf("0x%x", blockNumber.Int64()-1)
jsonData, _ := json.Marshal(r)
//fmt.Println(string(jsonData))
resp, _ := http.Post(nodeUrl, "application/json", bytes.NewBuffer(jsonData))
defer resp.Body.Close()
/*tmp, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(tmp))*/
jr := jsonresps{}
json.NewDecoder(resp.Body).Decode(&jr)
//fmt.Println(jr.Result)
// curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getCode","params":["0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b", "0x2"],"id":1}'
ret, _ := hex.DecodeString(jr.Result[2:])
//fmt.Println(ret)
if crypto.Keccak256Hash(ret) != codehash {
panic("wrong code hash")
}
os.WriteFile(cachePath, ret, 0644)
return ret
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment