Configurable max block weight for mining (aka soft limit) (#1976)

This commit is contained in:
Ignotus Peverell 2018-11-14 10:22:08 -08:00 committed by GitHub
parent 2e421191e2
commit e4be820671
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 8 deletions

View file

@ -247,6 +247,13 @@ fn comments() -> HashMap<String, String> {
".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(),
"

View file

@ -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<PoolEntry>,
@ -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<Vec<Transaction>, PoolError> {
pub fn prepare_mineable_transactions(&self, max_weight: usize) -> Result<Vec<Transaction>, 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,

View file

@ -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<Vec<Transaction>, PoolError> {
self.txpool.prepare_mineable_transactions()
self.txpool.prepare_mineable_transactions(self.config.mineable_max_weight)
}
}

View file

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

View file

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