Skip to content

Commit

Permalink
refactor(lib/runtime/storage): Don't rely on trie snapshots for stora…
Browse files Browse the repository at this point in the history
…ge transactions (#3777)

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Radosvet M <[email protected]>
Co-authored-by: Kirill <[email protected]>
Co-authored-by: JimboJ <[email protected]>
  • Loading branch information
5 people committed Mar 26, 2024
1 parent 7487e43 commit ca61197
Show file tree
Hide file tree
Showing 14 changed files with 1,602 additions and 437 deletions.
11 changes: 6 additions & 5 deletions dot/rpc/modules/childstate_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,13 @@ func setupChildStateStorage(t *testing.T) (*ChildStateModule, common.Hash) {
tr.Put([]byte(":first_key"), []byte(":value1"))
tr.Put([]byte(":second_key"), []byte(":second_value"))

childTr := trie.NewEmptyTrie()
childTr.Put([]byte(":child_first"), []byte(":child_first_value"))
childTr.Put([]byte(":child_second"), []byte(":child_second_value"))
childTr.Put([]byte(":another_child"), []byte("value"))
childStorageKey := []byte(":child_storage_key")

err = tr.SetChild([]byte(":child_storage_key"), childTr)
err = tr.SetChildStorage(childStorageKey, []byte(":child_first"), []byte(":child_first_value"))
require.NoError(t, err)
err = tr.SetChildStorage(childStorageKey, []byte(":child_second"), []byte(":child_second_value"))
require.NoError(t, err)
err = tr.SetChildStorage(childStorageKey, []byte(":another_child"), []byte("value"))
require.NoError(t, err)

stateRoot, err := tr.Root()
Expand Down
11 changes: 6 additions & 5 deletions dot/rpc/modules/childstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ func createTestTrieState(t *testing.T) (*trie.Trie, common.Hash) {
tr.Put([]byte(":first_key"), []byte(":value1"))
tr.Put([]byte(":second_key"), []byte(":second_value"))

childTr := trie.NewEmptyTrie()
childTr.Put([]byte(":child_first"), []byte(":child_first_value"))
childTr.Put([]byte(":child_second"), []byte(":child_second_value"))
childTr.Put([]byte(":another_child"), []byte("value"))
childStorageKey := []byte(":child_storage_key")

err := tr.SetChild([]byte(":child_storage_key"), childTr)
err := tr.SetChildStorage(childStorageKey, []byte(":child_first"), []byte(":child_first_value"))
require.NoError(t, err)
err = tr.SetChildStorage(childStorageKey, []byte(":child_second"), []byte(":child_second_value"))
require.NoError(t, err)
err = tr.SetChildStorage(childStorageKey, []byte(":another_child"), []byte("value"))
require.NoError(t, err)

stateRoot, err := tr.Root()
Expand Down
17 changes: 4 additions & 13 deletions dot/state/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/ChainSafe/gossamer/lib/common"
runtime "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/pkg/trie"
"github.com/ChainSafe/gossamer/pkg/trie/node"
"go.uber.org/mock/gomock"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -44,10 +43,9 @@ func TestStorage_StoreAndLoadTrie(t *testing.T) {

trie, err := storage.LoadFromDB(root)
require.NoError(t, err)
ts2 := runtime.NewTrieState(trie)
newSnapshot := ts2.Snapshot()
ts2 := runtime.NewTrieState(trie).Trie()

require.True(t, ts.Trie().Equal(newSnapshot))
require.True(t, trie.Equal(ts2))
}

func TestStorage_GetStorageByBlockHash(t *testing.T) {
Expand Down Expand Up @@ -183,16 +181,9 @@ func TestGetStorageChildAndGetStorageFromChild(t *testing.T) {
trieDB := NewMockDatabase(ctrl)
trieDB.EXPECT().Get(gomock.Any()).Times(0)

trieRoot := &node.Node{
PartialKey: []byte{1, 2},
StorageValue: []byte{3, 4},
Dirty: true,
}
testChildTrie := trie.NewTrie(trieRoot, trieDB)

testChildTrie.Put([]byte("keyInsidechild"), []byte("voila"))
genTrie.PutIntoChild([]byte("keyToChild"), []byte{1, 2}, []byte{3, 4})
genTrie.PutIntoChild([]byte("keyToChild"), []byte("keyInsidechild"), []byte("voila"))

err = genTrie.SetChild([]byte("keyToChild"), testChildTrie)
require.NoError(t, err)

tries := newTriesEmpty()
Expand Down
39 changes: 29 additions & 10 deletions lib/runtime/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,53 @@ import (
"github.com/ChainSafe/gossamer/pkg/trie"
)

// Storage runtime interface.
type Storage interface {
// Trie storage interface.
type Trie interface {
Root() (common.Hash, error)
Put(key []byte, value []byte) (err error)
Get(key []byte) []byte
Root() (common.Hash, error)
SetChild(keyToChild []byte, child *trie.Trie) error
Delete(key []byte) (err error)
NextKey([]byte) []byte
ClearPrefix(prefix []byte) (err error)
ClearPrefixLimit(prefix []byte, limit uint32) (
deleted uint32, allDeleted bool, err error)
}

// ChildTrie storage interface.S
type ChildTrie interface {
GetChildRoot(keyToChild []byte) (common.Hash, error)
SetChildStorage(keyToChild, key, value []byte) error
GetChildStorage(keyToChild, key []byte) ([]byte, error)
Delete(key []byte) (err error)
DeleteChild(keyToChild []byte) (err error)
DeleteChildLimit(keyToChild []byte, limit *[]byte) (
deleted uint32, allDeleted bool, err error)
ClearChildStorage(keyToChild, key []byte) error
NextKey([]byte) []byte
ClearPrefixInChild(keyToChild, prefix []byte) error
ClearPrefixInChildWithLimit(keyToChild, prefix []byte, limit uint32) (uint32, bool, error)
GetChildNextKey(keyToChild, key []byte) ([]byte, error)
GetChild(keyToChild []byte) (*trie.Trie, error)
ClearPrefix(prefix []byte) (err error)
ClearPrefixLimit(prefix []byte, limit uint32) (
deleted uint32, allDeleted bool, err error)
}

// Transactional storage interface.
type Transactional interface {
StartTransaction()
CommitTransaction()
RollbackTransaction()
}

// Runtime storage interface.
type Runtime interface {
LoadCode() []byte
SetVersion(v trie.TrieLayout)
}

// Storage runtime interface.
type Storage interface {
Trie
ChildTrie
Transactional
Runtime
}

// BasicNetwork interface for functions used by runtime network state function
type BasicNetwork interface {
NetworkState() common.NetworkState
Expand Down
Loading

0 comments on commit ca61197

Please sign in to comment.