Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lib/trie): ClearFromChild should update parent trie #3482

Merged
merged 4 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/trie/child_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,25 @@ func (t *Trie) ClearFromChild(keyToChild, key []byte) error {
if err != nil {
return err
}

if child == nil {
return fmt.Errorf("%w at key 0x%x%x", ErrChildTrieDoesNotExist, ChildStorageKeyPrefix, keyToChild)
}

origChildHash, err := child.Hash()
if err != nil {
return err
}

err = child.Delete(key)
if err != nil {
return fmt.Errorf("deleting from child trie located at key 0x%x: %w", keyToChild, err)
}

return nil
delete(t.childTries, origChildHash)
if child.root == nil {
return t.DeleteChild(keyToChild)
}

return t.SetChild(keyToChild, child)
}
35 changes: 35 additions & 0 deletions lib/trie/child_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ package trie

import (
"bytes"
"encoding/binary"
"reflect"
"testing"

"github.com/stretchr/testify/require"
)

func TestPutAndGetChild(t *testing.T) {
Expand Down Expand Up @@ -71,3 +74,35 @@ func TestPutAndGetFromChild(t *testing.T) {
t.Fatalf("Fail: got %x expected %x", valueRes, testValue)
}
}

func TestChildTrieHashAfterClear(t *testing.T) {
trieThatHoldsAChildTrie := NewEmptyTrie()
originalEmptyHash := trieThatHoldsAChildTrie.MustHash()

keyToChild := []byte("crowdloan")
keyInChild := []byte("account-alice")
contributed := uint64(1000)
contributedWith := make([]byte, 8)
binary.BigEndian.PutUint64(contributedWith, contributed)

err := trieThatHoldsAChildTrie.PutIntoChild(keyToChild, keyInChild, contributedWith)
require.NoError(t, err)

// the parent trie hash SHOULT NOT BE EQUAL to the original
// empty hash since it contains a value
require.NotEqual(t, originalEmptyHash, trieThatHoldsAChildTrie.MustHash())

// ensure the value is inside the child trie
valueStored, err := trieThatHoldsAChildTrie.GetFromChild(keyToChild, keyInChild)
require.NoError(t, err)
require.Equal(t, contributed, binary.BigEndian.Uint64(valueStored))

// clear child trie key value
err = trieThatHoldsAChildTrie.ClearFromChild(keyToChild, keyInChild)
require.NoError(t, err)

// the parent trie hash SHOULD BE EQUAL to the original
// empty hash since now it does not have any other value in it
require.Equal(t, originalEmptyHash, trieThatHoldsAChildTrie.MustHash())

}
Loading