Add a maximum size to stempool (#1895)

* Add a maximum size to stempool

* Simplify logic
This commit is contained in:
Quentin Le Sceller 2018-11-05 13:51:52 +01:00 committed by GitHub
parent d3a8613e43
commit 24ed4b787a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 2 deletions

View file

@ -240,6 +240,13 @@ fn comments() -> HashMap<String, String> {
".to_string(), ".to_string(),
); );
retval.insert(
"max_stempool_size".to_string(),
"
#maximum number of transactions allowed in the stempool
".to_string(),
);
retval.insert( retval.insert(
"[server.stratum_mining_config]".to_string(), "[server.stratum_mining_config]".to_string(),
" "

View file

@ -141,7 +141,7 @@ impl TransactionPool {
} }
// Do we have the capacity to accept this transaction? // Do we have the capacity to accept this transaction?
self.is_acceptable(&tx)?; self.is_acceptable(&tx, stem)?;
// Make sure the transaction is valid before anything else. // Make sure the transaction is valid before anything else.
tx.validate(self.verifier_cache.clone()) tx.validate(self.verifier_cache.clone())
@ -215,12 +215,20 @@ impl TransactionPool {
/// Whether the transaction is acceptable to the pool, given both how /// Whether the transaction is acceptable to the pool, given both how
/// full the pool is and the transaction weight. /// full the pool is and the transaction weight.
fn is_acceptable(&self, tx: &Transaction) -> Result<(), PoolError> { fn is_acceptable(&self, tx: &Transaction, stem: bool) -> Result<(), PoolError> {
if self.total_size() > self.config.max_pool_size { if self.total_size() > self.config.max_pool_size {
// TODO evict old/large transactions instead // TODO evict old/large transactions instead
return Err(PoolError::OverCapacity); return Err(PoolError::OverCapacity);
} }
// Check that the stempool can accept this transaction
if stem {
if self.stempool.size() > self.config.max_stempool_size {
// TODO evict old/large transactions instead
return Err(PoolError::OverCapacity);
}
}
// for a basic transaction (1 input, 2 outputs) - // for a basic transaction (1 input, 2 outputs) -
// (-1 * 1) + (4 * 2) + 1 = 8 // (-1 * 1) + (4 * 2) + 1 = 8
// 8 * 10 = 80 // 8 * 10 = 80

View file

@ -96,6 +96,10 @@ pub struct PoolConfig {
/// Maximum capacity of the pool in number of transactions /// Maximum capacity of the pool in number of transactions
#[serde = "default_max_pool_size"] #[serde = "default_max_pool_size"]
pub max_pool_size: usize, pub max_pool_size: usize,
/// Maximum capacity of the pool in number of transactions
#[serde = "default_max_stempool_size"]
pub max_stempool_size: usize,
} }
impl Default for PoolConfig { impl Default for PoolConfig {
@ -103,6 +107,7 @@ impl Default for PoolConfig {
PoolConfig { PoolConfig {
accept_fee_base: default_accept_fee_base(), accept_fee_base: default_accept_fee_base(),
max_pool_size: default_max_pool_size(), max_pool_size: default_max_pool_size(),
max_stempool_size: default_max_stempool_size(),
} }
} }
} }
@ -113,6 +118,9 @@ fn default_accept_fee_base() -> u64 {
fn default_max_pool_size() -> usize { fn default_max_pool_size() -> usize {
50_000 50_000
} }
fn default_max_stempool_size() -> usize {
50_000
}
/// Represents a single entry in the pool. /// Represents a single entry in the pool.
/// A single (possibly aggregated) transaction. /// A single (possibly aggregated) transaction.

View file

@ -168,6 +168,7 @@ pub fn test_setup(
PoolConfig { PoolConfig {
accept_fee_base: 0, accept_fee_base: 0,
max_pool_size: 50, max_pool_size: 50,
max_stempool_size: 50,
}, },
chain.clone(), chain.clone(),
verifier_cache.clone(), verifier_cache.clone(),