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
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
Prev Previous commit
Next Next commit
accept []byte key in trie.Load instead of common.Hash
kishansagathiya committed Mar 7, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 4eed7673e7ccdeee3db2aa83e553fb9d65d6d967
2 changes: 1 addition & 1 deletion dot/state/base_test.go
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ func TestTrie_StoreAndLoadFromDB(t *testing.T) {
expected := tt.MustHash()

tt = trie.NewEmptyTrie()
err = tt.Load(db, encroot)
err = tt.Load(db, encroot.ToBytes())
require.NoError(t, err)
require.Equal(t, expected, tt.MustHash())
}
2 changes: 1 addition & 1 deletion dot/state/storage.go
Original file line number Diff line number Diff line change
@@ -142,7 +142,7 @@ func (s *StorageState) TrieState(root *common.Hash) (*rtstorage.TrieState, error
// LoadFromDB loads an encoded trie from the DB where the key is `root`
func (s *StorageState) LoadFromDB(root common.Hash) (*trie.Trie, error) {
t := trie.NewEmptyTrie()
err := t.Load(s.db, root)
err := t.Load(s.db, root.ToBytes())
if err != nil {
return nil, err
}
29 changes: 14 additions & 15 deletions lib/trie/database.go
Original file line number Diff line number Diff line change
@@ -145,26 +145,26 @@ func (t *Trie) loadProof(proofHashToNode map[string]Node, n Node) {

// Load reconstructs the trie from the database from the given root hash.
// It is used when restarting the node to load the current state trie.
func (t *Trie) Load(db chaindb.Database, rootHash common.Hash) error {
if rootHash == EmptyHash {
func (t *Trie) Load(db chaindb.Database, rootHashBytes []byte) error {
if len(rootHashBytes) == 0 {
t.root = nil
return nil
}

counter := 0
for _, v := range rootHash.ToBytes() {
if v != 0 {
break
}
counter++
}
// counter := 0
// for _, v := range rootHash.ToBytes() {
// if v != 0 {
// break
// }
// counter++
// }

// remove initial 0 bits
rootHashBytes := rootHash[counter:]
// // remove initial 0 bits
// rootHashBytes := rootHash[counter:]

encodedNode, err := db.Get(rootHashBytes)
if err != nil {
return fmt.Errorf("failed to find root key %s: %w", rootHash, err)
return fmt.Errorf("failed to find root key 0x%x: %w", rootHashBytes, err)
}

reader := bytes.NewReader(encodedNode)
@@ -221,10 +221,9 @@ func (t *Trie) load(db chaindb.Database, n Node) error {
childTrie := NewEmptyTrie()
value := t.Get(key)
// TODO: Tests this error
rootHash := common.BytesToHash(value)
err := childTrie.Load(db, rootHash)
err := childTrie.Load(db, value)
if err != nil {
return fmt.Errorf("failed to load child trie with root hash=%s: %w", rootHash, err)
return fmt.Errorf("failed to load child trie with root hash=0x%x: %w", value, err)
}

// TODO: Test this error
10 changes: 5 additions & 5 deletions lib/trie/database_test.go
Original file line number Diff line number Diff line change
@@ -63,7 +63,7 @@ func TestTrie_DatabaseStoreAndLoad(t *testing.T) {
require.NoError(t, err)

res := NewEmptyTrie()
err = res.Load(db, trie.MustHash())
err = res.Load(db, trie.MustHash().ToBytes())
require.NoError(t, err)
require.Equal(t, trie.MustHash(), res.MustHash())
require.Equal(t, trie.String(), res.String())
@@ -135,7 +135,7 @@ func TestTrie_WriteDirty_Put(t *testing.T) {
require.NoError(t, err)

res := NewEmptyTrie()
err = res.Load(db, trie.MustHash())
err = res.Load(db, trie.MustHash().ToBytes())
require.NoError(t, err)
require.Equal(t, trie.MustHash(), res.MustHash())

@@ -205,7 +205,7 @@ func TestTrie_WriteDirty_PutReplace(t *testing.T) {
}

res := NewEmptyTrie()
err := res.Load(db, trie.MustHash())
err := res.Load(db, trie.MustHash().ToBytes())
require.NoError(t, err)
require.Equal(t, trie.MustHash(), res.MustHash())

@@ -265,7 +265,7 @@ func TestTrie_WriteDirty_Delete(t *testing.T) {
require.NoError(t, err)

res := NewEmptyTrie()
err = res.Load(db, trie.MustHash())
err = res.Load(db, trie.MustHash().ToBytes())
require.NoError(t, err)
require.Equal(t, trie.MustHash(), res.MustHash())

@@ -330,7 +330,7 @@ func TestTrie_WriteDirty_ClearPrefix(t *testing.T) {
require.NoError(t, err)

res := NewEmptyTrie()
err = res.Load(db, trie.MustHash())
err = res.Load(db, trie.MustHash().ToBytes())
require.NoError(t, err)

require.Equal(t, trie.MustHash(), res.MustHash())
2 changes: 1 addition & 1 deletion lib/trie/proof.go
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ func GenerateProof(root []byte, keys [][]byte, db chaindb.Database) ([][]byte, e
trackedProofs := make(map[string][]byte)

proofTrie := NewEmptyTrie()
if err := proofTrie.Load(db, common.BytesToHash(root)); err != nil {
if err := proofTrie.Load(db, root); err != nil {
return nil, err
}

3 changes: 1 addition & 2 deletions lib/trie/trie_endtoend_test.go
Original file line number Diff line number Diff line change
@@ -22,7 +22,6 @@ import (

"github.com/ChainSafe/gossamer/internal/trie/codec"
"github.com/ChainSafe/gossamer/internal/trie/node"
"github.com/ChainSafe/gossamer/lib/common"
)

const (
@@ -397,7 +396,7 @@ func TestTrieDiff(t *testing.T) {
}

dbTrie := NewEmptyTrie()
err = dbTrie.Load(storageDB, common.BytesToHash(newTrie.root.GetHash()))
err = dbTrie.Load(storageDB, newTrie.root.GetHash())
require.NoError(t, err)
}