Skip to content

Commit

Permalink
fix(lib/trie): handleDeletion generation propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jan 27, 2022
1 parent b47b695 commit 24c303d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 10 additions & 2 deletions lib/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ func handleDeletion(p *node.Branch, key []byte) Node {

// if branch has no children, just a value, turn it into a leaf
if bitmap == 0 && p.Value != nil {
n = node.NewLeaf(key[:length], p.Value, true, 0)
n = node.NewLeaf(key[:length], p.Value, true, p.Generation)
} else if p.NumChildren() == 1 && p.Value == nil {
// there is only 1 child and no value, combine the child branch with this branch
// find index of child
Expand All @@ -779,7 +779,14 @@ func handleDeletion(p *node.Branch, key []byte) Node {
child := p.Children[i]
switch c := child.(type) {
case *node.Leaf:
n = &node.Leaf{Key: append(append(p.Key, []byte{byte(i)}...), c.Key...), Value: c.Value}
key = append(append(p.Key, []byte{byte(i)}...), c.Key...)
const dirty = true
n = node.NewLeaf(
key,
c.Value,
dirty,
p.Generation,
)
case *node.Branch:
br := new(node.Branch)
br.Key = append(p.Key, append([]byte{byte(i)}, c.Key...)...)
Expand All @@ -792,6 +799,7 @@ func handleDeletion(p *node.Branch, key []byte) Node {
}

br.Value = c.Value
br.Generation = p.Generation
n = br
default:
// do nothing
Expand Down
5 changes: 3 additions & 2 deletions lib/trie/trie_endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -938,8 +938,9 @@ func TestClearPrefix_Small(t *testing.T) {
ssTrie.ClearPrefix([]byte("noo"))

expectedRoot := &node.Leaf{
Key: codec.KeyLEToNibbles([]byte("other")),
Value: []byte("other"),
Key: codec.KeyLEToNibbles([]byte("other")),
Value: []byte("other"),
Generation: 1,
}
expectedRoot.SetDirty(true)

Expand Down

0 comments on commit 24c303d

Please sign in to comment.