Skip to content

Commit

Permalink
chore(lib/runtime/storage): NewTrieState not returning an error (#2651
Browse files Browse the repository at this point in the history
)

- Remove unneeded `newTestTrieState` helpers
- Remove several test `NoError` assertions
- Remove production code error checks for `NewTrieState` returns
  • Loading branch information
qdm12 authored Jul 28, 2022
1 parent b42b867 commit b68e8a0
Show file tree
Hide file tree
Showing 20 changed files with 74 additions and 145 deletions.
5 changes: 2 additions & 3 deletions dot/core/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
if cfg.Runtime == nil {
var rtCfg runtime.InstanceConfig

var err error
rtCfg.Storage, err = rtstorage.NewTrieState(genTrie)
require.NoError(t, err)
rtCfg.Storage = rtstorage.NewTrieState(genTrie)

var err error
rtCfg.CodeHash, err = cfg.StorageState.LoadCodeHash(nil)
require.NoError(t, err)

Expand Down
10 changes: 3 additions & 7 deletions dot/core/service_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/utils"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -60,8 +59,7 @@ func generateTestValidRemarkTxns(t *testing.T, pubKey []byte, accInfo types.Acco
genTrie, err := genesis.NewTrieFromGenesis(gen)
require.NoError(t, err)

genState, err := rtstorage.NewTrieState(genTrie)
require.NoError(t, err)
genState := rtstorage.NewTrieState(genTrie)

nodeStorage := runtime.NodeStorage{
BaseDB: runtime.NewInMemoryDB(t),
Expand Down Expand Up @@ -665,8 +663,7 @@ func TestService_HandleCodeSubstitutes(t *testing.T) {

s.blockState.StoreRuntime(blockHash, rt)

ts, err := rtstorage.NewTrieState(trie.NewEmptyTrie())
require.NoError(t, err)
ts := rtstorage.NewTrieState(nil)

err = s.handleCodeSubstitution(blockHash, ts, wasmer.NewInstance)
require.NoError(t, err)
Expand Down Expand Up @@ -694,8 +691,7 @@ func TestService_HandleRuntimeChangesAfterCodeSubstitutes(t *testing.T) {
Body: *body,
}

ts, err := rtstorage.NewTrieState(trie.NewEmptyTrie())
require.NoError(t, err)
ts := rtstorage.NewTrieState(nil)

err = s.handleCodeSubstitution(blockHash, ts, wasmer.NewInstance)
require.NoError(t, err)
Expand Down
42 changes: 13 additions & 29 deletions dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/pkg/scale"
cscale "github.com/centrifuge/go-substrate-rpc-client/v3/scale"
"github.com/centrifuge/go-substrate-rpc-client/v3/signature"
Expand Down Expand Up @@ -118,9 +117,9 @@ func generateExtrinsic(t *testing.T) (extrinsic, externalExtrinsic types.Extrins

func Test_Service_StorageRoot(t *testing.T) {
t.Parallel()
emptyTrie := trie.NewEmptyTrie()
ts, err := rtstorage.NewTrieState(emptyTrie)
require.NoError(t, err)

ts := rtstorage.NewTrieState(nil)

tests := []struct {
name string
service *Service
Expand Down Expand Up @@ -290,9 +289,7 @@ func Test_Service_handleBlock(t *testing.T) {

t.Run("storeTrie error", func(t *testing.T) {
t.Parallel()
emptyTrie := trie.NewEmptyTrie()
trieState, err := rtstorage.NewTrieState(emptyTrie)
require.NoError(t, err)
trieState := rtstorage.NewTrieState(nil)

testHeader := types.NewEmptyHeader()
block := types.NewBlock(*testHeader, *types.NewBody([]types.Extrinsic{[]byte{21}}))
Expand All @@ -308,9 +305,7 @@ func Test_Service_handleBlock(t *testing.T) {

t.Run("addBlock quit error", func(t *testing.T) {
t.Parallel()
emptyTrie := trie.NewEmptyTrie()
trieState, err := rtstorage.NewTrieState(emptyTrie)
require.NoError(t, err)
trieState := rtstorage.NewTrieState(nil)

testHeader := types.NewEmptyHeader()
block := types.NewBlock(*testHeader, *types.NewBody([]types.Extrinsic{[]byte{21}}))
Expand All @@ -331,9 +326,7 @@ func Test_Service_handleBlock(t *testing.T) {

t.Run("addBlock parent not found error", func(t *testing.T) {
t.Parallel()
emptyTrie := trie.NewEmptyTrie()
trieState, err := rtstorage.NewTrieState(emptyTrie)
require.NoError(t, err)
trieState := rtstorage.NewTrieState(nil)

testHeader := types.NewEmptyHeader()
block := types.NewBlock(*testHeader, *types.NewBody([]types.Extrinsic{[]byte{21}}))
Expand All @@ -354,9 +347,7 @@ func Test_Service_handleBlock(t *testing.T) {

t.Run("addBlock error continue", func(t *testing.T) {
t.Parallel()
emptyTrie := trie.NewEmptyTrie()
trieState, err := rtstorage.NewTrieState(emptyTrie)
require.NoError(t, err)
trieState := rtstorage.NewTrieState(nil)

testHeader := types.NewEmptyHeader()
block := types.NewBlock(*testHeader, *types.NewBody([]types.Extrinsic{[]byte{21}}))
Expand All @@ -378,9 +369,7 @@ func Test_Service_handleBlock(t *testing.T) {

t.Run("handle runtime changes error", func(t *testing.T) {
t.Parallel()
emptyTrie := trie.NewEmptyTrie()
trieState, err := rtstorage.NewTrieState(emptyTrie)
require.NoError(t, err)
trieState := rtstorage.NewTrieState(nil)

testHeader := types.NewEmptyHeader()
block := types.NewBlock(*testHeader, *types.NewBody([]types.Extrinsic{[]byte{21}}))
Expand All @@ -405,9 +394,7 @@ func Test_Service_handleBlock(t *testing.T) {

t.Run("code substitution ok", func(t *testing.T) {
t.Parallel()
emptyTrie := trie.NewEmptyTrie()
trieState, err := rtstorage.NewTrieState(emptyTrie)
require.NoError(t, err)
trieState := rtstorage.NewTrieState(nil)

testHeader := types.NewEmptyHeader()
block := types.NewBlock(*testHeader, *types.NewBody([]types.Extrinsic{[]byte{21}}))
Expand Down Expand Up @@ -448,12 +435,10 @@ func Test_Service_HandleBlockProduced(t *testing.T) {

t.Run("happy path", func(t *testing.T) {
t.Parallel()
emptyTrie := trie.NewEmptyTrie()
trieState, err := rtstorage.NewTrieState(emptyTrie)
require.NoError(t, err)
trieState := rtstorage.NewTrieState(nil)

digest := types.NewDigest()
err = digest.Add(
err := digest.Add(
types.PreRuntimeDigest{
ConsensusEngineID: types.BabeEngineID,
Data: common.MustHexToBytes("0x0201000000ef55a50f00000000"),
Expand Down Expand Up @@ -974,9 +959,8 @@ func TestServiceGetRuntimeVersion(t *testing.T) {
[]runtime.APIItem{testAPIItem},
transactionVersion,
)
emptyTrie := trie.NewEmptyTrie()
ts, err := rtstorage.NewTrieState(emptyTrie)
require.NoError(t, err)

ts := rtstorage.NewTrieState(nil)

execTest := func(t *testing.T, s *Service, bhash *common.Hash, exp runtime.Version, expErr error) {
res, err := s.GetRuntimeVersion(bhash)
Expand Down
3 changes: 1 addition & 2 deletions dot/rpc/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,7 @@ func newCoreServiceTest(t *testing.T) *core.Service {

var rtCfg runtime.InstanceConfig

rtCfg.Storage, err = rtstorage.NewTrieState(genTrie)
require.NoError(t, err)
rtCfg.Storage = rtstorage.NewTrieState(genTrie)

rtCfg.CodeHash, err = cfg.StorageState.LoadCodeHash(nil)
require.NoError(t, err)
Expand Down
3 changes: 1 addition & 2 deletions dot/rpc/modules/chain_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,7 @@ func newTestStateService(t *testing.T) *state.Service {

var rtCfg runtime.InstanceConfig

rtCfg.Storage, err = rtstorage.NewTrieState(genTrie)
require.NoError(t, err)
rtCfg.Storage = rtstorage.NewTrieState(genTrie)

if stateSrvc != nil {
rtCfg.NodeStorage.BaseDB = stateSrvc.Base
Expand Down
5 changes: 2 additions & 3 deletions dot/rpc/modules/childstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ func createTestTrieState(t *testing.T) (*trie.Trie, common.Hash) {
t.Helper()

_, genTrie, _ := genesis.NewTestGenesisWithTrieAndHeader(t)
tr, err := rtstorage.NewTrieState(genTrie)
require.NoError(t, err)
tr := rtstorage.NewTrieState(genTrie)

tr.Set([]byte(":first_key"), []byte(":value1"))
tr.Set([]byte(":second_key"), []byte(":second_value"))
Expand All @@ -33,7 +32,7 @@ func createTestTrieState(t *testing.T) (*trie.Trie, common.Hash) {
childTr.Put([]byte(":child_second"), []byte(":child_second_value"))
childTr.Put([]byte(":another_child"), []byte("value"))

err = tr.SetChild([]byte(":child_storage_key"), childTr)
err := tr.SetChild([]byte(":child_storage_key"), childTr)
require.NoError(t, err)

stateRoot, err := tr.Root()
Expand Down
3 changes: 1 addition & 2 deletions dot/services_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ func newStateServiceWithoutMock(t *testing.T) *state.Service {

var rtCfg runtime.InstanceConfig

rtCfg.Storage, err = rtstorage.NewTrieState(genTrie)
require.NoError(t, err)
rtCfg.Storage = rtstorage.NewTrieState(genTrie)

rtCfg.CodeHash, err = stateSrvc.Storage.LoadCodeHash(nil)
require.NoError(t, err)
Expand Down
3 changes: 1 addition & 2 deletions dot/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,7 @@ func newStateService(t *testing.T, ctrl *gomock.Controller) *state.Service {

var rtCfg runtime.InstanceConfig

rtCfg.Storage, err = rtstorage.NewTrieState(genTrie)
require.NoError(t, err)
rtCfg.Storage = rtstorage.NewTrieState(genTrie)

rtCfg.CodeHash, err = stateSrvc.Storage.LoadCodeHash(nil)
require.NoError(t, err)
Expand Down
5 changes: 1 addition & 4 deletions dot/state/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,7 @@ func (s *Service) storeInitialValues(data *genesis.Data, t *trie.Trie) error {
// CreateGenesisRuntime creates runtime instance form genesis
func (s *Service) CreateGenesisRuntime(t *trie.Trie, gen *genesis.Genesis) (runtime.Instance, error) {
// load genesis state into database
genTrie, err := rtstorage.NewTrieState(t)
if err != nil {
return nil, fmt.Errorf("failed to instantiate TrieState: %w", err)
}
genTrie := rtstorage.NewTrieState(t)

// create genesis runtime
rtCfg := runtime.InstanceConfig{
Expand Down
5 changes: 1 addition & 4 deletions dot/state/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ func (s *StorageState) TrieState(root *common.Hash) (*rtstorage.TrieState, error
}

nextTrie := t.Snapshot()
next, err := rtstorage.NewTrieState(nextTrie)
if err != nil {
return nil, err
}
next := rtstorage.NewTrieState(nextTrie)

logger.Tracef("returning trie with root %s to be modified", root)
return next, nil
Expand Down
6 changes: 2 additions & 4 deletions dot/state/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ func TestStorage_StoreAndLoadTrie(t *testing.T) {

trie, err := storage.LoadFromDB(root)
require.NoError(t, err)
ts2, err := runtime.NewTrieState(trie)
require.NoError(t, err)
ts2 := runtime.NewTrieState(trie)
new := ts2.Snapshot()
require.Equal(t, ts.Trie(), new)
}
Expand Down Expand Up @@ -200,8 +199,7 @@ func TestGetStorageChildAndGetStorageFromChild(t *testing.T) {
storage, err := NewStorageState(db, blockState, tries, pruner.Config{})
require.NoError(t, err)

trieState, err := runtime.NewTrieState(genTrie)
require.NoError(t, err)
trieState := runtime.NewTrieState(genTrie)

header, err := types.NewHeader(blockState.GenesisHash(), trieState.MustRoot(),
common.Hash{}, 1, types.NewDigest())
Expand Down
24 changes: 8 additions & 16 deletions dot/sync/chain_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

//go:generate mockgen -destination=mock_instance_test.go -package=$GOPACKAGE github.com/ChainSafe/gossamer/lib/runtime Instance
Expand Down Expand Up @@ -68,7 +67,7 @@ func Test_chainProcessor_handleBlock(t *testing.T) {
}, nil)
mockBlockState.EXPECT().GetRuntime(&testParentHash).Return(nil, mockError)
chainProcessor.blockState = mockBlockState
trieState := newTrieState(t)
trieState := storage.NewTrieState(nil)
mockStorageState := NewMockStorageState(ctrl)
mockStorageState.EXPECT().Lock()
mockStorageState.EXPECT().TrieState(&testHash).Return(trieState, nil)
Expand All @@ -83,7 +82,7 @@ func Test_chainProcessor_handleBlock(t *testing.T) {
},
"handle runtime ExecuteBlock error": {
chainProcessorBuilder: func(ctrl *gomock.Controller) (chainProcessor chainProcessor) {
trieState := newTrieState(t)
trieState := storage.NewTrieState(nil)
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().GetHeader(common.Hash{}).Return(&types.Header{
StateRoot: testHash,
Expand All @@ -107,7 +106,7 @@ func Test_chainProcessor_handleBlock(t *testing.T) {
},
"handle block import error": {
chainProcessorBuilder: func(ctrl *gomock.Controller) (chainProcessor chainProcessor) {
trieState := newTrieState(t)
trieState := storage.NewTrieState(nil)
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().GetHeader(common.Hash{}).Return(&types.Header{
StateRoot: testHash,
Expand Down Expand Up @@ -138,7 +137,7 @@ func Test_chainProcessor_handleBlock(t *testing.T) {
mockBlock := &types.Block{
Body: types.Body{}, // empty slice of extrinsics
}
trieState := newTrieState(t)
trieState := storage.NewTrieState(nil)
mockBlockState := NewMockBlockState(ctrl)
mockHeader := &types.Header{
Number: 0,
Expand Down Expand Up @@ -194,7 +193,7 @@ func Test_chainProcessor_handleBlock(t *testing.T) {
blockState := NewMockBlockState(ctrl)
blockState.EXPECT().GetHeader(common.Hash{1}).
Return(&types.Header{StateRoot: common.Hash{2}}, nil)
trieState := newTrieState(t)
trieState := storage.NewTrieState(nil)
storageState := NewMockStorageState(ctrl)
lockCall := storageState.EXPECT().Lock()
trieStateCall := storageState.EXPECT().TrieState(&common.Hash{2}).
Expand All @@ -211,13 +210,6 @@ func Test_chainProcessor_handleBlock(t *testing.T) {
})
}

func newTrieState(t *testing.T) *storage.TrieState {
t.Helper()
trieState, err := storage.NewTrieState(nil)
require.NoError(t, err)
return trieState
}

func Test_chainProcessor_handleBody(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -574,7 +566,7 @@ func Test_chainProcessor_processBlockData(t *testing.T) {
},
"handle block import": {
chainProcessorBuilder: func(ctrl *gomock.Controller) chainProcessor {
mockTrieState := newTrieState(t)
mockTrieState := storage.NewTrieState(nil)
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().HasHeader(common.Hash{1, 2, 3}).Return(true, nil)
mockBlockState.EXPECT().HasBlockBody(common.Hash{1, 2, 3}).Return(true, nil)
Expand Down Expand Up @@ -614,7 +606,7 @@ func Test_chainProcessor_processBlockData(t *testing.T) {
"handle header": {
chainProcessorBuilder: func(ctrl *gomock.Controller) chainProcessor {
stateRootHash := common.MustHexToHash("0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314")
mockTrieState := newTrieState(t)
mockTrieState := storage.NewTrieState(nil)
runtimeHash := common.MustHexToHash("0x7db9db5ed9967b80143100189ba69d9e4deab85ac3570e5df25686cabe32964a")
mockInstance := NewMockInstance(ctrl)
mockInstance.EXPECT().SetContextStorage(mockTrieState)
Expand Down Expand Up @@ -659,7 +651,7 @@ func Test_chainProcessor_processBlockData(t *testing.T) {
chainProcessorBuilder: func(ctrl *gomock.Controller) chainProcessor {
stateRootHash := common.MustHexToHash("0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314")
runtimeHash := common.MustHexToHash("0x7db9db5ed9967b80143100189ba69d9e4deab85ac3570e5df25686cabe32964a")
mockTrieState, _ := storage.NewTrieState(nil)
mockTrieState := storage.NewTrieState(nil)
mockInstance := NewMockInstance(ctrl)
mockInstance.EXPECT().SetContextStorage(mockTrieState)
mockInstance.EXPECT().ExecuteBlock(&types.Block{Header: types.Header{}, Body: types.Body{}})
Expand Down
3 changes: 1 addition & 2 deletions dot/sync/syncer_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ func newTestSyncer(t *testing.T) *Service {
}

// initialise runtime
genState, err := rtstorage.NewTrieState(genTrie)
require.NoError(t, err)
genState := rtstorage.NewTrieState(genTrie)

rtCfg := runtime.InstanceConfig{
Storage: genState,
Expand Down
10 changes: 5 additions & 5 deletions lib/babe/babe_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ func createTestService(t *testing.T, cfg *ServiceConfig) *Service {
}

var rtCfg runtime.InstanceConfig
rtCfg.Storage, err = rtstorage.NewTrieState(genTrie)
require.NoError(t, err)
rtCfg.Storage = rtstorage.NewTrieState(genTrie)

storageState := cfg.StorageState.(core.StorageState)
rtCfg.CodeHash, err = storageState.LoadCodeHash(nil)
Expand Down Expand Up @@ -178,9 +177,10 @@ func newTestServiceSetupParameters(t *testing.T) (*Service, *state.EpochState, *
_ = dbSrv.Stop()
})

var rtCfg runtime.InstanceConfig
rtCfg.Storage, err = rtstorage.NewTrieState(genTrie)
require.NoError(t, err)
rtCfg := runtime.InstanceConfig{
Storage: rtstorage.NewTrieState(genTrie),
}

rt, err := wasmer.NewRuntimeFromGenesis(rtCfg)
require.NoError(t, err)

Expand Down
Loading

0 comments on commit b68e8a0

Please sign in to comment.