Skip to content

Commit b3d8c0e

Browse files
committed
Fix differentiating nil and empty subvalues
1 parent e124180 commit b3d8c0e

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

internal/trie/node/subvalue.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package node
2+
3+
import "bytes"
4+
5+
func (n Node) SubValueEqual(subValue []byte) (equal bool) {
6+
if len(subValue) == 0 && len(n.SubValue) == 0 {
7+
return (subValue == nil && n.SubValue == nil) ||
8+
(subValue != nil && n.SubValue != nil)
9+
}
10+
return bytes.Equal(n.SubValue, subValue)
11+
}

internal/trie/node/subvalue_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package node
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_Node_SubValueEqual(t *testing.T) {
10+
t.Parallel()
11+
12+
testCases := map[string]struct {
13+
node Node
14+
subValue []byte
15+
equal bool
16+
}{
17+
"nil node subvalue and nil subvalue": {
18+
equal: true,
19+
},
20+
"empty node subvalue and empty subvalue": {
21+
node: Node{SubValue: []byte{}},
22+
subValue: []byte{},
23+
equal: true,
24+
},
25+
"nil node subvalue and empty subvalue": {
26+
subValue: []byte{},
27+
},
28+
"empty node subvalue and nil subvalue": {
29+
node: Node{SubValue: []byte{}},
30+
},
31+
"equal non empty values": {
32+
node: Node{SubValue: []byte{1, 2}},
33+
subValue: []byte{1, 2},
34+
equal: true,
35+
},
36+
"not equal non empty values": {
37+
node: Node{SubValue: []byte{1, 2}},
38+
subValue: []byte{1, 3},
39+
},
40+
}
41+
42+
for name, testCase := range testCases {
43+
testCase := testCase
44+
t.Run(name, func(t *testing.T) {
45+
t.Parallel()
46+
47+
node := testCase.node
48+
49+
equal := node.SubValueEqual(testCase.subValue)
50+
51+
assert.Equal(t, testCase.equal, equal)
52+
})
53+
}
54+
}

lib/trie/trie.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ func (t *Trie) insertInLeaf(parentLeaf *Node, key, value []byte) (
351351
newParent *Node, mutated bool, nodesCreated uint32) {
352352
if bytes.Equal(parentLeaf.Key, key) {
353353
nodesCreated = 0
354-
if bytes.Equal(value, parentLeaf.SubValue) {
354+
if parentLeaf.SubValueEqual(value) {
355355
const mutated = false
356356
return parentLeaf, mutated, nodesCreated
357357
}
@@ -431,7 +431,7 @@ func (t *Trie) insertInBranch(parentBranch *Node, key, value []byte) (
431431
copySettings := node.DefaultCopySettings
432432

433433
if bytes.Equal(key, parentBranch.Key) {
434-
if bytes.Equal(parentBranch.SubValue, value) {
434+
if parentBranch.SubValueEqual(value) {
435435
const mutated = false
436436
return parentBranch, mutated, 0
437437
}
@@ -463,8 +463,9 @@ func (t *Trie) insertInBranch(parentBranch *Node, key, value []byte) (
463463
return parentBranch, mutated, nodesCreated
464464
}
465465

466-
child, mutated, nodesCreated := t.insert(child, remainingKey, value)
467-
if !mutated {
466+
child, childMutated, nodesCreated := t.insert(child, remainingKey, value)
467+
if !childMutated {
468+
const mutated = false
468469
return parentBranch, mutated, 0
469470
}
470471

0 commit comments

Comments
 (0)