Skip to content

Commit

Permalink
fix(dot/core): Add only extrinsic during chain reorg. (ChainSafe#1609)
Browse files Browse the repository at this point in the history
  • Loading branch information
arijitAD committed Jun 7, 2021
1 parent 6bf66a4 commit 29413d4
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 166 deletions.
3 changes: 2 additions & 1 deletion dot/core/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) erro

for _, tx := range txs {
// validate each transaction
val, err := s.rt.ValidateTransaction(append([]byte{byte(types.TxnExternal)}, tx...))
externalExt := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, tx...))
val, err := s.rt.ValidateTransaction(externalExt)
if err != nil {
logger.Error("failed to validate transaction", "err", err)
return err
Expand Down
33 changes: 28 additions & 5 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
"github.com/ChainSafe/gossamer/lib/crypto"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/scale"
"github.com/ChainSafe/gossamer/lib/services"
"github.com/ChainSafe/gossamer/lib/transaction"

log "github.com/ChainSafe/log15"
)

Expand Down Expand Up @@ -323,6 +323,11 @@ func (s *Service) handleChainReorg(prev, curr common.Hash) error {
return err
}

// subchain contains the ancestor as well so we need to remove it.
if len(subchain) > 0 {
subchain = subchain[1:]
}

// for each block in the previous chain, re-add its extrinsics back into the pool
for _, hash := range subchain {
body, err := s.blockState.GetBlockBody(hash)
Expand All @@ -340,13 +345,30 @@ func (s *Service) handleChainReorg(prev, curr common.Hash) error {
for _, ext := range exts {
logger.Debug("validating transaction on re-org chain", "extrinsic", ext)

txv, err := s.rt.ValidateTransaction(ext)
decExt := &types.ExtrinsicData{}
err = decExt.DecodeVersion(ext)
if err != nil {
return err
}

// Inherent are not signed.
if !decExt.IsSigned() {
continue
}

encExt, err := scale.Encode(ext)
if err != nil {
return err
}

externalExt := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, encExt...))
txv, err := s.rt.ValidateTransaction(externalExt)
if err != nil {
logger.Debug("failed to validate transaction", "extrinsic", ext)
logger.Debug("failed to validate transaction", "error", err, "extrinsic", ext)
continue
}

vtx := transaction.NewValidTransaction(ext, txv)
vtx := transaction.NewValidTransaction(encExt, txv)
s.transactionState.AddToPool(vtx)
}
}
Expand Down Expand Up @@ -442,7 +464,8 @@ func (s *Service) HandleSubmittedExtrinsic(ext types.Extrinsic) error {

// the transaction source is External
// validate the transaction
txv, err := s.rt.ValidateTransaction(append([]byte{byte(types.TxnExternal)}, ext...))
externalExt := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, ext...))
txv, err := s.rt.ValidateTransaction(externalExt)
if err != nil {
return err
}
Expand Down
49 changes: 49 additions & 0 deletions dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/sync"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/keystore"
Expand Down Expand Up @@ -166,6 +167,54 @@ func TestHandleChainReorg_NoReorg(t *testing.T) {
require.NoError(t, err)
}

func TestHandleChainReorg_WithReorg_Trans(t *testing.T) {
s := NewTestService(t, nil)

bs := s.blockState

parent, err := bs.BestBlockHeader()
require.NoError(t, err)

block1 := sync.BuildBlock(t, s.rt, parent, nil)
err = bs.AddBlock(block1)
require.NoError(t, err)

block2 := sync.BuildBlock(t, s.rt, block1.Header, nil)
err = bs.AddBlock(block2)
require.NoError(t, err)

block3 := sync.BuildBlock(t, s.rt, block2.Header, nil)
err = bs.AddBlock(block3)
require.NoError(t, err)

block4 := sync.BuildBlock(t, s.rt, block3.Header, nil)
err = bs.AddBlock(block4)
require.NoError(t, err)

block5 := sync.BuildBlock(t, s.rt, block4.Header, nil)
err = bs.AddBlock(block5)
require.NoError(t, err)

block31 := sync.BuildBlock(t, s.rt, block2.Header, nil)
err = bs.AddBlock(block31)
require.NoError(t, err)

nonce := uint64(1)

// Add extrinsic to block `block31`
ext := createExtrinsics(t, s.rt, bs.GenesisHash(), nonce)

block41 := sync.BuildBlock(t, s.rt, block31.Header, ext)
err = bs.AddBlock(block41)
require.NoError(t, err)

err = s.handleChainReorg(block41.Header.Hash(), block5.Header.Hash())
require.NoError(t, err)

pending := s.transactionState.(*state.TransactionState).Pending()
require.Equal(t, 1, len(pending))
}

func TestHandleChainReorg_WithReorg_NoTransactions(t *testing.T) {
s := NewTestService(t, nil)
height := 5
Expand Down
8 changes: 4 additions & 4 deletions dot/sync/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestMain(m *testing.M) {
}

func TestService_CreateBlockResponse_MaxSize(t *testing.T) {
s := newTestSyncer(t)
s := NewTestSyncer(t)
addTestBlocksToState(t, int(maxResponseSize), s.blockState)

start, err := variadic.NewUint64OrHash(uint64(1))
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestService_CreateBlockResponse_MaxSize(t *testing.T) {
}

func TestService_CreateBlockResponse_StartHash(t *testing.T) {
s := newTestSyncer(t)
s := NewTestSyncer(t)
addTestBlocksToState(t, int(maxResponseSize), s.blockState)

startHash, err := s.blockState.GetHashByNumber(big.NewInt(1))
Expand All @@ -115,7 +115,7 @@ func TestService_CreateBlockResponse_StartHash(t *testing.T) {
}

func TestService_CreateBlockResponse_Ascending(t *testing.T) {
s := newTestSyncer(t)
s := NewTestSyncer(t)
addTestBlocksToState(t, int(maxResponseSize), s.blockState)

startHash, err := s.blockState.GetHashByNumber(big.NewInt(1))
Expand All @@ -141,7 +141,7 @@ func TestService_CreateBlockResponse_Ascending(t *testing.T) {

// tests the ProcessBlockRequestMessage method
func TestService_CreateBlockResponse(t *testing.T) {
s := newTestSyncer(t)
s := NewTestSyncer(t)
addTestBlocksToState(t, 2, s.blockState)

bestHash := s.blockState.BestBlockHash()
Expand Down
Loading

0 comments on commit 29413d4

Please sign in to comment.