Skip to content

Commit d52bfaa

Browse files
gzliudanrjl493456442holiman
authored
all: introduce trie owner notion ethereum#24750 (#1090)
* cmd, core/state, light, trie, eth: add trie owner notion * all: refactor * tests: fix goimports * core/state/snapshot: fix ineffasigns Co-authored-by: rjl493456442 <[email protected]> Co-authored-by: Martin Holst Swende <[email protected]>
1 parent b96310e commit d52bfaa

File tree

20 files changed

+212
-136
lines changed

20 files changed

+212
-136
lines changed

XDCx/tradingstate/XDCx_trie.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func NewXDCXTrie(root common.Hash, db *trie.Database) (*XDCXTrie, error) {
5858
if db == nil {
5959
panic("trie.NewXDCXTrie called without a database")
6060
}
61-
trie, err := trie.New(root, db)
61+
trie, err := trie.New(common.Hash{}, root, db)
6262
if err != nil {
6363
return nil, err
6464
}

XDCxlending/lendingstate/XDCx_trie.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func NewXDCXTrie(root common.Hash, db *trie.Database) (*XDCXTrie, error) {
5858
if db == nil {
5959
panic("trie.NewXDCXTrie called without a database")
6060
}
61-
trie, err := trie.New(root, db)
61+
trie, err := trie.New(common.Hash{}, root, db)
6262
if err != nil {
6363
return nil, err
6464
}

core/blockchain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ func (bc *BlockChain) FastSyncCommitHead(hash common.Hash) error {
529529
if block == nil {
530530
return fmt.Errorf("non existent block [%x..]", hash[:4])
531531
}
532-
if _, err := trie.NewSecure(block.Root(), bc.stateCache.TrieDB()); err != nil {
532+
if _, err := trie.NewSecure(common.Hash{}, block.Root(), bc.stateCache.TrieDB()); err != nil {
533533
return err
534534
}
535535

core/state/database.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ type cachingDB struct {
131131

132132
// OpenTrie opens the main account trie at a specific root hash.
133133
func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
134-
return trie.NewSecure(root, db.db)
134+
return trie.NewSecure(common.Hash{}, root, db.db)
135135
}
136136

137137
// OpenStorageTrie opens the storage trie of an account.
138138
func (db *cachingDB) OpenStorageTrie(addrHash, root common.Hash) (Trie, error) {
139-
return trie.NewSecure(root, db.db)
139+
return trie.NewSecure(addrHash, root, db.db)
140140
}
141141

142142
// CopyTrie returns an independent copy of the given trie.

core/state/sync_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func checkTrieConsistency(db ethdb.Database, root common.Hash) error {
104104
if v, _ := db.Get(root[:]); v == nil {
105105
return nil // Consider a non existent state consistent.
106106
}
107-
trie, err := trie.New(root, trie.NewDatabase(db))
107+
trie, err := trie.New(common.Hash{}, root, trie.NewDatabase(db))
108108
if err != nil {
109109
return err
110110
}
@@ -165,7 +165,7 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) {
165165
if commit {
166166
srcDb.TrieDB().Commit(srcRoot, false)
167167
}
168-
srcTrie, _ := trie.New(srcRoot, srcDb.TrieDB())
168+
srcTrie, _ := trie.New(common.Hash{}, srcRoot, srcDb.TrieDB())
169169

170170
// Create a destination state and sync with the scheduler
171171
dstDb := rawdb.NewMemoryDatabase()
@@ -206,7 +206,7 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) {
206206
if err := rlp.DecodeBytes(srcTrie.Get(path[0]), &acc); err != nil {
207207
t.Fatalf("failed to decode account on path %x: %v", path, err)
208208
}
209-
stTrie, err := trie.New(acc.Root, srcDb.TrieDB())
209+
stTrie, err := trie.New(common.BytesToHash(path[0]), acc.Root, srcDb.TrieDB())
210210
if err != nil {
211211
t.Fatalf("failed to retriev storage trie for path %x: %v", path, err)
212212
}

core/types/hashing_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ func TestDeriveSha(t *testing.T) {
2323
t.Fatal(err)
2424
}
2525
for len(txs) < 1000 {
26-
tr, _ := trie.New(common.Hash{}, trie.NewDatabase(rawdb.NewMemoryDatabase()))
27-
exp := types.DeriveSha(txs, tr)
26+
exp := types.DeriveSha(txs, trie.NewEmpty(trie.NewDatabase(rawdb.NewMemoryDatabase())))
2827
got := types.DeriveSha(txs, trie.NewStackTrie(nil))
2928
if !bytes.Equal(got[:], exp[:]) {
3029
t.Fatalf("%d txs: got %x exp %x", len(txs), got, exp)
@@ -71,8 +70,7 @@ func BenchmarkDeriveSha200(b *testing.B) {
7170
b.ResetTimer()
7271
b.ReportAllocs()
7372
for i := 0; i < b.N; i++ {
74-
tr, _ := trie.New(common.Hash{}, trie.NewDatabase(rawdb.NewMemoryDatabase()))
75-
exp = types.DeriveSha(txs, tr)
73+
exp = types.DeriveSha(txs, trie.NewEmpty(trie.NewDatabase(rawdb.NewMemoryDatabase())))
7674
}
7775
})
7876

@@ -93,8 +91,7 @@ func TestFuzzDeriveSha(t *testing.T) {
9391
rndSeed := mrand.Int()
9492
for i := 0; i < 10; i++ {
9593
seed := rndSeed + i
96-
tr, _ := trie.New(common.Hash{}, trie.NewDatabase(rawdb.NewMemoryDatabase()))
97-
exp := types.DeriveSha(newDummy(i), tr)
94+
exp := types.DeriveSha(newDummy(i), trie.NewEmpty(trie.NewDatabase(rawdb.NewMemoryDatabase())))
9895
got := types.DeriveSha(newDummy(i), trie.NewStackTrie(nil))
9996
if !bytes.Equal(got[:], exp[:]) {
10097
printList(newDummy(seed))
@@ -122,8 +119,7 @@ func TestDerivableList(t *testing.T) {
122119
},
123120
}
124121
for i, tc := range tcs[1:] {
125-
tr, _ := trie.New(common.Hash{}, trie.NewDatabase(rawdb.NewMemoryDatabase()))
126-
exp := types.DeriveSha(flatList(tc), tr)
122+
exp := types.DeriveSha(flatList(tc), trie.NewEmpty(trie.NewDatabase(rawdb.NewMemoryDatabase())))
127123
got := types.DeriveSha(flatList(tc), trie.NewStackTrie(nil))
128124
if !bytes.Equal(got[:], exp[:]) {
129125
t.Fatalf("case %d: got %x exp %x", i, got, exp)

eth/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,11 +488,11 @@ func (api *DebugAPI) getModifiedAccounts(startBlock, endBlock *types.Block) ([]c
488488
}
489489
triedb := api.eth.BlockChain().StateCache().TrieDB()
490490

491-
oldTrie, err := trie.NewSecure(startBlock.Root(), triedb)
491+
oldTrie, err := trie.NewSecure(common.Hash{}, startBlock.Root(), triedb)
492492
if err != nil {
493493
return nil, err
494494
}
495-
newTrie, err := trie.NewSecure(endBlock.Root(), triedb)
495+
newTrie, err := trie.NewSecure(common.Hash{}, endBlock.Root(), triedb)
496496
if err != nil {
497497
return nil, err
498498
}

eth/downloader/downloader_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func (dl *downloadTester) CurrentFastBlock() *types.Block {
192192
func (dl *downloadTester) FastSyncCommitHead(hash common.Hash) error {
193193
// For now only check that the state trie is correct
194194
if block := dl.GetBlockByHash(hash); block != nil {
195-
_, err := trie.NewSecure(block.Root(), trie.NewDatabase(dl.stateDb))
195+
_, err := trie.NewSecure(common.Hash{}, block.Root(), trie.NewDatabase(dl.stateDb))
196196
return err
197197
}
198198
return fmt.Errorf("non existent block: %x", hash[:4])

trie/errors.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,24 @@ import (
2323
)
2424

2525
// MissingNodeError is returned by the trie functions (TryGet, TryUpdate, TryDelete)
26-
// in the case where a trie Node is not present in the local database. It contains
27-
// information necessary for retrieving the missing Node.
26+
// in the case where a trie node is not present in the local database. It contains
27+
// information necessary for retrieving the missing node.
2828
type MissingNodeError struct {
29-
NodeHash common.Hash // hash of the missing Node
30-
Path []byte // hex-encoded path to the missing Node
29+
Owner common.Hash // owner of the trie if it's 2-layered trie
30+
NodeHash common.Hash // hash of the missing node
31+
Path []byte // hex-encoded path to the missing node
32+
err error // concrete error for missing trie node
33+
}
34+
35+
// Unwrap returns the concrete error for missing trie node which
36+
// allows us for further analysis outside.
37+
func (err *MissingNodeError) Unwrap() error {
38+
return err.err
3139
}
3240

3341
func (err *MissingNodeError) Error() string {
34-
return fmt.Sprintf("missing trie Node %x (path %x)", err.NodeHash, err.Path)
42+
if err.Owner == (common.Hash{}) {
43+
return fmt.Sprintf("missing trie node %x (path %x) %v", err.NodeHash, err.Path, err.err)
44+
}
45+
return fmt.Sprintf("missing trie node %x (owner %x) (path %x) %v", err.NodeHash, err.Owner, err.Path, err.err)
3546
}

trie/iterator_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525

2626
"github.com/XinFinOrg/XDPoSChain/common"
2727
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
28-
"github.com/XinFinOrg/XDPoSChain/core/types"
2928
"github.com/XinFinOrg/XDPoSChain/crypto"
3029
"github.com/XinFinOrg/XDPoSChain/ethdb"
3130
"github.com/XinFinOrg/XDPoSChain/ethdb/memorydb"
@@ -298,7 +297,7 @@ func TestUnionIterator(t *testing.T) {
298297
}
299298

300299
func TestIteratorNoDups(t *testing.T) {
301-
tr, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()))
300+
tr := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase()))
302301
for _, val := range testdata1 {
303302
tr.Update([]byte(val.k), []byte(val.v))
304303
}
@@ -313,7 +312,7 @@ func testIteratorContinueAfterError(t *testing.T, memonly bool) {
313312
diskdb := memorydb.New()
314313
triedb := NewDatabase(diskdb)
315314

316-
tr, _ := New(types.EmptyRootHash, triedb)
315+
tr := NewEmpty(triedb)
317316
for _, val := range testdata1 {
318317
tr.Update([]byte(val.k), []byte(val.v))
319318
}
@@ -338,7 +337,7 @@ func testIteratorContinueAfterError(t *testing.T, memonly bool) {
338337
}
339338
for i := 0; i < 20; i++ {
340339
// Create trie that will load all nodes from DB.
341-
tr, _ := New(tr.Hash(), triedb)
340+
tr, _ := New(common.Hash{}, tr.Hash(), triedb)
342341

343342
// Remove a random Node from the database. It can't be the root Node
344343
// because that one is already loaded.
@@ -404,7 +403,7 @@ func testIteratorContinueAfterSeekError(t *testing.T, memonly bool) {
404403
diskdb := memorydb.New()
405404
triedb := NewDatabase(diskdb)
406405

407-
ctr, _ := New(types.EmptyRootHash, triedb)
406+
ctr := NewEmpty(triedb)
408407
for _, val := range testdata1 {
409408
ctr.Update([]byte(val.k), []byte(val.v))
410409
}
@@ -425,8 +424,8 @@ func testIteratorContinueAfterSeekError(t *testing.T, memonly bool) {
425424
diskdb.Delete(barNodeHash[:])
426425
}
427426
// Create a new iterator that seeks to "bars". Seeking can't proceed because
428-
// the Node is missing.
429-
tr, _ := New(root, triedb)
427+
// the node is missing.
428+
tr, _ := New(common.Hash{}, root, triedb)
430429
it := tr.NodeIterator([]byte("bars"))
431430
missing, ok := it.Error().(*MissingNodeError)
432431
if !ok {
@@ -510,7 +509,7 @@ func makeLargeTestTrie() (*Database, *SecureTrie, *loggingDb) {
510509
// Create an empty trie
511510
logDb := &loggingDb{0, memorydb.New()}
512511
triedb := NewDatabase(logDb)
513-
trie, _ := NewSecure(common.Hash{}, triedb)
512+
trie, _ := NewSecure(common.Hash{}, common.Hash{}, triedb)
514513

515514
// Fill it with some arbitrary data
516515
for i := 0; i < 10000; i++ {
@@ -543,9 +542,9 @@ func TestNodeIteratorLargeTrie(t *testing.T) {
543542

544543
func TestIteratorNodeBlob(t *testing.T) {
545544
var (
546-
db = memorydb.New()
547-
triedb = NewDatabase(db)
548-
trie, _ = New(common.Hash{}, triedb)
545+
db = memorydb.New()
546+
triedb = NewDatabase(db)
547+
trie = NewEmpty(triedb)
549548
)
550549
vals := []struct{ k, v string }{
551550
{"do", "verb"},

0 commit comments

Comments
 (0)