From 4f7fa732cf25c40e5f49174e0aea9bc9c2a8dfab Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Mon, 14 Sep 2020 22:57:03 +0300 Subject: [PATCH] core|config: make max(free)txperblock height-dependent See neo-project/neo-modules#336. --- config/protocol.mainnet.yml | 4 ++-- config/protocol.testnet.yml | 4 ++-- pkg/config/protocol_config.go | 34 +++++++++++++++++++++++++++++----- pkg/core/blockchain.go | 15 ++++----------- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/config/protocol.mainnet.yml b/config/protocol.mainnet.yml index e809b7eca..6eb5fe008 100644 --- a/config/protocol.mainnet.yml +++ b/config/protocol.mainnet.yml @@ -33,8 +33,8 @@ ProtocolConfiguration: VerifyBlocks: true VerifyTransactions: false FreeGasLimit: {0: 10.0, 6216000: 50.0} - MaxTransactionsPerBlock: 500 - MaxFreeTransactionsPerBlock: 20 + MaxTransactionsPerBlock: {0: 500, 6216000: 200} + MaxFreeTransactionsPerBlock: {0: 20, 6216000: 199} MaxFreeTransactionSize: 1024 MinimumNetworkFee: 0 FeePerExtraByte: 0.00001 diff --git a/config/protocol.testnet.yml b/config/protocol.testnet.yml index 85214ee85..427c1f28b 100644 --- a/config/protocol.testnet.yml +++ b/config/protocol.testnet.yml @@ -33,8 +33,8 @@ ProtocolConfiguration: VerifyBlocks: true VerifyTransactions: false FreeGasLimit: {0: 10.0, 5220000: 50.0} - MaxTransactionsPerBlock: 500 - MaxFreeTransactionsPerBlock: 20 + MaxTransactionsPerBlock: {0: 500, 5220000: 200} + MaxFreeTransactionsPerBlock: {0: 20, 5220000: 199} MaxFreeTransactionSize: 1024 MinimumNetworkFee: 0 FeePerExtraByte: 0.00001 diff --git a/pkg/config/protocol_config.go b/pkg/config/protocol_config.go index 13c2deba5..e0d9bf0cd 100644 --- a/pkg/config/protocol_config.go +++ b/pkg/config/protocol_config.go @@ -28,14 +28,15 @@ type ( // FreeGasLimit is an amount of GAS which can be spent for free. // It can change over time, thus it's a map of block height to the // respective GAS limit. - FreeGasLimit map[uint32]util.Fixed8 `yaml:"FreeGasLimit"` - LowPriorityThreshold float64 `yaml:"LowPriorityThreshold"` - Magic NetMode `yaml:"Magic"` - MaxTransactionsPerBlock int `yaml:"MaxTransactionsPerBlock"` + FreeGasLimit map[uint32]util.Fixed8 `yaml:"FreeGasLimit"` + LowPriorityThreshold float64 `yaml:"LowPriorityThreshold"` + Magic NetMode `yaml:"Magic"` + // Maximum number of transactions allowed to be packed into block. + MaxTransactionsPerBlock map[uint32]int `yaml:"MaxTransactionsPerBlock"` // Maximum size of low priority transaction in bytes. MaxFreeTransactionSize int `yaml:"MaxFreeTransactionSize"` // Maximum number of low priority transactions accepted into block. - MaxFreeTransactionsPerBlock int `yaml:"MaxFreeTransactionsPerBlock"` + MaxFreeTransactionsPerBlock map[uint32]int `yaml:"MaxFreeTransactionsPerBlock"` // MinimumNetworkFee sets the minimum required network fee for transaction to pass validation. MinimumNetworkFee util.Fixed8 `yaml:"MinimumNetworkFee"` MemPoolSize int `yaml:"MemPoolSize"` @@ -79,6 +80,29 @@ func (p *ProtocolConfiguration) GetFreeGas(block uint32) util.Fixed8 { return gas } +func getIntFromMap(m map[uint32]int, block uint32) int { + var res int + var height uint32 + for h, i := range m { + if h > block || h < height || i < 0 { + continue + } + res = i + height = h + } + return res +} + +// GetMaxTxPerBlock returns MaxTransactionsPerBlock value for given block height. +func (p *ProtocolConfiguration) GetMaxTxPerBlock(block uint32) int { + return getIntFromMap(p.MaxTransactionsPerBlock, block) +} + +// GetMaxFreeTxPerBlock returns MaxFreeTransactionsPerBlock value for given block height. +func (p *ProtocolConfiguration) GetMaxFreeTxPerBlock(block uint32) int { + return getIntFromMap(p.MaxFreeTransactionsPerBlock, block) +} + // String implements the stringer interface. func (n NetMode) String() string { switch n { diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 812925fcc..2ae8116e6 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -156,14 +156,6 @@ func NewBlockchain(s storage.Store, cfg config.ProtocolConfiguration, log *zap.L cfg.MemPoolSize = defaultMemPoolSize log.Info("mempool size is not set or wrong, setting default value", zap.Int("MemPoolSize", cfg.MemPoolSize)) } - if cfg.MaxTransactionsPerBlock <= 0 { - cfg.MaxTransactionsPerBlock = 0 - log.Info("MaxTransactionsPerBlock is not set or wrong, setting default value (unlimited)", zap.Int("MaxTransactionsPerBlock", cfg.MaxTransactionsPerBlock)) - } - if cfg.MaxFreeTransactionsPerBlock <= 0 { - cfg.MaxFreeTransactionsPerBlock = 0 - log.Info("MaxFreeTransactionsPerBlock is not set or wrong, setting default value (unlimited)", zap.Int("MaxFreeTransactionsPerBlock", cfg.MaxFreeTransactionsPerBlock)) - } if cfg.MaxFreeTransactionSize <= 0 { cfg.MaxFreeTransactionSize = 0 log.Info("MaxFreeTransactionSize is not set or wrong, setting default value (unlimited)", zap.Int("MaxFreeTransactionSize", cfg.MaxFreeTransactionSize)) @@ -1646,10 +1638,11 @@ func (bc *Blockchain) GetMemPool() *mempool.Pool { // ApplyPolicyToTxSet applies configured policies to given transaction set. It // expects slice to be ordered by fee and returns a subslice of it. func (bc *Blockchain) ApplyPolicyToTxSet(txes []mempool.TxWithFee) []mempool.TxWithFee { - if bc.config.MaxTransactionsPerBlock != 0 && len(txes) > bc.config.MaxTransactionsPerBlock { - txes = txes[:bc.config.MaxTransactionsPerBlock] + maxTx := bc.config.GetMaxTxPerBlock(bc.BlockHeight()) + if maxTx != 0 && len(txes) > maxTx { + txes = txes[:maxTx] } - maxFree := bc.config.MaxFreeTransactionsPerBlock + maxFree := bc.config.GetMaxFreeTxPerBlock(bc.BlockHeight()) if maxFree != 0 && len(txes) > maxFree { // Transactions are sorted by fee, so we just find the first free one. freeStart := sort.Search(len(txes), func(i int) bool {