Commit b7e2e8b6 authored by George Hotz's avatar George Hotz

move prefetch into oracle

parent 20c6eba6
......@@ -9,6 +9,7 @@ import (
"math/big"
"net/http"
"os"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
......@@ -91,21 +92,46 @@ func unhash(addrHash common.Hash) common.Address {
return unhashMap[addrHash]
}
func GetProofAccount(blockNumber *big.Int, stateRoot common.Hash, addr common.Address) []string {
var preimages = make(map[common.Hash][]byte)
func Preimage(hash common.Hash) []byte {
val, ok := preimages[hash]
if !ok {
panic("preimage missing")
}
return val
}
func PrefetchAddress(blockNumber *big.Int, addr common.Address) {
ap := GetProofAccount(blockNumber, addr)
for _, s := range ap {
ret, _ := hex.DecodeString(s[2:])
hash := crypto.Keccak256Hash(ret)
preimages[hash] = ret
}
}
func GetProofAccount(blockNumber *big.Int, addr common.Address) []string {
key := fmt.Sprintf("proof_%d_%s", blockNumber, addr)
addrHash := crypto.Keccak256Hash(addr[:])
unhashMap[addrHash] = addr
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)
return jr.Result.AccountProof
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[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 {
......
package trie
import (
"encoding/hex"
"fmt"
"io"
"math/big"
......@@ -9,7 +8,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/oracle"
)
......@@ -30,7 +28,6 @@ type Database struct {
BlockNumber *big.Int
Root common.Hash
lock sync.RWMutex
lookup map[common.Hash][]byte
preimages map[common.Hash][]byte // Preimages of nodes from the secure trie
preimagesSize common.StorageSize // Storage size of the preimages cache
......@@ -64,25 +61,11 @@ func (db *Database) preimage(hash common.Hash) []byte {
return db.preimages[hash]
}
func (db *Database) Fetch(addr common.Address) {
fmt.Println("prefetch", addr)
ap := oracle.GetProofAccount(db.BlockNumber, db.Root, addr)
for i, s := range ap {
ret, _ := hex.DecodeString(s[2:])
hash := crypto.Keccak256Hash(ret)
db.lookup[hash] = ret
fmt.Println(i, hash)
}
}
func NewDatabase(header types.Header) Database {
triedb := Database{BlockNumber: header.Number, Root: header.Root}
triedb.lookup = make(map[common.Hash][]byte)
triedb.preimages = make(map[common.Hash][]byte)
fmt.Println("init database")
// fetch the root node
triedb.Fetch(common.Address{})
oracle.PrefetchAddress(header.Number, common.Address{})
//panic("preseed")
return triedb
......@@ -91,27 +74,15 @@ func NewDatabase(header types.Header) Database {
// Node retrieves an encoded cached trie node from memory. If it cannot be found
// cached, the method queries the persistent database for the content.
func (db *Database) Node(hash common.Hash) ([]byte, error) {
fmt.Println("trie Node", hash)
return []byte{}, nil
/*fmt.Println("trie Node", hash)
return []byte{}, nil*/
panic("no Node")
}
// node retrieves a cached trie node from memory, or returns nil if none can be
// found in the memory cache.
func (db *Database) node(hash common.Hash) node {
fmt.Println("trie node", hash)
emptyHash := common.Hash{}
if hash == emptyHash {
panic("empty")
}
/*emptyHash := common.Hash{}
if hash == emptyHash {
return nilValueNode
}
//return hashNode(hash.Bytes())*/
enc := db.lookup[hash]
return mustDecodeNode(hash[:], enc)
//return nilValueNode
return mustDecodeNode(hash[:], oracle.Preimage(hash))
}
// insert inserts a collapsed trie node into the memory database.
......
......@@ -21,6 +21,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/oracle"
)
// SecureTrie wraps a trie with key hashing. In a secure trie, all
......@@ -76,9 +77,9 @@ func (t *SecureTrie) Get(key []byte) []byte {
// The value bytes must not be modified by the caller.
// If a node was not found in the database, a MissingNodeError is returned.
func (t *SecureTrie) TryGet(key []byte) ([]byte, error) {
fmt.Println("TryGet", key)
t.trie.db.Fetch(common.BytesToAddress(key))
oracle.PrefetchAddress(t.trie.db.BlockNumber, common.BytesToAddress(key))
//t.trie.db.Fetch(common.BytesToAddress(key))
return t.trie.TryGet(t.hashKey(key))
}
......
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