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): Make sure writing and reading a trie to disk gives the same trie and cover more store/load child trie related test cases #2302

Merged
merged 27 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
47b6d27
Cover more store/load child trie related test cases
kishansagathiya Feb 16, 2022
29ace8a
test error `failed to insert child trie with root` in trie.Load
kishansagathiya Feb 21, 2022
772b6c2
temp
kishansagathiya Feb 22, 2022
061b9bf
test TestGetStorageChildAndGetStorageFromChild with non-empty trie
kishansagathiya Mar 3, 2022
bf0f5ee
tackle the case when encoding and hash is same
kishansagathiya Mar 3, 2022
46af47a
Merge branch 'development' into kishan/task/trie-test
kishansagathiya Mar 3, 2022
8a5f18c
so far things work, no error finding any nodes, child tries get
kishansagathiya Mar 7, 2022
e4f434a
child tries don't have empty bits in their encoding
kishansagathiya Mar 7, 2022
13f3e59
cleaning up
kishansagathiya Mar 7, 2022
d6d5a87
more clean up
kishansagathiya Mar 7, 2022
4eed767
accept []byte key in trie.Load instead of common.Hash
kishansagathiya Mar 7, 2022
c24a76d
uncomment the test TestLoadWithChildTriesFails
kishansagathiya Mar 7, 2022
d6f63bd
more clean up
kishansagathiya Mar 7, 2022
46d590c
more cleanup
kishansagathiya Mar 8, 2022
68e4469
remove mockleaf
kishansagathiya Mar 8, 2022
82db221
Merge branch 'development' into kishan/task/trie-test
kishansagathiya Mar 10, 2022
42d1532
fix the commit
kishansagathiya Mar 10, 2022
2dd3a2c
fix(lib/trie): Make sure writing and reading to disk gives the same t…
kishansagathiya Mar 14, 2022
f174ab9
Merge branch 'development' into kishan/task/trie-test
kishansagathiya Mar 14, 2022
fe91011
Merge branch 'kishan/task/trie-test' of github.com:ChainSafe/gossamer…
kishansagathiya Mar 14, 2022
019953b
remove todos
kishansagathiya Mar 15, 2022
ccca175
Update lib/trie/child_storage.go
kishansagathiya Mar 17, 2022
03af4b0
Update dot/state/storage_test.go
kishansagathiya Mar 17, 2022
9d4f195
Update dot/state/storage.go
kishansagathiya Mar 18, 2022
d6cfcc2
addressed reviews
kishansagathiya Mar 18, 2022
31d2b8e
Merge branch 'kishan/task/trie-test' of github.com:ChainSafe/gossamer…
kishansagathiya Mar 18, 2022
5c7ec38
renaming Test to keyValue
kishansagathiya Mar 18, 2022
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
253 changes: 253 additions & 0 deletions internal/trie/node/node_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/trie/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,13 @@ func (t *Trie) load(db chaindb.Database, n Node) error {
for _, key := range t.GetKeysWithPrefix(ChildStorageKeyPrefix) {
childTrie := NewEmptyTrie()
value := t.Get(key)
// TODO: Tests this error
err := childTrie.Load(db, common.NewHash(value))
if err != nil {
return fmt.Errorf("failed to load child trie with root hash=0x%x: %w", value, err)
}

// TODO: Test this error
err = t.PutChild(value, childTrie)
if err != nil {
return fmt.Errorf("failed to insert child trie with root hash=0x%x into main trie: %w",
Expand Down
53 changes: 53 additions & 0 deletions lib/trie/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ package trie

import (
"bytes"
"errors"
"fmt"
"testing"

"github.com/ChainSafe/chaindb"
"github.com/ChainSafe/gossamer/internal/trie/node"
"github.com/ChainSafe/gossamer/lib/utils"
gomock "github.com/golang/mock/gomock"
qdm12 marked this conversation as resolved.
Show resolved Hide resolved
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -387,3 +390,53 @@ func TestTrie_GetFromDB(t *testing.T) {
}
}
}

func TestLoadWithChildTriesFails(t *testing.T) {
// Use a fake node implementation in which Encode always fails
// Make that root of one of the childtries
// Run load and check that it fails

testCase := []Test{
{key: []byte{0x01, 0x35}, value: []byte("pen")},
{key: []byte{0x01, 0x35, 0x79}, value: []byte("penguin")},
{key: []byte{0x01, 0x35, 0x7}, value: []byte("g")},
{key: []byte{0xf2}, value: []byte("feather")},
{key: []byte{0xf2, 0x3}, value: []byte("f")},
{key: []byte{0x09, 0xd3}, value: []byte("noot")},
{key: []byte{0x07}, value: []byte("ramen")},
{key: []byte{0}, value: nil},
}

trie := NewEmptyTrie()

for _, test := range testCase {
trie.Put(test.key, test.value)
}
sampleChildTrie := NewEmptyTrie()

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockNode := node.NewMockNode(mockCtrl)
mockNode.EXPECT().Encode(gomock.Any()).AnyTimes().Return(nil)
// TODO: Mock remaining methods

sampleChildTrie.root = mockNode

keyToChild := []byte("test")
err := trie.PutChild(keyToChild, sampleChildTrie)
require.NoError(t, err)

db := newTestDB(t)
err = trie.Store(db)
require.NoError(t, err)

mockNode.EXPECT().Encode(gomock.Any()).AnyTimes().Return(errors.New("some error"))

res := NewEmptyTrie()
err = res.Load(db, trie.MustHash())
require.NoError(t, err)
fmt.Printf("expected:\n %s\n", trie.String())
fmt.Printf("actual:\n %s\n", res.String())

require.Equal(t, trie.MustHash(), res.MustHash())
}