From 81f23ccdfc176c8054099d369143422011c5472b Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Wed, 23 Jun 2021 10:07:47 -0400 Subject: [PATCH] fix(dot/core): check transaction Validity.Propagate field to determine whether to propagate tx (#1643) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- dot/core/messages.go | 18 +++++++++++++----- dot/core/messages_test.go | 10 ++++++++-- dot/network/mock_transaction_handler.go | 19 +++++++++++++------ dot/network/state.go | 2 +- dot/network/transaction.go | 2 +- dot/network/transaction_test.go | 2 +- go.sum | 8 ++++++++ 7 files changed, 45 insertions(+), 16 deletions(-) diff --git a/dot/core/messages.go b/dot/core/messages.go index e604da2ff3..9fa5e97b51 100644 --- a/dot/core/messages.go +++ b/dot/core/messages.go @@ -24,19 +24,20 @@ import ( // 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 { +// returns boolean for transaction propagation, true - transactions should be propagated +func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) (bool, error) { logger.Debug("received TransactionMessage") // get transactions from message extrinsics txs := msg.Extrinsics - + var toPropagate []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) - return err + logger.Debug("failed to validate transaction", "err", err) + continue } // create new valid transaction @@ -45,7 +46,14 @@ 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 propagate + if val.Propagate { + toPropagate = append(toPropagate, tx) + } } - return nil + msg.Extrinsics = toPropagate + + return len(msg.Extrinsics) > 0, nil } diff --git a/dot/core/messages_test.go b/dot/core/messages_test.go index 701c5db2b4..2717d3704e 100644 --- a/dot/core/messages_test.go +++ b/dot/core/messages_test.go @@ -155,12 +155,18 @@ func TestService_HandleTransactionMessage(t *testing.T) { require.NoError(t, err) extBytes := createExtrinsics(t, s.rt, genHash, 0) - msg := &network.TransactionMessage{Extrinsics: []types.Extrinsic{extBytes}} - err = s.HandleTransactionMessage(msg) + b, err := s.HandleTransactionMessage(msg) require.NoError(t, err) + require.True(t, b) pending := s.transactionState.(*state.TransactionState).Pending() require.NotEqual(t, 0, len(pending)) require.Equal(t, extBytes, pending[0].Extrinsic) + + extBytes = []byte(`bogus extrinsic`) + msg = &network.TransactionMessage{Extrinsics: []types.Extrinsic{extBytes}} + b, err = s.HandleTransactionMessage(msg) + require.NoError(t, err) + require.False(t, b) } diff --git a/dot/network/mock_transaction_handler.go b/dot/network/mock_transaction_handler.go index 45ad335bdc..c3eaa07972 100644 --- a/dot/network/mock_transaction_handler.go +++ b/dot/network/mock_transaction_handler.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v1.0.0. DO NOT EDIT. package network @@ -10,15 +10,22 @@ type MockTransactionHandler struct { } // HandleTransactionMessage provides a mock function with given fields: _a0 -func (_m *MockTransactionHandler) HandleTransactionMessage(_a0 *TransactionMessage) error { +func (_m *MockTransactionHandler) HandleTransactionMessage(_a0 *TransactionMessage) (bool, error) { ret := _m.Called(_a0) - var r0 error - if rf, ok := ret.Get(0).(func(*TransactionMessage) error); ok { + var r0 bool + if rf, ok := ret.Get(0).(func(*TransactionMessage) bool); ok { r0 = rf(_a0) } else { - r0 = ret.Error(0) + r0 = ret.Get(0).(bool) } - return r0 + var r1 error + if rf, ok := ret.Get(1).(func(*TransactionMessage) error); ok { + r1 = rf(_a0) + } else { + r1 = ret.Error(1) + } + + return r0, r1 } diff --git a/dot/network/state.go b/dot/network/state.go index 70b948d11f..61c777526b 100644 --- a/dot/network/state.go +++ b/dot/network/state.go @@ -55,5 +55,5 @@ type Syncer interface { // TransactionHandler is the interface used by the transactions sub-protocol type TransactionHandler interface { - HandleTransactionMessage(*TransactionMessage) error + HandleTransactionMessage(*TransactionMessage) (bool, error) } diff --git a/dot/network/transaction.go b/dot/network/transaction.go index 2a2c13f9a2..7bf9cb925b 100644 --- a/dot/network/transaction.go +++ b/dot/network/transaction.go @@ -162,5 +162,5 @@ func (s *Service) handleTransactionMessage(_ peer.ID, msg NotificationsMessage) return false, errors.New("invalid transaction type") } - return true, s.transactionHandler.HandleTransactionMessage(txMsg) + return s.transactionHandler.HandleTransactionMessage(txMsg) } diff --git a/dot/network/transaction_test.go b/dot/network/transaction_test.go index d38cf14f8d..cb78794c31 100644 --- a/dot/network/transaction_test.go +++ b/dot/network/transaction_test.go @@ -55,7 +55,7 @@ func TestDecodeTransactionMessage(t *testing.T) { func TestHandleTransactionMessage(t *testing.T) { basePath := utils.NewTestBasePath(t, "nodeA") mockhandler := &MockTransactionHandler{} - mockhandler.On("HandleTransactionMessage", mock.AnythingOfType("*network.TransactionMessage")).Return(nil) + mockhandler.On("HandleTransactionMessage", mock.AnythingOfType("*network.TransactionMessage")).Return(true, nil) config := &Config{ BasePath: basePath, diff --git a/go.sum b/go.sum index 37101993d9..a4d9235f07 100644 --- a/go.sum +++ b/go.sum @@ -179,6 +179,7 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuWpEqDnvIw251EVy4zlP8gWbsGj4BsUKCRpYs= github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/holiman/bloomfilter/v2 v2.0.3 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZKVu0fao= 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 github.com/huin/goupnp v1.0.1-0.20200620063722-49508fba0031 h1:HarGZ5h9HD9LgEg1yRVMXyfiw4wlXiLiYM2oMjeA/SE= github.com/huin/goupnp v1.0.1-0.20200620063722-49508fba0031/go.mod h1:nNs7wvRfN1eKaMknBydLNQU6146XQim8t4h+q90biWo= github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/ipfs/go-cid v0.0.1/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM= 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 github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= github.com/minio/sha256-simd v0.1.1 h1:5QHSlgo3nt5yKOJrC7W8w7X+NFl8cMPZm96iu8kKUJU= github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= 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 github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= +github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea h1:okKoivlkNRRLqXraEtatHfEhW+D71QTwkaj+4n4M2Xc= 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 github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= +github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= +github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=