Skip to content

Commit 2bc4470

Browse files
committed
all: port boring changes from pbss
1 parent 5d3f580 commit 2bc4470

29 files changed

+1045
-549
lines changed

core/blockchain.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -982,8 +982,8 @@ func (bc *BlockChain) Stop() {
982982
}
983983
}
984984
// Flush the collected preimages to disk
985-
if err := bc.stateCache.TrieDB().CommitPreimages(); err != nil {
986-
log.Error("Failed to commit trie preimages", "err", err)
985+
if err := bc.stateCache.TrieDB().Close(); err != nil {
986+
log.Error("Failed to close trie db", "err", err)
987987
}
988988
// Ensure all live cached entries be saved into disk, so that we can skip
989989
// cache warmup when node restarts.

core/blockchain_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,7 @@ func TestTrieForkGC(t *testing.T) {
17011701
chain.stateCache.TrieDB().Dereference(blocks[len(blocks)-1-i].Root())
17021702
chain.stateCache.TrieDB().Dereference(forks[len(blocks)-1-i].Root())
17031703
}
1704-
if len(chain.stateCache.TrieDB().Nodes()) > 0 {
1704+
if nodes, _ := chain.TrieDB().Size(); nodes > 0 {
17051705
t.Fatalf("stale tries still alive after garbase collection")
17061706
}
17071707
}

core/rawdb/schema.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"encoding/binary"
2323

2424
"github.com/ethereum/go-ethereum/common"
25+
"github.com/ethereum/go-ethereum/crypto"
2526
"github.com/ethereum/go-ethereum/metrics"
2627
)
2728

@@ -248,3 +249,52 @@ func accountTrieNodeKey(path []byte) []byte {
248249
func storageTrieNodeKey(accountHash common.Hash, path []byte) []byte {
249250
return append(append(trieNodeStoragePrefix, accountHash.Bytes()...), path...)
250251
}
252+
253+
// IsLegacyTrieNode reports whether a provided database entry is a legacy trie
254+
// node. The characteristics of legacy trie node are:
255+
// - the key length is 32 bytes
256+
// - the key is the hash of val
257+
func IsLegacyTrieNode(key []byte, val []byte) bool {
258+
if len(key) != common.HashLength {
259+
return false
260+
}
261+
return bytes.Equal(key, crypto.Keccak256(val))
262+
}
263+
264+
// IsAccountTrieNode reports whether a provided database entry is an account
265+
// trie node in path-based state scheme.
266+
func IsAccountTrieNode(key []byte) (bool, []byte) {
267+
if !bytes.HasPrefix(key, trieNodeAccountPrefix) {
268+
return false, nil
269+
}
270+
// The remaining key should only consist a hex node path
271+
// whose length is in the range 0 to 64 (64 is excluded
272+
// since leaves are always embedded in parent).
273+
remain := key[len(trieNodeAccountPrefix):]
274+
if len(remain) >= common.HashLength*2 {
275+
return false, nil
276+
}
277+
return true, remain
278+
}
279+
280+
// IsStorageTrieNode reports whether a provided database entry is a storage
281+
// trie node in path-based state scheme.
282+
func IsStorageTrieNode(key []byte) (bool, common.Hash, []byte) {
283+
if !bytes.HasPrefix(key, trieNodeStoragePrefix) {
284+
return false, common.Hash{}, nil
285+
}
286+
// The remaining key consists of 2 parts:
287+
// - 32 bytes account hash
288+
// - hex node path whose length is in the range 0 to 64
289+
remain := key[len(trieNodeStoragePrefix):]
290+
if len(remain) < common.HashLength {
291+
return false, common.Hash{}, nil
292+
}
293+
accountHash := common.BytesToHash(remain[:common.HashLength])
294+
remain = remain[common.HashLength:]
295+
296+
if len(remain) >= common.HashLength*2 {
297+
return false, common.Hash{}, nil
298+
}
299+
return true, accountHash, remain
300+
}

core/state/database.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/ethereum/go-ethereum/core/types"
2727
"github.com/ethereum/go-ethereum/ethdb"
2828
"github.com/ethereum/go-ethereum/trie"
29+
"github.com/ethereum/go-ethereum/trie/trienode"
2930
)
3031

3132
const (
@@ -109,7 +110,7 @@ type Trie interface {
109110
// The returned nodeset can be nil if the trie is clean(nothing to commit).
110111
// Once the trie is committed, it's not usable anymore. A new trie must
111112
// be created with new root and updated trie database for following usage
112-
Commit(collectLeaf bool) (common.Hash, *trie.NodeSet)
113+
Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet)
113114

114115
// NodeIterator returns an iterator that returns nodes of the trie. Iteration
115116
// starts at the key after the given start key.

core/state/iterator_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/ethereum/go-ethereum/common"
2323
"github.com/ethereum/go-ethereum/core/rawdb"
24+
"github.com/ethereum/go-ethereum/crypto"
2425
)
2526

2627
// Tests that the node iterator indeed walks over the entire database contents.
@@ -85,9 +86,19 @@ func TestNodeIteratorCoverage(t *testing.T) {
8586
// database entry belongs to a trie node or not.
8687
func isTrieNode(scheme string, key, val []byte) (bool, common.Hash) {
8788
if scheme == rawdb.HashScheme {
88-
if len(key) == common.HashLength {
89+
ok := rawdb.IsLegacyTrieNode(key, val)
90+
if ok {
8991
return true, common.BytesToHash(key)
9092
}
93+
} else {
94+
ok, _ := rawdb.IsAccountTrieNode(key)
95+
if ok {
96+
return true, crypto.Keccak256Hash(val)
97+
}
98+
ok, _, _ = rawdb.IsStorageTrieNode(key)
99+
if ok {
100+
return true, crypto.Keccak256Hash(val)
101+
}
91102
}
92103
return false, common.Hash{}
93104
}

core/state/snapshot/generate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/ethereum/go-ethereum/log"
3333
"github.com/ethereum/go-ethereum/rlp"
3434
"github.com/ethereum/go-ethereum/trie"
35+
"github.com/ethereum/go-ethereum/trie/trienode"
3536
)
3637

3738
var (
@@ -363,7 +364,7 @@ func (dl *diskLayer) generateRange(ctx *generatorContext, trieId *trie.ID, prefi
363364
}
364365
root, nodes := snapTrie.Commit(false)
365366
if nodes != nil {
366-
tdb.Update(trie.NewWithNodeSet(nodes))
367+
tdb.Update(root, common.Hash{}, trienode.NewWithNodeSet(nodes))
367368
tdb.Commit(root, false)
368369
}
369370
resolver = func(owner common.Hash, path []byte, hash common.Hash) []byte {

core/state/snapshot/generate_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/ethereum/go-ethereum/log"
3131
"github.com/ethereum/go-ethereum/rlp"
3232
"github.com/ethereum/go-ethereum/trie"
33+
"github.com/ethereum/go-ethereum/trie/trienode"
3334
"golang.org/x/crypto/sha3"
3435
)
3536

@@ -144,7 +145,7 @@ type testHelper struct {
144145
diskdb ethdb.Database
145146
triedb *trie.Database
146147
accTrie *trie.StateTrie
147-
nodes *trie.MergedNodeSet
148+
nodes *trienode.MergedNodeSet
148149
}
149150

150151
func newHelper() *testHelper {
@@ -155,7 +156,7 @@ func newHelper() *testHelper {
155156
diskdb: diskdb,
156157
triedb: triedb,
157158
accTrie: accTrie,
158-
nodes: trie.NewMergedNodeSet(),
159+
nodes: trienode.NewMergedNodeSet(),
159160
}
160161
}
161162

@@ -203,7 +204,7 @@ func (t *testHelper) Commit() common.Hash {
203204
if nodes != nil {
204205
t.nodes.Merge(nodes)
205206
}
206-
t.triedb.Update(t.nodes)
207+
t.triedb.Update(root, common.Hash{}, t.nodes)
207208
t.triedb.Commit(root, false)
208209
return root
209210
}

core/state/state_object.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"github.com/ethereum/go-ethereum/crypto"
2929
"github.com/ethereum/go-ethereum/metrics"
3030
"github.com/ethereum/go-ethereum/rlp"
31-
"github.com/ethereum/go-ethereum/trie"
31+
"github.com/ethereum/go-ethereum/trie/trienode"
3232
)
3333

3434
type Code []byte
@@ -350,7 +350,7 @@ func (s *stateObject) updateRoot(db Database) {
350350

351351
// commitTrie submits the storage changes into the storage trie and re-computes
352352
// the root. Besides, all trie changes will be collected in a nodeset and returned.
353-
func (s *stateObject) commitTrie(db Database) (*trie.NodeSet, error) {
353+
func (s *stateObject) commitTrie(db Database) (*trienode.NodeSet, error) {
354354
tr, err := s.updateTrie(db)
355355
if err != nil {
356356
return nil, err

core/state/statedb.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/ethereum/go-ethereum/params"
3535
"github.com/ethereum/go-ethereum/rlp"
3636
"github.com/ethereum/go-ethereum/trie"
37+
"github.com/ethereum/go-ethereum/trie/trienode"
3738
)
3839

3940
type revision struct {
@@ -971,7 +972,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
971972
accountTrieNodesDeleted int
972973
storageTrieNodesUpdated int
973974
storageTrieNodesDeleted int
974-
nodes = trie.NewMergedNodeSet()
975+
nodes = trienode.NewMergedNodeSet()
975976
codeWriter = s.db.DiskDB().NewBatch()
976977
)
977978
for addr := range s.stateObjectsDirty {
@@ -986,7 +987,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
986987
if err != nil {
987988
return common.Hash{}, err
988989
}
989-
// Merge the dirty nodes of storage trie into global set
990+
// Merge the dirty nodes of storage trie into global set.
990991
if set != nil {
991992
if err := nodes.Merge(set); err != nil {
992993
return common.Hash{}, err
@@ -1071,7 +1072,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
10711072
}
10721073
if root != origin {
10731074
start := time.Now()
1074-
if err := s.db.TrieDB().Update(nodes); err != nil {
1075+
if err := s.db.TrieDB().Update(root, origin, nodes); err != nil {
10751076
return common.Hash{}, err
10761077
}
10771078
s.originalRoot = root

core/state/sync_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,8 @@ func TestIncompleteStateSync(t *testing.T) {
602602
if len(nodeQueue) > 0 {
603603
results := make([]trie.NodeSyncResult, 0, len(nodeQueue))
604604
for path, element := range nodeQueue {
605-
data, err := srcDb.TrieDB().Node(element.hash)
605+
owner, inner := trie.ResolvePath([]byte(element.path))
606+
data, err := srcDb.TrieDB().GetReader(srcRoot).Node(owner, inner, element.hash)
606607
if err != nil {
607608
t.Fatalf("failed to retrieve node data for %x", element.hash)
608609
}

0 commit comments

Comments
 (0)