From 1a3c3ae87a68ba5909c8664c21a8cfca00a4dc82 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Thu, 22 Sep 2022 09:15:11 -0400 Subject: [PATCH] fix(trie): use direct Merkle value for database keys (#2725) --- dot/state/offline_pruner.go | 10 ++-- dot/state/pruner/pruner.go | 73 ++++++++++++------------- dot/state/storage.go | 6 +-- lib/runtime/storage/trie.go | 14 ++--- lib/trie/database.go | 47 ++++++++-------- lib/trie/trie.go | 52 +++++++++--------- lib/trie/trie_endtoend_test.go | 8 +-- lib/trie/trie_test.go | 98 ++++++++++++++++------------------ 8 files changed, 150 insertions(+), 158 deletions(-) diff --git a/dot/state/offline_pruner.go b/dot/state/offline_pruner.go index 1a59c471e2..1c9d03d997 100644 --- a/dot/state/offline_pruner.go +++ b/dot/state/offline_pruner.go @@ -105,7 +105,7 @@ func (p *OfflinePruner) SetBloomFilter() (err error) { } latestBlockNum := header.Number - keys := make(map[common.Hash]struct{}) + merkleValues := make(map[string]struct{}) logger.Infof("Latest block number is %d", latestBlockNum) @@ -121,7 +121,7 @@ func (p *OfflinePruner) SetBloomFilter() (err error) { return err } - tr.PopulateNodeHashes(tr.RootNode(), keys) + tr.PopulateMerkleValues(tr.RootNode(), merkleValues) // get parent header of current block header, err = p.blockState.GetHeader(header.ParentHash) @@ -131,14 +131,14 @@ func (p *OfflinePruner) SetBloomFilter() (err error) { blockNum = header.Number } - for key := range keys { - err = p.bloom.put(key.ToBytes()) + for key := range merkleValues { + err = p.bloom.put([]byte(key)) if err != nil { return err } } - logger.Infof("Total keys added in bloom filter: %d", len(keys)) + logger.Infof("Total keys added in bloom filter: %d", len(merkleValues)) return nil } diff --git a/dot/state/pruner/pruner.go b/dot/state/pruner/pruner.go index bbfa074dc1..123eaed268 100644 --- a/dot/state/pruner/pruner.go +++ b/dot/state/pruner/pruner.go @@ -51,7 +51,7 @@ type Config struct { // Pruner is implemented by FullNode and ArchiveNode. type Pruner interface { - StoreJournalRecord(deletedHashesSet, insertedHashesSet map[common.Hash]struct{}, + StoreJournalRecord(deletedMerkleValues, insertedMerkleValues map[string]struct{}, blockHash common.Hash, blockNum int64) error } @@ -59,25 +59,26 @@ type Pruner interface { type ArchiveNode struct{} // StoreJournalRecord for archive node doesn't do anything. -func (*ArchiveNode) StoreJournalRecord(_, _ map[common.Hash]struct{}, +func (*ArchiveNode) StoreJournalRecord(_, _ map[string]struct{}, _ common.Hash, _ int64) error { return nil } type deathRecord struct { - blockHash common.Hash - deletedKeys map[common.Hash]int64 // Mapping from deleted key hash to block number. + blockHash common.Hash + deletedMerkleValueToBlockNumber map[string]int64 } type deathRow []*deathRecord // FullNode stores state trie diff and allows online state trie pruning type FullNode struct { - logger log.LeveledLogger - deathList []deathRow - storageDB chaindb.Database - journalDB chaindb.Database - deathIndex map[common.Hash]int64 // Mapping from deleted key hash to block number. + logger log.LeveledLogger + deathList []deathRow + storageDB chaindb.Database + journalDB chaindb.Database + // deathIndex is the mapping from deleted node Merkle value to block number. + deathIndex map[string]int64 // pendingNumber is the block number to be pruned. // Initial value is set to 1 and is incremented after every block pruning. pendingNumber int64 @@ -88,10 +89,10 @@ type FullNode struct { type journalRecord struct { // blockHash of the block corresponding to journal record blockHash common.Hash - // Hash of keys that are inserted into state trie of the block - insertedHashesSet map[common.Hash]struct{} - // Hash of keys that are deleted from state trie of the block - deletedHashesSet map[common.Hash]struct{} + // Merkle values of nodes inserted in the state trie of the block + insertedMerkleValues map[string]struct{} + // Merkle values of nodes deleted from the state trie of the block + deletedMerkleValues map[string]struct{} } type journalKey struct { @@ -99,12 +100,12 @@ type journalKey struct { blockHash common.Hash } -func newJournalRecord(hash common.Hash, insertedHashesSet, - deletedHashesSet map[common.Hash]struct{}) *journalRecord { +func newJournalRecord(hash common.Hash, insertedMerkleValues, + deletedMerkleValues map[string]struct{}) *journalRecord { return &journalRecord{ - blockHash: hash, - insertedHashesSet: insertedHashesSet, - deletedHashesSet: deletedHashesSet, + blockHash: hash, + insertedMerkleValues: insertedMerkleValues, + deletedMerkleValues: deletedMerkleValues, } } @@ -112,7 +113,7 @@ func newJournalRecord(hash common.Hash, insertedHashesSet, func NewFullNode(db, storageDB chaindb.Database, retainBlocks int64, l log.LeveledLogger) (Pruner, error) { p := &FullNode{ deathList: make([]deathRow, 0), - deathIndex: make(map[common.Hash]int64), + deathIndex: make(map[string]int64), storageDB: storageDB, journalDB: chaindb.NewTable(db, journalPrefix), retainBlocks: retainBlocks, @@ -140,9 +141,9 @@ func NewFullNode(db, storageDB chaindb.Database, retainBlocks int64, l log.Level } // StoreJournalRecord stores journal record into DB and add deathRow into deathList -func (p *FullNode) StoreJournalRecord(deletedHashesSet, insertedHashesSet map[common.Hash]struct{}, +func (p *FullNode) StoreJournalRecord(deletedMerkleValues, insertedMerkleValues map[string]struct{}, blockHash common.Hash, blockNum int64) error { - jr := newJournalRecord(blockHash, insertedHashesSet, deletedHashesSet) + jr := newJournalRecord(blockHash, insertedMerkleValues, deletedMerkleValues) key := &journalKey{blockNum, blockHash} err := p.storeJournal(key, jr) @@ -168,13 +169,13 @@ func (p *FullNode) addDeathRow(jr *journalRecord, blockNum int64) { return } - p.processInsertedKeys(jr.insertedHashesSet, jr.blockHash) + p.processInsertedKeys(jr.insertedMerkleValues, jr.blockHash) - // add deleted keys from journal to death index - deletedKeys := make(map[common.Hash]int64, len(jr.deletedHashesSet)) - for k := range jr.deletedHashesSet { + // add deleted node Merkle values from journal to death index + deletedMerkleValueToBlockNumber := make(map[string]int64, len(jr.deletedMerkleValues)) + for k := range jr.deletedMerkleValues { p.deathIndex[k] = blockNum - deletedKeys[k] = blockNum + deletedMerkleValueToBlockNumber[k] = blockNum } blockIndex := blockNum - p.pendingNumber @@ -183,8 +184,8 @@ func (p *FullNode) addDeathRow(jr *journalRecord, blockNum int64) { } record := &deathRecord{ - blockHash: jr.blockHash, - deletedKeys: deletedKeys, + blockHash: jr.blockHash, + deletedMerkleValueToBlockNumber: deletedMerkleValueToBlockNumber, } // add deathRow to deathList @@ -192,8 +193,8 @@ func (p *FullNode) addDeathRow(jr *journalRecord, blockNum int64) { } // Remove re-inserted keys -func (p *FullNode) processInsertedKeys(insertedHashesSet map[common.Hash]struct{}, blockHash common.Hash) { - for k := range insertedHashesSet { +func (p *FullNode) processInsertedKeys(insertedMerkleValues map[string]struct{}, blockHash common.Hash) { + for k := range insertedMerkleValues { num, ok := p.deathIndex[k] if !ok { continue @@ -201,7 +202,7 @@ func (p *FullNode) processInsertedKeys(insertedHashesSet map[common.Hash]struct{ records := p.deathList[num-p.pendingNumber] for _, v := range records { if v.blockHash == blockHash { - delete(v.deletedKeys, k) + delete(v.deletedMerkleValueToBlockNumber, k) } } delete(p.deathIndex, k) @@ -229,14 +230,14 @@ func (p *FullNode) start() { sdbBatch := p.storageDB.NewBatch() for _, record := range row { - err := p.deleteKeys(sdbBatch, record.deletedKeys) + err := p.deleteKeys(sdbBatch, record.deletedMerkleValueToBlockNumber) if err != nil { p.logger.Warnf("failed to prune keys for block number %d: %s", blockNum, err) sdbBatch.Reset() return } - for k := range record.deletedKeys { + for k := range record.deletedMerkleValueToBlockNumber { delete(p.deathIndex, k) } } @@ -373,9 +374,9 @@ func (p *FullNode) getLastPrunedIndex() (int64, error) { return blockNum, nil } -func (*FullNode) deleteKeys(b chaindb.Batch, nodesHash map[common.Hash]int64) error { - for k := range nodesHash { - err := b.Del(k.ToBytes()) +func (*FullNode) deleteKeys(b chaindb.Batch, deletedMerkleValueToBlockNumber map[string]int64) error { + for merkleValue := range deletedMerkleValueToBlockNumber { + err := b.Del([]byte(merkleValue)) if err != nil { return err } diff --git a/dot/state/storage.go b/dot/state/storage.go index 06da909707..b2ba64870e 100644 --- a/dot/state/storage.go +++ b/dot/state/storage.go @@ -85,13 +85,13 @@ func (s *StorageState) StoreTrie(ts *rtstorage.TrieState, header *types.Header) } if header != nil { - insertedNodeHashes, err := ts.GetInsertedNodeHashes() + insertedMerkleValues, err := ts.GetInsertedMerkleValues() if err != nil { return fmt.Errorf("failed to get state trie inserted keys: block %s %w", header.Hash(), err) } - deletedNodeHashes := ts.GetDeletedNodeHashes() - err = s.pruner.StoreJournalRecord(deletedNodeHashes, insertedNodeHashes, header.Hash(), int64(header.Number)) + deletedMerkleValues := ts.GetDeletedMerkleValues() + err = s.pruner.StoreJournalRecord(deletedMerkleValues, insertedMerkleValues, header.Hash(), int64(header.Number)) if err != nil { return err } diff --git a/lib/runtime/storage/trie.go b/lib/runtime/storage/trie.go index aace0977b4..e7dc4ca72c 100644 --- a/lib/runtime/storage/trie.go +++ b/lib/runtime/storage/trie.go @@ -271,18 +271,18 @@ func (s *TrieState) LoadCodeHash() (common.Hash, error) { return common.Blake2bHash(code) } -// GetInsertedNodeHashes returns a set of hashes of all nodes -// that were inserted into state trie since the last block produced. -func (s *TrieState) GetInsertedNodeHashes() (hashesSet map[common.Hash]struct{}, err error) { +// GetInsertedMerkleValues returns the set of all node Merkle value inserted +// into the state trie since the last block produced. +func (s *TrieState) GetInsertedMerkleValues() (merkleValues map[string]struct{}, err error) { s.lock.RLock() defer s.lock.RUnlock() - return s.t.GetInsertedNodeHashes() + return s.t.GetInsertedMerkleValues() } -// GetDeletedNodeHashes returns the hash of nodes that were deleted +// GetDeletedMerkleValues returns the set of all node Merkle values deleted // from the state trie since the last block produced. -func (s *TrieState) GetDeletedNodeHashes() (hashesSet map[common.Hash]struct{}) { +func (s *TrieState) GetDeletedMerkleValues() (merkleValues map[string]struct{}) { s.lock.RLock() defer s.lock.RUnlock() - return s.t.GetDeletedNodeHashes() + return s.t.GetDeletedMerkleValues() } diff --git a/lib/trie/database.go b/lib/trie/database.go index 0999048660..fe908df6ec 100644 --- a/lib/trie/database.go +++ b/lib/trie/database.go @@ -185,9 +185,9 @@ func (t *Trie) loadNode(db Database, n *Node) error { return nil } -// PopulateNodeHashes writes hashes of each children of the node given -// as keys to the map hashesSet. -func (t *Trie) PopulateNodeHashes(n *Node, hashesSet map[common.Hash]struct{}) { +// PopulateMerkleValues writes the Merkle value of each children of the node given +// as keys to the map merkleValues. +func (t *Trie) PopulateMerkleValues(n *Node, merkleValues map[string]struct{}) { if n.Kind() != node.Branch { return } @@ -198,10 +198,9 @@ func (t *Trie) PopulateNodeHashes(n *Node, hashesSet map[common.Hash]struct{}) { continue } - hash := common.BytesToHash(child.MerkleValue) - hashesSet[hash] = struct{}{} + merkleValues[string(child.MerkleValue)] = struct{}{} - t.PopulateNodeHashes(child, hashesSet) + t.PopulateMerkleValues(child, merkleValues) } } @@ -354,20 +353,18 @@ func (t *Trie) writeDirtyNode(db chaindb.Batch, n *Node) (err error) { return nil } -// GetInsertedNodeHashes returns a set of hashes with all -// the hashes of all nodes that were inserted in the state trie -// since the last snapshot. -// We need to compute the hash values of each newly inserted node. -func (t *Trie) GetInsertedNodeHashes() (hashesSet map[common.Hash]struct{}, err error) { - hashesSet = make(map[common.Hash]struct{}) - err = t.getInsertedNodeHashesAtNode(t.root, hashesSet) +// GetInsertedMerkleValues returns the set of node Merkle values +// for each node that was inserted in the state trie since the last snapshot. +func (t *Trie) GetInsertedMerkleValues() (merkleValues map[string]struct{}, err error) { + merkleValues = make(map[string]struct{}) + err = t.getInsertedNodeHashesAtNode(t.root, merkleValues) if err != nil { return nil, err } - return hashesSet, nil + return merkleValues, nil } -func (t *Trie) getInsertedNodeHashesAtNode(n *Node, hashes map[common.Hash]struct{}) (err error) { +func (t *Trie) getInsertedNodeHashesAtNode(n *Node, merkleValues map[string]struct{}) (err error) { if n == nil || !n.Dirty { return nil } @@ -384,7 +381,7 @@ func (t *Trie) getInsertedNodeHashesAtNode(n *Node, hashes map[common.Hash]struc n.MerkleValue, err) } - hashes[common.BytesToHash(merkleValue)] = struct{}{} + merkleValues[string(merkleValue)] = struct{}{} if n.Kind() != node.Branch { return nil @@ -395,7 +392,7 @@ func (t *Trie) getInsertedNodeHashesAtNode(n *Node, hashes map[common.Hash]struc continue } - err := t.getInsertedNodeHashesAtNode(child, hashes) + err := t.getInsertedNodeHashesAtNode(child, merkleValues) if err != nil { // Note: do not wrap error since this is called recursively. return err @@ -405,13 +402,13 @@ func (t *Trie) getInsertedNodeHashesAtNode(n *Node, hashes map[common.Hash]struc return nil } -// GetDeletedNodeHashes returns a set of all the hashes of nodes that were -// deleted from the trie since the last snapshot was made. -// The returned set is a copy of the internal set to prevent data races. -func (t *Trie) GetDeletedNodeHashes() (hashesSet map[common.Hash]struct{}) { - hashesSet = make(map[common.Hash]struct{}, len(t.deletedKeys)) - for k := range t.deletedKeys { - hashesSet[k] = struct{}{} +// GetDeletedMerkleValues returns a set of all the node Merkle values for each +// node that was deleted from the trie since the last snapshot was made. +// The returned set is a copy of the internal set to prevent data corruption. +func (t *Trie) GetDeletedMerkleValues() (merkleValues map[string]struct{}) { + merkleValues = make(map[string]struct{}, len(t.deletedMerkleValues)) + for k := range t.deletedMerkleValues { + merkleValues[k] = struct{}{} } - return hashesSet + return merkleValues } diff --git a/lib/trie/trie.go b/lib/trie/trie.go index 6c851f7634..96e9c9192e 100644 --- a/lib/trie/trie.go +++ b/lib/trie/trie.go @@ -18,10 +18,10 @@ var EmptyHash, _ = NewEmptyTrie().Hash() // Trie is a base 16 modified Merkle Patricia trie. type Trie struct { - generation uint64 - root *Node - childTries map[common.Hash]*Trie - deletedKeys map[common.Hash]struct{} + generation uint64 + root *Node + childTries map[common.Hash]*Trie + deletedMerkleValues map[string]struct{} } // NewEmptyTrie creates a trie with a nil root @@ -32,10 +32,10 @@ func NewEmptyTrie() *Trie { // NewTrie creates a trie with an existing root node func NewTrie(root *Node) *Trie { return &Trie{ - root: root, - childTries: make(map[common.Hash]*Trie), - generation: 0, // Initially zero but increases after every snapshot. - deletedKeys: make(map[common.Hash]struct{}), + root: root, + childTries: make(map[common.Hash]*Trie), + generation: 0, // Initially zero but increases after every snapshot. + deletedMerkleValues: make(map[string]struct{}), } } @@ -50,17 +50,17 @@ func (t *Trie) Snapshot() (newTrie *Trie) { rootCopySettings.CopyCached = true for rootHash, childTrie := range t.childTries { childTries[rootHash] = &Trie{ - generation: childTrie.generation + 1, - root: childTrie.root.Copy(rootCopySettings), - deletedKeys: make(map[common.Hash]struct{}), + generation: childTrie.generation + 1, + root: childTrie.root.Copy(rootCopySettings), + deletedMerkleValues: make(map[string]struct{}), } } return &Trie{ - generation: t.generation + 1, - root: t.root, - childTries: childTries, - deletedKeys: make(map[common.Hash]struct{}), + generation: t.generation + 1, + root: t.root, + childTries: childTries, + deletedMerkleValues: make(map[string]struct{}), } } @@ -71,7 +71,7 @@ func (t *Trie) prepLeafForMutation(currentLeaf *Node, // of current leaf. newLeaf = currentLeaf } else { - newLeaf = updateGeneration(currentLeaf, t.generation, t.deletedKeys, copySettings) + newLeaf = updateGeneration(currentLeaf, t.generation, t.deletedMerkleValues, copySettings) } newLeaf.SetDirty() return newLeaf @@ -84,7 +84,7 @@ func (t *Trie) prepBranchForMutation(currentBranch *Node, // of current branch. newBranch = currentBranch } else { - newBranch = updateGeneration(currentBranch, t.generation, t.deletedKeys, copySettings) + newBranch = updateGeneration(currentBranch, t.generation, t.deletedMerkleValues, copySettings) } newBranch.SetDirty() return newBranch @@ -94,17 +94,17 @@ func (t *Trie) prepBranchForMutation(currentBranch *Node, // an older trie generation (snapshot) so we deep copy the // node and update the generation on the newer copy. func updateGeneration(currentNode *Node, trieGeneration uint64, - deletedHashes map[common.Hash]struct{}, copySettings node.CopySettings) ( + deletedMerkleValues map[string]struct{}, copySettings node.CopySettings) ( newNode *Node) { newNode = currentNode.Copy(copySettings) newNode.Generation = trieGeneration // The hash of the node from a previous snapshotted trie // is usually already computed. - deletedHashBytes := currentNode.MerkleValue - if len(deletedHashBytes) > 0 { - deletedHash := common.BytesToHash(deletedHashBytes) - deletedHashes[deletedHash] = struct{}{} + deletedMerkleValue := currentNode.MerkleValue + if len(deletedMerkleValue) > 0 { + deletedMerkleValueString := string(deletedMerkleValue) + deletedMerkleValues[deletedMerkleValueString] = struct{}{} } return newNode @@ -124,10 +124,10 @@ func (t *Trie) DeepCopy() (trieCopy *Trie) { generation: t.generation, } - if t.deletedKeys != nil { - trieCopy.deletedKeys = make(map[common.Hash]struct{}, len(t.deletedKeys)) - for k := range t.deletedKeys { - trieCopy.deletedKeys[k] = struct{}{} + if t.deletedMerkleValues != nil { + trieCopy.deletedMerkleValues = make(map[string]struct{}, len(t.deletedMerkleValues)) + for k := range t.deletedMerkleValues { + trieCopy.deletedMerkleValues[k] = struct{}{} } } diff --git a/lib/trie/trie_endtoend_test.go b/lib/trie/trie_endtoend_test.go index 9d8d9de3cb..c089a2efd4 100644 --- a/lib/trie/trie_endtoend_test.go +++ b/lib/trie/trie_endtoend_test.go @@ -292,14 +292,14 @@ func TestTrieDiff(t *testing.T) { for _, test := range tests { newTrie.Put(test.key, test.value) } - deletedKeys := newTrie.deletedKeys - require.Len(t, deletedKeys, 3) + deletedMerkleValues := newTrie.deletedMerkleValues + require.Len(t, deletedMerkleValues, 3) err = newTrie.WriteDirty(storageDB) require.NoError(t, err) - for key := range deletedKeys { - err = storageDB.Del(key.ToBytes()) + for deletedMerkleValue := range deletedMerkleValues { + err = storageDB.Del([]byte(deletedMerkleValue)) require.NoError(t, err) } diff --git a/lib/trie/trie_test.go b/lib/trie/trie_test.go index 060ce69451..c68dd23433 100644 --- a/lib/trie/trie_test.go +++ b/lib/trie/trie_test.go @@ -18,8 +18,8 @@ import ( func Test_NewEmptyTrie(t *testing.T) { expectedTrie := &Trie{ - childTries: make(map[common.Hash]*Trie), - deletedKeys: map[common.Hash]struct{}{}, + childTries: make(map[common.Hash]*Trie), + deletedMerkleValues: map[string]struct{}{}, } trie := NewEmptyTrie() assert.Equal(t, expectedTrie, trie) @@ -35,8 +35,8 @@ func Test_NewTrie(t *testing.T) { Key: []byte{0}, SubValue: []byte{17}, }, - childTries: make(map[common.Hash]*Trie), - deletedKeys: map[common.Hash]struct{}{}, + childTries: make(map[common.Hash]*Trie), + deletedMerkleValues: map[string]struct{}{}, } trie := NewTrie(root) assert.Equal(t, expectedTrie, trie) @@ -52,21 +52,21 @@ func Test_Trie_Snapshot(t *testing.T) { {1}: { generation: 1, root: &Node{Key: []byte{1}, SubValue: []byte{1}}, - deletedKeys: map[common.Hash]struct{}{ - {1}: {}, + deletedMerkleValues: map[string]struct{}{ + "a": {}, }, }, {2}: { generation: 2, root: &Node{Key: []byte{2}, SubValue: []byte{1}}, - deletedKeys: map[common.Hash]struct{}{ - {2}: {}, + deletedMerkleValues: map[string]struct{}{ + "b": {}, }, }, }, - deletedKeys: map[common.Hash]struct{}{ - {1}: {}, - {2}: {}, + deletedMerkleValues: map[string]struct{}{ + "a": {}, + "b": {}, }, } @@ -75,17 +75,17 @@ func Test_Trie_Snapshot(t *testing.T) { root: &Node{Key: []byte{8}, SubValue: []byte{1}}, childTries: map[common.Hash]*Trie{ {1}: { - generation: 2, - root: &Node{Key: []byte{1}, SubValue: []byte{1}}, - deletedKeys: map[common.Hash]struct{}{}, + generation: 2, + root: &Node{Key: []byte{1}, SubValue: []byte{1}}, + deletedMerkleValues: map[string]struct{}{}, }, {2}: { - generation: 3, - root: &Node{Key: []byte{2}, SubValue: []byte{1}}, - deletedKeys: map[common.Hash]struct{}{}, + generation: 3, + root: &Node{Key: []byte{2}, SubValue: []byte{1}}, + deletedMerkleValues: map[string]struct{}{}, }, }, - deletedKeys: map[common.Hash]struct{}{}, + deletedMerkleValues: map[string]struct{}{}, } newTrie := trie.Snapshot() @@ -97,12 +97,12 @@ func Test_Trie_updateGeneration(t *testing.T) { t.Parallel() testCases := map[string]struct { - trieGeneration uint64 - node *Node - copySettings node.CopySettings - newNode *Node - copied bool - expectedDeletedHashes map[common.Hash]struct{} + trieGeneration uint64 + node *Node + copySettings node.CopySettings + newNode *Node + copied bool + expectedDeletedMerkleValues map[string]struct{} }{ "trie generation higher and empty hash": { trieGeneration: 2, @@ -115,8 +115,8 @@ func Test_Trie_updateGeneration(t *testing.T) { Generation: 2, Key: []byte{1}, }, - copied: true, - expectedDeletedHashes: map[common.Hash]struct{}{}, + copied: true, + expectedDeletedMerkleValues: map[string]struct{}{}, }, "trie generation higher and hash": { trieGeneration: 2, @@ -131,13 +131,8 @@ func Test_Trie_updateGeneration(t *testing.T) { Key: []byte{1}, }, copied: true, - expectedDeletedHashes: map[common.Hash]struct{}{ - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 1, 2, 3, - }: {}, + expectedDeletedMerkleValues: map[string]struct{}{ + string([]byte{1, 2, 3}): {}, }, }, } @@ -147,13 +142,13 @@ func Test_Trie_updateGeneration(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - deletedHashes := make(map[common.Hash]struct{}) + deletedMerkleValues := make(map[string]struct{}) newNode := updateGeneration(testCase.node, testCase.trieGeneration, - deletedHashes, testCase.copySettings) + deletedMerkleValues, testCase.copySettings) assert.Equal(t, testCase.newNode, newNode) - assert.Equal(t, testCase.expectedDeletedHashes, deletedHashes) + assert.Equal(t, testCase.expectedDeletedMerkleValues, deletedMerkleValues) // Check for deep copy if newNode != nil && testCase.copied { @@ -201,7 +196,7 @@ func testTrieForDeepCopy(t *testing.T, original, copy *Trie) { return } assertPointersNotEqual(t, original.generation, copy.generation) - assertPointersNotEqual(t, original.deletedKeys, copy.deletedKeys) + assertPointersNotEqual(t, original.deletedMerkleValues, copy.deletedMerkleValues) assertPointersNotEqual(t, original.childTries, copy.childTries) for hashKey, childTrie := range copy.childTries { originalChildTrie := original.childTries[hashKey] @@ -230,15 +225,15 @@ func Test_Trie_DeepCopy(t *testing.T) { {1, 2, 3}: { generation: 2, root: &Node{Key: []byte{1}, SubValue: []byte{1}}, - deletedKeys: map[common.Hash]struct{}{ - {1, 2, 3}: {}, - {3, 4, 5}: {}, + deletedMerkleValues: map[string]struct{}{ + "a": {}, + "b": {}, }, }, }, - deletedKeys: map[common.Hash]struct{}{ - {1, 2, 3}: {}, - {3, 4, 5}: {}, + deletedMerkleValues: map[string]struct{}{ + "a": {}, + "b": {}, }, }, trieCopy: &Trie{ @@ -248,15 +243,15 @@ func Test_Trie_DeepCopy(t *testing.T) { {1, 2, 3}: { generation: 2, root: &Node{Key: []byte{1}, SubValue: []byte{1}}, - deletedKeys: map[common.Hash]struct{}{ - {1, 2, 3}: {}, - {3, 4, 5}: {}, + deletedMerkleValues: map[string]struct{}{ + "a": {}, + "b": {}, }, }, }, - deletedKeys: map[common.Hash]struct{}{ - {1, 2, 3}: {}, - {3, 4, 5}: {}, + deletedMerkleValues: map[string]struct{}{ + "a": {}, + "b": {}, }, }, }, @@ -648,9 +643,8 @@ func Test_Trie_Entries(t *testing.T) { t.Parallel() trie := Trie{ - root: nil, - childTries: make(map[common.Hash]*Trie), - deletedKeys: make(map[common.Hash]struct{}), + root: nil, + childTries: make(map[common.Hash]*Trie), } kv := map[string][]byte{