From 60f164877860d30cc88ec5d3a0766bc2e3f34bff Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 29 Nov 2019 15:40:21 +0300 Subject: [PATCH] consensus: fix a bug with index out of range --- pkg/consensus/consensus.go | 4 +-- pkg/consensus/consensus_test.go | 62 +++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 pkg/consensus/consensus_test.go diff --git a/pkg/consensus/consensus.go b/pkg/consensus/consensus.go index f2aff036b..45362b789 100644 --- a/pkg/consensus/consensus.go +++ b/pkg/consensus/consensus.go @@ -304,8 +304,8 @@ func (s *service) getVerifiedTx(count int) []block.Transaction { txx := pool.GetVerifiedTransactions() res := make([]block.Transaction, len(txx)+1) - for i := 1; i < len(res); i++ { - res[i] = txx[i] + for i := range txx { + res[i+1] = txx[i] } for { diff --git a/pkg/consensus/consensus_test.go b/pkg/consensus/consensus_test.go new file mode 100644 index 000000000..e62d53185 --- /dev/null +++ b/pkg/consensus/consensus_test.go @@ -0,0 +1,62 @@ +package consensus + +import ( + "testing" + + "github.com/CityOfZion/neo-go/config" + "github.com/CityOfZion/neo-go/pkg/core" + "github.com/CityOfZion/neo-go/pkg/core/storage" + "github.com/CityOfZion/neo-go/pkg/core/transaction" + "github.com/CityOfZion/neo-go/pkg/util" + "github.com/nspcc-dev/dbft/block" + "github.com/stretchr/testify/require" +) + +func TestNewService(t *testing.T) { + srv := newTestService(t) + tx := &transaction.Transaction{ + Type: transaction.MinerType, + Data: &transaction.MinerTX{Nonce: 12345}, + } + item := core.NewPoolItem(tx, new(feer)) + srv.Chain.GetMemPool().TryAdd(tx.Hash(), item) + + var txx []block.Transaction + require.NotPanics(t, func() { txx = srv.getVerifiedTx(1) }) + require.Len(t, txx, 2) + require.Equal(t, tx, txx[1]) +} + +func newTestService(t *testing.T) *service { + srv, err := NewService(Config{ + Broadcast: func(*Payload) {}, + Chain: newTestChain(t), + RequestTx: func(...util.Uint256) {}, + Wallet: &config.WalletConfig{ + Path: "6PYLmjBYJ4wQTCEfqvnznGJwZeW9pfUcV5m5oreHxqryUgqKpTRAFt9L8Y", + Password: "one", + }, + }) + require.NoError(t, err) + + return srv.(*service) +} + +func newTestChain(t *testing.T) *core.Blockchain { + unitTestNetCfg, err := config.Load("../../config", config.ModeUnitTestNet) + require.NoError(t, err) + + chain, err := core.NewBlockchain(storage.NewMemoryStore(), unitTestNetCfg.ProtocolConfiguration) + require.NoError(t, err) + + go chain.Run() + + return chain +} + +type feer struct{} + +func (fs *feer) NetworkFee(*transaction.Transaction) util.Fixed8 { return util.Fixed8(0) } +func (fs *feer) IsLowPriority(*transaction.Transaction) bool { return false } +func (fs *feer) FeePerByte(*transaction.Transaction) util.Fixed8 { return util.Fixed8(0) } +func (fs *feer) SystemFee(*transaction.Transaction) util.Fixed8 { return util.Fixed8(0) }