-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(trie): equality differentiate nil and empty storage values (#2969)
- Loading branch information
Showing
3 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
// StorageValueEqual returns true if the node storage value is equal to the | ||
// storage value given as argument. In particular, it returns false | ||
// if one storage value is nil and the other storage value is the empty slice. | ||
func (n Node) StorageValueEqual(stoageValue []byte) (equal bool) { | ||
if len(stoageValue) == 0 && len(n.StorageValue) == 0 { | ||
return (stoageValue == nil && n.StorageValue == nil) || | ||
(stoageValue != nil && n.StorageValue != nil) | ||
} | ||
return bytes.Equal(n.StorageValue, stoageValue) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_StorageValueEqual(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{StorageValue: []byte{}}, | ||
subValue: []byte{}, | ||
equal: true, | ||
}, | ||
"nil node subvalue and empty subvalue": { | ||
subValue: []byte{}, | ||
}, | ||
"empty node subvalue and nil subvalue": { | ||
node: Node{StorageValue: []byte{}}, | ||
}, | ||
"equal non empty values": { | ||
node: Node{StorageValue: []byte{1, 2}}, | ||
subValue: []byte{1, 2}, | ||
equal: true, | ||
}, | ||
"not equal non empty values": { | ||
node: Node{StorageValue: []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.StorageValueEqual(testCase.subValue) | ||
|
||
assert.Equal(t, testCase.equal, equal) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters