Commit b7e2e8b6 authored by George Hotz's avatar George Hotz

move prefetch into oracle

parent 20c6eba6
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"math/big" "math/big"
"net/http" "net/http"
"os" "os"
"strings"
"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"
...@@ -91,21 +92,46 @@ func unhash(addrHash common.Hash) common.Address { ...@@ -91,21 +92,46 @@ func unhash(addrHash common.Hash) common.Address {
return unhashMap[addrHash] 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[:]) addrHash := crypto.Keccak256Hash(addr[:])
unhashMap[addrHash] = addr unhashMap[addrHash] = addr
r := jsonreq{Jsonrpc: "2.0", Method: "eth_getProof", Id: 1} if !cacheExists(key) {
r.Params = make([]interface{}, 3) r := jsonreq{Jsonrpc: "2.0", Method: "eth_getProof", Id: 1}
r.Params[0] = addr r.Params = make([]interface{}, 3)
r.Params[1] = []common.Hash{} r.Params[0] = addr
r.Params[2] = fmt.Sprintf("0x%x", blockNumber.Int64()) r.Params[1] = []common.Hash{}
jsonData, _ := json.Marshal(r) r.Params[2] = fmt.Sprintf("0x%x", blockNumber.Int64())
resp, _ := http.Post(nodeUrl, "application/json", bytes.NewBuffer(jsonData)) jsonData, _ := json.Marshal(r)
defer resp.Body.Close() resp, _ := http.Post(nodeUrl, "application/json", bytes.NewBuffer(jsonData))
jr := jsonresp{} defer resp.Body.Close()
json.NewDecoder(resp.Body).Decode(&jr) jr := jsonresp{}
return jr.Result.AccountProof 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 { func GetProvedAccountBytes(blockNumber *big.Int, stateRoot common.Hash, addr common.Address) []byte {
......
package trie package trie
import ( import (
"encoding/hex"
"fmt" "fmt"
"io" "io"
"math/big" "math/big"
...@@ -9,7 +8,6 @@ import ( ...@@ -9,7 +8,6 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/oracle" "github.com/ethereum/go-ethereum/oracle"
) )
...@@ -30,7 +28,6 @@ type Database struct { ...@@ -30,7 +28,6 @@ type Database struct {
BlockNumber *big.Int BlockNumber *big.Int
Root common.Hash Root common.Hash
lock sync.RWMutex lock sync.RWMutex
lookup map[common.Hash][]byte
preimages map[common.Hash][]byte // Preimages of nodes from the secure trie preimages map[common.Hash][]byte // Preimages of nodes from the secure trie
preimagesSize common.StorageSize // Storage size of the preimages cache preimagesSize common.StorageSize // Storage size of the preimages cache
...@@ -64,25 +61,11 @@ func (db *Database) preimage(hash common.Hash) []byte { ...@@ -64,25 +61,11 @@ func (db *Database) preimage(hash common.Hash) []byte {
return db.preimages[hash] 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 { func NewDatabase(header types.Header) Database {
triedb := Database{BlockNumber: header.Number, Root: header.Root} 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") fmt.Println("init database")
oracle.PrefetchAddress(header.Number, common.Address{})
// fetch the root node
triedb.Fetch(common.Address{})
//panic("preseed") //panic("preseed")
return triedb return triedb
...@@ -91,27 +74,15 @@ func NewDatabase(header types.Header) Database { ...@@ -91,27 +74,15 @@ func NewDatabase(header types.Header) Database {
// Node retrieves an encoded cached trie node from memory. If it cannot be found // Node retrieves an encoded cached trie node from memory. If it cannot be found
// cached, the method queries the persistent database for the content. // cached, the method queries the persistent database for the content.
func (db *Database) Node(hash common.Hash) ([]byte, error) { func (db *Database) Node(hash common.Hash) ([]byte, error) {
fmt.Println("trie Node", hash) /*fmt.Println("trie Node", hash)
return []byte{}, nil return []byte{}, nil*/
panic("no Node")
} }
// node retrieves a cached trie node from memory, or returns nil if none can be // node retrieves a cached trie node from memory, or returns nil if none can be
// found in the memory cache. // found in the memory cache.
func (db *Database) node(hash common.Hash) node { func (db *Database) node(hash common.Hash) node {
fmt.Println("trie node", hash) return mustDecodeNode(hash[:], oracle.Preimage(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
} }
// insert inserts a collapsed trie node into the memory database. // insert inserts a collapsed trie node into the memory database.
......
...@@ -21,6 +21,7 @@ import ( ...@@ -21,6 +21,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/oracle"
) )
// SecureTrie wraps a trie with key hashing. In a secure trie, all // SecureTrie wraps a trie with key hashing. In a secure trie, all
...@@ -76,9 +77,9 @@ func (t *SecureTrie) Get(key []byte) []byte { ...@@ -76,9 +77,9 @@ func (t *SecureTrie) Get(key []byte) []byte {
// The value bytes must not be modified by the caller. // The value bytes must not be modified by the caller.
// If a node was not found in the database, a MissingNodeError is returned. // If a node was not found in the database, a MissingNodeError is returned.
func (t *SecureTrie) TryGet(key []byte) ([]byte, error) { func (t *SecureTrie) TryGet(key []byte) ([]byte, error) {
fmt.Println("TryGet", key) oracle.PrefetchAddress(t.trie.db.BlockNumber, common.BytesToAddress(key))
t.trie.db.Fetch(common.BytesToAddress(key))
//t.trie.db.Fetch(common.BytesToAddress(key))
return t.trie.TryGet(t.hashKey(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