Add mass limit to mempool (#1627)

* Add mass limit to mempool

* Pass only params instead of multiple configuration options

* Remove acceptNonStd from mempool constructor

* Remove acceptNonStd from mempool constructor

* Fix test compilation
This commit is contained in:
Ori Newman 2021-03-30 15:37:56 +03:00 committed by GitHub
parent 70f3fa9893
commit 321792778e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 15 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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 {

View File

@ -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 {