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: fix Kusama sync; add storageState lock in core.HandleTransactionMessage #1783

Merged
merged 9 commits into from
Sep 13, 2021
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
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move this lock outside of the loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured to put it inside the loop since if it locks outside the loop instead of for each transaction, it might cause more lock contention

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