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(dot/core): fix the race condition in TrieState #2499

Merged
merged 8 commits into from
May 3, 2022
Merged
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
12 changes: 10 additions & 2 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"context"
"errors"
"fmt"
"sync"

"github.com/ChainSafe/gossamer/dot/network"
Expand Down Expand Up @@ -495,12 +496,19 @@ func (s *Service) HandleSubmittedExtrinsic(ext types.Extrinsic) error {
return nil
}

ts, err := s.storageState.TrieState(nil)
bestBlockHash := s.blockState.BestBlockHash()

stateRoot, err := s.storageState.GetStateRootFromBlock(&bestBlockHash)
if err != nil {
return fmt.Errorf("could not get state root from block %s: %w", bestBlockHash, err)
}

ts, err := s.storageState.TrieState(stateRoot)
if err != nil {
return err
}

rt, err := s.blockState.GetRuntime(nil)
rt, err := s.blockState.GetRuntime(&bestBlockHash)
if err != nil {
logger.Critical("failed to get runtime")
return err
Expand Down
42 changes: 31 additions & 11 deletions dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1068,10 +1068,15 @@ func TestServiceHandleSubmittedExtrinsic(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockStorageState := NewMockStorageState(ctrl)
mockStorageState.EXPECT().TrieState(nil).Return(nil, errDummyErr)
mockStorageState.EXPECT().TrieState(&common.Hash{}).Return(nil, errDummyErr)
mockStorageState.EXPECT().GetStateRootFromBlock(&common.Hash{}).Return(&common.Hash{}, nil)

mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().BestBlockHash().Return(common.Hash{})
mockTxnState := NewMockTransactionState(ctrl)
mockTxnState.EXPECT().Exists(nil)
service := &Service{
blockState: mockBlockState,
storageState: mockStorageState,
transactionState: mockTxnState,
net: NewMockNetwork(ctrl),
Expand All @@ -1082,10 +1087,15 @@ func TestServiceHandleSubmittedExtrinsic(t *testing.T) {
t.Run("get runtime err", func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockStorageState := NewMockStorageState(ctrl)
mockStorageState.EXPECT().TrieState(nil).Return(&rtstorage.TrieState{}, nil)

mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().GetRuntime(nil).Return(nil, errDummyErr)
mockBlockState.EXPECT().BestBlockHash().Return(common.Hash{})
mockBlockState.EXPECT().GetRuntime(&common.Hash{}).Return(nil, errDummyErr)

mockStorageState := NewMockStorageState(ctrl)
mockStorageState.EXPECT().TrieState(&common.Hash{}).Return(&rtstorage.TrieState{}, nil)
mockStorageState.EXPECT().GetStateRootFromBlock(&common.Hash{}).Return(&common.Hash{}, nil)

mockTxnState := NewMockTransactionState(ctrl)
mockTxnState.EXPECT().Exists(nil).MaxTimes(2)
service := &Service{
Expand All @@ -1100,13 +1110,18 @@ func TestServiceHandleSubmittedExtrinsic(t *testing.T) {
t.Run("validate txn err", func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().BestBlockHash().Return(common.Hash{})
runtimeMockErr := new(mocksruntime.Instance)
mockBlockState.EXPECT().GetRuntime(&common.Hash{}).Return(runtimeMockErr, nil).MaxTimes(2)

mockStorageState := NewMockStorageState(ctrl)
mockStorageState.EXPECT().TrieState(nil).Return(&rtstorage.TrieState{}, nil)
mockStorageState.EXPECT().TrieState(&common.Hash{}).Return(&rtstorage.TrieState{}, nil)
mockStorageState.EXPECT().GetStateRootFromBlock(&common.Hash{}).Return(&common.Hash{}, nil)

mockTxnState := NewMockTransactionState(ctrl)
mockTxnState.EXPECT().Exists(types.Extrinsic{})
runtimeMockErr := new(mocksruntime.Instance)
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().GetRuntime(nil).Return(runtimeMockErr, nil).MaxTimes(2)

runtimeMockErr.On("SetContextStorage", &rtstorage.TrieState{})
runtimeMockErr.On("ValidateTransaction", externalExt).Return(nil, errDummyErr)
service := &Service{
Expand All @@ -1121,14 +1136,19 @@ func TestServiceHandleSubmittedExtrinsic(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockStorageState := NewMockStorageState(ctrl)
mockStorageState.EXPECT().TrieState(nil).Return(&rtstorage.TrieState{}, nil)

runtimeMock := new(mocksruntime.Instance)
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().GetRuntime(nil).Return(runtimeMock, nil).MaxTimes(2)
mockBlockState.EXPECT().BestBlockHash().Return(common.Hash{})
mockBlockState.EXPECT().GetRuntime(&common.Hash{}).Return(runtimeMock, nil).MaxTimes(2)
runtimeMock.On("SetContextStorage", &rtstorage.TrieState{})
runtimeMock.On("ValidateTransaction", externalExt).
Return(&transaction.Validity{Propagate: true}, nil)

mockStorageState := NewMockStorageState(ctrl)
mockStorageState.EXPECT().TrieState(&common.Hash{}).Return(&rtstorage.TrieState{}, nil)
mockStorageState.EXPECT().GetStateRootFromBlock(&common.Hash{}).Return(&common.Hash{}, nil)

mockTxnState := NewMockTransactionState(ctrl)
mockTxnState.EXPECT().Exists(types.Extrinsic{}).MaxTimes(2)
mockTxnState.EXPECT().AddToPool(transaction.NewValidTransaction(ext, &transaction.Validity{Propagate: true}))
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,6 @@ func TestHTTPServer_ServeHTTP(t *testing.T) {

_, message, err := c.ReadMessage()
require.NoError(t, err)
require.Equal(t, item.expected, message)
require.Equal(t, string(item.expected), string(message))
}
}