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

chore(all): version trie calls #2774

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 dot/build_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,19 @@ func BuildFromDB(path string) (*BuildSpec, error) {
if err != nil {
return nil, fmt.Errorf("cannot start state service: %w", err)
}

bestBlockStateRoot, err := stateSrvc.Block.BestBlockStateRoot()
if err != nil {
return nil, fmt.Errorf("getting best block state root: %w", err)
}

instance, err := stateSrvc.Block.GetRuntime(&bestBlockStateRoot)
if err != nil {
return nil, fmt.Errorf("getting runtime instance for best block state root: %w", err)
}

// set genesis fields data
ent, err := stateSrvc.Storage.Entries(nil)
ent, err := stateSrvc.Storage.Entries(&bestBlockStateRoot, instance.StateVersion())
if err != nil {
return nil, fmt.Errorf("failed to get storage trie entries: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion dot/build_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func TestBuildFromDB(t *testing.T) {
if tt.err != nil {
assert.EqualError(t, err, tt.err.Error())
} else {
assert.NoError(t, err)
require.NoError(t, err)
}
if tt.want != nil {
got.genesis.Genesis.Raw = map[string]map[string]string{}
Expand Down
5 changes: 4 additions & 1 deletion dot/core/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,14 @@ func newTestGenesisWithTrieAndHeader(t *testing.T) (
require.NoError(t, err)
gen = *genPtr

stateVersion, err := wasmer.StateVersionFromGenesis(gen)
require.NoError(t, err)

genesisTrie, err = wasmer.NewTrieFromGenesis(gen)
require.NoError(t, err)

parentHash := common.NewHash([]byte{0})
stateRoot := genesisTrie.MustHash()
stateRoot := genesisTrie.MustHash(stateVersion)
extrinsicRoot := trie.EmptyHash
const number = 0
digest := types.NewDigest()
Expand Down
12 changes: 8 additions & 4 deletions dot/core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/ChainSafe/gossamer/lib/runtime"
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/lib/trie"
)

// RuntimeInstance for runtime methods
Expand All @@ -30,6 +31,7 @@ type RuntimeInstance interface {
SetContextStorage(s runtime.Storage)
GetCodeHash() common.Hash
Version() (version runtime.Version)
StateVersion() (stateVersion trie.Version)
Metadata() ([]byte, error)
BabeConfiguration() (*types.BabeConfiguration, error)
GrandpaAuthorities() ([]types.Authority, error)
Expand Down Expand Up @@ -70,19 +72,21 @@ type BlockState interface {
SubChain(start, end common.Hash) ([]common.Hash, error)
GetBlockBody(hash common.Hash) (*types.Body, error)
HandleRuntimeChanges(newState *rtstorage.TrieState, in runtime.Instance, bHash common.Hash) error
GetRuntime(*common.Hash) (runtime.Instance, error)
GetRuntime(blockHash *common.Hash) (instance runtime.Instance, err error)
StoreRuntime(common.Hash, runtime.Instance)
}

// StorageState interface for storage state methods
type StorageState interface {
LoadCode(root *common.Hash) ([]byte, error)
LoadCodeHash(root *common.Hash) (common.Hash, error)
TrieState(root *common.Hash) (*rtstorage.TrieState, error)
StoreTrie(*rtstorage.TrieState, *types.Header) error
TrieState(root *common.Hash, stateVersion trie.Version) (*rtstorage.TrieState, error)
StoreTrie(trieState *rtstorage.TrieState, blockHeader *types.Header,
stateVersion trie.Version) error
GetStateRootFromBlock(bhash *common.Hash) (*common.Hash, error)
GetStorage(root *common.Hash, key []byte) ([]byte, error)
GenerateTrieProof(stateRoot common.Hash, keys [][]byte) ([][]byte, error)
GenerateTrieProof(stateRoot common.Hash, keys [][]byte,
stateVersion trie.Version) (encodedNodes [][]byte, err error)
sync.Locker
}

Expand Down
6 changes: 5 additions & 1 deletion dot/core/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ func (s *Service) validateTransaction(head *types.Header, rt RuntimeInstance,
tx types.Extrinsic) (validity *transaction.Validity, err error) {
s.storageState.Lock()

ts, err := s.storageState.TrieState(&head.StateRoot)
// Note this is a cheap call getting the runtime cached version
// so we can call this in this function and not pass it as argument.
stateVersion := rt.StateVersion()

ts, err := s.storageState.TrieState(&head.StateRoot, stateVersion)
s.storageState.Unlock()
if err != nil {
return nil, fmt.Errorf("cannot get trie state from storage for root %s: %w", head.StateRoot, err)
Expand Down
7 changes: 5 additions & 2 deletions dot/core/messages_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/pkg/scale"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -107,7 +108,8 @@ func TestService_HandleBlockProduced(t *testing.T) {

net.EXPECT().GossipMessage(expected)

state, err := s.storageState.TrieState(nil)
const stateVersion = trie.V0 // TODO should we get this from something?
state, err := s.storageState.TrieState(nil, stateVersion)
require.NoError(t, err)

err = s.HandleBlockProduced(&newBlock, state)
Expand Down Expand Up @@ -153,7 +155,8 @@ func TestService_HandleTransactionMessage(t *testing.T) {
rt, err := s.blockState.GetRuntime(nil)
require.NoError(t, err)

ts, err := s.storageState.TrieState(nil)
const stateVersion = trie.V0 // TODO should we get this from something?
ts, err := s.storageState.TrieState(nil, stateVersion)
require.NoError(t, err)
rt.SetContextStorage(ts)

Expand Down
4 changes: 3 additions & 1 deletion dot/core/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/lib/trie"

"github.com/golang/mock/gomock"
"github.com/libp2p/go-libp2p-core/peer"
Expand Down Expand Up @@ -350,7 +351,8 @@ func TestServiceHandleTransactionMessage(t *testing.T) {
storageState := NewMockStorageState(ctrl)
storageState.EXPECT().Lock()
storageState.EXPECT().Unlock()
storageState.EXPECT().TrieState(tt.mockStorageState.input).Return(
const stateVersion = trie.V0
storageState.EXPECT().TrieState(tt.mockStorageState.input, stateVersion).Return(
tt.mockStorageState.trieState,
tt.mockStorageState.err)
s.storageState = storageState
Expand Down
39 changes: 27 additions & 12 deletions dot/core/mocks_test.go

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

Loading