Commit 8c5d5b43 authored by George Hotz's avatar George Hotz

much simpler oracle

parent e805ff1e
package state
import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
......@@ -55,7 +54,12 @@ func (db *Database) OpenTrie(root common.Hash) (Trie, error) {
// OpenStorageTrie opens the storage trie of an account.
func (db *Database) OpenStorageTrie(addrHash, root common.Hash) (Trie, error) {
return SimpleTrie{db.BlockNumber, root, true, addrHash}, nil
//return SimpleTrie{db.BlockNumber, root, true, addrHash}, nil
tr, err := trie.NewSecure(root, db.db)
if err != nil {
return nil, err
}
return tr, nil
}
type Trie interface {
......@@ -83,38 +87,6 @@ type Trie interface {
Commit(onleaf trie.LeafCallback) (common.Hash, error)
}
type SimpleTrie struct {
BlockNumber *big.Int
Root common.Hash
Storage bool
AddressHash common.Hash
}
func (trie SimpleTrie) Commit(onleaf trie.LeafCallback) (common.Hash, error) {
fmt.Println("trie.Commit")
return trie.Root, nil
}
func (trie SimpleTrie) Hash() common.Hash {
fmt.Println("trie.Hash")
return trie.Root
}
func (trie SimpleTrie) TryUpdate(key, value []byte) error {
fmt.Println("trie.TryUpdate")
return nil
}
func (trie SimpleTrie) TryDelete(key []byte) error {
fmt.Println("trie.TryDelete")
return nil
}
func (trie SimpleTrie) TryGet(key []byte) ([]byte, error) {
enc := oracle.GetProvedStorage(trie.BlockNumber, trie.AddressHash, trie.Root, common.BytesToHash(key))
return enc.Bytes(), nil
}
// stubbed: we don't prefetch
type triePrefetcher struct {
......
......@@ -26,6 +26,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/oracle"
"github.com/ethereum/go-ethereum/rlp"
)
......@@ -247,6 +248,7 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
if metrics.EnabledExpensive {
meter = &s.db.StorageReads
}
oracle.PrefetchAddress(db.BlockNumber, s.address, key)
if enc, err = s.getTrie(db).TryGet(key.Bytes()); err != nil {
s.setError(err)
return common.Hash{}
......
......@@ -513,7 +513,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject {
if metrics.EnabledExpensive {
defer func(start time.Time) { s.AccountReads += time.Since(start) }(time.Now())
}
oracle.PrefetchAddress(s.db.BlockNumber, addr)
oracle.PrefetchAddress(s.db.BlockNumber, addr, common.Hash{})
enc, err := s.trie.TryGet(addr.Bytes())
if err != nil {
s.setError(fmt.Errorf("getDeleteStateObject (%x) error: %v", addr.Bytes(), err))
......
......@@ -14,7 +14,6 @@ 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 {
......@@ -102,8 +101,8 @@ func Preimage(hash common.Hash) []byte {
return val
}
func PrefetchAddress(blockNumber *big.Int, addr common.Address) {
ap := GetProofAccount(blockNumber, addr)
func PrefetchAddress(blockNumber *big.Int, addr common.Address, skey common.Hash) {
ap := GetProofAccount(blockNumber, addr, skey)
for _, s := range ap {
ret, _ := hex.DecodeString(s[2:])
hash := crypto.Keccak256Hash(ret)
......@@ -111,8 +110,8 @@ func PrefetchAddress(blockNumber *big.Int, addr common.Address) {
}
}
func GetProofAccount(blockNumber *big.Int, addr common.Address) []string {
key := fmt.Sprintf("proof_%d_%s", blockNumber, addr)
func GetProofAccount(blockNumber *big.Int, addr common.Address, skey common.Hash) []string {
key := fmt.Sprintf("proof_%d_%s_%s", blockNumber, addr, skey)
addrHash := crypto.Keccak256Hash(addr[:])
unhashMap[addrHash] = addr
......@@ -121,31 +120,7 @@ func GetProofAccount(blockNumber *big.Int, addr common.Address) []string {
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())
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)
cacheWrite(key, []byte(strings.Join(jr.Result.AccountProof, "\n")))
}
return strings.Split(string(cacheRead(key)), "\n")
}
func GetProvedAccountBytes(blockNumber *big.Int, stateRoot common.Hash, addr common.Address) []byte {
fmt.Println("ORACLE GetProvedAccountBytes:", blockNumber, stateRoot, addr)
key := fmt.Sprintf("accounts_%d_%s", blockNumber, addr)
addrHash := crypto.Keccak256Hash(addr[:])
unhashMap[addrHash] = addr
if !cacheExists(key) {
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[1] = [1]common.Hash{skey}
r.Params[2] = fmt.Sprintf("0x%x", blockNumber.Int64())
jsonData, _ := json.Marshal(r)
resp, _ := http.Post(nodeUrl, "application/json", bytes.NewBuffer(jsonData))
......@@ -153,23 +128,12 @@ func GetProvedAccountBytes(blockNumber *big.Int, stateRoot common.Hash, addr com
jr := jsonresp{}
json.NewDecoder(resp.Body).Decode(&jr)
// TODO: check proof
account := Account{
Nonce: uint64(jr.Result.Nonce),
Balance: jr.Result.Balance.ToInt(),
Root: jr.Result.StorageHash,
CodeHash: jr.Result.CodeHash.Bytes(),
}
/*fmt.Println(string(jsonData))
fmt.Println(resp)
fmt.Println(jr)*/
arr := jr.Result.AccountProof
arr = append(arr, jr.Result.StorageProof[0].Proof...)
ret, _ := rlp.EncodeToBytes(account)
cacheWrite(key, ret)
cacheWrite(key, []byte(strings.Join(arr, "\n")))
}
return cacheRead(key)
return strings.Split(string(cacheRead(key)), "\n")
}
func GetProvedCodeBytes(blockNumber *big.Int, addrHash common.Hash, codehash common.Hash) []byte {
......@@ -207,35 +171,3 @@ func GetProvedCodeBytes(blockNumber *big.Int, addrHash common.Hash, codehash com
return cacheRead(key)
}
func GetProvedStorage(blockNumber *big.Int, addrHash common.Hash, root common.Hash, skey common.Hash) common.Hash {
addr := unhash(addrHash)
key := fmt.Sprintf("storage_%d_%s_%s_%s", blockNumber, addr, root, skey)
if !cacheExists(key) {
r := jsonreq{Jsonrpc: "2.0", Method: "eth_getProof", Id: 1}
r.Params = make([]interface{}, 3)
r.Params[0] = addr
r.Params[1] = [1]common.Hash{skey}
r.Params[2] = fmt.Sprintf("0x%x", blockNumber.Int64())
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)
//fmt.Println(string(jsonData))
/*fmt.Println(resp)*/
// TODO: check proof
val := jr.Result.StorageProof[0].Value
ret := common.HexToHash(val.String())
//fmt.Println(ret)
//ret, _ := rlp.EncodeToBytes(account)
cacheWrite(key, ret.Bytes())
}
ret := common.BytesToHash(cacheRead(key))
fmt.Println("ORACLE GetProvedStorage:", blockNumber, addr, root, skey, ret)
return ret
}
......@@ -65,7 +65,7 @@ func NewDatabase(header types.Header) Database {
triedb := Database{BlockNumber: header.Number, Root: header.Root}
triedb.preimages = make(map[common.Hash][]byte)
fmt.Println("init database")
oracle.PrefetchAddress(header.Number, common.Address{})
oracle.PrefetchAddress(header.Number, common.Address{}, common.Hash{})
//panic("preseed")
return triedb
......
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