Skip to content

Commit

Permalink
fix: fix Kusama sync; add storageState lock in core.HandleTransaction…
Browse files Browse the repository at this point in the history
…Message (ChainSafe#1783)
  • Loading branch information
noot authored Sep 13, 2021
1 parent a91de8b commit 1d688e4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
2 changes: 2 additions & 0 deletions dot/core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package core

import (
"math/big"
"sync"

"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/types"
Expand Down Expand Up @@ -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
Expand Down
60 changes: 38 additions & 22 deletions dot/core/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions dot/rpc/modules/author_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build integration
// +build integration

package modules
Expand Down

0 comments on commit 1d688e4

Please sign in to comment.