Skip to content

Commit

Permalink
Fix differentiating nil and empty subvalues
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Sep 20, 2022
1 parent e36334b commit 63483e7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
14 changes: 14 additions & 0 deletions internal/trie/node/subvalue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package node

import "bytes"

// SubValueEqual returns true if the node subvalue is equal to the
// subvalue given as argument. In particular, it returns false
// if one subvalue is nil and the other subvalue is the empty slice.
func (n Node) SubValueEqual(subValue []byte) (equal bool) {
if len(subValue) == 0 && len(n.SubValue) == 0 {
return (subValue == nil && n.SubValue == nil) ||
(subValue != nil && n.SubValue != nil)
}
return bytes.Equal(n.SubValue, subValue)
}
54 changes: 54 additions & 0 deletions internal/trie/node/subvalue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package node

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_Node_SubValueEqual(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
node Node
subValue []byte
equal bool
}{
"nil node subvalue and nil subvalue": {
equal: true,
},
"empty node subvalue and empty subvalue": {
node: Node{SubValue: []byte{}},
subValue: []byte{},
equal: true,
},
"nil node subvalue and empty subvalue": {
subValue: []byte{},
},
"empty node subvalue and nil subvalue": {
node: Node{SubValue: []byte{}},
},
"equal non empty values": {
node: Node{SubValue: []byte{1, 2}},
subValue: []byte{1, 2},
equal: true,
},
"not equal non empty values": {
node: Node{SubValue: []byte{1, 2}},
subValue: []byte{1, 3},
},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

node := testCase.node

equal := node.SubValueEqual(testCase.subValue)

assert.Equal(t, testCase.equal, equal)
})
}
}
6 changes: 3 additions & 3 deletions lib/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func (t *Trie) insertInLeaf(parentLeaf *Node, key, value []byte) (
newParent *Node, mutated bool, nodesCreated uint32) {
if bytes.Equal(parentLeaf.Key, key) {
nodesCreated = 0
if bytes.Equal(value, parentLeaf.SubValue) {
if parentLeaf.SubValueEqual(value) {
const mutated = false
return parentLeaf, mutated, nodesCreated
}
Expand Down Expand Up @@ -431,7 +431,7 @@ func (t *Trie) insertInBranch(parentBranch *Node, key, value []byte) (
copySettings := node.DefaultCopySettings

if bytes.Equal(key, parentBranch.Key) {
if bytes.Equal(parentBranch.SubValue, value) {
if parentBranch.SubValueEqual(value) {
const mutated = false
return parentBranch, mutated, 0
}
Expand Down Expand Up @@ -463,7 +463,7 @@ func (t *Trie) insertInBranch(parentBranch *Node, key, value []byte) (
return parentBranch, mutated, nodesCreated
}

child, mutated, nodesCreated := t.insert(child, remainingKey, value)
child, mutated, nodesCreated = t.insert(child, remainingKey, value)
if !mutated {
return parentBranch, mutated, 0
}
Expand Down

0 comments on commit 63483e7

Please sign in to comment.