Skip to content

Commit 1d688e4

Browse files
authored
fix: fix Kusama sync; add storageState lock in core.HandleTransactionMessage (ChainSafe#1783)
1 parent a91de8b commit 1d688e4

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

dot/core/interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package core
1818

1919
import (
2020
"math/big"
21+
"sync"
2122

2223
"github.com/ChainSafe/gossamer/dot/network"
2324
"github.com/ChainSafe/gossamer/dot/types"
@@ -61,6 +62,7 @@ type StorageState interface {
6162
StoreTrie(*rtstorage.TrieState, *types.Header) error
6263
GetStateRootFromBlock(bhash *common.Hash) (*common.Hash, error)
6364
GetStorage(root *common.Hash, key []byte) ([]byte, error)
65+
sync.Locker
6466
}
6567

6668
// TransactionState is the interface for transaction state methods

dot/core/messages.go

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,58 @@ func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) (boo
3232
txs := msg.Extrinsics
3333
var toPropagate []types.Extrinsic
3434

35-
rt, err := s.blockState.GetRuntime(nil)
35+
head, err := s.blockState.BestBlockHeader()
36+
if err != nil {
37+
return false, err
38+
}
39+
40+
hash := head.Hash()
41+
rt, err := s.blockState.GetRuntime(&hash)
3642
if err != nil {
3743
return false, err
3844
}
3945

4046
for _, tx := range txs {
41-
ts, err := s.storageState.TrieState(nil)
42-
if err != nil {
43-
return false, err
44-
}
47+
err = func() error {
48+
s.storageState.Lock()
49+
defer s.storageState.Unlock()
4550

46-
rt.SetContextStorage(ts)
51+
ts, err := s.storageState.TrieState(&head.StateRoot) //nolint
52+
if err != nil {
53+
return err
54+
}
4755

48-
// validate each transaction
49-
externalExt := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, tx...))
50-
val, err := rt.ValidateTransaction(externalExt)
51-
if err != nil {
52-
logger.Debug("failed to validate transaction", "err", err)
53-
continue
54-
}
56+
rt.SetContextStorage(ts)
57+
58+
// validate each transaction
59+
externalExt := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, tx...))
60+
val, err := rt.ValidateTransaction(externalExt)
61+
if err != nil {
62+
logger.Debug("failed to validate transaction", "err", err)
63+
return nil
64+
}
65+
66+
// create new valid transaction
67+
vtx := transaction.NewValidTransaction(tx, val)
68+
69+
// push to the transaction queue of BABE session
70+
hash := s.transactionState.AddToPool(vtx)
71+
logger.Trace("added transaction to pool", "hash", hash)
5572

56-
// create new valid transaction
57-
vtx := transaction.NewValidTransaction(tx, val)
73+
// find tx(s) that should propagate
74+
if val.Propagate {
75+
toPropagate = append(toPropagate, tx)
76+
}
5877

59-
// push to the transaction queue of BABE session
60-
hash := s.transactionState.AddToPool(vtx)
61-
logger.Trace("Added transaction to queue", "hash", hash)
78+
return nil
79+
}()
6280

63-
// find tx(s) that should propagate
64-
if val.Propagate {
65-
toPropagate = append(toPropagate, tx)
81+
if err != nil {
82+
return false, err
6683
}
6784
}
6885

6986
msg.Extrinsics = toPropagate
70-
7187
return len(msg.Extrinsics) > 0, nil
7288
}
7389

dot/rpc/modules/author_integration_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build integration
12
// +build integration
23

34
package modules

0 commit comments

Comments
 (0)