From cbc286495c2938dff1efe1ae8e3ac8d03514b9e3 Mon Sep 17 00:00:00 2001 From: SW van Heerden Date: Mon, 18 Sep 2023 10:03:48 +0200 Subject: [PATCH] chore: move mempool fee check (#5777) Description --- Moves the mempool fee check to before validation Motivation and Context --- This is a very cheap check compared to full validation. We should do this before we check if the tx is valid so that we don't do validation and then through away the tx because the fee is too low. --- base_layer/core/src/mempool/mempool_storage.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/base_layer/core/src/mempool/mempool_storage.rs b/base_layer/core/src/mempool/mempool_storage.rs index b7365a8227..e00d970975 100644 --- a/base_layer/core/src/mempool/mempool_storage.rs +++ b/base_layer/core/src/mempool/mempool_storage.rs @@ -77,6 +77,11 @@ impl MempoolStorage { .map(|k| k.excess_sig.get_signature().to_hex()) .unwrap_or_else(|| "None?!".into()); let timer = Instant::now(); + // This check is almost free, so lets check this before we do any expensive validation. + if tx.body.get_total_fee().as_u64() < self.unconfirmed_pool.config.min_fee { + debug!(target: LOG_TARGET, "Tx: ({}) fee too low, rejecting",tx_id); + return Ok(TxStorageResponse::NotStoredFeeTooLow); + } debug!(target: LOG_TARGET, "Inserting tx into mempool: {}", tx_id); match self.validator.validate(&tx) { Ok(()) => { @@ -88,10 +93,6 @@ impl MempoolStorage { ); let timer = Instant::now(); let weight = self.get_transaction_weighting(); - if tx.body.get_total_fee().as_u64() < self.unconfirmed_pool.config.min_fee { - debug!(target: LOG_TARGET, "Tx: ({}) fee too low, rejecting",tx_id); - return Ok(TxStorageResponse::NotStoredFeeTooLow); - } self.unconfirmed_pool.insert(tx, None, &weight)?; debug!( target: LOG_TARGET, @@ -104,10 +105,6 @@ impl MempoolStorage { Err(ValidationError::UnknownInputs(dependent_outputs)) => { if self.unconfirmed_pool.contains_all_outputs(&dependent_outputs) { let weight = self.get_transaction_weighting(); - if tx.body.get_total_fee().as_u64() < self.unconfirmed_pool.config.min_fee { - debug!(target: LOG_TARGET, "Tx: ({}) fee too low, rejecting",tx_id); - return Ok(TxStorageResponse::NotStoredFeeTooLow); - } self.unconfirmed_pool.insert(tx, Some(dependent_outputs), &weight)?; Ok(TxStorageResponse::UnconfirmedPool) } else {