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 30, 2022
1 parent dfc902a commit 5fed227
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
17 changes: 17 additions & 0 deletions internal/trie/node/subvalue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2022 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

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)
}
57 changes: 57 additions & 0 deletions internal/trie/node/subvalue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2022 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

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)
})
}
}
4 changes: 2 additions & 2 deletions lib/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,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 @@ -433,7 +433,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

0 comments on commit 5fed227

Please sign in to comment.