forked from cerc-io/plugeth
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent * push current state for feedback * les: fix unit test * core, les, light: fix les unittests * core/state, trie, les, light: fix state iterator * core, les: address comments * les: fix lint --------- Co-authored-by: Gary Rong <garyrong0905@gmail.com>
This commit is contained in:
co-authored by
Gary Rong
parent
85b8d1c06c
commit
8bbb16b70e
+10
-9
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/ethereum/go-ethereum/common/lru"
|
||||
"github.com/ethereum/go-ethereum/core/rawdb"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/trie"
|
||||
"github.com/ethereum/go-ethereum/trie/trienode"
|
||||
@@ -43,16 +44,16 @@ type Database interface {
|
||||
OpenTrie(root common.Hash) (Trie, error)
|
||||
|
||||
// OpenStorageTrie opens the storage trie of an account.
|
||||
OpenStorageTrie(stateRoot common.Hash, addrHash, root common.Hash) (Trie, error)
|
||||
OpenStorageTrie(stateRoot common.Hash, address common.Address, root common.Hash) (Trie, error)
|
||||
|
||||
// CopyTrie returns an independent copy of the given trie.
|
||||
CopyTrie(Trie) Trie
|
||||
|
||||
// ContractCode retrieves a particular contract's code.
|
||||
ContractCode(addrHash, codeHash common.Hash) ([]byte, error)
|
||||
ContractCode(addr common.Address, codeHash common.Hash) ([]byte, error)
|
||||
|
||||
// ContractCodeSize retrieves a particular contracts code's size.
|
||||
ContractCodeSize(addrHash, codeHash common.Hash) (int, error)
|
||||
ContractCodeSize(addr common.Address, codeHash common.Hash) (int, error)
|
||||
|
||||
// DiskDB returns the underlying key-value disk database.
|
||||
DiskDB() ethdb.KeyValueStore
|
||||
@@ -177,8 +178,8 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
|
||||
}
|
||||
|
||||
// OpenStorageTrie opens the storage trie of an account.
|
||||
func (db *cachingDB) OpenStorageTrie(stateRoot common.Hash, addrHash, root common.Hash) (Trie, error) {
|
||||
tr, err := trie.NewStateTrie(trie.StorageTrieID(stateRoot, addrHash, root), db.triedb)
|
||||
func (db *cachingDB) OpenStorageTrie(stateRoot common.Hash, address common.Address, root common.Hash) (Trie, error) {
|
||||
tr, err := trie.NewStateTrie(trie.StorageTrieID(stateRoot, crypto.Keccak256Hash(address.Bytes()), root), db.triedb)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -196,7 +197,7 @@ func (db *cachingDB) CopyTrie(t Trie) Trie {
|
||||
}
|
||||
|
||||
// ContractCode retrieves a particular contract's code.
|
||||
func (db *cachingDB) ContractCode(addrHash, codeHash common.Hash) ([]byte, error) {
|
||||
func (db *cachingDB) ContractCode(address common.Address, codeHash common.Hash) ([]byte, error) {
|
||||
code, _ := db.codeCache.Get(codeHash)
|
||||
if len(code) > 0 {
|
||||
return code, nil
|
||||
@@ -213,7 +214,7 @@ func (db *cachingDB) ContractCode(addrHash, codeHash common.Hash) ([]byte, error
|
||||
// ContractCodeWithPrefix retrieves a particular contract's code. If the
|
||||
// code can't be found in the cache, then check the existence with **new**
|
||||
// db scheme.
|
||||
func (db *cachingDB) ContractCodeWithPrefix(addrHash, codeHash common.Hash) ([]byte, error) {
|
||||
func (db *cachingDB) ContractCodeWithPrefix(address common.Address, codeHash common.Hash) ([]byte, error) {
|
||||
code, _ := db.codeCache.Get(codeHash)
|
||||
if len(code) > 0 {
|
||||
return code, nil
|
||||
@@ -228,11 +229,11 @@ func (db *cachingDB) ContractCodeWithPrefix(addrHash, codeHash common.Hash) ([]b
|
||||
}
|
||||
|
||||
// ContractCodeSize retrieves a particular contracts code's size.
|
||||
func (db *cachingDB) ContractCodeSize(addrHash, codeHash common.Hash) (int, error) {
|
||||
func (db *cachingDB) ContractCodeSize(addr common.Address, codeHash common.Hash) (int, error) {
|
||||
if cached, ok := db.codeSizeCache.Get(codeHash); ok {
|
||||
return cached, nil
|
||||
}
|
||||
code, err := db.ContractCode(addrHash, codeHash)
|
||||
code, err := db.ContractCode(addr, codeHash)
|
||||
return len(code), err
|
||||
}
|
||||
|
||||
|
||||
+13
-4
@@ -18,6 +18,7 @@ package state
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
@@ -27,7 +28,8 @@ import (
|
||||
)
|
||||
|
||||
// nodeIterator is an iterator to traverse the entire state trie post-order,
|
||||
// including all of the contract code and contract state tries.
|
||||
// including all of the contract code and contract state tries. Preimage is
|
||||
// required in order to resolve the contract address.
|
||||
type nodeIterator struct {
|
||||
state *StateDB // State being iterated
|
||||
|
||||
@@ -113,7 +115,15 @@ func (it *nodeIterator) step() error {
|
||||
if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob()), &account); err != nil {
|
||||
return err
|
||||
}
|
||||
dataTrie, err := it.state.db.OpenStorageTrie(it.state.originalRoot, common.BytesToHash(it.stateIt.LeafKey()), account.Root)
|
||||
// Lookup the preimage of account hash
|
||||
preimage := it.state.trie.GetKey(it.stateIt.LeafKey())
|
||||
if preimage == nil {
|
||||
return errors.New("account address is not available")
|
||||
}
|
||||
address := common.BytesToAddress(preimage)
|
||||
|
||||
// Traverse the storage slots belong to the account
|
||||
dataTrie, err := it.state.db.OpenStorageTrie(it.state.originalRoot, address, account.Root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -126,8 +136,7 @@ func (it *nodeIterator) step() error {
|
||||
}
|
||||
if !bytes.Equal(account.CodeHash, types.EmptyCodeHash.Bytes()) {
|
||||
it.codeHash = common.BytesToHash(account.CodeHash)
|
||||
addrHash := common.BytesToHash(it.stateIt.LeafKey())
|
||||
it.code, err = it.state.db.ContractCode(addrHash, common.BytesToHash(account.CodeHash))
|
||||
it.code, err = it.state.db.ContractCode(address, common.BytesToHash(account.CodeHash))
|
||||
if err != nil {
|
||||
return fmt.Errorf("code %x: %v", account.CodeHash, err)
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ func (s *stateObject) getTrie(db Database) (Trie, error) {
|
||||
s.trie = s.db.prefetcher.trie(s.addrHash, s.data.Root)
|
||||
}
|
||||
if s.trie == nil {
|
||||
tr, err := db.OpenStorageTrie(s.db.originalRoot, s.addrHash, s.data.Root)
|
||||
tr, err := db.OpenStorageTrie(s.db.originalRoot, s.address, s.data.Root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -441,7 +441,7 @@ func (s *stateObject) Code(db Database) []byte {
|
||||
if bytes.Equal(s.CodeHash(), types.EmptyCodeHash.Bytes()) {
|
||||
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.db.setError(fmt.Errorf("can't load code hash %x: %v", s.CodeHash(), err))
|
||||
}
|
||||
@@ -459,7 +459,7 @@ func (s *stateObject) CodeSize(db Database) int {
|
||||
if bytes.Equal(s.CodeHash(), types.EmptyCodeHash.Bytes()) {
|
||||
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.db.setError(fmt.Errorf("can't load code size %x: %v", s.CodeHash(), err))
|
||||
}
|
||||
|
||||
+43
-40
@@ -42,7 +42,7 @@ type testAccount struct {
|
||||
func makeTestState() (ethdb.Database, Database, common.Hash, []*testAccount) {
|
||||
// Create an empty state
|
||||
db := rawdb.NewMemoryDatabase()
|
||||
sdb := NewDatabase(db)
|
||||
sdb := NewDatabaseWithConfig(db, &trie.Config{Preimages: true})
|
||||
state, _ := New(types.EmptyRootHash, sdb, nil)
|
||||
|
||||
// Fill it with some arbitrary data
|
||||
@@ -100,28 +100,9 @@ func checkStateAccounts(t *testing.T, db ethdb.Database, root common.Hash, accou
|
||||
}
|
||||
}
|
||||
|
||||
// checkTrieConsistency checks that all nodes in a (sub-)trie are indeed present.
|
||||
func checkTrieConsistency(db ethdb.Database, root common.Hash) error {
|
||||
if v, _ := db.Get(root[:]); v == nil {
|
||||
return nil // Consider a non existent state consistent.
|
||||
}
|
||||
trie, err := trie.New(trie.StateTrieID(root), trie.NewDatabase(db))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
it := trie.MustNodeIterator(nil)
|
||||
for it.Next(true) {
|
||||
}
|
||||
return it.Error()
|
||||
}
|
||||
|
||||
// checkStateConsistency checks that all data of a state root is present.
|
||||
func checkStateConsistency(db ethdb.Database, root common.Hash) error {
|
||||
// Create and iterate a state trie rooted in a sub-node
|
||||
if _, err := db.Get(root.Bytes()); err != nil {
|
||||
return nil // Consider a non existent state consistent.
|
||||
}
|
||||
state, err := New(root, NewDatabase(db), nil)
|
||||
state, err := New(root, NewDatabaseWithConfig(db, &trie.Config{Preimages: true}), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -171,7 +152,7 @@ type stateElement struct {
|
||||
|
||||
func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) {
|
||||
// Create a random state to copy
|
||||
_, srcDb, srcRoot, srcAccounts := makeTestState()
|
||||
srcDisk, srcDb, srcRoot, srcAccounts := makeTestState()
|
||||
if commit {
|
||||
srcDb.TrieDB().Commit(srcRoot, false)
|
||||
}
|
||||
@@ -204,7 +185,7 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) {
|
||||
codeResults = make([]trie.CodeSyncResult, len(codeElements))
|
||||
)
|
||||
for i, element := range codeElements {
|
||||
data, err := srcDb.ContractCode(common.Hash{}, element.code)
|
||||
data, err := srcDb.ContractCode(common.Address{}, element.code)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to retrieve contract bytecode for hash %x", element.code)
|
||||
}
|
||||
@@ -274,6 +255,10 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) {
|
||||
})
|
||||
}
|
||||
}
|
||||
// Copy the preimages from source db in order to traverse the state.
|
||||
srcDb.TrieDB().WritePreimages()
|
||||
copyPreimages(srcDisk, dstDb)
|
||||
|
||||
// Cross check that the two states are in sync
|
||||
checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
|
||||
}
|
||||
@@ -282,7 +267,7 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) {
|
||||
// partial results are returned, and the others sent only later.
|
||||
func TestIterativeDelayedStateSync(t *testing.T) {
|
||||
// Create a random state to copy
|
||||
_, srcDb, srcRoot, srcAccounts := makeTestState()
|
||||
srcDisk, srcDb, srcRoot, srcAccounts := makeTestState()
|
||||
|
||||
// Create a destination state and sync with the scheduler
|
||||
dstDb := rawdb.NewMemoryDatabase()
|
||||
@@ -312,7 +297,7 @@ func TestIterativeDelayedStateSync(t *testing.T) {
|
||||
if len(codeElements) > 0 {
|
||||
codeResults := make([]trie.CodeSyncResult, len(codeElements)/2+1)
|
||||
for i, element := range codeElements[:len(codeResults)] {
|
||||
data, err := srcDb.ContractCode(common.Hash{}, element.code)
|
||||
data, err := srcDb.ContractCode(common.Address{}, element.code)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to retrieve contract bytecode for %x", element.code)
|
||||
}
|
||||
@@ -363,6 +348,10 @@ func TestIterativeDelayedStateSync(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
// Copy the preimages from source db in order to traverse the state.
|
||||
srcDb.TrieDB().WritePreimages()
|
||||
copyPreimages(srcDisk, dstDb)
|
||||
|
||||
// Cross check that the two states are in sync
|
||||
checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
|
||||
}
|
||||
@@ -375,7 +364,7 @@ func TestIterativeRandomStateSyncBatched(t *testing.T) { testIterativeRandomS
|
||||
|
||||
func testIterativeRandomStateSync(t *testing.T, count int) {
|
||||
// Create a random state to copy
|
||||
_, srcDb, srcRoot, srcAccounts := makeTestState()
|
||||
srcDisk, srcDb, srcRoot, srcAccounts := makeTestState()
|
||||
|
||||
// Create a destination state and sync with the scheduler
|
||||
dstDb := rawdb.NewMemoryDatabase()
|
||||
@@ -399,7 +388,7 @@ func testIterativeRandomStateSync(t *testing.T, count int) {
|
||||
if len(codeQueue) > 0 {
|
||||
results := make([]trie.CodeSyncResult, 0, len(codeQueue))
|
||||
for hash := range codeQueue {
|
||||
data, err := srcDb.ContractCode(common.Hash{}, hash)
|
||||
data, err := srcDb.ContractCode(common.Address{}, hash)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to retrieve node data for %x", hash)
|
||||
}
|
||||
@@ -447,6 +436,10 @@ func testIterativeRandomStateSync(t *testing.T, count int) {
|
||||
codeQueue[hash] = struct{}{}
|
||||
}
|
||||
}
|
||||
// Copy the preimages from source db in order to traverse the state.
|
||||
srcDb.TrieDB().WritePreimages()
|
||||
copyPreimages(srcDisk, dstDb)
|
||||
|
||||
// Cross check that the two states are in sync
|
||||
checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
|
||||
}
|
||||
@@ -455,7 +448,7 @@ func testIterativeRandomStateSync(t *testing.T, count int) {
|
||||
// partial results are returned (Even those randomly), others sent only later.
|
||||
func TestIterativeRandomDelayedStateSync(t *testing.T) {
|
||||
// Create a random state to copy
|
||||
_, srcDb, srcRoot, srcAccounts := makeTestState()
|
||||
srcDisk, srcDb, srcRoot, srcAccounts := makeTestState()
|
||||
|
||||
// Create a destination state and sync with the scheduler
|
||||
dstDb := rawdb.NewMemoryDatabase()
|
||||
@@ -481,7 +474,7 @@ func TestIterativeRandomDelayedStateSync(t *testing.T) {
|
||||
for hash := range codeQueue {
|
||||
delete(codeQueue, hash)
|
||||
|
||||
data, err := srcDb.ContractCode(common.Hash{}, hash)
|
||||
data, err := srcDb.ContractCode(common.Address{}, hash)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to retrieve node data for %x", hash)
|
||||
}
|
||||
@@ -537,6 +530,10 @@ func TestIterativeRandomDelayedStateSync(t *testing.T) {
|
||||
codeQueue[hash] = struct{}{}
|
||||
}
|
||||
}
|
||||
// Copy the preimages from source db in order to traverse the state.
|
||||
srcDb.TrieDB().WritePreimages()
|
||||
copyPreimages(srcDisk, dstDb)
|
||||
|
||||
// Cross check that the two states are in sync
|
||||
checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
|
||||
}
|
||||
@@ -555,7 +552,6 @@ func TestIncompleteStateSync(t *testing.T) {
|
||||
}
|
||||
}
|
||||
isCode[types.EmptyCodeHash] = struct{}{}
|
||||
checkTrieConsistency(db, srcRoot)
|
||||
|
||||
// Create a destination state and sync with the scheduler
|
||||
dstDb := rawdb.NewMemoryDatabase()
|
||||
@@ -588,7 +584,7 @@ func TestIncompleteStateSync(t *testing.T) {
|
||||
if len(codeQueue) > 0 {
|
||||
results := make([]trie.CodeSyncResult, 0, len(codeQueue))
|
||||
for hash := range codeQueue {
|
||||
data, err := srcDb.ContractCode(common.Hash{}, hash)
|
||||
data, err := srcDb.ContractCode(common.Address{}, hash)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to retrieve node data for %x", hash)
|
||||
}
|
||||
@@ -602,7 +598,6 @@ func TestIncompleteStateSync(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
var nodehashes []common.Hash
|
||||
if len(nodeQueue) > 0 {
|
||||
results := make([]trie.NodeSyncResult, 0, len(nodeQueue))
|
||||
for path, element := range nodeQueue {
|
||||
@@ -617,7 +612,6 @@ func TestIncompleteStateSync(t *testing.T) {
|
||||
addedPaths = append(addedPaths, element.path)
|
||||
addedHashes = append(addedHashes, element.hash)
|
||||
}
|
||||
nodehashes = append(nodehashes, element.hash)
|
||||
}
|
||||
// Process each of the state nodes
|
||||
for _, result := range results {
|
||||
@@ -632,13 +626,6 @@ func TestIncompleteStateSync(t *testing.T) {
|
||||
}
|
||||
batch.Write()
|
||||
|
||||
for _, root := range nodehashes {
|
||||
// Can't use checkStateConsistency here because subtrie keys may have odd
|
||||
// length and crash in LeafKey.
|
||||
if err := checkTrieConsistency(dstDb, root); err != nil {
|
||||
t.Fatalf("state inconsistent: %v", err)
|
||||
}
|
||||
}
|
||||
// Fetch the next batch to retrieve
|
||||
nodeQueue = make(map[string]stateElement)
|
||||
codeQueue = make(map[common.Hash]struct{})
|
||||
@@ -654,6 +641,10 @@ func TestIncompleteStateSync(t *testing.T) {
|
||||
codeQueue[hash] = struct{}{}
|
||||
}
|
||||
}
|
||||
// Copy the preimages from source db in order to traverse the state.
|
||||
srcDb.TrieDB().WritePreimages()
|
||||
copyPreimages(db, dstDb)
|
||||
|
||||
// Sanity check that removing any node from the database is detected
|
||||
for _, node := range addedCodes {
|
||||
val := rawdb.ReadCode(dstDb, node)
|
||||
@@ -678,3 +669,15 @@ func TestIncompleteStateSync(t *testing.T) {
|
||||
rawdb.WriteTrieNode(dstDb, owner, inner, hash, val, scheme)
|
||||
}
|
||||
}
|
||||
|
||||
func copyPreimages(srcDb, dstDb ethdb.Database) {
|
||||
it := srcDb.NewIterator(rawdb.PreimagePrefix, nil)
|
||||
defer it.Release()
|
||||
|
||||
preimages := make(map[common.Hash][]byte)
|
||||
for it.Next() {
|
||||
hash := it.Key()[len(rawdb.PreimagePrefix):]
|
||||
preimages[common.BytesToHash(hash)] = common.CopyBytes(it.Value())
|
||||
}
|
||||
rawdb.WritePreimages(dstDb, preimages)
|
||||
}
|
||||
|
||||
@@ -302,7 +302,7 @@ func (sf *subfetcher) loop() {
|
||||
}
|
||||
sf.trie = trie
|
||||
} else {
|
||||
trie, err := sf.db.OpenStorageTrie(sf.state, sf.owner, sf.root)
|
||||
trie, err := sf.db.OpenStorageTrie(sf.state, sf.addr, sf.root)
|
||||
if err != nil {
|
||||
log.Warn("Trie prefetcher failed opening trie", "root", sf.root, "err", err)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user