Repository: kaspanet/kaspad
Pull request #1627 (2021-03-24 15:28:36)

Add mass limit to mempool

codecov[bot]: # [Codecov](https://codecov.io/gh/kaspanet/kaspad/pull/1627?src=pr&el=h1) Report
> Merging [#1627](https://codecov.io/gh/kaspanet/kaspad/pull/1627?src=pr&el=desc) (5d2ca8a) into [v0.10.0-dev](https://codecov.io/gh/kaspanet/kaspad/commit/6ec0a8a559b17957ce2456de5bb3c94e1ce36c6c?el=desc) (6ec0a8a) will **increase** coverage by `0.00%`.
> The diff coverage is `50.00%`.

> :exclamation: Current head 5d2ca8a differs from pull request most recent head 5fb9e27. Consider uploading reports for the commit 5fb9e27 to get more accurate results
[![Impacted file tree graph](https://codecov.io/gh/kaspanet/kaspad/pull/1627/graphs/tree.svg?width=650&height=150&src=pr&token=2FBIEEJTNU)](https://codecov.io/gh/kaspanet/kaspad/pull/1627?src=pr&el=tree)

```diff
@@             Coverage Diff              @@
##           v0.10.0-dev    #1627   +/-   ##
============================================
  Coverage        59.92%   59.92%           
============================================
  Files              516      516           
  Lines            20469    20475    +6     
============================================
+ Hits             12266    12270    +4     
- Misses            6246     6248    +2     
  Partials          1957     1957           
```


| [Impacted Files](https://codecov.io/gh/kaspanet/kaspad/pull/1627?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [domain/miningmanager/mempool/mempool.go](https://codecov.io/gh/kaspanet/kaspad/pull/1627/diff?src=pr&el=tree#diff-ZG9tYWluL21pbmluZ21hbmFnZXIvbWVtcG9vbC9tZW1wb29sLmdv) | `39.18% <25.00%> (-0.17%)` | :arrow_down: |
| [domain/domain.go](https://codecov.io/gh/kaspanet/kaspad/pull/1627/diff?src=pr&el=tree#diff-ZG9tYWluL2RvbWFpbi5nbw==) | `84.61% <100.00%> (ø)` | |
| [domain/miningmanager/factory.go](https://codecov.io/gh/kaspanet/kaspad/pull/1627/diff?src=pr&el=tree#diff-ZG9tYWluL21pbmluZ21hbmFnZXIvZmFjdG9yeS5nbw==) | `100.00% <100.00%> (ø)` | |
| [...ain/consensus/database/binaryserialization/hash.go](https://codecov.io/gh/kaspanet/kaspad/pull/1627/diff?src=pr&el=tree#diff-ZG9tYWluL2NvbnNlbnN1cy9kYXRhYmFzZS9iaW5hcnlzZXJpYWxpemF0aW9uL2hhc2guZ28=) | `64.70% <0.00%> (-5.89%)` | :arrow_down: |
| [...ersselectedchainstore/headersselectedchainstore.go](https://codecov.io/gh/kaspanet/kaspad/pull/1627/diff?src=pr&el=tree#diff-ZG9tYWluL2NvbnNlbnN1cy9kYXRhc3RydWN0dXJlcy9oZWFkZXJzc2VsZWN0ZWRjaGFpbnN0b3JlL2hlYWRlcnNzZWxlY3RlZGNoYWluc3RvcmUuZ28=) | `68.22% <0.00%> (ø)` | |
| [.../consensus/processes/blockbuilder/block\_builder.go](https://codecov.io/gh/kaspanet/kaspad/pull/1627/diff?src=pr&el=tree#diff-ZG9tYWluL2NvbnNlbnN1cy9wcm9jZXNzZXMvYmxvY2tidWlsZGVyL2Jsb2NrX2J1aWxkZXIuZ28=) | `77.96% <0.00%> (+1.69%)` | :arrow_up: |
| [domain/consensus/utils/multiset/multiset.go](https://codecov.io/gh/kaspanet/kaspad/pull/1627/diff?src=pr&el=tree#diff-ZG9tYWluL2NvbnNlbnN1cy91dGlscy9tdWx0aXNldC9tdWx0aXNldC5nbw==) | `72.22% <0.00%> (+3.47%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/kaspanet/kaspad/pull/1627?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/kaspanet/kaspad/pull/1627?src=pr&el=footer). Last update [6ec0a8a...5fb9e27](https://codecov.io/gh/kaspanet/kaspad/pull/1627?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/domain/domain.go b/domain/domain.go
index 84a50b98..124cfd87 100644
--- a/domain/domain.go
+++ b/domain/domain.go
@@ -36,8 +36,7 @@ func New(dagParams *dagconfig.Params, db infrastructuredatabase.Database, isArch
 	}
 
 	miningManagerFactory := miningmanager.NewFactory()
-	miningManager := miningManagerFactory.NewMiningManager(consensusInstance, dagParams.MaxMassAcceptedByBlock,
-		dagParams.RelayNonStdTxs)
+	miningManager := miningManagerFactory.NewMiningManager(consensusInstance, dagParams)
 
 	return &domain{
 		consensus:     consensusInstance,
diff --git a/domain/miningmanager/factory.go b/domain/miningmanager/factory.go
index 53ccfae9..a578c9bb 100644
--- a/domain/miningmanager/factory.go
+++ b/domain/miningmanager/factory.go
@@ -2,21 +2,22 @@ package miningmanager
 
 import (
 	"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
+	"github.com/kaspanet/kaspad/domain/dagconfig"
 	"github.com/kaspanet/kaspad/domain/miningmanager/blocktemplatebuilder"
 	mempoolpkg "github.com/kaspanet/kaspad/domain/miningmanager/mempool"
 )
 
 // Factory instantiates new mining managers
 type Factory interface {
-	NewMiningManager(consensus externalapi.Consensus, blockMaxMass uint64, acceptNonStd bool) MiningManager
+	NewMiningManager(consensus externalapi.Consensus, params *dagconfig.Params) MiningManager
 }
 
 type factory struct{}
 
 // NewMiningManager instantiate a new mining manager
-func (f *factory) NewMiningManager(consensus externalapi.Consensus, blockMaxMass uint64, acceptNonStd bool) MiningManager {
-	mempool := mempoolpkg.New(consensus, acceptNonStd)
-	blockTemplateBuilder := blocktemplatebuilder.New(consensus, mempool, blockMaxMass)
+func (f *factory) NewMiningManager(consensus externalapi.Consensus, params *dagconfig.Params) MiningManager {
+	mempool := mempoolpkg.New(consensus, params)
+	blockTemplateBuilder := blocktemplatebuilder.New(consensus, mempool, params.MaxMassAcceptedByBlock)
 
 	return &miningManager{
 		mempool:              mempool,
diff --git a/domain/miningmanager/mempool/mempool.go b/domain/miningmanager/mempool/mempool.go
index 397b0bc4..9ff7c9d6 100644
--- a/domain/miningmanager/mempool/mempool.go
+++ b/domain/miningmanager/mempool/mempool.go
@@ -7,6 +7,7 @@ package mempool
 import (
 	"container/list"
 	"fmt"
+	"github.com/kaspanet/kaspad/domain/dagconfig"
 	"sort"
 	"sync"
 	"time"
@@ -88,16 +89,17 @@ type mempool struct {
 	// to on an unconditional timer.
 	nextExpireScan mstime.Time
 
-	mtx    sync.RWMutex
-	policy policy
+	mtx       sync.RWMutex
+	policy    policy
+	dagParams *dagconfig.Params
 }
 
 // New returns a new memory pool for validating and storing standalone
 // transactions until they are mined into a block.
-func New(consensus consensusexternalapi.Consensus, acceptNonStd bool) miningmanagermodel.Mempool {
+func New(consensus consensusexternalapi.Consensus, dagParams *dagconfig.Params) miningmanagermodel.Mempool {
 	policy := policy{
 		MaxTxVersion:    constants.MaxTransactionVersion,
-		AcceptNonStd:    acceptNonStd,
+		AcceptNonStd:    dagParams.RelayNonStdTxs,
 		MaxOrphanTxs:    5,
 		MaxOrphanTxSize: 100000,
 		MinRelayTxFee:   1000, // 1 sompi per byte
@@ -113,6 +115,7 @@ func New(consensus consensusexternalapi.Consensus, acceptNonStd bool) miningmana
 		mempoolUTXOSet:                       newMempoolUTXOSet(),
 		consensus:                            consensus,
 		nextExpireScan:                       mstime.Now().Add(orphanExpireScanInterval),
+		dagParams:                            dagParams,
 	}
 }
 
@@ -723,6 +726,11 @@ func (mp *mempool) maybeAcceptTransaction(tx *consensusexternalapi.DomainTransac
 		return nil, nil, err
 	}
 
+	if tx.Mass > mp.dagParams.MaxMassAcceptedByBlock {
+		return nil, nil, newRuleError(errors.Errorf("The transaction mass is %d which is "+
+			"higher than the maxmimum of %d", tx.Mass, mp.dagParams.MaxMassAcceptedByBlock))
+	}
+
 	// Don't allow transactions with non-standard inputs if the network
 	// parameters forbid their acceptance.
 	if !mp.policy.AcceptNonStd {


Test patch:
diff --git a/domain/miningmanager/miningmanager_test.go b/domain/miningmanager/miningmanager_test.go
index f1ee75dc..e19c102c 100644
--- a/domain/miningmanager/miningmanager_test.go
+++ b/domain/miningmanager/miningmanager_test.go
@@ -35,7 +35,7 @@ func TestValidateAndInsertTransaction(t *testing.T) {
 		defer teardown(false)
 
 		miningFactory := miningmanager.NewFactory()
-		miningManager := miningFactory.NewMiningManager(tc, blockMaxMass, false)
+		miningManager := miningFactory.NewMiningManager(tc, params)
 		transactionsToInsert := make([]*externalapi.DomainTransaction, 10)
 		for i := range transactionsToInsert {
 			transactionsToInsert[i] = createTransactionWithUTXOEntry(t, i)
@@ -86,7 +86,7 @@ func TestInsertDoubleTransactionsToMempool(t *testing.T) {
 		defer teardown(false)
 
 		miningFactory := miningmanager.NewFactory()
-		miningManager := miningFactory.NewMiningManager(tc, blockMaxMass, false)
+		miningManager := miningFactory.NewMiningManager(tc, params)
 		transaction := createTransactionWithUTXOEntry(t, 0)
 		err = miningManager.ValidateAndInsertTransaction(transaction, true)
 		if err != nil {
@@ -111,7 +111,7 @@ func TestHandleNewBlockTransactions(t *testing.T) {
 		defer teardown(false)
 
 		miningFactory := miningmanager.NewFactory()
-		miningManager := miningFactory.NewMiningManager(tc, blockMaxMass, false)
+		miningManager := miningFactory.NewMiningManager(tc, params)
 		transactionsToInsert := make([]*externalapi.DomainTransaction, 10)
 		for i := range transactionsToInsert {
 			transaction := createTransactionWithUTXOEntry(t, i)
@@ -177,7 +177,7 @@ func TestDoubleSpends(t *testing.T) {
 		defer teardown(false)
 
 		miningFactory := miningmanager.NewFactory()
-		miningManager := miningFactory.NewMiningManager(tc, blockMaxMass, false)
+		miningManager := miningFactory.NewMiningManager(tc, params)
 		transactionInTheMempool := createTransactionWithUTXOEntry(t, 0)
 		err = miningManager.ValidateAndInsertTransaction(transactionInTheMempool, true)
 		if err != nil {
@@ -210,7 +210,7 @@ func TestOrphanTransactions(t *testing.T) {
 		defer teardown(false)
 
 		miningFactory := miningmanager.NewFactory()
-		miningManager := miningFactory.NewMiningManager(tc, blockMaxMass, false)
+		miningManager := miningFactory.NewMiningManager(tc, params)
 		// Before each parent transaction, We will add two blocks by consensus in order to fund the parent transactions.
 		parentTransactions, childTransactions, err := createArraysOfParentAndChildrenTransactions(tc)
 		if err != nil {