diff --git a/pkg/trie/triedb/node.go b/pkg/trie/triedb/node.go index eea0c0b7d1..ccf9556eab 100644 --- a/pkg/trie/triedb/node.go +++ b/pkg/trie/triedb/node.go @@ -111,13 +111,12 @@ func NewValue(data []byte, threshold int) nodeValue { return inline(data) } -func NewValueFromEncoded(prefix []byte, encodedValue codec.EncodedValue) nodeValue { +func NewValueFromEncoded(encodedValue codec.EncodedValue) nodeValue { switch v := encodedValue.(type) { case codec.InlineValue: return inline(v) case codec.HashedValue: - prefixedKey := bytes.Join([][]byte{prefix, v[:]}, nil) - return valueRef(common.NewHash(prefixedKey)) + return valueRef(v) } return nil @@ -178,7 +177,7 @@ func newNodeFromEncoded(nodeHash common.Hash, data []byte, storage nodeStorage) case codec.Empty: return Empty{}, nil case codec.Leaf: - return Leaf{partialKey: encoded.PartialKey, value: NewValueFromEncoded(encoded.PartialKey, encoded.Value)}, nil + return Leaf{partialKey: encoded.PartialKey, value: NewValueFromEncoded(encoded.Value)}, nil case codec.Branch: key := encoded.PartialKey encodedChildren := encoded.Children @@ -204,7 +203,7 @@ func newNodeFromEncoded(nodeHash common.Hash, data []byte, storage nodeStorage) children[i] = child } - return Branch{partialKey: key, children: children, value: NewValueFromEncoded(encoded.PartialKey, value)}, nil + return Branch{partialKey: key, children: children, value: NewValueFromEncoded(value)}, nil default: panic("unreachable") } diff --git a/pkg/trie/triedb/triedb.go b/pkg/trie/triedb/triedb.go index 654a7da501..4e9af6ba9d 100644 --- a/pkg/trie/triedb/triedb.go +++ b/pkg/trie/triedb/triedb.go @@ -52,7 +52,8 @@ type TrieDB struct { // trieDB session (before nodes are committed to db) storage nodeStorage // deathRow is a set of nodes that we want to delete from db - deathRow map[common.Hash]interface{} + // uses string since it's comparable []byte + deathRow map[string]interface{} // Optional cache to speed up the db lookups cache cache.TrieCache // Optional recorder for recording trie accesses @@ -74,7 +75,7 @@ func NewTrieDB(rootHash common.Hash, db db.RWDatabase, opts ...TrieDBOpts) *Trie db: db, storage: newNodeStorage(), rootHandle: rootHandle, - deathRow: make(map[common.Hash]interface{}), + deathRow: make(map[string]interface{}), } for _, opt := range opts { @@ -369,10 +370,10 @@ func (t *TrieDB) inspect( case restoreNode: return &InspectResult{CachedStoredNode{a.node, n.hash}, false}, nil case replaceNode: - t.deathRow[n.hash] = nil + t.deathRow[string(n.hash.ToBytes())] = nil return &InspectResult{NewStoredNode(a), true}, nil case deleteNode: - t.deathRow[n.hash] = nil + t.deathRow[string(n.hash.ToBytes())] = nil return nil, nil default: panic("unreachable") @@ -427,7 +428,7 @@ func (t *TrieDB) fix(branch Branch) (Node, error) { case NewStoredNode: childNode = n.node case CachedStoredNode: - t.deathRow[n.hash] = nil + t.deathRow[string(n.hash.ToBytes())] = nil childNode = n.node } @@ -456,8 +457,9 @@ func (t *TrieDB) removeInspector(stored Node, keyNibbles []byte, oldValue *nodeV return deleteNode{}, nil case Leaf: if bytes.Equal(n.partialKey, partial) { + // This is the node we are looking for so we delete it - t.replaceOldValue(oldValue, n.value) + t.replaceOldValue(oldValue, n.value, partial) return deleteNode{}, nil } // Wrong partial, so we return the node as is @@ -469,7 +471,7 @@ func (t *TrieDB) removeInspector(stored Node, keyNibbles []byte, oldValue *nodeV return restoreNode{n}, nil } // The branch contains the value so we delete it - t.replaceOldValue(oldValue, n.value) + t.replaceOldValue(oldValue, n.value, partial) newNode, err := t.fix(Branch{n.partialKey, n.children, nil}) if err != nil { return nil, err @@ -483,7 +485,7 @@ func (t *TrieDB) removeInspector(stored Node, keyNibbles []byte, oldValue *nodeV if common == existingLength && common == len(partial) { // Replace value if n.value != nil { - t.replaceOldValue(oldValue, n.value) + t.replaceOldValue(oldValue, n.value, partial) newNode, err := t.fix(Branch{n.partialKey, n.children, nil}) return replaceNode{newNode}, err } @@ -543,7 +545,7 @@ func (t *TrieDB) insertInspector(stored Node, keyNibbles []byte, value []byte, o // to replace the value value := NewValue(value, t.version.MaxInlineValue()) unchanged := n.value.equal(value) - t.replaceOldValue(oldValue, n.value) + t.replaceOldValue(oldValue, n.value, partial) leaf := Leaf{partialKey: n.partialKey, value: value} if unchanged { // If the value didn't change we can restore this leaf previously @@ -605,7 +607,7 @@ func (t *TrieDB) insertInspector(stored Node, keyNibbles []byte, value []byte, o } branch := Branch{existingKey, n.children, value} - t.replaceOldValue(oldValue, n.value) + t.replaceOldValue(oldValue, n.value, partial) if unchanged { // If the value didn't change we can restore this leaf previously // taken from storage @@ -694,12 +696,14 @@ func (t *TrieDB) insertInspector(stored Node, keyNibbles []byte, value []byte, o func (t *TrieDB) replaceOldValue( oldValue *nodeValue, storedValue nodeValue, + prefix []byte, ) { switch oldv := storedValue.(type) { case valueRef, newValueRef: hash := oldv.getHash() if hash != common.EmptyHash { - t.deathRow[oldv.getHash()] = nil + prefixedKey := append(prefix, oldv.getHash().ToBytes()...) + t.deathRow[string(prefixedKey)] = nil } } *oldValue = storedValue @@ -739,14 +743,14 @@ func (t *TrieDB) commit() error { }() for hash := range t.deathRow { - err := dbBatch.Del(hash[:]) + err := dbBatch.Del([]byte(hash)) if err != nil { return err } } // Reset deathRow - t.deathRow = make(map[common.Hash]interface{}) + t.deathRow = make(map[string]interface{}) var handle storageHandle switch h := t.rootHandle.(type) {