diff --git a/dot/core/interface.go b/dot/core/interface.go index 83fe0ab21f..3cdeb0c326 100644 --- a/dot/core/interface.go +++ b/dot/core/interface.go @@ -18,6 +18,7 @@ package core import ( "math/big" + "sync" "github.com/ChainSafe/gossamer/dot/network" "github.com/ChainSafe/gossamer/dot/types" @@ -61,6 +62,7 @@ type StorageState interface { StoreTrie(*rtstorage.TrieState, *types.Header) error GetStateRootFromBlock(bhash *common.Hash) (*common.Hash, error) GetStorage(root *common.Hash, key []byte) ([]byte, error) + sync.Locker } // TransactionState is the interface for transaction state methods diff --git a/dot/core/messages.go b/dot/core/messages.go index 69ec6325a4..8cf9f2ca5d 100644 --- a/dot/core/messages.go +++ b/dot/core/messages.go @@ -32,42 +32,58 @@ func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) (boo txs := msg.Extrinsics var toPropagate []types.Extrinsic - rt, err := s.blockState.GetRuntime(nil) + head, err := s.blockState.BestBlockHeader() + if err != nil { + return false, err + } + + hash := head.Hash() + rt, err := s.blockState.GetRuntime(&hash) if err != nil { return false, err } for _, tx := range txs { - ts, err := s.storageState.TrieState(nil) - if err != nil { - return false, err - } + err = func() error { + s.storageState.Lock() + defer s.storageState.Unlock() - rt.SetContextStorage(ts) + ts, err := s.storageState.TrieState(&head.StateRoot) //nolint + if err != nil { + return err + } - // validate each transaction - externalExt := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, tx...)) - val, err := rt.ValidateTransaction(externalExt) - if err != nil { - logger.Debug("failed to validate transaction", "err", err) - continue - } + rt.SetContextStorage(ts) + + // validate each transaction + externalExt := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, tx...)) + val, err := rt.ValidateTransaction(externalExt) + if err != nil { + logger.Debug("failed to validate transaction", "err", err) + return nil + } + + // create new valid transaction + vtx := transaction.NewValidTransaction(tx, val) + + // push to the transaction queue of BABE session + hash := s.transactionState.AddToPool(vtx) + logger.Trace("added transaction to pool", "hash", hash) - // create new valid transaction - vtx := transaction.NewValidTransaction(tx, val) + // find tx(s) that should propagate + if val.Propagate { + toPropagate = append(toPropagate, tx) + } - // push to the transaction queue of BABE session - hash := s.transactionState.AddToPool(vtx) - logger.Trace("Added transaction to queue", "hash", hash) + return nil + }() - // find tx(s) that should propagate - if val.Propagate { - toPropagate = append(toPropagate, tx) + if err != nil { + return false, err } } msg.Extrinsics = toPropagate - return len(msg.Extrinsics) > 0, nil } diff --git a/dot/rpc/modules/author_integration_test.go b/dot/rpc/modules/author_integration_test.go index be641b4f54..611a878330 100644 --- a/dot/rpc/modules/author_integration_test.go +++ b/dot/rpc/modules/author_integration_test.go @@ -1,3 +1,4 @@ +//go:build integration // +build integration package modules