From 4a51d4485fc2f49a3eb38c1f2d05b68112cfb26b Mon Sep 17 00:00:00 2001 From: Stan Bondi Date: Wed, 8 May 2024 15:09:05 +0400 Subject: [PATCH] fix(mempool): insert transaction for failed before validation (#1032) Description --- Insert transaction in database if before-execution validations fail Motivation and Context --- This case will cause the error: `Storage Error: Not found: item: transaction`. We insert to allow submitters to later request this result. We might want to return this validation result to the caller and not persist the transaction. How Has This Been Tested? --- Not tested What process can a PR reviewer use to test or verify this change? --- Breaking Changes --- - [x] None - [ ] Requires data directory to be deleted - [ ] Other - Please specify --- .../tari_validator_node/src/p2p/services/mempool/service.rs | 2 +- .../mempool/validators/after/has_involved_shards.rs | 2 +- .../p2p/services/mempool/validators/before/epoch_range.rs | 5 +++++ .../src/p2p/services/mempool/validators/before/fee.rs | 4 ++++ .../p2p/services/mempool/validators/before/has_inputs.rs | 2 +- .../src/p2p/services/mempool/validators/before/signature.rs | 4 ++++ .../services/mempool/validators/before/template_exists.rs | 6 +++++- 7 files changed, 21 insertions(+), 4 deletions(-) diff --git a/applications/tari_validator_node/src/p2p/services/mempool/service.rs b/applications/tari_validator_node/src/p2p/services/mempool/service.rs index 43c27128f..cf2d23f24 100644 --- a/applications/tari_validator_node/src/p2p/services/mempool/service.rs +++ b/applications/tari_validator_node/src/p2p/services/mempool/service.rs @@ -315,7 +315,7 @@ where self.state_store.with_write_tx(|tx| { TransactionRecord::new(transaction) .set_abort(format!("Mempool validation failed: {e}")) - .update(tx) + .insert(tx) })?; #[cfg(feature = "metrics")] diff --git a/applications/tari_validator_node/src/p2p/services/mempool/validators/after/has_involved_shards.rs b/applications/tari_validator_node/src/p2p/services/mempool/validators/after/has_involved_shards.rs index da48cd9a3..ec3b2a313 100644 --- a/applications/tari_validator_node/src/p2p/services/mempool/validators/after/has_involved_shards.rs +++ b/applications/tari_validator_node/src/p2p/services/mempool/validators/after/has_involved_shards.rs @@ -26,7 +26,7 @@ impl Validator for HasInvolvedShards { async fn validate(&self, executed: &ExecutedTransaction) -> Result<(), Self::Error> { if executed.num_inputs_and_outputs() == 0 { - debug!(target: LOG_TARGET, "HasInvolvedShards - FAIL: No input or output shards"); + warn!(target: LOG_TARGET, "HasInvolvedShards - FAIL: No input or output shards"); return Err(MempoolError::NoInvolvedShards { transaction_id: *executed.id(), }); diff --git a/applications/tari_validator_node/src/p2p/services/mempool/validators/before/epoch_range.rs b/applications/tari_validator_node/src/p2p/services/mempool/validators/before/epoch_range.rs index a1d72c33b..3327cc86b 100644 --- a/applications/tari_validator_node/src/p2p/services/mempool/validators/before/epoch_range.rs +++ b/applications/tari_validator_node/src/p2p/services/mempool/validators/before/epoch_range.rs @@ -2,12 +2,15 @@ // SPDX-License-Identifier: BSD-3-Clause use async_trait::async_trait; +use log::warn; use tari_dan_common_types::NodeAddressable; use tari_epoch_manager::{base_layer::EpochManagerHandle, EpochManagerReader}; use tari_transaction::Transaction; use crate::p2p::services::mempool::{MempoolError, Validator}; +const LOG_TARGET: &str = "tari::dan::mempool::validators::epoch_range"; + #[derive(Debug)] pub struct EpochRangeValidator { epoch_manager: EpochManagerHandle, @@ -27,6 +30,7 @@ impl Validator for EpochRangeValidator Validator for EpochRangeValidator max_epoch { + warn!(target: LOG_TARGET, "EpochRangeValidator - FAIL: Current epoch {current_epoch} greater than maximum epoch {max_epoch}."); return Err(MempoolError::CurrentEpochGreaterThanMaximum { current_epoch, max_epoch, diff --git a/applications/tari_validator_node/src/p2p/services/mempool/validators/before/fee.rs b/applications/tari_validator_node/src/p2p/services/mempool/validators/before/fee.rs index d6d69c40e..0bff2bbe3 100644 --- a/applications/tari_validator_node/src/p2p/services/mempool/validators/before/fee.rs +++ b/applications/tari_validator_node/src/p2p/services/mempool/validators/before/fee.rs @@ -2,10 +2,13 @@ // SPDX-License-Identifier: BSD-3-Clause use async_trait::async_trait; +use log::warn; use tari_transaction::Transaction; use crate::p2p::services::mempool::{MempoolError, Validator}; +const LOG_TARGET: &str = "tari::dan::mempool::validators::fee"; + #[derive(Debug)] pub struct FeeTransactionValidator; @@ -15,6 +18,7 @@ impl Validator for FeeTransactionValidator { async fn validate(&self, transaction: &Transaction) -> Result<(), MempoolError> { if transaction.fee_instructions().is_empty() { + warn!(target: LOG_TARGET, "FeeTransactionValidator - FAIL: No fee instructions"); return Err(MempoolError::NoFeeInstructions); } Ok(()) diff --git a/applications/tari_validator_node/src/p2p/services/mempool/validators/before/has_inputs.rs b/applications/tari_validator_node/src/p2p/services/mempool/validators/before/has_inputs.rs index ed190bb99..1cec19e96 100644 --- a/applications/tari_validator_node/src/p2p/services/mempool/validators/before/has_inputs.rs +++ b/applications/tari_validator_node/src/p2p/services/mempool/validators/before/has_inputs.rs @@ -36,7 +36,7 @@ impl Validator for HasInputs { return Ok(()); } - debug!(target: LOG_TARGET, "HasInputs - FAIL: No input shards"); + warn!(target: LOG_TARGET, "HasInputs - FAIL: No input shards"); return Err(MempoolError::NoInputs { transaction_id: *transaction.id(), }); diff --git a/applications/tari_validator_node/src/p2p/services/mempool/validators/before/signature.rs b/applications/tari_validator_node/src/p2p/services/mempool/validators/before/signature.rs index fb2dbbdba..0488985e6 100644 --- a/applications/tari_validator_node/src/p2p/services/mempool/validators/before/signature.rs +++ b/applications/tari_validator_node/src/p2p/services/mempool/validators/before/signature.rs @@ -2,10 +2,13 @@ // SPDX-License-Identifier: BSD-3-Clause use async_trait::async_trait; +use log::warn; use tari_transaction::Transaction; use crate::p2p::services::mempool::{MempoolError, Validator}; +const LOG_TARGET: &str = "tari::dan::mempool::validators::signature"; + #[derive(Debug)] pub struct TransactionSignatureValidator; @@ -15,6 +18,7 @@ impl Validator for TransactionSignatureValidator { async fn validate(&self, transaction: &Transaction) -> Result<(), MempoolError> { if !transaction.signature().verify(&transaction.into()) { + warn!(target: LOG_TARGET, "TransactionSignatureValidator - FAIL: Invalid signature"); return Err(MempoolError::InvalidSignature); } diff --git a/applications/tari_validator_node/src/p2p/services/mempool/validators/before/template_exists.rs b/applications/tari_validator_node/src/p2p/services/mempool/validators/before/template_exists.rs index 11abd09c1..213d67c12 100644 --- a/applications/tari_validator_node/src/p2p/services/mempool/validators/before/template_exists.rs +++ b/applications/tari_validator_node/src/p2p/services/mempool/validators/before/template_exists.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: BSD-3-Clause use async_trait::async_trait; +use log::warn; use tari_dan_app_utilities::template_manager::{implementation::TemplateManager, interface::TemplateManagerError}; use tari_dan_common_types::NodeAddressable; use tari_engine_types::instruction::Instruction; @@ -9,6 +10,8 @@ use tari_transaction::Transaction; use crate::p2p::services::mempool::{MempoolError, Validator}; +const LOG_TARGET: &str = "tari::dan::mempool::validators::template_exists"; + #[derive(Debug)] pub struct TemplateExistsValidator { template_manager: TemplateManager, @@ -33,11 +36,12 @@ impl Validator for TemplateExistsValidator< match template_exists { Err(e) => return Err(MempoolError::InvalidTemplateAddress(e)), Ok(false) => { + warn!(target: LOG_TARGET, "TemplateExistsValidator - FAIL: Template not found"); return Err(MempoolError::InvalidTemplateAddress( TemplateManagerError::TemplateNotFound { address: *template_address, }, - )) + )); }, _ => continue, }