From 38103dcc7a90fabe79a618fbd8e579225b76b7e8 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 9 Mar 2021 21:30:09 +0300 Subject: [PATCH] consensus: check transactions count during PrepareRequest verification --- pkg/consensus/consensus.go | 10 +++++++--- pkg/consensus/consensus_test.go | 7 +++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pkg/consensus/consensus.go b/pkg/consensus/consensus.go index 311766998..298346fc5 100644 --- a/pkg/consensus/consensus.go +++ b/pkg/consensus/consensus.go @@ -466,9 +466,10 @@ func (s *service) verifyBlock(b block.Block) bool { } var ( - errInvalidPrevHash = errors.New("invalid PrevHash") - errInvalidVersion = errors.New("invalid Version") - errInvalidStateRoot = errors.New("state root mismatch") + errInvalidPrevHash = errors.New("invalid PrevHash") + errInvalidVersion = errors.New("invalid Version") + errInvalidStateRoot = errors.New("state root mismatch") + errInvalidTransactionsCount = errors.New("invalid transactions count") ) func (s *service) verifyRequest(p payload.ConsensusPayload) error { @@ -487,6 +488,9 @@ func (s *service) verifyRequest(p payload.ConsensusPayload) error { return fmt.Errorf("%w: %s != %s", errInvalidStateRoot, sr.Root, req.stateRoot) } } + if len(req.TransactionHashes()) > int(s.ProtocolConfiguration.MaxTransactionsPerBlock) { + return fmt.Errorf("%w: max = %d, got %d", errInvalidTransactionsCount, s.ProtocolConfiguration.MaxTransactionsPerBlock, len(req.TransactionHashes())) + } // Save lastProposal for getVerified(). s.lastProposal = req.transactionHashes diff --git a/pkg/consensus/consensus_test.go b/pkg/consensus/consensus_test.go index 8500cbd90..ce287eed8 100644 --- a/pkg/consensus/consensus_test.go +++ b/pkg/consensus/consensus_test.go @@ -323,6 +323,13 @@ func TestService_PrepareRequest(t *testing.T) { sr, err := srv.Chain.GetStateModule().GetStateRoot(srv.dbft.BlockIndex - 1) require.NoError(t, err) + + checkRequest(t, errInvalidTransactionsCount, &prepareRequest{stateRootEnabled: true, + prevHash: prevHash, + stateRoot: sr.Root, + transactionHashes: make([]util.Uint256, srv.ProtocolConfiguration.MaxTransactionsPerBlock+1), + }) + checkRequest(t, nil, &prepareRequest{ stateRootEnabled: true, prevHash: prevHash,