Skip to content

Commit

Permalink
Do not track deleted deltas for generation 0
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Oct 18, 2022
1 parent 66082d5 commit 11cbfd0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ func (t *Trie) Snapshot() (newTrie *Trie) {
// handleTrackedDeltas sets the pending deleted Merkle values in
// the trie deleted merkle values set if and only if success is true.
func (t *Trie) handleTrackedDeltas(success bool, pendingDeletedMerkleValues map[string]struct{}) {
if !success {
if !success || t.generation == 0 {
// Do not persist tracked deleted node hashes if the operation failed or
// if the trie generation is zero (first block, no trie snapshot done yet).
return
}

Expand Down
45 changes: 40 additions & 5 deletions lib/trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,42 @@ func Test_Trie_handleTrackedDeltas(t *testing.T) {
pendingDeletedMerkleValues map[string]struct{}
expectedTrie Trie
}{
"no success": {
"no success and generation 1": {
trie: Trie{
generation: 1,
deletedMerkleValues: map[string]struct{}{
"a": {},
},
},
pendingDeletedMerkleValues: map[string]struct{}{
"b": {},
},
expectedTrie: Trie{
generation: 1,
deletedMerkleValues: map[string]struct{}{
"a": {},
},
},
},
"success and generation 0": {
trie: Trie{
deletedMerkleValues: map[string]struct{}{
"a": {},
},
},
success: true,
pendingDeletedMerkleValues: map[string]struct{}{
"b": {},
},
expectedTrie: Trie{
deletedMerkleValues: map[string]struct{}{
"a": {},
},
},
},
"success": {
"success and generation 1": {
trie: Trie{
generation: 1,
deletedMerkleValues: map[string]struct{}{
"a": {},
},
Expand All @@ -129,6 +148,7 @@ func Test_Trie_handleTrackedDeltas(t *testing.T) {
"b": {},
},
expectedTrie: Trie{
generation: 1,
deletedMerkleValues: map[string]struct{}{
"a": {},
"b": {},
Expand Down Expand Up @@ -3093,9 +3113,11 @@ func Test_Trie_ClearPrefix(t *testing.T) {
"nil prefix": {
trie: Trie{
root: &Node{SubValue: []byte{1}},
generation: 1,
deletedMerkleValues: map[string]struct{}{},
},
expectedTrie: Trie{
generation: 1,
deletedMerkleValues: map[string]struct{}{
"\xf9jt\x15\"\xbc\xc1O\n\xea/p`DR$\x1dY\xb5\xf2ݫ\x9aiH\xfd\xb3\xfe\xf5\xf9\x86C": {},
},
Expand All @@ -3104,10 +3126,12 @@ func Test_Trie_ClearPrefix(t *testing.T) {
"empty prefix": {
trie: Trie{
root: &Node{SubValue: []byte{1}},
generation: 1,
deletedMerkleValues: map[string]struct{}{},
},
prefix: []byte{},
expectedTrie: Trie{
generation: 1,
deletedMerkleValues: map[string]struct{}{
"\xf9jt\x15\"\xbc\xc1O\n\xea/p`DR$\x1dY\xb5\xf2ݫ\x9aiH\xfd\xb3\xfe\xf5\xf9\x86C": {},
},
Expand All @@ -3118,6 +3142,7 @@ func Test_Trie_ClearPrefix(t *testing.T) {
},
"clear prefix": {
trie: Trie{
generation: 1,
root: &Node{
Key: []byte{1, 2},
Descendants: 3,
Expand All @@ -3138,13 +3163,19 @@ func Test_Trie_ClearPrefix(t *testing.T) {
},
}),
},
deletedMerkleValues: map[string]struct{}{},
},
prefix: []byte{0x12, 0x16},
expectedTrie: Trie{
generation: 1,
root: &Node{
Key: []byte{1, 2, 0, 5},
SubValue: []byte{1},
Dirty: true,
Key: []byte{1, 2, 0, 5},
SubValue: []byte{1},
Generation: 1,
Dirty: true,
},
deletedMerkleValues: map[string]struct{}{
"_\xe1\b\xc8=\b2\x93\x8e\x01\x04\xda̝!\x87\xfd\x9d\xaf\xa5\x82\xd1\xc52\xe5\xfe{.P": {},
},
},
},
Expand Down Expand Up @@ -3513,9 +3544,11 @@ func Test_Trie_Delete(t *testing.T) {
"nil key": {
trie: Trie{
root: &Node{SubValue: []byte{1}},
generation: 1,
deletedMerkleValues: map[string]struct{}{},
},
expectedTrie: Trie{
generation: 1,
deletedMerkleValues: map[string]struct{}{
"\xf9jt\x15\"\xbc\xc1O\n\xea/p`DR$\x1dY\xb5\xf2ݫ\x9aiH\xfd\xb3\xfe\xf5\xf9\x86C": {},
},
Expand All @@ -3524,9 +3557,11 @@ func Test_Trie_Delete(t *testing.T) {
"empty key": {
trie: Trie{
root: &Node{SubValue: []byte{1}},
generation: 1,
deletedMerkleValues: map[string]struct{}{},
},
expectedTrie: Trie{
generation: 1,
deletedMerkleValues: map[string]struct{}{
"\xf9jt\x15\"\xbc\xc1O\n\xea/p`DR$\x1dY\xb5\xf2ݫ\x9aiH\xfd\xb3\xfe\xf5\xf9\x86C": {},
},
Expand Down

0 comments on commit 11cbfd0

Please sign in to comment.