Skip to content

Commit 81f23cc

Browse files
fix(dot/core): check transaction Validity.Propagate field to determine whether to propagate tx (#1643)
* check validate.Propagate field * add functionality to remove non-propagate txs * fix mocks * chore: adding verbosity to golangci * chore: change the out-format argument golangci * using print-issued-lines arg * ínstalling golangci instead use github actions * lint * clean-up logic checks * run make mock * update comments, fix mock output * add test for bogus extrinsic * fix spelling * change logging level Co-authored-by: Eclésio Júnior <[email protected]>
1 parent 204af0e commit 81f23cc

File tree

7 files changed

+45
-16
lines changed

7 files changed

+45
-16
lines changed

dot/core/messages.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,20 @@ import (
2424

2525
// HandleTransactionMessage validates each transaction in the message and
2626
// adds valid transactions to the transaction queue of the BABE session
27-
func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) error {
27+
// returns boolean for transaction propagation, true - transactions should be propagated
28+
func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) (bool, error) {
2829
logger.Debug("received TransactionMessage")
2930

3031
// get transactions from message extrinsics
3132
txs := msg.Extrinsics
32-
33+
var toPropagate []types.Extrinsic
3334
for _, tx := range txs {
3435
// validate each transaction
3536
externalExt := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, tx...))
3637
val, err := s.rt.ValidateTransaction(externalExt)
3738
if err != nil {
38-
logger.Error("failed to validate transaction", "err", err)
39-
return err
39+
logger.Debug("failed to validate transaction", "err", err)
40+
continue
4041
}
4142

4243
// create new valid transaction
@@ -45,7 +46,14 @@ func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) erro
4546
// push to the transaction queue of BABE session
4647
hash := s.transactionState.AddToPool(vtx)
4748
logger.Trace("Added transaction to queue", "hash", hash)
49+
50+
// find tx(s) that should propagate
51+
if val.Propagate {
52+
toPropagate = append(toPropagate, tx)
53+
}
4854
}
4955

50-
return nil
56+
msg.Extrinsics = toPropagate
57+
58+
return len(msg.Extrinsics) > 0, nil
5159
}

dot/core/messages_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,18 @@ func TestService_HandleTransactionMessage(t *testing.T) {
155155
require.NoError(t, err)
156156

157157
extBytes := createExtrinsics(t, s.rt, genHash, 0)
158-
159158
msg := &network.TransactionMessage{Extrinsics: []types.Extrinsic{extBytes}}
160-
err = s.HandleTransactionMessage(msg)
159+
b, err := s.HandleTransactionMessage(msg)
161160
require.NoError(t, err)
161+
require.True(t, b)
162162

163163
pending := s.transactionState.(*state.TransactionState).Pending()
164164
require.NotEqual(t, 0, len(pending))
165165
require.Equal(t, extBytes, pending[0].Extrinsic)
166+
167+
extBytes = []byte(`bogus extrinsic`)
168+
msg = &network.TransactionMessage{Extrinsics: []types.Extrinsic{extBytes}}
169+
b, err = s.HandleTransactionMessage(msg)
170+
require.NoError(t, err)
171+
require.False(t, b)
166172
}

dot/network/mock_transaction_handler.go

Lines changed: 13 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/network/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ type Syncer interface {
5555

5656
// TransactionHandler is the interface used by the transactions sub-protocol
5757
type TransactionHandler interface {
58-
HandleTransactionMessage(*TransactionMessage) error
58+
HandleTransactionMessage(*TransactionMessage) (bool, error)
5959
}

dot/network/transaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,5 @@ func (s *Service) handleTransactionMessage(_ peer.ID, msg NotificationsMessage)
162162
return false, errors.New("invalid transaction type")
163163
}
164164

165-
return true, s.transactionHandler.HandleTransactionMessage(txMsg)
165+
return s.transactionHandler.HandleTransactionMessage(txMsg)
166166
}

dot/network/transaction_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestDecodeTransactionMessage(t *testing.T) {
5555
func TestHandleTransactionMessage(t *testing.T) {
5656
basePath := utils.NewTestBasePath(t, "nodeA")
5757
mockhandler := &MockTransactionHandler{}
58-
mockhandler.On("HandleTransactionMessage", mock.AnythingOfType("*network.TransactionMessage")).Return(nil)
58+
mockhandler.On("HandleTransactionMessage", mock.AnythingOfType("*network.TransactionMessage")).Return(true, nil)
5959

6060
config := &Config{
6161
BasePath: basePath,

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
179179
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
180180
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuWpEqDnvIw251EVy4zlP8gWbsGj4BsUKCRpYs=
181181
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
182+
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
182183
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
183184
github.com/holiman/bloomfilter/v2 v2.0.3 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZKVu0fao=
184185
github.com/holiman/bloomfilter/v2 v2.0.3/go.mod h1:zpoh+gs7qcpqrHr3dB55AMiJwo0iURXE7ZOP9L9hSkA=
@@ -187,6 +188,7 @@ github.com/huin/goupnp v1.0.0/go.mod h1:n9v9KO1tAxYH82qOn+UTIFQDmx5n1Zxd/ClZDMX7
187188
github.com/huin/goupnp v1.0.1-0.20200620063722-49508fba0031 h1:HarGZ5h9HD9LgEg1yRVMXyfiw4wlXiLiYM2oMjeA/SE=
188189
github.com/huin/goupnp v1.0.1-0.20200620063722-49508fba0031/go.mod h1:nNs7wvRfN1eKaMknBydLNQU6146XQim8t4h+q90biWo=
189190
github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o=
191+
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
190192
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
191193
github.com/ipfs/go-cid v0.0.1/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
192194
github.com/ipfs/go-cid v0.0.2/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
@@ -490,7 +492,9 @@ github.com/minio/sha256-simd v0.1.0/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV
490492
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
491493
github.com/minio/sha256-simd v0.1.1 h1:5QHSlgo3nt5yKOJrC7W8w7X+NFl8cMPZm96iu8kKUJU=
492494
github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
495+
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
493496
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
497+
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
494498
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
495499
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
496500
github.com/mr-tron/base58 v1.1.1/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
@@ -576,6 +580,7 @@ github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFSt
576580
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
577581
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
578582
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
583+
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
579584
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
580585
github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea h1:okKoivlkNRRLqXraEtatHfEhW+D71QTwkaj+4n4M2Xc=
581586
github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea/go.mod h1:3KEU5Dm8MAYWZqity880wOFJ9PhQjyKVZGwAEfc5Q4E=
@@ -604,9 +609,12 @@ github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572/go.mod h1:w
604609
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
605610
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
606611
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
612+
github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
607613
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
614+
github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
608615
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
609616
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
617+
github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
610618
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
611619
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
612620
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=

0 commit comments

Comments
 (0)