-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(lib/trie): prepare trie nodes for mutation only when needed (#2834)
- Only prepare nodes for mutation if mutation is guaranteed - Fixes 'reported deleted hashes' when no mutation is done, indirectly fixing the online pruning that would previously prune database keys that may still be in use. - Add comment on `deletedKeys` trie struct field - Update tests to maintain full test coverage on trie code
- Loading branch information
Showing
4 changed files
with
197 additions
and
29 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" | ||
|
||
// 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) | ||
} |
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_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) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.