Unverified Commit 5021d36d authored by rjl493456442's avatar rjl493456442 Committed by GitHub

all: port boring changes from pbss (#27176)

* all: port boring changes from pbss

* core, trie: address comments from martin

* trie: minor fixes

* core/rawdb: update comment

* core, eth, tests, trie: address comments

* tests, trie: add extra check when update trie database

* trie/triedb/hashdb: degrade the error to warning
parent 81d328a7
...@@ -982,8 +982,8 @@ func (bc *BlockChain) Stop() { ...@@ -982,8 +982,8 @@ func (bc *BlockChain) Stop() {
} }
} }
// Flush the collected preimages to disk // Flush the collected preimages to disk
if err := bc.stateCache.TrieDB().CommitPreimages(); err != nil { if err := bc.stateCache.TrieDB().Close(); err != nil {
log.Error("Failed to commit trie preimages", "err", err) log.Error("Failed to close trie db", "err", err)
} }
// Ensure all live cached entries be saved into disk, so that we can skip // Ensure all live cached entries be saved into disk, so that we can skip
// cache warmup when node restarts. // cache warmup when node restarts.
......
...@@ -1701,7 +1701,7 @@ func TestTrieForkGC(t *testing.T) { ...@@ -1701,7 +1701,7 @@ func TestTrieForkGC(t *testing.T) {
chain.stateCache.TrieDB().Dereference(blocks[len(blocks)-1-i].Root()) chain.stateCache.TrieDB().Dereference(blocks[len(blocks)-1-i].Root())
chain.stateCache.TrieDB().Dereference(forks[len(blocks)-1-i].Root()) chain.stateCache.TrieDB().Dereference(forks[len(blocks)-1-i].Root())
} }
if len(chain.stateCache.TrieDB().Nodes()) > 0 { if nodes, _ := chain.TrieDB().Size(); nodes > 0 {
t.Fatalf("stale tries still alive after garbase collection") t.Fatalf("stale tries still alive after garbase collection")
} }
} }
......
...@@ -22,6 +22,7 @@ import ( ...@@ -22,6 +22,7 @@ import (
"encoding/binary" "encoding/binary"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/metrics"
) )
...@@ -100,7 +101,7 @@ var ( ...@@ -100,7 +101,7 @@ var (
CodePrefix = []byte("c") // CodePrefix + code hash -> account code CodePrefix = []byte("c") // CodePrefix + code hash -> account code
skeletonHeaderPrefix = []byte("S") // skeletonHeaderPrefix + num (uint64 big endian) -> header skeletonHeaderPrefix = []byte("S") // skeletonHeaderPrefix + num (uint64 big endian) -> header
// Path-based trie node scheme. // Path-based storage scheme of merkle patricia trie.
trieNodeAccountPrefix = []byte("A") // trieNodeAccountPrefix + hexPath -> trie node trieNodeAccountPrefix = []byte("A") // trieNodeAccountPrefix + hexPath -> trie node
trieNodeStoragePrefix = []byte("O") // trieNodeStoragePrefix + accountHash + hexPath -> trie node trieNodeStoragePrefix = []byte("O") // trieNodeStoragePrefix + accountHash + hexPath -> trie node
...@@ -248,3 +249,48 @@ func accountTrieNodeKey(path []byte) []byte { ...@@ -248,3 +249,48 @@ func accountTrieNodeKey(path []byte) []byte {
func storageTrieNodeKey(accountHash common.Hash, path []byte) []byte { func storageTrieNodeKey(accountHash common.Hash, path []byte) []byte {
return append(append(trieNodeStoragePrefix, accountHash.Bytes()...), path...) return append(append(trieNodeStoragePrefix, accountHash.Bytes()...), path...)
} }
// IsLegacyTrieNode reports whether a provided database entry is a legacy trie
// node. The characteristics of legacy trie node are:
// - the key length is 32 bytes
// - the key is the hash of val
func IsLegacyTrieNode(key []byte, val []byte) bool {
if len(key) != common.HashLength {
return false
}
return bytes.Equal(key, crypto.Keccak256(val))
}
// IsAccountTrieNode reports whether a provided database entry is an account
// trie node in path-based state scheme.
func IsAccountTrieNode(key []byte) (bool, []byte) {
if !bytes.HasPrefix(key, trieNodeAccountPrefix) {
return false, nil
}
// The remaining key should only consist a hex node path
// whose length is in the range 0 to 64 (64 is excluded
// since leaves are always wrapped with shortNode).
if len(key) >= len(trieNodeAccountPrefix)+common.HashLength*2 {
return false, nil
}
return true, key[len(trieNodeAccountPrefix):]
}
// IsStorageTrieNode reports whether a provided database entry is a storage
// trie node in path-based state scheme.
func IsStorageTrieNode(key []byte) (bool, common.Hash, []byte) {
if !bytes.HasPrefix(key, trieNodeStoragePrefix) {
return false, common.Hash{}, nil
}
// The remaining key consists of 2 parts:
// - 32 bytes account hash
// - hex node path whose length is in the range 0 to 64
if len(key) < len(trieNodeStoragePrefix)+common.HashLength {
return false, common.Hash{}, nil
}
if len(key) >= len(trieNodeStoragePrefix)+common.HashLength+common.HashLength*2 {
return false, common.Hash{}, nil
}
accountHash := common.BytesToHash(key[len(trieNodeStoragePrefix) : len(trieNodeStoragePrefix)+common.HashLength])
return true, accountHash, key[len(trieNodeStoragePrefix)+common.HashLength:]
}
...@@ -26,6 +26,7 @@ import ( ...@@ -26,6 +26,7 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/trienode"
) )
const ( const (
...@@ -109,7 +110,7 @@ type Trie interface { ...@@ -109,7 +110,7 @@ type Trie interface {
// The returned nodeset can be nil if the trie is clean(nothing to commit). // The returned nodeset can be nil if the trie is clean(nothing to commit).
// Once the trie is committed, it's not usable anymore. A new trie must // Once the trie is committed, it's not usable anymore. A new trie must
// be created with new root and updated trie database for following usage // be created with new root and updated trie database for following usage
Commit(collectLeaf bool) (common.Hash, *trie.NodeSet) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet)
// NodeIterator returns an iterator that returns nodes of the trie. Iteration // NodeIterator returns an iterator that returns nodes of the trie. Iteration
// starts at the key after the given start key. // starts at the key after the given start key.
......
...@@ -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/core/rawdb" "github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/crypto"
) )
// Tests that the node iterator indeed walks over the entire database contents. // Tests that the node iterator indeed walks over the entire database contents.
...@@ -85,9 +86,18 @@ func TestNodeIteratorCoverage(t *testing.T) { ...@@ -85,9 +86,18 @@ func TestNodeIteratorCoverage(t *testing.T) {
// database entry belongs to a trie node or not. // database entry belongs to a trie node or not.
func isTrieNode(scheme string, key, val []byte) (bool, common.Hash) { func isTrieNode(scheme string, key, val []byte) (bool, common.Hash) {
if scheme == rawdb.HashScheme { if scheme == rawdb.HashScheme {
if len(key) == common.HashLength { if rawdb.IsLegacyTrieNode(key, val) {
return true, common.BytesToHash(key) return true, common.BytesToHash(key)
} }
} else {
ok, _ := rawdb.IsAccountTrieNode(key)
if ok {
return true, crypto.Keccak256Hash(val)
}
ok, _, _ = rawdb.IsStorageTrieNode(key)
if ok {
return true, crypto.Keccak256Hash(val)
}
} }
return false, common.Hash{} return false, common.Hash{}
} }
...@@ -32,6 +32,7 @@ import ( ...@@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/trienode"
) )
var ( var (
...@@ -363,7 +364,7 @@ func (dl *diskLayer) generateRange(ctx *generatorContext, trieId *trie.ID, prefi ...@@ -363,7 +364,7 @@ func (dl *diskLayer) generateRange(ctx *generatorContext, trieId *trie.ID, prefi
} }
root, nodes := snapTrie.Commit(false) root, nodes := snapTrie.Commit(false)
if nodes != nil { if nodes != nil {
tdb.Update(trie.NewWithNodeSet(nodes)) tdb.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
tdb.Commit(root, false) tdb.Commit(root, false)
} }
resolver = func(owner common.Hash, path []byte, hash common.Hash) []byte { resolver = func(owner common.Hash, path []byte, hash common.Hash) []byte {
......
...@@ -30,6 +30,7 @@ import ( ...@@ -30,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/trienode"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
) )
...@@ -144,7 +145,7 @@ type testHelper struct { ...@@ -144,7 +145,7 @@ type testHelper struct {
diskdb ethdb.Database diskdb ethdb.Database
triedb *trie.Database triedb *trie.Database
accTrie *trie.StateTrie accTrie *trie.StateTrie
nodes *trie.MergedNodeSet nodes *trienode.MergedNodeSet
} }
func newHelper() *testHelper { func newHelper() *testHelper {
...@@ -155,7 +156,7 @@ func newHelper() *testHelper { ...@@ -155,7 +156,7 @@ func newHelper() *testHelper {
diskdb: diskdb, diskdb: diskdb,
triedb: triedb, triedb: triedb,
accTrie: accTrie, accTrie: accTrie,
nodes: trie.NewMergedNodeSet(), nodes: trienode.NewMergedNodeSet(),
} }
} }
...@@ -203,7 +204,7 @@ func (t *testHelper) Commit() common.Hash { ...@@ -203,7 +204,7 @@ func (t *testHelper) Commit() common.Hash {
if nodes != nil { if nodes != nil {
t.nodes.Merge(nodes) t.nodes.Merge(nodes)
} }
t.triedb.Update(t.nodes) t.triedb.Update(root, types.EmptyRootHash, t.nodes)
t.triedb.Commit(root, false) t.triedb.Commit(root, false)
return root return root
} }
......
...@@ -28,7 +28,7 @@ import ( ...@@ -28,7 +28,7 @@ import (
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie/trienode"
) )
type Code []byte type Code []byte
...@@ -350,7 +350,7 @@ func (s *stateObject) updateRoot(db Database) { ...@@ -350,7 +350,7 @@ func (s *stateObject) updateRoot(db Database) {
// commitTrie submits the storage changes into the storage trie and re-computes // commitTrie submits the storage changes into the storage trie and re-computes
// the root. Besides, all trie changes will be collected in a nodeset and returned. // the root. Besides, all trie changes will be collected in a nodeset and returned.
func (s *stateObject) commitTrie(db Database) (*trie.NodeSet, error) { func (s *stateObject) commitTrie(db Database) (*trienode.NodeSet, error) {
tr, err := s.updateTrie(db) tr, err := s.updateTrie(db)
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -34,6 +34,7 @@ import ( ...@@ -34,6 +34,7 @@ import (
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/trienode"
) )
type revision struct { type revision struct {
...@@ -971,7 +972,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) { ...@@ -971,7 +972,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
accountTrieNodesDeleted int accountTrieNodesDeleted int
storageTrieNodesUpdated int storageTrieNodesUpdated int
storageTrieNodesDeleted int storageTrieNodesDeleted int
nodes = trie.NewMergedNodeSet() nodes = trienode.NewMergedNodeSet()
codeWriter = s.db.DiskDB().NewBatch() codeWriter = s.db.DiskDB().NewBatch()
) )
for addr := range s.stateObjectsDirty { for addr := range s.stateObjectsDirty {
...@@ -986,7 +987,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) { ...@@ -986,7 +987,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
if err != nil { if err != nil {
return common.Hash{}, err return common.Hash{}, err
} }
// Merge the dirty nodes of storage trie into global set // Merge the dirty nodes of storage trie into global set.
if set != nil { if set != nil {
if err := nodes.Merge(set); err != nil { if err := nodes.Merge(set); err != nil {
return common.Hash{}, err return common.Hash{}, err
...@@ -1071,7 +1072,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) { ...@@ -1071,7 +1072,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
} }
if root != origin { if root != origin {
start := time.Now() start := time.Now()
if err := s.db.TrieDB().Update(nodes); err != nil { if err := s.db.TrieDB().Update(root, origin, nodes); err != nil {
return common.Hash{}, err return common.Hash{}, err
} }
s.originalRoot = root s.originalRoot = root
......
...@@ -602,7 +602,8 @@ func TestIncompleteStateSync(t *testing.T) { ...@@ -602,7 +602,8 @@ func TestIncompleteStateSync(t *testing.T) {
if len(nodeQueue) > 0 { if len(nodeQueue) > 0 {
results := make([]trie.NodeSyncResult, 0, len(nodeQueue)) results := make([]trie.NodeSyncResult, 0, len(nodeQueue))
for path, element := range nodeQueue { for path, element := range nodeQueue {
data, err := srcDb.TrieDB().Node(element.hash) owner, inner := trie.ResolvePath([]byte(element.path))
data, err := srcDb.TrieDB().Reader(srcRoot).Node(owner, inner, element.hash)
if err != nil { if err != nil {
t.Fatalf("failed to retrieve node data for %x", element.hash) t.Fatalf("failed to retrieve node data for %x", element.hash)
} }
......
...@@ -36,6 +36,7 @@ import ( ...@@ -36,6 +36,7 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/trienode"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
) )
...@@ -1389,7 +1390,7 @@ func makeAccountTrieNoStorage(n int) (string, *trie.Trie, entrySlice) { ...@@ -1389,7 +1390,7 @@ func makeAccountTrieNoStorage(n int) (string, *trie.Trie, entrySlice) {
// Commit the state changes into db and re-create the trie // Commit the state changes into db and re-create the trie
// for accessing later. // for accessing later.
root, nodes := accTrie.Commit(false) root, nodes := accTrie.Commit(false)
db.Update(trie.NewWithNodeSet(nodes)) db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
accTrie, _ = trie.New(trie.StateTrieID(root), db) accTrie, _ = trie.New(trie.StateTrieID(root), db)
return db.Scheme(), accTrie, entries return db.Scheme(), accTrie, entries
...@@ -1451,7 +1452,7 @@ func makeBoundaryAccountTrie(n int) (string, *trie.Trie, entrySlice) { ...@@ -1451,7 +1452,7 @@ func makeBoundaryAccountTrie(n int) (string, *trie.Trie, entrySlice) {
// Commit the state changes into db and re-create the trie // Commit the state changes into db and re-create the trie
// for accessing later. // for accessing later.
root, nodes := accTrie.Commit(false) root, nodes := accTrie.Commit(false)
db.Update(trie.NewWithNodeSet(nodes)) db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
accTrie, _ = trie.New(trie.StateTrieID(root), db) accTrie, _ = trie.New(trie.StateTrieID(root), db)
return db.Scheme(), accTrie, entries return db.Scheme(), accTrie, entries
...@@ -1467,7 +1468,7 @@ func makeAccountTrieWithStorageWithUniqueStorage(accounts, slots int, code bool) ...@@ -1467,7 +1468,7 @@ func makeAccountTrieWithStorageWithUniqueStorage(accounts, slots int, code bool)
storageRoots = make(map[common.Hash]common.Hash) storageRoots = make(map[common.Hash]common.Hash)
storageTries = make(map[common.Hash]*trie.Trie) storageTries = make(map[common.Hash]*trie.Trie)
storageEntries = make(map[common.Hash]entrySlice) storageEntries = make(map[common.Hash]entrySlice)
nodes = trie.NewMergedNodeSet() nodes = trienode.NewMergedNodeSet()
) )
// Create n accounts in the trie // Create n accounts in the trie
for i := uint64(1); i <= uint64(accounts); i++ { for i := uint64(1); i <= uint64(accounts); i++ {
...@@ -1500,7 +1501,7 @@ func makeAccountTrieWithStorageWithUniqueStorage(accounts, slots int, code bool) ...@@ -1500,7 +1501,7 @@ func makeAccountTrieWithStorageWithUniqueStorage(accounts, slots int, code bool)
nodes.Merge(set) nodes.Merge(set)
// Commit gathered dirty nodes into database // Commit gathered dirty nodes into database
db.Update(nodes) db.Update(root, types.EmptyRootHash, nodes)
// Re-create tries with new root // Re-create tries with new root
accTrie, _ = trie.New(trie.StateTrieID(root), db) accTrie, _ = trie.New(trie.StateTrieID(root), db)
...@@ -1522,7 +1523,7 @@ func makeAccountTrieWithStorage(accounts, slots int, code, boundary bool) (strin ...@@ -1522,7 +1523,7 @@ func makeAccountTrieWithStorage(accounts, slots int, code, boundary bool) (strin
storageRoots = make(map[common.Hash]common.Hash) storageRoots = make(map[common.Hash]common.Hash)
storageTries = make(map[common.Hash]*trie.Trie) storageTries = make(map[common.Hash]*trie.Trie)
storageEntries = make(map[common.Hash]entrySlice) storageEntries = make(map[common.Hash]entrySlice)
nodes = trie.NewMergedNodeSet() nodes = trienode.NewMergedNodeSet()
) )
// Create n accounts in the trie // Create n accounts in the trie
for i := uint64(1); i <= uint64(accounts); i++ { for i := uint64(1); i <= uint64(accounts); i++ {
...@@ -1534,7 +1535,7 @@ func makeAccountTrieWithStorage(accounts, slots int, code, boundary bool) (strin ...@@ -1534,7 +1535,7 @@ func makeAccountTrieWithStorage(accounts, slots int, code, boundary bool) (strin
// Make a storage trie // Make a storage trie
var ( var (
stRoot common.Hash stRoot common.Hash
stNodes *trie.NodeSet stNodes *trienode.NodeSet
stEntries entrySlice stEntries entrySlice
) )
if boundary { if boundary {
...@@ -1565,7 +1566,7 @@ func makeAccountTrieWithStorage(accounts, slots int, code, boundary bool) (strin ...@@ -1565,7 +1566,7 @@ func makeAccountTrieWithStorage(accounts, slots int, code, boundary bool) (strin
nodes.Merge(set) nodes.Merge(set)
// Commit gathered dirty nodes into database // Commit gathered dirty nodes into database
db.Update(nodes) db.Update(root, types.EmptyRootHash, nodes)
// Re-create tries with new root // Re-create tries with new root
accTrie, err := trie.New(trie.StateTrieID(root), db) accTrie, err := trie.New(trie.StateTrieID(root), db)
...@@ -1587,7 +1588,7 @@ func makeAccountTrieWithStorage(accounts, slots int, code, boundary bool) (strin ...@@ -1587,7 +1588,7 @@ func makeAccountTrieWithStorage(accounts, slots int, code, boundary bool) (strin
// makeStorageTrieWithSeed fills a storage trie with n items, returning the // makeStorageTrieWithSeed fills a storage trie with n items, returning the
// not-yet-committed trie and the sorted entries. The seeds can be used to ensure // not-yet-committed trie and the sorted entries. The seeds can be used to ensure
// that tries are unique. // that tries are unique.
func makeStorageTrieWithSeed(owner common.Hash, n, seed uint64, db *trie.Database) (common.Hash, *trie.NodeSet, entrySlice) { func makeStorageTrieWithSeed(owner common.Hash, n, seed uint64, db *trie.Database) (common.Hash, *trienode.NodeSet, entrySlice) {
trie, _ := trie.New(trie.StorageTrieID(common.Hash{}, owner, common.Hash{}), db) trie, _ := trie.New(trie.StorageTrieID(common.Hash{}, owner, common.Hash{}), db)
var entries entrySlice var entries entrySlice
for i := uint64(1); i <= n; i++ { for i := uint64(1); i <= n; i++ {
...@@ -1610,7 +1611,7 @@ func makeStorageTrieWithSeed(owner common.Hash, n, seed uint64, db *trie.Databas ...@@ -1610,7 +1611,7 @@ func makeStorageTrieWithSeed(owner common.Hash, n, seed uint64, db *trie.Databas
// makeBoundaryStorageTrie constructs a storage trie. Instead of filling // makeBoundaryStorageTrie constructs a storage trie. Instead of filling
// storage slots normally, this function will fill a few slots which have // storage slots normally, this function will fill a few slots which have
// boundary hash. // boundary hash.
func makeBoundaryStorageTrie(owner common.Hash, n int, db *trie.Database) (common.Hash, *trie.NodeSet, entrySlice) { func makeBoundaryStorageTrie(owner common.Hash, n int, db *trie.Database) (common.Hash, *trienode.NodeSet, entrySlice) {
var ( var (
entries entrySlice entries entrySlice
boundaries []common.Hash boundaries []common.Hash
......
...@@ -35,6 +35,7 @@ import ( ...@@ -35,6 +35,7 @@ import (
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/trienode"
) )
// IndexerConfig includes a set of configs for chain indexers. // IndexerConfig includes a set of configs for chain indexers.
...@@ -134,6 +135,7 @@ type ChtIndexerBackend struct { ...@@ -134,6 +135,7 @@ type ChtIndexerBackend struct {
section, sectionSize uint64 section, sectionSize uint64
lastHash common.Hash lastHash common.Hash
trie *trie.Trie trie *trie.Trie
originRoot common.Hash
} }
// NewChtIndexer creates a Cht chain indexer // NewChtIndexer creates a Cht chain indexer
...@@ -191,6 +193,7 @@ func (c *ChtIndexerBackend) Reset(ctx context.Context, section uint64, lastSecti ...@@ -191,6 +193,7 @@ func (c *ChtIndexerBackend) Reset(ctx context.Context, section uint64, lastSecti
} }
} }
c.section = section c.section = section
c.originRoot = root
return err return err
} }
...@@ -214,7 +217,7 @@ func (c *ChtIndexerBackend) Commit() error { ...@@ -214,7 +217,7 @@ func (c *ChtIndexerBackend) Commit() error {
root, nodes := c.trie.Commit(false) root, nodes := c.trie.Commit(false)
// Commit trie changes into trie database in case it's not nil. // Commit trie changes into trie database in case it's not nil.
if nodes != nil { if nodes != nil {
if err := c.triedb.Update(trie.NewWithNodeSet(nodes)); err != nil { if err := c.triedb.Update(root, c.originRoot, trienode.NewWithNodeSet(nodes)); err != nil {
return err return err
} }
if err := c.triedb.Commit(root, false); err != nil { if err := c.triedb.Commit(root, false); err != nil {
...@@ -332,6 +335,7 @@ type BloomTrieIndexerBackend struct { ...@@ -332,6 +335,7 @@ type BloomTrieIndexerBackend struct {
size uint64 size uint64
bloomTrieRatio uint64 bloomTrieRatio uint64
trie *trie.Trie trie *trie.Trie
originRoot common.Hash
sectionHeads []common.Hash sectionHeads []common.Hash
} }
...@@ -413,6 +417,7 @@ func (b *BloomTrieIndexerBackend) Reset(ctx context.Context, section uint64, las ...@@ -413,6 +417,7 @@ func (b *BloomTrieIndexerBackend) Reset(ctx context.Context, section uint64, las
} }
} }
b.section = section b.section = section
b.originRoot = root
return err return err
} }
...@@ -463,7 +468,7 @@ func (b *BloomTrieIndexerBackend) Commit() error { ...@@ -463,7 +468,7 @@ func (b *BloomTrieIndexerBackend) Commit() error {
root, nodes := b.trie.Commit(false) root, nodes := b.trie.Commit(false)
// Commit trie changes into trie database in case it's not nil. // Commit trie changes into trie database in case it's not nil.
if nodes != nil { if nodes != nil {
if err := b.triedb.Update(trie.NewWithNodeSet(nodes)); err != nil { if err := b.triedb.Update(root, b.originRoot, trienode.NewWithNodeSet(nodes)); err != nil {
return err return err
} }
if err := b.triedb.Commit(root, false); err != nil { if err := b.triedb.Commit(root, false); err != nil {
......
...@@ -29,6 +29,7 @@ import ( ...@@ -29,6 +29,7 @@ import (
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/trienode"
) )
var ( var (
...@@ -156,7 +157,7 @@ func (t *odrTrie) DeleteStorage(_ common.Address, key []byte) error { ...@@ -156,7 +157,7 @@ func (t *odrTrie) DeleteStorage(_ common.Address, key []byte) error {
}) })
} }
// TryDeleteAccount abstracts an account deletion from the trie. // DeleteAccount abstracts an account deletion from the trie.
func (t *odrTrie) DeleteAccount(address common.Address) error { func (t *odrTrie) DeleteAccount(address common.Address) error {
key := crypto.Keccak256(address.Bytes()) key := crypto.Keccak256(address.Bytes())
return t.do(key, func() error { return t.do(key, func() error {
...@@ -164,7 +165,7 @@ func (t *odrTrie) DeleteAccount(address common.Address) error { ...@@ -164,7 +165,7 @@ func (t *odrTrie) DeleteAccount(address common.Address) error {
}) })
} }
func (t *odrTrie) Commit(collectLeaf bool) (common.Hash, *trie.NodeSet) { func (t *odrTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet) {
if t.trie == nil { if t.trie == nil {
return t.id.Root, nil return t.id.Root, nil
} }
......
...@@ -27,9 +27,11 @@ import ( ...@@ -27,9 +27,11 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb" "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/crypto"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/trienode"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
) )
...@@ -184,7 +186,7 @@ func (f *fuzzer) fuzz() int { ...@@ -184,7 +186,7 @@ func (f *fuzzer) fuzz() int {
// Flush trie -> database // Flush trie -> database
rootA, nodes := trieA.Commit(false) rootA, nodes := trieA.Commit(false)
if nodes != nil { if nodes != nil {
dbA.Update(trie.NewWithNodeSet(nodes)) dbA.Update(rootA, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
} }
// Flush memdb -> disk (sponge) // Flush memdb -> disk (sponge)
dbA.Commit(rootA, false) dbA.Commit(rootA, false)
......
...@@ -22,7 +22,9 @@ import ( ...@@ -22,7 +22,9 @@ import (
"fmt" "fmt"
"github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/trienode"
) )
// randTest performs random trie operations. // randTest performs random trie operations.
...@@ -139,11 +141,12 @@ func Fuzz(input []byte) int { ...@@ -139,11 +141,12 @@ func Fuzz(input []byte) int {
} }
func runRandTest(rt randTest) error { func runRandTest(rt randTest) error {
triedb := trie.NewDatabase(rawdb.NewMemoryDatabase()) var (
triedb = trie.NewDatabase(rawdb.NewMemoryDatabase())
tr := trie.NewEmpty(triedb) tr = trie.NewEmpty(triedb)
values := make(map[string]string) // tracks content of the trie origin = types.EmptyRootHash
values = make(map[string]string) // tracks content of the trie
)
for i, step := range rt { for i, step := range rt {
switch step.op { switch step.op {
case opUpdate: case opUpdate:
...@@ -163,7 +166,7 @@ func runRandTest(rt randTest) error { ...@@ -163,7 +166,7 @@ func runRandTest(rt randTest) error {
case opCommit: case opCommit:
hash, nodes := tr.Commit(false) hash, nodes := tr.Commit(false)
if nodes != nil { if nodes != nil {
if err := triedb.Update(trie.NewWithNodeSet(nodes)); err != nil { if err := triedb.Update(hash, origin, trienode.NewWithNodeSet(nodes)); err != nil {
return err return err
} }
} }
...@@ -172,6 +175,7 @@ func runRandTest(rt randTest) error { ...@@ -172,6 +175,7 @@ func runRandTest(rt randTest) error {
return err return err
} }
tr = newtr tr = newtr
origin = hash
case opItercheckhash: case opItercheckhash:
checktr := trie.NewEmpty(triedb) checktr := trie.NewEmpty(triedb)
it := trie.NewIterator(tr.NodeIterator(nil)) it := trie.NewIterator(tr.NodeIterator(nil))
......
...@@ -23,23 +23,17 @@ import ( ...@@ -23,23 +23,17 @@ import (
"github.com/ethereum/go-ethereum/trie/trienode" "github.com/ethereum/go-ethereum/trie/trienode"
) )
// leaf represents a trie leaf node
type leaf struct {
blob []byte // raw blob of leaf
parent common.Hash // the hash of parent node
}
// committer is the tool used for the trie Commit operation. The committer will // committer is the tool used for the trie Commit operation. The committer will
// capture all dirty nodes during the commit process and keep them cached in // capture all dirty nodes during the commit process and keep them cached in
// insertion order. // insertion order.
type committer struct { type committer struct {
nodes *NodeSet nodes *trienode.NodeSet
tracer *tracer tracer *tracer
collectLeaf bool collectLeaf bool
} }
// newCommitter creates a new committer or picks one from the pool. // newCommitter creates a new committer or picks one from the pool.
func newCommitter(nodeset *NodeSet, tracer *tracer, collectLeaf bool) *committer { func newCommitter(nodeset *trienode.NodeSet, tracer *tracer, collectLeaf bool) *committer {
return &committer{ return &committer{
nodes: nodeset, nodes: nodeset,
tracer: tracer, tracer: tracer,
...@@ -139,7 +133,7 @@ func (c *committer) store(path []byte, n node) node { ...@@ -139,7 +133,7 @@ func (c *committer) store(path []byte, n node) node {
// deleted only if the node was existent in database before. // deleted only if the node was existent in database before.
prev, ok := c.tracer.accessList[string(path)] prev, ok := c.tracer.accessList[string(path)]
if ok { if ok {
c.nodes.addNode(path, trienode.NewWithPrev(common.Hash{}, nil, prev)) c.nodes.AddNode(path, trienode.NewWithPrev(common.Hash{}, nil, prev))
} }
return n return n
} }
...@@ -152,7 +146,7 @@ func (c *committer) store(path []byte, n node) node { ...@@ -152,7 +146,7 @@ func (c *committer) store(path []byte, n node) node {
c.tracer.accessList[string(path)], c.tracer.accessList[string(path)],
) )
) )
c.nodes.addNode(path, node) c.nodes.AddNode(path, node)
// Collect the corresponding leaf node if it's required. We don't check // Collect the corresponding leaf node if it's required. We don't check
// full node since it's impossible to store value in fullNode. The key // full node since it's impossible to store value in fullNode. The key
...@@ -160,7 +154,7 @@ func (c *committer) store(path []byte, n node) node { ...@@ -160,7 +154,7 @@ func (c *committer) store(path []byte, n node) node {
if c.collectLeaf { if c.collectLeaf {
if sn, ok := n.(*shortNode); ok { if sn, ok := n.(*shortNode); ok {
if val, ok := sn.Val.(valueNode); ok { if val, ok := sn.Val.(valueNode); ok {
c.nodes.addLeaf(&leaf{blob: val, parent: nhash}) c.nodes.AddLeaf(nhash, val)
} }
} }
} }
...@@ -172,7 +166,7 @@ type mptResolver struct{} ...@@ -172,7 +166,7 @@ type mptResolver struct{}
// ForEach implements childResolver, decodes the provided node and // ForEach implements childResolver, decodes the provided node and
// traverses the children inside. // traverses the children inside.
func (resolver mptResolver) forEach(node []byte, onChild func(common.Hash)) { func (resolver mptResolver) ForEach(node []byte, onChild func(common.Hash)) {
forGatherChildren(mustDecodeNodeUnsafe(nil, node), onChild) forGatherChildren(mustDecodeNodeUnsafe(nil, node), onChild)
} }
......
...@@ -17,17 +17,19 @@ ...@@ -17,17 +17,19 @@
package trie package trie
import ( import (
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/trie/triedb/hashdb"
) )
// Tests that the trie database returns a missing trie node error if attempting // newTestDatabase initializes the trie database with specified scheme.
// to retrieve the meta root. func newTestDatabase(diskdb ethdb.Database, scheme string) *Database {
func TestDatabaseMetarootFetch(t *testing.T) { db := prepare(diskdb, nil)
db := NewDatabase(rawdb.NewMemoryDatabase()) if scheme == rawdb.HashScheme {
if _, err := db.Node(common.Hash{}); err == nil { db.backend = hashdb.New(diskdb, db.cleans, mptResolver{})
t.Fatalf("metaroot retrieval succeeded")
} }
//} else {
// db.backend = snap.New(diskdb, db.cleans, nil)
//}
return db
} }
// Copyright 2022 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package trie
import (
"errors"
"runtime"
"time"
"github.com/VictoriaMetrics/fastcache"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/trie/triedb/hashdb"
"github.com/ethereum/go-ethereum/trie/trienode"
)
// Config defines all necessary options for database.
type Config struct {
Cache int // Memory allowance (MB) to use for caching trie nodes in memory
Journal string // Journal of clean cache to survive node restarts
Preimages bool // Flag whether the preimage of trie key is recorded
}
// backend defines the methods needed to access/update trie nodes in different
// state scheme.
type backend interface {
// Scheme returns the identifier of used storage scheme.
Scheme() string
// Initialized returns an indicator if the state data is already initialized
// according to the state scheme.
Initialized(genesisRoot common.Hash) bool
// Size returns the current storage size of the memory cache in front of the
// persistent database layer.
Size() common.StorageSize
// Update performs a state transition by committing dirty nodes contained
// in the given set in order to update state from the specified parent to
// the specified root.
Update(root common.Hash, parent common.Hash, nodes *trienode.MergedNodeSet) error
// Commit writes all relevant trie nodes belonging to the specified state
// to disk. Report specifies whether logs will be displayed in info level.
Commit(root common.Hash, report bool) error
// Close closes the trie database backend and releases all held resources.
Close() error
}
// Database is the wrapper of the underlying backend which is shared by different
// types of node backend as an entrypoint. It's responsible for all interactions
// relevant with trie nodes and node preimages.
type Database struct {
config *Config // Configuration for trie database
diskdb ethdb.Database // Persistent database to store the snapshot
cleans *fastcache.Cache // Megabytes permitted using for read caches
preimages *preimageStore // The store for caching preimages
backend backend // The backend for managing trie nodes
}
// prepare initializes the database with provided configs, but the
// database backend is still left as nil.
func prepare(diskdb ethdb.Database, config *Config) *Database {
var cleans *fastcache.Cache
if config != nil && config.Cache > 0 {
if config.Journal == "" {
cleans = fastcache.New(config.Cache * 1024 * 1024)
} else {
cleans = fastcache.LoadFromFileOrNew(config.Journal, config.Cache*1024*1024)
}
}
var preimages *preimageStore
if config != nil && config.Preimages {
preimages = newPreimageStore(diskdb)
}
return &Database{
config: config,
diskdb: diskdb,
cleans: cleans,
preimages: preimages,
}
}
// NewDatabase initializes the trie database with default settings, namely
// the legacy hash-based scheme is used by default.
func NewDatabase(diskdb ethdb.Database) *Database {
return NewDatabaseWithConfig(diskdb, nil)
}
// NewDatabaseWithConfig initializes the trie database with provided configs.
// The path-based scheme is not activated yet, always initialized with legacy
// hash-based scheme by default.
func NewDatabaseWithConfig(diskdb ethdb.Database, config *Config) *Database {
db := prepare(diskdb, config)
db.backend = hashdb.New(diskdb, db.cleans, mptResolver{})
return db
}
// Reader returns a reader for accessing all trie nodes with provided state root.
// Nil is returned in case the state is not available.
func (db *Database) Reader(blockRoot common.Hash) Reader {
return db.backend.(*hashdb.Database).Reader(blockRoot)
}
// Update performs a state transition by committing dirty nodes contained in the
// given set in order to update state from the specified parent to the specified
// root. The held pre-images accumulated up to this point will be flushed in case
// the size exceeds the threshold.
func (db *Database) Update(root common.Hash, parent common.Hash, nodes *trienode.MergedNodeSet) error {
if db.preimages != nil {
db.preimages.commit(false)
}
return db.backend.Update(root, parent, nodes)
}
// Commit iterates over all the children of a particular node, writes them out
// to disk. As a side effect, all pre-images accumulated up to this point are
// also written.
func (db *Database) Commit(root common.Hash, report bool) error {
if db.preimages != nil {
db.preimages.commit(true)
}
return db.backend.Commit(root, report)
}
// Size returns the storage size of dirty trie nodes in front of the persistent
// database and the size of cached preimages.
func (db *Database) Size() (common.StorageSize, common.StorageSize) {
var (
storages common.StorageSize
preimages common.StorageSize
)
storages = db.backend.Size()
if db.preimages != nil {
preimages = db.preimages.size()
}
return storages, preimages
}
// Initialized returns an indicator if the state data is already initialized
// according to the state scheme.
func (db *Database) Initialized(genesisRoot common.Hash) bool {
return db.backend.Initialized(genesisRoot)
}
// Scheme returns the node scheme used in the database.
func (db *Database) Scheme() string {
return db.backend.Scheme()
}
// Close flushes the dangling preimages to disk and closes the trie database.
// It is meant to be called when closing the blockchain object, so that all
// resources held can be released correctly.
func (db *Database) Close() error {
if db.preimages != nil {
db.preimages.commit(true)
}
return db.backend.Close()
}
// saveCache saves clean state cache to given directory path
// using specified CPU cores.
func (db *Database) saveCache(dir string, threads int) error {
if db.cleans == nil {
return nil
}
log.Info("Writing clean trie cache to disk", "path", dir, "threads", threads)
start := time.Now()
err := db.cleans.SaveToFileConcurrent(dir, threads)
if err != nil {
log.Error("Failed to persist clean trie cache", "error", err)
return err
}
log.Info("Persisted the clean trie cache", "path", dir, "elapsed", common.PrettyDuration(time.Since(start)))
return nil
}
// SaveCache atomically saves fast cache data to the given dir using all
// available CPU cores.
func (db *Database) SaveCache(dir string) error {
return db.saveCache(dir, runtime.GOMAXPROCS(0))
}
// SaveCachePeriodically atomically saves fast cache data to the given dir with
// the specified interval. All dump operation will only use a single CPU core.
func (db *Database) SaveCachePeriodically(dir string, interval time.Duration, stopCh <-chan struct{}) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
db.saveCache(dir, 1)
case <-stopCh:
return
}
}
}
// Cap iteratively flushes old but still referenced trie nodes until the total
// memory usage goes below the given threshold. The held pre-images accumulated
// up to this point will be flushed in case the size exceeds the threshold.
//
// It's only supported by hash-based database and will return an error for others.
func (db *Database) Cap(limit common.StorageSize) error {
hdb, ok := db.backend.(*hashdb.Database)
if !ok {
return errors.New("not supported")
}
if db.preimages != nil {
db.preimages.commit(false)
}
return hdb.Cap(limit)
}
// Reference adds a new reference from a parent node to a child node. This function
// is used to add reference between internal trie node and external node(e.g. storage
// trie root), all internal trie nodes are referenced together by database itself.
//
// It's only supported by hash-based database and will return an error for others.
func (db *Database) Reference(root common.Hash, parent common.Hash) error {
hdb, ok := db.backend.(*hashdb.Database)
if !ok {
return errors.New("not supported")
}
hdb.Reference(root, parent)
return nil
}
// Dereference removes an existing reference from a root node. It's only
// supported by hash-based database and will return an error for others.
func (db *Database) Dereference(root common.Hash) error {
hdb, ok := db.backend.(*hashdb.Database)
if !ok {
return errors.New("not supported")
}
hdb.Dereference(root)
return nil
}
// Node retrieves the rlp-encoded node blob with provided node hash. It's
// only supported by hash-based database and will return an error for others.
// Note, this function should be deprecated once ETH66 is deprecated.
func (db *Database) Node(hash common.Hash) ([]byte, error) {
hdb, ok := db.backend.(*hashdb.Database)
if !ok {
return nil, errors.New("not supported")
}
return hdb.Node(hash)
}
This diff is collapsed.
// Copyright 2022 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package trie
import (
"fmt"
"sort"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/trie/trienode"
)
// NodeSet contains all dirty nodes collected during the commit operation.
// Each node is keyed by path. It's not thread-safe to use.
type NodeSet struct {
owner common.Hash // the identifier of the trie
leaves []*leaf // the list of dirty leaves
updates int // the count of updated and inserted nodes
deletes int // the count of deleted nodes
// The set of all dirty nodes. Dirty nodes include newly inserted nodes,
// deleted nodes and updated nodes. The original value of the newly
// inserted node must be nil, and the original value of the other two
// types must be non-nil.
nodes map[string]*trienode.WithPrev
}
// NewNodeSet initializes an empty node set to be used for tracking dirty nodes
// from a specific account or storage trie. The owner is zero for the account
// trie and the owning account address hash for storage tries.
func NewNodeSet(owner common.Hash) *NodeSet {
return &NodeSet{
owner: owner,
nodes: make(map[string]*trienode.WithPrev),
}
}
// forEachWithOrder iterates the dirty nodes with the order from bottom to top,
// right to left, nodes with the longest path will be iterated first.
func (set *NodeSet) forEachWithOrder(callback func(path string, n *trienode.Node)) {
var paths sort.StringSlice
for path := range set.nodes {
paths = append(paths, path)
}
// Bottom-up, longest path first
sort.Sort(sort.Reverse(paths))
for _, path := range paths {
callback(path, set.nodes[path].Unwrap())
}
}
// addNode adds the provided dirty node into set.
func (set *NodeSet) addNode(path []byte, n *trienode.WithPrev) {
if n.IsDeleted() {
set.deletes += 1
} else {
set.updates += 1
}
set.nodes[string(path)] = n
}
// addLeaf adds the provided leaf node into set.
func (set *NodeSet) addLeaf(node *leaf) {
set.leaves = append(set.leaves, node)
}
// Size returns the number of dirty nodes in set.
func (set *NodeSet) Size() (int, int) {
return set.updates, set.deletes
}
// Hashes returns the hashes of all updated nodes. TODO(rjl493456442) how can
// we get rid of it?
func (set *NodeSet) Hashes() []common.Hash {
var ret []common.Hash
for _, node := range set.nodes {
ret = append(ret, node.Hash)
}
return ret
}
// Summary returns a string-representation of the NodeSet.
func (set *NodeSet) Summary() string {
var out = new(strings.Builder)
fmt.Fprintf(out, "nodeset owner: %v\n", set.owner)
if set.nodes != nil {
for path, n := range set.nodes {
// Deletion
if n.IsDeleted() {
fmt.Fprintf(out, " [-]: %x prev: %x\n", path, n.Prev)
continue
}
// Insertion
if len(n.Prev) == 0 {
fmt.Fprintf(out, " [+]: %x -> %v\n", path, n.Hash)
continue
}
// Update
fmt.Fprintf(out, " [*]: %x -> %v prev: %x\n", path, n.Hash, n.Prev)
}
}
for _, n := range set.leaves {
fmt.Fprintf(out, "[leaf]: %v\n", n)
}
return out.String()
}
// MergedNodeSet represents a merged dirty node set for a group of tries.
type MergedNodeSet struct {
sets map[common.Hash]*NodeSet
}
// NewMergedNodeSet initializes an empty merged set.
func NewMergedNodeSet() *MergedNodeSet {
return &MergedNodeSet{sets: make(map[common.Hash]*NodeSet)}
}
// NewWithNodeSet constructs a merged nodeset with the provided single set.
func NewWithNodeSet(set *NodeSet) *MergedNodeSet {
merged := NewMergedNodeSet()
merged.Merge(set)
return merged
}
// Merge merges the provided dirty nodes of a trie into the set. The assumption
// is held that no duplicated set belonging to the same trie will be merged twice.
func (set *MergedNodeSet) Merge(other *NodeSet) error {
_, present := set.sets[other.owner]
if present {
return fmt.Errorf("duplicate trie for owner %#x", other.owner)
}
set.sets[other.owner] = other
return nil
}
...@@ -20,6 +20,7 @@ import ( ...@@ -20,6 +20,7 @@ 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/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie/trienode"
) )
// SecureTrie is the old name of StateTrie. // SecureTrie is the old name of StateTrie.
...@@ -212,7 +213,7 @@ func (t *StateTrie) GetKey(shaKey []byte) []byte { ...@@ -212,7 +213,7 @@ func (t *StateTrie) GetKey(shaKey []byte) []byte {
// All cached preimages will be also flushed if preimages recording is enabled. // All cached preimages will be also flushed if preimages recording is enabled.
// Once the trie is committed, it's not usable anymore. A new trie must // Once the trie is committed, it's not usable anymore. A new trie must
// be created with new root and updated trie database for following usage // be created with new root and updated trie database for following usage
func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *NodeSet) { func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet) {
// Write all the pre-images to the actual disk database // Write all the pre-images to the actual disk database
if len(t.getSecKeyCache()) > 0 { if len(t.getSecKeyCache()) > 0 {
if t.preimages != nil { if t.preimages != nil {
......
...@@ -25,7 +25,9 @@ import ( ...@@ -25,7 +25,9 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb" "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/crypto"
"github.com/ethereum/go-ethereum/trie/trienode"
) )
func newEmptySecure() *StateTrie { func newEmptySecure() *StateTrie {
...@@ -59,7 +61,7 @@ func makeTestStateTrie() (*Database, *StateTrie, map[string][]byte) { ...@@ -59,7 +61,7 @@ func makeTestStateTrie() (*Database, *StateTrie, map[string][]byte) {
} }
} }
root, nodes := trie.Commit(false) root, nodes := trie.Commit(false)
if err := triedb.Update(NewWithNodeSet(nodes)); err != nil { if err := triedb.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes)); err != nil {
panic(fmt.Errorf("failed to commit db %v", err)) panic(fmt.Errorf("failed to commit db %v", err))
} }
// Re-create the trie based on the new state // Re-create the trie based on the new state
......
This diff is collapsed.
...@@ -115,7 +115,7 @@ func (t *tracer) copy() *tracer { ...@@ -115,7 +115,7 @@ func (t *tracer) copy() *tracer {
} }
// markDeletions puts all tracked deletions into the provided nodeset. // markDeletions puts all tracked deletions into the provided nodeset.
func (t *tracer) markDeletions(set *NodeSet) { func (t *tracer) markDeletions(set *trienode.NodeSet) {
for path := range t.deletes { for path := range t.deletes {
// It's possible a few deleted nodes were embedded // It's possible a few deleted nodes were embedded
// in their parent before, the deletions can be no // in their parent before, the deletions can be no
...@@ -124,6 +124,6 @@ func (t *tracer) markDeletions(set *NodeSet) { ...@@ -124,6 +124,6 @@ func (t *tracer) markDeletions(set *NodeSet) {
if !ok { if !ok {
continue continue
} }
set.addNode([]byte(path), trienode.NewWithPrev(common.Hash{}, nil, prev)) set.AddNode([]byte(path), trienode.NewWithPrev(common.Hash{}, nil, prev))
} }
} }
...@@ -22,6 +22,8 @@ import ( ...@@ -22,6 +22,8 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/trie/trienode"
) )
var ( var (
...@@ -69,7 +71,7 @@ func testTrieTracer(t *testing.T, vals []struct{ k, v string }) { ...@@ -69,7 +71,7 @@ func testTrieTracer(t *testing.T, vals []struct{ k, v string }) {
insertSet := copySet(trie.tracer.inserts) // copy before commit insertSet := copySet(trie.tracer.inserts) // copy before commit
deleteSet := copySet(trie.tracer.deletes) // copy before commit deleteSet := copySet(trie.tracer.deletes) // copy before commit
root, nodes := trie.Commit(false) root, nodes := trie.Commit(false)
db.Update(NewWithNodeSet(nodes)) db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
seen := setKeys(iterNodes(db, root)) seen := setKeys(iterNodes(db, root))
if !compareSet(insertSet, seen) { if !compareSet(insertSet, seen) {
...@@ -135,7 +137,7 @@ func testAccessList(t *testing.T, vals []struct{ k, v string }) { ...@@ -135,7 +137,7 @@ func testAccessList(t *testing.T, vals []struct{ k, v string }) {
trie.MustUpdate([]byte(val.k), []byte(val.v)) trie.MustUpdate([]byte(val.k), []byte(val.v))
} }
root, nodes := trie.Commit(false) root, nodes := trie.Commit(false)
db.Update(NewWithNodeSet(nodes)) db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
trie, _ = New(TrieID(root), db) trie, _ = New(TrieID(root), db)
if err := verifyAccessList(orig, trie, nodes); err != nil { if err := verifyAccessList(orig, trie, nodes); err != nil {
...@@ -143,13 +145,14 @@ func testAccessList(t *testing.T, vals []struct{ k, v string }) { ...@@ -143,13 +145,14 @@ func testAccessList(t *testing.T, vals []struct{ k, v string }) {
} }
// Update trie // Update trie
parent := root
trie, _ = New(TrieID(root), db) trie, _ = New(TrieID(root), db)
orig = trie.Copy() orig = trie.Copy()
for _, val := range vals { for _, val := range vals {
trie.MustUpdate([]byte(val.k), randBytes(32)) trie.MustUpdate([]byte(val.k), randBytes(32))
} }
root, nodes = trie.Commit(false) root, nodes = trie.Commit(false)
db.Update(NewWithNodeSet(nodes)) db.Update(root, parent, trienode.NewWithNodeSet(nodes))
trie, _ = New(TrieID(root), db) trie, _ = New(TrieID(root), db)
if err := verifyAccessList(orig, trie, nodes); err != nil { if err := verifyAccessList(orig, trie, nodes); err != nil {
...@@ -157,6 +160,7 @@ func testAccessList(t *testing.T, vals []struct{ k, v string }) { ...@@ -157,6 +160,7 @@ func testAccessList(t *testing.T, vals []struct{ k, v string }) {
} }
// Add more new nodes // Add more new nodes
parent = root
trie, _ = New(TrieID(root), db) trie, _ = New(TrieID(root), db)
orig = trie.Copy() orig = trie.Copy()
var keys []string var keys []string
...@@ -166,7 +170,7 @@ func testAccessList(t *testing.T, vals []struct{ k, v string }) { ...@@ -166,7 +170,7 @@ func testAccessList(t *testing.T, vals []struct{ k, v string }) {
trie.MustUpdate(key, randBytes(32)) trie.MustUpdate(key, randBytes(32))
} }
root, nodes = trie.Commit(false) root, nodes = trie.Commit(false)
db.Update(NewWithNodeSet(nodes)) db.Update(root, parent, trienode.NewWithNodeSet(nodes))
trie, _ = New(TrieID(root), db) trie, _ = New(TrieID(root), db)
if err := verifyAccessList(orig, trie, nodes); err != nil { if err := verifyAccessList(orig, trie, nodes); err != nil {
...@@ -174,13 +178,14 @@ func testAccessList(t *testing.T, vals []struct{ k, v string }) { ...@@ -174,13 +178,14 @@ func testAccessList(t *testing.T, vals []struct{ k, v string }) {
} }
// Partial deletions // Partial deletions
parent = root
trie, _ = New(TrieID(root), db) trie, _ = New(TrieID(root), db)
orig = trie.Copy() orig = trie.Copy()
for _, key := range keys { for _, key := range keys {
trie.MustUpdate([]byte(key), nil) trie.MustUpdate([]byte(key), nil)
} }
root, nodes = trie.Commit(false) root, nodes = trie.Commit(false)
db.Update(NewWithNodeSet(nodes)) db.Update(root, parent, trienode.NewWithNodeSet(nodes))
trie, _ = New(TrieID(root), db) trie, _ = New(TrieID(root), db)
if err := verifyAccessList(orig, trie, nodes); err != nil { if err := verifyAccessList(orig, trie, nodes); err != nil {
...@@ -188,13 +193,14 @@ func testAccessList(t *testing.T, vals []struct{ k, v string }) { ...@@ -188,13 +193,14 @@ func testAccessList(t *testing.T, vals []struct{ k, v string }) {
} }
// Delete all // Delete all
parent = root
trie, _ = New(TrieID(root), db) trie, _ = New(TrieID(root), db)
orig = trie.Copy() orig = trie.Copy()
for _, val := range vals { for _, val := range vals {
trie.MustUpdate([]byte(val.k), nil) trie.MustUpdate([]byte(val.k), nil)
} }
root, nodes = trie.Commit(false) root, nodes = trie.Commit(false)
db.Update(NewWithNodeSet(nodes)) db.Update(root, parent, trienode.NewWithNodeSet(nodes))
trie, _ = New(TrieID(root), db) trie, _ = New(TrieID(root), db)
if err := verifyAccessList(orig, trie, nodes); err != nil { if err := verifyAccessList(orig, trie, nodes); err != nil {
...@@ -213,7 +219,7 @@ func TestAccessListLeak(t *testing.T) { ...@@ -213,7 +219,7 @@ func TestAccessListLeak(t *testing.T) {
trie.MustUpdate([]byte(val.k), []byte(val.v)) trie.MustUpdate([]byte(val.k), []byte(val.v))
} }
root, nodes := trie.Commit(false) root, nodes := trie.Commit(false)
db.Update(NewWithNodeSet(nodes)) db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
var cases = []struct { var cases = []struct {
op func(tr *Trie) op func(tr *Trie)
...@@ -263,15 +269,16 @@ func TestTinyTree(t *testing.T) { ...@@ -263,15 +269,16 @@ func TestTinyTree(t *testing.T) {
trie.MustUpdate([]byte(val.k), randBytes(32)) trie.MustUpdate([]byte(val.k), randBytes(32))
} }
root, set := trie.Commit(false) root, set := trie.Commit(false)
db.Update(NewWithNodeSet(set)) db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(set))
parent := root
trie, _ = New(TrieID(root), db) trie, _ = New(TrieID(root), db)
orig := trie.Copy() orig := trie.Copy()
for _, val := range tiny { for _, val := range tiny {
trie.MustUpdate([]byte(val.k), []byte(val.v)) trie.MustUpdate([]byte(val.k), []byte(val.v))
} }
root, set = trie.Commit(false) root, set = trie.Commit(false)
db.Update(NewWithNodeSet(set)) db.Update(root, parent, trienode.NewWithNodeSet(set))
trie, _ = New(TrieID(root), db) trie, _ = New(TrieID(root), db)
if err := verifyAccessList(orig, trie, set); err != nil { if err := verifyAccessList(orig, trie, set); err != nil {
......
...@@ -25,6 +25,7 @@ import ( ...@@ -25,6 +25,7 @@ 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/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/trie/trienode"
) )
// Trie is a Merkle Patricia Trie. Use New to create a trie that sits on // Trie is a Merkle Patricia Trie. Use New to create a trie that sits on
...@@ -95,7 +96,7 @@ func New(id *ID, db NodeReader) (*Trie, error) { ...@@ -95,7 +96,7 @@ func New(id *ID, db NodeReader) (*Trie, error) {
// NewEmpty is a shortcut to create empty tree. It's mostly used in tests. // NewEmpty is a shortcut to create empty tree. It's mostly used in tests.
func NewEmpty(db *Database) *Trie { func NewEmpty(db *Database) *Trie {
tr, _ := New(TrieID(common.Hash{}), db) tr, _ := New(TrieID(types.EmptyRootHash), db)
return tr return tr
} }
...@@ -571,10 +572,10 @@ func (t *Trie) Hash() common.Hash { ...@@ -571,10 +572,10 @@ func (t *Trie) Hash() common.Hash {
// The returned nodeset can be nil if the trie is clean (nothing to commit). // The returned nodeset can be nil if the trie is clean (nothing to commit).
// Once the trie is committed, it's not usable anymore. A new trie must // Once the trie is committed, it's not usable anymore. A new trie must
// be created with new root and updated trie database for following usage // be created with new root and updated trie database for following usage
func (t *Trie) Commit(collectLeaf bool) (common.Hash, *NodeSet) { func (t *Trie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet) {
defer t.tracer.reset() defer t.tracer.reset()
nodes := NewNodeSet(t.owner) nodes := trienode.NewNodeSet(t.owner)
t.tracer.markDeletions(nodes) t.tracer.markDeletions(nodes)
// Trie is empty and can be classified into two types of situations: // Trie is empty and can be classified into two types of situations:
......
...@@ -32,9 +32,9 @@ type Reader interface { ...@@ -32,9 +32,9 @@ type Reader interface {
// NodeReader wraps all the necessary functions for accessing trie node. // NodeReader wraps all the necessary functions for accessing trie node.
type NodeReader interface { type NodeReader interface {
// GetReader returns a reader for accessing all trie nodes with provided // Reader returns a reader for accessing all trie nodes with provided
// state root. Nil is returned in case the state is not available. // state root. Nil is returned in case the state is not available.
GetReader(root common.Hash) Reader Reader(root common.Hash) Reader
} }
// trieReader is a wrapper of the underlying node reader. It's not safe // trieReader is a wrapper of the underlying node reader. It's not safe
...@@ -47,7 +47,7 @@ type trieReader struct { ...@@ -47,7 +47,7 @@ type trieReader struct {
// newTrieReader initializes the trie reader with the given node reader. // newTrieReader initializes the trie reader with the given node reader.
func newTrieReader(stateRoot, owner common.Hash, db NodeReader) (*trieReader, error) { func newTrieReader(stateRoot, owner common.Hash, db NodeReader) (*trieReader, error) {
reader := db.GetReader(stateRoot) reader := db.Reader(stateRoot)
if reader == nil { if reader == nil {
return nil, fmt.Errorf("state not found #%x", stateRoot) return nil, fmt.Errorf("state not found #%x", stateRoot)
} }
......
...@@ -35,6 +35,7 @@ import ( ...@@ -35,6 +35,7 @@ import (
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie/trienode"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
) )
...@@ -73,18 +74,23 @@ func TestMissingRoot(t *testing.T) { ...@@ -73,18 +74,23 @@ func TestMissingRoot(t *testing.T) {
} }
} }
func TestMissingNodeDisk(t *testing.T) { testMissingNode(t, false) } func TestMissingNode(t *testing.T) {
func TestMissingNodeMemonly(t *testing.T) { testMissingNode(t, true) } testMissingNode(t, false, rawdb.HashScheme)
//testMissingNode(t, false, rawdb.PathScheme)
testMissingNode(t, true, rawdb.HashScheme)
//testMissingNode(t, true, rawdb.PathScheme)
}
func testMissingNode(t *testing.T, memonly bool) { func testMissingNode(t *testing.T, memonly bool, scheme string) {
diskdb := rawdb.NewMemoryDatabase() diskdb := rawdb.NewMemoryDatabase()
triedb := NewDatabase(diskdb) triedb := newTestDatabase(diskdb, scheme)
trie := NewEmpty(triedb) trie := NewEmpty(triedb)
updateString(trie, "120000", "qwerqwerqwerqwerqwerqwerqwerqwer") updateString(trie, "120000", "qwerqwerqwerqwerqwerqwerqwerqwer")
updateString(trie, "123456", "asdfasdfasdfasdfasdfasdfasdfasdf") updateString(trie, "123456", "asdfasdfasdfasdfasdfasdfasdfasdf")
root, nodes := trie.Commit(false) root, nodes := trie.Commit(false)
triedb.Update(NewWithNodeSet(nodes)) triedb.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
if !memonly { if !memonly {
triedb.Commit(root, false) triedb.Commit(root, false)
} }
...@@ -115,34 +121,39 @@ func testMissingNode(t *testing.T, memonly bool) { ...@@ -115,34 +121,39 @@ func testMissingNode(t *testing.T, memonly bool) {
t.Errorf("Unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
} }
hash := common.HexToHash("0xe1d943cc8f061a0c0b98162830b970395ac9315654824bf21b73b891365262f9") var (
path []byte
hash = common.HexToHash("0xe1d943cc8f061a0c0b98162830b970395ac9315654824bf21b73b891365262f9")
)
for p, n := range nodes.Nodes {
if n.Hash == hash {
path = common.CopyBytes([]byte(p))
break
}
}
trie, _ = New(TrieID(root), triedb)
if memonly { if memonly {
delete(triedb.dirties, hash) trie.reader.banned = map[string]struct{}{string(path): {}}
} else { } else {
diskdb.Delete(hash[:]) rawdb.DeleteTrieNode(diskdb, common.Hash{}, path, hash, scheme)
} }
trie, _ = New(TrieID(root), triedb)
_, err = trie.Get([]byte("120000")) _, err = trie.Get([]byte("120000"))
if _, ok := err.(*MissingNodeError); !ok { if _, ok := err.(*MissingNodeError); !ok {
t.Errorf("Wrong error: %v", err) t.Errorf("Wrong error: %v", err)
} }
trie, _ = New(TrieID(root), triedb)
_, err = trie.Get([]byte("120099")) _, err = trie.Get([]byte("120099"))
if _, ok := err.(*MissingNodeError); !ok { if _, ok := err.(*MissingNodeError); !ok {
t.Errorf("Wrong error: %v", err) t.Errorf("Wrong error: %v", err)
} }
trie, _ = New(TrieID(root), triedb)
_, err = trie.Get([]byte("123456")) _, err = trie.Get([]byte("123456"))
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
} }
trie, _ = New(TrieID(root), triedb)
err = trie.Update([]byte("120099"), []byte("zxcv")) err = trie.Update([]byte("120099"), []byte("zxcv"))
if _, ok := err.(*MissingNodeError); !ok { if _, ok := err.(*MissingNodeError); !ok {
t.Errorf("Wrong error: %v", err) t.Errorf("Wrong error: %v", err)
} }
trie, _ = New(TrieID(root), triedb)
err = trie.Delete([]byte("123456")) err = trie.Delete([]byte("123456"))
if _, ok := err.(*MissingNodeError); !ok { if _, ok := err.(*MissingNodeError); !ok {
t.Errorf("Wrong error: %v", err) t.Errorf("Wrong error: %v", err)
...@@ -192,7 +203,7 @@ func TestGet(t *testing.T) { ...@@ -192,7 +203,7 @@ func TestGet(t *testing.T) {
return return
} }
root, nodes := trie.Commit(false) root, nodes := trie.Commit(false)
db.Update(NewWithNodeSet(nodes)) db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
trie, _ = New(TrieID(root), db) trie, _ = New(TrieID(root), db)
} }
} }
...@@ -249,8 +260,8 @@ func TestEmptyValues(t *testing.T) { ...@@ -249,8 +260,8 @@ func TestEmptyValues(t *testing.T) {
} }
func TestReplication(t *testing.T) { func TestReplication(t *testing.T) {
triedb := NewDatabase(rawdb.NewMemoryDatabase()) db := NewDatabase(rawdb.NewMemoryDatabase())
trie := NewEmpty(triedb) trie := NewEmpty(db)
vals := []struct{ k, v string }{ vals := []struct{ k, v string }{
{"do", "verb"}, {"do", "verb"},
{"ether", "wookiedoo"}, {"ether", "wookiedoo"},
...@@ -263,13 +274,13 @@ func TestReplication(t *testing.T) { ...@@ -263,13 +274,13 @@ func TestReplication(t *testing.T) {
for _, val := range vals { for _, val := range vals {
updateString(trie, val.k, val.v) updateString(trie, val.k, val.v)
} }
exp, nodes := trie.Commit(false) root, nodes := trie.Commit(false)
triedb.Update(NewWithNodeSet(nodes)) db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
// create a new trie on top of the database and check that lookups work. // create a new trie on top of the database and check that lookups work.
trie2, err := New(TrieID(exp), triedb) trie2, err := New(TrieID(root), db)
if err != nil { if err != nil {
t.Fatalf("can't recreate trie at %x: %v", exp, err) t.Fatalf("can't recreate trie at %x: %v", root, err)
} }
for _, kv := range vals { for _, kv := range vals {
if string(getString(trie2, kv.k)) != kv.v { if string(getString(trie2, kv.k)) != kv.v {
...@@ -277,17 +288,17 @@ func TestReplication(t *testing.T) { ...@@ -277,17 +288,17 @@ func TestReplication(t *testing.T) {
} }
} }
hash, nodes := trie2.Commit(false) hash, nodes := trie2.Commit(false)
if hash != exp { if hash != root {
t.Errorf("root failure. expected %x got %x", exp, hash) t.Errorf("root failure. expected %x got %x", root, hash)
} }
// recreate the trie after commit // recreate the trie after commit
if nodes != nil { if nodes != nil {
triedb.Update(NewWithNodeSet(nodes)) db.Update(hash, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
} }
trie2, err = New(TrieID(hash), triedb) trie2, err = New(TrieID(hash), db)
if err != nil { if err != nil {
t.Fatalf("can't recreate trie at %x: %v", exp, err) t.Fatalf("can't recreate trie at %x: %v", hash, err)
} }
// perform some insertions on the new trie. // perform some insertions on the new trie.
vals2 := []struct{ k, v string }{ vals2 := []struct{ k, v string }{
...@@ -304,8 +315,8 @@ func TestReplication(t *testing.T) { ...@@ -304,8 +315,8 @@ func TestReplication(t *testing.T) {
for _, val := range vals2 { for _, val := range vals2 {
updateString(trie2, val.k, val.v) updateString(trie2, val.k, val.v)
} }
if hash := trie2.Hash(); hash != exp { if trie2.Hash() != hash {
t.Errorf("root failure. expected %x got %x", exp, hash) t.Errorf("root failure. expected %x got %x", hash, hash)
} }
} }
...@@ -402,12 +413,12 @@ func (randTest) Generate(r *rand.Rand, size int) reflect.Value { ...@@ -402,12 +413,12 @@ func (randTest) Generate(r *rand.Rand, size int) reflect.Value {
return reflect.ValueOf(steps) return reflect.ValueOf(steps)
} }
func verifyAccessList(old *Trie, new *Trie, set *NodeSet) error { func verifyAccessList(old *Trie, new *Trie, set *trienode.NodeSet) error {
deletes, inserts, updates := diffTries(old, new) deletes, inserts, updates := diffTries(old, new)
// Check insertion set // Check insertion set
for path := range inserts { for path := range inserts {
n, ok := set.nodes[path] n, ok := set.Nodes[path]
if !ok || n.IsDeleted() { if !ok || n.IsDeleted() {
return errors.New("expect new node") return errors.New("expect new node")
} }
...@@ -417,7 +428,7 @@ func verifyAccessList(old *Trie, new *Trie, set *NodeSet) error { ...@@ -417,7 +428,7 @@ func verifyAccessList(old *Trie, new *Trie, set *NodeSet) error {
} }
// Check deletion set // Check deletion set
for path, blob := range deletes { for path, blob := range deletes {
n, ok := set.nodes[path] n, ok := set.Nodes[path]
if !ok || !n.IsDeleted() { if !ok || !n.IsDeleted() {
return errors.New("expect deleted node") return errors.New("expect deleted node")
} }
...@@ -430,7 +441,7 @@ func verifyAccessList(old *Trie, new *Trie, set *NodeSet) error { ...@@ -430,7 +441,7 @@ func verifyAccessList(old *Trie, new *Trie, set *NodeSet) error {
} }
// Check update set // Check update set
for path, blob := range updates { for path, blob := range updates {
n, ok := set.nodes[path] n, ok := set.Nodes[path]
if !ok || n.IsDeleted() { if !ok || n.IsDeleted() {
return errors.New("expect updated node") return errors.New("expect updated node")
} }
...@@ -445,8 +456,13 @@ func verifyAccessList(old *Trie, new *Trie, set *NodeSet) error { ...@@ -445,8 +456,13 @@ func verifyAccessList(old *Trie, new *Trie, set *NodeSet) error {
} }
func runRandTest(rt randTest) bool { func runRandTest(rt randTest) bool {
var scheme = rawdb.HashScheme
//if rand.Intn(2) == 0 {
// scheme = rawdb.PathScheme
//}
var ( var (
triedb = NewDatabase(rawdb.NewMemoryDatabase()) origin = types.EmptyRootHash
triedb = newTestDatabase(rawdb.NewMemoryDatabase(), scheme)
tr = NewEmpty(triedb) tr = NewEmpty(triedb)
values = make(map[string]string) // tracks content of the trie values = make(map[string]string) // tracks content of the trie
origTrie = NewEmpty(triedb) origTrie = NewEmpty(triedb)
...@@ -487,7 +503,7 @@ func runRandTest(rt randTest) bool { ...@@ -487,7 +503,7 @@ func runRandTest(rt randTest) bool {
case opCommit: case opCommit:
root, nodes := tr.Commit(true) root, nodes := tr.Commit(true)
if nodes != nil { if nodes != nil {
triedb.Update(NewWithNodeSet(nodes)) triedb.Update(root, origin, trienode.NewWithNodeSet(nodes))
} }
newtr, err := New(TrieID(root), triedb) newtr, err := New(TrieID(root), triedb)
if err != nil { if err != nil {
...@@ -502,6 +518,7 @@ func runRandTest(rt randTest) bool { ...@@ -502,6 +518,7 @@ func runRandTest(rt randTest) bool {
} }
tr = newtr tr = newtr
origTrie = tr.Copy() origTrie = tr.Copy()
origin = root
case opItercheckhash: case opItercheckhash:
checktr := NewEmpty(triedb) checktr := NewEmpty(triedb)
it := NewIterator(tr.NodeIterator(nil)) it := NewIterator(tr.NodeIterator(nil))
...@@ -821,7 +838,7 @@ func TestCommitSequence(t *testing.T) { ...@@ -821,7 +838,7 @@ func TestCommitSequence(t *testing.T) {
} }
// Flush trie -> database // Flush trie -> database
root, nodes := trie.Commit(false) root, nodes := trie.Commit(false)
db.Update(NewWithNodeSet(nodes)) db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
// Flush memdb -> disk (sponge) // Flush memdb -> disk (sponge)
db.Commit(root, false) db.Commit(root, false)
if got, exp := s.sponge.Sum(nil), tc.expWriteSeqHash; !bytes.Equal(got, exp) { if got, exp := s.sponge.Sum(nil), tc.expWriteSeqHash; !bytes.Equal(got, exp) {
...@@ -862,7 +879,7 @@ func TestCommitSequenceRandomBlobs(t *testing.T) { ...@@ -862,7 +879,7 @@ func TestCommitSequenceRandomBlobs(t *testing.T) {
} }
// Flush trie -> database // Flush trie -> database
root, nodes := trie.Commit(false) root, nodes := trie.Commit(false)
db.Update(NewWithNodeSet(nodes)) db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
// Flush memdb -> disk (sponge) // Flush memdb -> disk (sponge)
db.Commit(root, false) db.Commit(root, false)
if got, exp := s.sponge.Sum(nil), tc.expWriteSeqHash; !bytes.Equal(got, exp) { if got, exp := s.sponge.Sum(nil), tc.expWriteSeqHash; !bytes.Equal(got, exp) {
...@@ -902,7 +919,7 @@ func TestCommitSequenceStackTrie(t *testing.T) { ...@@ -902,7 +919,7 @@ func TestCommitSequenceStackTrie(t *testing.T) {
// Flush trie -> database // Flush trie -> database
root, nodes := trie.Commit(false) root, nodes := trie.Commit(false)
// Flush memdb -> disk (sponge) // Flush memdb -> disk (sponge)
db.Update(NewWithNodeSet(nodes)) db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
db.Commit(root, false) db.Commit(root, false)
// And flush stacktrie -> disk // And flush stacktrie -> disk
stRoot, err := stTrie.Commit() stRoot, err := stTrie.Commit()
...@@ -950,7 +967,7 @@ func TestCommitSequenceSmallRoot(t *testing.T) { ...@@ -950,7 +967,7 @@ func TestCommitSequenceSmallRoot(t *testing.T) {
// Flush trie -> database // Flush trie -> database
root, nodes := trie.Commit(false) root, nodes := trie.Commit(false)
// Flush memdb -> disk (sponge) // Flush memdb -> disk (sponge)
db.Update(NewWithNodeSet(nodes)) db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
db.Commit(root, false) db.Commit(root, false)
// And flush stacktrie -> disk // And flush stacktrie -> disk
stRoot, err := stTrie.Commit() stRoot, err := stTrie.Commit()
...@@ -1121,8 +1138,8 @@ func benchmarkDerefRootFixedSize(b *testing.B, addresses [][20]byte, accounts [] ...@@ -1121,8 +1138,8 @@ func benchmarkDerefRootFixedSize(b *testing.B, addresses [][20]byte, accounts []
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i]) trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i])
} }
h := trie.Hash() h := trie.Hash()
_, nodes := trie.Commit(false) root, nodes := trie.Commit(false)
triedb.Update(NewWithNodeSet(nodes)) triedb.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
b.StartTimer() b.StartTimer()
triedb.Dereference(h) triedb.Dereference(h)
b.StopTimer() b.StopTimer()
......
...@@ -16,7 +16,13 @@ ...@@ -16,7 +16,13 @@
package trienode package trienode
import "github.com/ethereum/go-ethereum/common" import (
"fmt"
"sort"
"strings"
"github.com/ethereum/go-ethereum/common"
)
// Node is a wrapper which contains the encoded blob of the trie node and its // Node is a wrapper which contains the encoded blob of the trie node and its
// unique hash identifier. It is general enough that can be used to represent // unique hash identifier. It is general enough that can be used to represent
...@@ -65,3 +71,127 @@ func NewWithPrev(hash common.Hash, blob []byte, prev []byte) *WithPrev { ...@@ -65,3 +71,127 @@ func NewWithPrev(hash common.Hash, blob []byte, prev []byte) *WithPrev {
Prev: prev, Prev: prev,
} }
} }
// leaf represents a trie leaf node
type leaf struct {
Blob []byte // raw blob of leaf
Parent common.Hash // the hash of parent node
}
// NodeSet contains a set of nodes collected during the commit operation.
// Each node is keyed by path. It's not thread-safe to use.
type NodeSet struct {
Owner common.Hash
Leaves []*leaf
Nodes map[string]*WithPrev
updates int // the count of updated and inserted nodes
deletes int // the count of deleted nodes
}
// NewNodeSet initializes a node set. The owner is zero for the account trie and
// the owning account address hash for storage tries.
func NewNodeSet(owner common.Hash) *NodeSet {
return &NodeSet{
Owner: owner,
Nodes: make(map[string]*WithPrev),
}
}
// ForEachWithOrder iterates the nodes with the order from bottom to top,
// right to left, nodes with the longest path will be iterated first.
func (set *NodeSet) ForEachWithOrder(callback func(path string, n *Node)) {
var paths sort.StringSlice
for path := range set.Nodes {
paths = append(paths, path)
}
// Bottom-up, longest path first
sort.Sort(sort.Reverse(paths))
for _, path := range paths {
callback(path, set.Nodes[path].Unwrap())
}
}
// AddNode adds the provided node into set.
func (set *NodeSet) AddNode(path []byte, n *WithPrev) {
if n.IsDeleted() {
set.deletes += 1
} else {
set.updates += 1
}
set.Nodes[string(path)] = n
}
// AddLeaf adds the provided leaf node into set. TODO(rjl493456442) how can
// we get rid of it?
func (set *NodeSet) AddLeaf(parent common.Hash, blob []byte) {
set.Leaves = append(set.Leaves, &leaf{Blob: blob, Parent: parent})
}
// Size returns the number of dirty nodes in set.
func (set *NodeSet) Size() (int, int) {
return set.updates, set.deletes
}
// Hashes returns the hashes of all updated nodes. TODO(rjl493456442) how can
// we get rid of it?
func (set *NodeSet) Hashes() []common.Hash {
var ret []common.Hash
for _, node := range set.Nodes {
ret = append(ret, node.Hash)
}
return ret
}
// Summary returns a string-representation of the NodeSet.
func (set *NodeSet) Summary() string {
var out = new(strings.Builder)
fmt.Fprintf(out, "nodeset owner: %v\n", set.Owner)
if set.Nodes != nil {
for path, n := range set.Nodes {
// Deletion
if n.IsDeleted() {
fmt.Fprintf(out, " [-]: %x prev: %x\n", path, n.Prev)
continue
}
// Insertion
if len(n.Prev) == 0 {
fmt.Fprintf(out, " [+]: %x -> %v\n", path, n.Hash)
continue
}
// Update
fmt.Fprintf(out, " [*]: %x -> %v prev: %x\n", path, n.Hash, n.Prev)
}
}
for _, n := range set.Leaves {
fmt.Fprintf(out, "[leaf]: %v\n", n)
}
return out.String()
}
// MergedNodeSet represents a merged node set for a group of tries.
type MergedNodeSet struct {
Sets map[common.Hash]*NodeSet
}
// NewMergedNodeSet initializes an empty merged set.
func NewMergedNodeSet() *MergedNodeSet {
return &MergedNodeSet{Sets: make(map[common.Hash]*NodeSet)}
}
// NewWithNodeSet constructs a merged nodeset with the provided single set.
func NewWithNodeSet(set *NodeSet) *MergedNodeSet {
merged := NewMergedNodeSet()
merged.Merge(set)
return merged
}
// Merge merges the provided dirty nodes of a trie into the set. The assumption
// is held that no duplicated set belonging to the same trie will be merged twice.
func (set *MergedNodeSet) Merge(other *NodeSet) error {
_, present := set.Sets[other.Owner]
if present {
return fmt.Errorf("duplicate trie for owner %#x", other.Owner)
}
set.Sets[other.Owner] = other
return nil
}
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