-
Notifications
You must be signed in to change notification settings - Fork 124
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(dot/core): check transaction Validity.Propagate field to determine whether to propagate tx #1643
fix(dot/core): check transaction Validity.Propagate field to determine whether to propagate tx #1643
Changes from 11 commits
04b1aaf
1094eee
9417c83
39071d8
38850fe
f2b8582
aee53d3
49d5b71
bd77c71
5d84423
04a2750
7f32dae
9638b61
1f31c57
7dd754a
54ac571
e4bb319
43aeeef
1d74a8b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,29 +17,31 @@ | |
package core | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/ChainSafe/gossamer/dot/network" | ||
"github.com/ChainSafe/gossamer/dot/types" | ||
"github.com/ChainSafe/gossamer/lib/transaction" | ||
) | ||
|
||
// HandleTransactionMessage validates each transaction in the message and | ||
// adds valid transactions to the transaction queue of the BABE session | ||
func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) error { | ||
func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) (bool, error) { | ||
logger.Debug("received TransactionMessage") | ||
if !s.isBlockProducer { | ||
return nil | ||
return false, nil | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can remove these lines, we should be validating + propagating tx messages even if we aren't block producers There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed. |
||
|
||
// get transactions from message extrinsics | ||
txs := msg.Extrinsics | ||
|
||
var toRemove []types.Extrinsic | ||
for _, tx := range txs { | ||
// validate each transaction | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd change this to debug since it's not an error There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point, updated. |
||
return err | ||
return false, err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't return here but continue, and mark this tx "do not propagate" instead There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. makes sense, updated. |
||
} | ||
|
||
// create new valid transaction | ||
|
@@ -48,7 +50,28 @@ func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) erro | |
// push to the transaction queue of BABE session | ||
hash := s.transactionState.AddToPool(vtx) | ||
logger.Trace("Added transaction to queue", "hash", hash) | ||
|
||
// find tx(s) that should not propagate | ||
if !val.Propagate { | ||
toRemove = append(toRemove, tx) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it might be more straightforward to have a list of extrinsics to propagate instead of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, more straightforward and readable, updated. |
||
} | ||
|
||
// remove tx(s) that should not propagate | ||
for _, v := range toRemove { | ||
msg.Extrinsics = findAndDelete(msg.Extrinsics, v) | ||
} | ||
|
||
return nil | ||
return len(msg.Extrinsics) > 0, nil | ||
} | ||
|
||
func findAndDelete(s []types.Extrinsic, item types.Extrinsic) []types.Extrinsic { | ||
index := 0 | ||
for _, i := range s { | ||
if !reflect.DeepEqual(i, item) { | ||
s[index] = i | ||
index++ | ||
} | ||
} | ||
return s[:index] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,8 +157,9 @@ func TestService_HandleTransactionMessage(t *testing.T) { | |
extBytes := createExtrinsics(t, s.rt, genHash, 0) | ||
|
||
msg := &network.TransactionMessage{Extrinsics: []types.Extrinsic{extBytes}} | ||
err = s.HandleTransactionMessage(msg) | ||
b, err := s.HandleTransactionMessage(msg) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you also add a test case for invalid extrinsics? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added. |
||
require.NoError(t, err) | ||
require.True(t, b) | ||
|
||
pending := s.transactionState.(*state.TransactionState).Pending() | ||
require.NotEqual(t, 0, len(pending)) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to have a description that the bool return means there is extrinsic to propagate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point... Updated.