From e4be82067187dfa7c30cb2d49509a41b43678253 Mon Sep 17 00:00:00 2001 From: Ignotus Peverell Date: Wed, 14 Nov 2018 10:22:08 -0800 Subject: [PATCH] Configurable max block weight for mining (aka soft limit) (#1976) --- config/src/comments.rs | 7 +++++++ pool/src/pool.rs | 9 ++------- pool/src/transaction_pool.rs | 2 +- pool/src/types.rs | 10 ++++++++++ pool/tests/common/mod.rs | 1 + 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/config/src/comments.rs b/config/src/comments.rs index ada34f345..e0356428d 100644 --- a/config/src/comments.rs +++ b/config/src/comments.rs @@ -247,6 +247,13 @@ fn comments() -> HashMap { ".to_string(), ); + retval.insert( + "mineable_max_weight".to_string(), + " +#maximum total weight of transactions that can get selected to build a block +".to_string(), + ); + retval.insert( "[server.stratum_mining_config]".to_string(), " diff --git a/pool/src/pool.rs b/pool/src/pool.rs index a62ec0dfb..39f037335 100644 --- a/pool/src/pool.rs +++ b/pool/src/pool.rs @@ -19,7 +19,6 @@ use std::collections::{HashMap, HashSet}; use std::sync::Arc; use util::RwLock; -use core::consensus; use core::core::hash::{Hash, Hashed}; use core::core::id::{ShortId, ShortIdentifiable}; use core::core::transaction; @@ -27,10 +26,6 @@ use core::core::verifier_cache::VerifierCache; use core::core::{Block, BlockHeader, BlockSums, Committed, Transaction, TxKernel}; use types::{BlockChain, PoolEntry, PoolEntryState, PoolError}; -// max weight leaving minimum space for a coinbase -const MAX_MINEABLE_WEIGHT: usize = - consensus::MAX_BLOCK_WEIGHT - consensus::BLOCK_OUTPUT_WEIGHT - consensus::BLOCK_KERNEL_WEIGHT; - pub struct Pool { /// Entries in the pool (tx + info + timer) in simple insertion order. pub entries: Vec, @@ -120,7 +115,7 @@ impl Pool { /// appropriate to put in a mined block. Aggregates chains of dependent /// transactions, orders by fee over weight and ensures to total weight /// doesn't exceed block limits. - pub fn prepare_mineable_transactions(&self) -> Result, PoolError> { + pub fn prepare_mineable_transactions(&self, max_weight: usize) -> Result, PoolError> { let header = self.blockchain.chain_head()?; let tx_buckets = self.bucket_transactions(); @@ -139,7 +134,7 @@ impl Pool { let mut weight = 0; flat_txs.retain(|tx| { weight += tx.tx_weight_as_block() as usize; - weight < MAX_MINEABLE_WEIGHT + weight < max_weight }); // Iteratively apply the txs to the current chain state, diff --git a/pool/src/transaction_pool.rs b/pool/src/transaction_pool.rs index 4da3aeb7d..63c922268 100644 --- a/pool/src/transaction_pool.rs +++ b/pool/src/transaction_pool.rs @@ -255,6 +255,6 @@ impl TransactionPool { /// Returns a vector of transactions from the txpool so we can build a /// block from them. pub fn prepare_mineable_transactions(&self) -> Result, PoolError> { - self.txpool.prepare_mineable_transactions() + self.txpool.prepare_mineable_transactions(self.config.mineable_max_weight) } } diff --git a/pool/src/types.rs b/pool/src/types.rs index c21fcf264..b708dfc89 100644 --- a/pool/src/types.rs +++ b/pool/src/types.rs @@ -100,6 +100,12 @@ pub struct PoolConfig { /// Maximum capacity of the pool in number of transactions #[serde = "default_max_stempool_size"] pub max_stempool_size: usize, + + /// Maximum total weight of transactions that can get selected to build a + /// block from. Allows miners to restrict the maximum weight of their + /// blocks. + #[serde = "default_mineable_max_weight"] + pub mineable_max_weight: usize, } impl Default for PoolConfig { @@ -108,6 +114,7 @@ impl Default for PoolConfig { accept_fee_base: default_accept_fee_base(), max_pool_size: default_max_pool_size(), max_stempool_size: default_max_stempool_size(), + mineable_max_weight: default_mineable_max_weight(), } } } @@ -121,6 +128,9 @@ fn default_max_pool_size() -> usize { fn default_max_stempool_size() -> usize { 50_000 } +fn default_mineable_max_weight() -> usize { + consensus::MAX_BLOCK_WEIGHT - consensus::BLOCK_OUTPUT_WEIGHT - consensus::BLOCK_KERNEL_WEIGHT +} /// Represents a single entry in the pool. /// A single (possibly aggregated) transaction. diff --git a/pool/tests/common/mod.rs b/pool/tests/common/mod.rs index 2a1a4b620..848f3f0fd 100644 --- a/pool/tests/common/mod.rs +++ b/pool/tests/common/mod.rs @@ -169,6 +169,7 @@ pub fn test_setup( accept_fee_base: 0, max_pool_size: 50, max_stempool_size: 50, + mineable_max_weight: 10_000, }, chain.clone(), verifier_cache.clone(),