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

we are running code

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