From 04b1aaf5d276f6129c8a8a32e562d65709b4d5d6 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Tue, 15 Jun 2021 17:49:30 -0400 Subject: [PATCH 01/14] check validate.Propagate field --- dot/core/messages.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/dot/core/messages.go b/dot/core/messages.go index 8b65d31790..a7b30fe751 100644 --- a/dot/core/messages.go +++ b/dot/core/messages.go @@ -41,13 +41,14 @@ func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) erro logger.Error("failed to validate transaction", "err", err) return err } + if val.Propagate { + // create new valid transaction + vtx := transaction.NewValidTransaction(tx, val) - // 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 queue", "hash", hash) + // push to the transaction queue of BABE session + hash := s.transactionState.AddToPool(vtx) + logger.Trace("Added transaction to queue", "hash", hash) + } } return nil From 9417c83f6b570abab7f98e7c38f213f885159e19 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Wed, 16 Jun 2021 17:18:42 -0400 Subject: [PATCH 02/14] add functionality to remove non-propagate txs --- dot/core/messages.go | 43 ++++++++++++++++++++++++++++---------- dot/core/messages_test.go | 4 +++- dot/network/state.go | 2 +- dot/network/transaction.go | 2 +- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/dot/core/messages.go b/dot/core/messages.go index a7b30fe751..e4a4eb4e43 100644 --- a/dot/core/messages.go +++ b/dot/core/messages.go @@ -20,36 +20,57 @@ import ( "github.com/ChainSafe/gossamer/dot/network" "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/transaction" + "reflect" ) // 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 } // 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) - return err + return false, err } - if val.Propagate { - // 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 queue", "hash", hash) + // 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 queue", "hash", hash) + + // find tx(s) that should not propagate + if !val.Propagate { + toRemove = append(toRemove, tx) } } - return nil + // remove tx(s) that should not propagate + for _, v := range toRemove { + msg.Extrinsics = findAndDelete(msg.Extrinsics, v) + } + + 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] +} \ No newline at end of file diff --git a/dot/core/messages_test.go b/dot/core/messages_test.go index cbeaceb990..147c3f94d5 100644 --- a/dot/core/messages_test.go +++ b/dot/core/messages_test.go @@ -17,6 +17,7 @@ package core import ( + "fmt" "math/big" "testing" "time" @@ -157,8 +158,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) require.NoError(t, err) + fmt.Printf("b %v\n", b) pending := s.transactionState.(*state.TransactionState).Pending() require.NotEqual(t, 0, len(pending)) 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) } From 39071d8758a726a2672dfcda8992e55f553e5274 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Wed, 16 Jun 2021 18:12:55 -0400 Subject: [PATCH 03/14] fix mocks --- dot/core/messages.go | 5 +++-- dot/core/messages_test.go | 3 +-- dot/network/mock_transaction_handler.go | 17 ++++++++++++----- go.sum | 9 +++++++++ 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/dot/core/messages.go b/dot/core/messages.go index e4a4eb4e43..2f9869ce86 100644 --- a/dot/core/messages.go +++ b/dot/core/messages.go @@ -17,10 +17,11 @@ package core import ( + "reflect" + "github.com/ChainSafe/gossamer/dot/network" "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/transaction" - "reflect" ) // HandleTransactionMessage validates each transaction in the message and @@ -73,4 +74,4 @@ func findAndDelete(s []types.Extrinsic, item types.Extrinsic) []types.Extrinsic } } return s[:index] -} \ No newline at end of file +} diff --git a/dot/core/messages_test.go b/dot/core/messages_test.go index 147c3f94d5..9977cd60d9 100644 --- a/dot/core/messages_test.go +++ b/dot/core/messages_test.go @@ -17,7 +17,6 @@ package core import ( - "fmt" "math/big" "testing" "time" @@ -160,7 +159,7 @@ func TestService_HandleTransactionMessage(t *testing.T) { msg := &network.TransactionMessage{Extrinsics: []types.Extrinsic{extBytes}} b, err := s.HandleTransactionMessage(msg) require.NoError(t, err) - fmt.Printf("b %v\n", b) + require.True(t, b) pending := s.transactionState.(*state.TransactionState).Pending() require.NotEqual(t, 0, len(pending)) diff --git a/dot/network/mock_transaction_handler.go b/dot/network/mock_transaction_handler.go index 45ad335bdc..7d924e5eaa 100644 --- a/dot/network/mock_transaction_handler.go +++ b/dot/network/mock_transaction_handler.go @@ -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/go.sum b/go.sum index 814e8c95c4..300effca17 100644 --- a/go.sum +++ b/go.sum @@ -184,6 +184,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= @@ -192,6 +193,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= @@ -498,7 +500,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/mmcloughlin/avo v0.0.0-20201105074841-5d2f697d268f/go.mod h1:6aKT4zZIrpGqB3RpFU14ByCSSyKY6LfJz4J/JJChHfI= github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= @@ -585,6 +589,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= @@ -615,12 +620,16 @@ 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.0-20170417170307-b6cb39589372/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= 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 v0.0.0-20170417173400-9e4c21054fa1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= 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= github.com/src-d/envconfig v1.0.0/go.mod h1:Q9YQZ7BKITldTBnoxsE5gOeB5y66RyPXeue/R4aaNBc= From f2b85826f23a3a90ceb5e830dfa43d60deccc8dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Wed, 16 Jun 2021 19:19:50 -0400 Subject: [PATCH 04/14] chore: adding verbosity to golangci --- .github/workflows/checks.yml | 1 + scripts/install-lint.sh | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index d517d7a1db..a95f67cf80 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -16,6 +16,7 @@ jobs: uses: golangci/golangci-lint-action@v2 with: version: 'latest' + args: --verbose vet-check: runs-on: ubuntu-latest diff --git a/scripts/install-lint.sh b/scripts/install-lint.sh index 8f0837bdbf..30b7c6baaa 100755 --- a/scripts/install-lint.sh +++ b/scripts/install-lint.sh @@ -6,7 +6,7 @@ fi if ! command -v golangci-lint &> /dev/null then - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.39.0 + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.41.0 fi export PATH=$PATH:$(go env GOPATH)/bin \ No newline at end of file From aee53d3debc0f6e0fb042f3a09ea907bd95b29a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Wed, 16 Jun 2021 19:24:52 -0400 Subject: [PATCH 05/14] chore: change the out-format argument golangci --- .github/workflows/checks.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index a95f67cf80..518a15e9e5 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -16,7 +16,7 @@ jobs: uses: golangci/golangci-lint-action@v2 with: version: 'latest' - args: --verbose + args: --out-format=colored-line-number vet-check: runs-on: ubuntu-latest From 49d5b710e6b94296141602a34a26277e3e4665ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Wed, 16 Jun 2021 19:27:01 -0400 Subject: [PATCH 06/14] using print-issued-lines arg --- .github/workflows/checks.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 518a15e9e5..750410032d 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -16,7 +16,7 @@ jobs: uses: golangci/golangci-lint-action@v2 with: version: 'latest' - args: --out-format=colored-line-number + args: --print-issued-lines vet-check: runs-on: ubuntu-latest From bd77c714d67a153ec6151519e46f990855b10a62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Wed, 16 Jun 2021 19:36:35 -0400 Subject: [PATCH 07/14] =?UTF-8?q?=C3=ADnstalling=20golangci=20instead=20us?= =?UTF-8?q?e=20github=20actions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/checks.yml | 7 +++---- scripts/install-lint.sh | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 750410032d..6ec47cc3b9 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -13,10 +13,9 @@ jobs: run: diff -u <(echo -n) <(gofmt -d -s .) - name: golangci-lint - uses: golangci/golangci-lint-action@v2 - with: - version: 'latest' - args: --print-issued-lines + run: | + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.41.0 + $(go env GOPATH)/bin/golangci-lint run vet-check: runs-on: ubuntu-latest diff --git a/scripts/install-lint.sh b/scripts/install-lint.sh index 30b7c6baaa..8f0837bdbf 100755 --- a/scripts/install-lint.sh +++ b/scripts/install-lint.sh @@ -6,7 +6,7 @@ fi if ! command -v golangci-lint &> /dev/null then - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.41.0 + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.39.0 fi export PATH=$PATH:$(go env GOPATH)/bin \ No newline at end of file From 5d84423e0ada63d80d7e90a50fb7b65edc105eca Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Thu, 17 Jun 2021 11:47:59 -0400 Subject: [PATCH 08/14] lint --- tests/polkadotjs_test/start_polkadotjs_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/polkadotjs_test/start_polkadotjs_test.go b/tests/polkadotjs_test/start_polkadotjs_test.go index 9f02aaf880..eec1dd5835 100644 --- a/tests/polkadotjs_test/start_polkadotjs_test.go +++ b/tests/polkadotjs_test/start_polkadotjs_test.go @@ -44,7 +44,7 @@ func TestStartGossamerAndPolkadotAPI(t *testing.T) { command := "npx mocha ./test" parts := strings.Fields(command) data, err := exec.Command(parts[0], parts[1:]...).Output() - require.NoError(t, err, fmt.Sprintf("%s", data)) + require.NoError(t, err, data) //uncomment this to see log results from javascript tests //fmt.Printf("%s\n", data) From 9638b61a4a603ac48cb8018d1ee38edf0a46a9f1 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Fri, 18 Jun 2021 10:20:10 -0400 Subject: [PATCH 09/14] clean-up logic checks --- dot/core/messages.go | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/dot/core/messages.go b/dot/core/messages.go index acd795af76..5ba768ef02 100644 --- a/dot/core/messages.go +++ b/dot/core/messages.go @@ -17,8 +17,6 @@ package core import ( - "reflect" - "github.com/ChainSafe/gossamer/dot/network" "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/transaction" @@ -31,14 +29,15 @@ func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) (boo // get transactions from message extrinsics txs := msg.Extrinsics - var toRemove []types.Extrinsic + 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 false, err + val.Propagate = false + continue } // create new valid transaction @@ -48,27 +47,13 @@ func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) (boo 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) + // find tx(s) that should propagate + if val.Propagate { + toPropagate = append(toPropagate, tx) } } - // remove tx(s) that should not propagate - for _, v := range toRemove { - msg.Extrinsics = findAndDelete(msg.Extrinsics, v) - } + msg.Extrinsics = toPropagate 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] -} From 7dd754a613fd87267bc54f99a9349508696b43f0 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Mon, 21 Jun 2021 10:43:43 -0400 Subject: [PATCH 10/14] run make mock --- dot/network/mock_transaction_handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dot/network/mock_transaction_handler.go b/dot/network/mock_transaction_handler.go index 7d924e5eaa..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 From 54ac571a49ab0082306efb491d825a8847cb18b3 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Mon, 21 Jun 2021 14:43:27 -0400 Subject: [PATCH 11/14] update comments, fix mock output --- dot/core/messages.go | 1 + dot/network/transaction_test.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/dot/core/messages.go b/dot/core/messages.go index 5ba768ef02..079d4d965d 100644 --- a/dot/core/messages.go +++ b/dot/core/messages.go @@ -24,6 +24,7 @@ import ( // HandleTransactionMessage validates each transaction in the message and // adds valid transactions to the transaction queue of the BABE session +// returns boolean for transaction propagation, true - transactions should be propagoted func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) (bool, error) { logger.Debug("received TransactionMessage") 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, From e4bb3194f309c179336e4cdcf6d8366f9c7fe460 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Tue, 22 Jun 2021 16:51:37 -0400 Subject: [PATCH 12/14] add test for bogus extrinsic --- dot/core/messages.go | 1 - dot/core/messages_test.go | 7 ++++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/dot/core/messages.go b/dot/core/messages.go index 079d4d965d..3c82f84f24 100644 --- a/dot/core/messages.go +++ b/dot/core/messages.go @@ -37,7 +37,6 @@ func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) (boo val, err := s.rt.ValidateTransaction(externalExt) if err != nil { logger.Error("failed to validate transaction", "err", err) - val.Propagate = false continue } diff --git a/dot/core/messages_test.go b/dot/core/messages_test.go index 6e87b3ee26..2717d3704e 100644 --- a/dot/core/messages_test.go +++ b/dot/core/messages_test.go @@ -155,7 +155,6 @@ func TestService_HandleTransactionMessage(t *testing.T) { require.NoError(t, err) extBytes := createExtrinsics(t, s.rt, genHash, 0) - msg := &network.TransactionMessage{Extrinsics: []types.Extrinsic{extBytes}} b, err := s.HandleTransactionMessage(msg) require.NoError(t, err) @@ -164,4 +163,10 @@ func TestService_HandleTransactionMessage(t *testing.T) { 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) } From 43aeeefdf8414d1fbc71e19ee0c5fbb376fc7d4e Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Tue, 22 Jun 2021 16:53:06 -0400 Subject: [PATCH 13/14] fix spelling --- dot/core/messages.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dot/core/messages.go b/dot/core/messages.go index 3c82f84f24..eb505371bf 100644 --- a/dot/core/messages.go +++ b/dot/core/messages.go @@ -24,7 +24,7 @@ import ( // HandleTransactionMessage validates each transaction in the message and // adds valid transactions to the transaction queue of the BABE session -// returns boolean for transaction propagation, true - transactions should be propagoted +// returns boolean for transaction propagation, true - transactions should be propagated func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) (bool, error) { logger.Debug("received TransactionMessage") From 1d74a8bea30b3ae88869d183f44b3db23b0bf58b Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Tue, 22 Jun 2021 18:15:34 -0400 Subject: [PATCH 14/14] change logging level --- dot/core/messages.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dot/core/messages.go b/dot/core/messages.go index eb505371bf..9fa5e97b51 100644 --- a/dot/core/messages.go +++ b/dot/core/messages.go @@ -36,7 +36,7 @@ func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) (boo 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) + logger.Debug("failed to validate transaction", "err", err) continue }