Skip to content

Commit

Permalink
updateGeneration panics when node and trie generations are equal
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Feb 17, 2022
1 parent 7b22b43 commit 204c115
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
6 changes: 6 additions & 0 deletions lib/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ func (t *Trie) Snapshot() (newTrie *Trie) {
// node and update the generation on the newer copy.
func updateGeneration(currentNode Node, trieGeneration uint64,
deletedHashes map[common.Hash]struct{}) (newNode Node) {
if currentNode.GetGeneration() == trieGeneration {
panic(fmt.Sprintf(
"current node has the same generation %d as the trie generation, "+
"make sure the caller properly checks for the node generation to "+
"be smaller than the trie generation.", trieGeneration))
}
const copyChildren = false
newNode = currentNode.Copy(copyChildren)
newNode.SetGeneration(trieGeneration)
Expand Down
25 changes: 13 additions & 12 deletions lib/trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,6 @@ func Test_Trie_updateGeneration(t *testing.T) {
copied bool
expectedDeletedHashes map[common.Hash]struct{}
}{
"same generation": {
trieGeneration: 1,
node: &node.Leaf{
Generation: 1,
Key: []byte{1},
},
newNode: &node.Leaf{
Generation: 1,
Key: []byte{1},
},
expectedDeletedHashes: map[common.Hash]struct{}{},
},
"trie generation higher and empty hash": {
trieGeneration: 2,
node: &node.Leaf{
Expand Down Expand Up @@ -169,6 +157,19 @@ func Test_Trie_updateGeneration(t *testing.T) {
}
})
}

t.Run("panic on same generation", func(t *testing.T) {
t.Parallel()
node := &node.Leaf{Generation: 1}
const trieGenration = 1
assert.PanicsWithValue(t,
"current node has the same generation 1 as the trie generation, "+
"make sure the caller properly checks for the node generation to "+
"be smaller than the trie generation.",
func() {
updateGeneration(node, trieGenration, nil)
})
})
}

func Test_Trie_RootNode(t *testing.T) {
Expand Down

0 comments on commit 204c115

Please sign in to comment.