From ba39812e143d14142ac36ba38d94044a163e6a9e Mon Sep 17 00:00:00 2001 From: D-Stacks <78099568+D-Stacks@users.noreply.github.com> Date: Tue, 3 Sep 2024 03:42:09 +0200 Subject: [PATCH 1/3] do pmt check lazily. --- .../body_validation_in_context.rs | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/consensus/src/pipeline/body_processor/body_validation_in_context.rs b/consensus/src/pipeline/body_processor/body_validation_in_context.rs index 042410fa85..f19f6d2e6f 100644 --- a/consensus/src/pipeline/body_processor/body_validation_in_context.rs +++ b/consensus/src/pipeline/body_processor/body_validation_in_context.rs @@ -4,7 +4,7 @@ use crate::{ model::stores::{ghostdag::GhostdagStoreReader, statuses::StatusesStoreReader}, processes::window::WindowManager, }; -use kaspa_consensus_core::block::Block; +use kaspa_consensus_core::{block::Block, constants::LOCK_TIME_THRESHOLD}; use kaspa_database::prelude::StoreResultExtensions; use kaspa_hashes::Hash; use kaspa_utils::option::OptionExtensions; @@ -25,12 +25,25 @@ impl BlockBodyProcessor { } fn check_block_transactions_in_context(self: &Arc, block: &Block) -> BlockProcessResult<()> { - let (pmt, _) = self.window_manager.calc_past_median_time(&self.ghostdag_store.get_data(block.hash()).unwrap())?; - for tx in block.transactions.iter() { - if let Err(e) = self.transaction_validator.utxo_free_tx_validation(tx, block.header.daa_score, pmt) { + // calculating the past median time for the block may be expensive here, so we try and avoid it if possible + let pmt = 0u64; // we consider 0 to be an uninitialized default value + for tx in block.transactions.iter().filter(|tx| tx.lock_time > 0) { + if tx.lock_time <= LOCK_TIME_THRESHOLD { + if let Err(e) = self.transaction_validator.utxo_free_tx_validation( + tx, + block.header.daa_score, + pmt // we don't need the past median time for this case + ) { + return Err(RuleError::TxInContextFailed(tx.id(), e)); + } else if let Err(e) = self.transaction_validator.utxo_free_tx_validation( + tx, + block.header.daa_score, + // we most intialize the pmt value to the past median time of the block + if pmt != 0 { pmt } else { self.window_manager.calc_past_median_time(&self.ghostdag_store.get_data(block.hash()).unwrap())?.0 }) { return Err(RuleError::TxInContextFailed(tx.id(), e)); } } + } Ok(()) } From 15afdd8253bb6f1654f836a320e4fdb2675efdb7 Mon Sep 17 00:00:00 2001 From: D-Stacks <78099568+D-Stacks@users.noreply.github.com> Date: Tue, 3 Sep 2024 03:50:40 +0200 Subject: [PATCH 2/3] do comparisons correctly. --- .../pipeline/body_processor/body_validation_in_context.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/consensus/src/pipeline/body_processor/body_validation_in_context.rs b/consensus/src/pipeline/body_processor/body_validation_in_context.rs index f19f6d2e6f..953bf18d43 100644 --- a/consensus/src/pipeline/body_processor/body_validation_in_context.rs +++ b/consensus/src/pipeline/body_processor/body_validation_in_context.rs @@ -28,7 +28,8 @@ impl BlockBodyProcessor { // calculating the past median time for the block may be expensive here, so we try and avoid it if possible let pmt = 0u64; // we consider 0 to be an uninitialized default value for tx in block.transactions.iter().filter(|tx| tx.lock_time > 0) { - if tx.lock_time <= LOCK_TIME_THRESHOLD { + if tx.lock_time < LOCK_TIME_THRESHOLD { + // will only check daa score if let Err(e) = self.transaction_validator.utxo_free_tx_validation( tx, block.header.daa_score, @@ -39,7 +40,7 @@ impl BlockBodyProcessor { tx, block.header.daa_score, // we most intialize the pmt value to the past median time of the block - if pmt != 0 { pmt } else { self.window_manager.calc_past_median_time(&self.ghostdag_store.get_data(block.hash()).unwrap())?.0 }) { + if pmt > 0 { pmt } else { self.window_manager.calc_past_median_time(&self.ghostdag_store.get_data(block.hash()).unwrap())?.0 }) { return Err(RuleError::TxInContextFailed(tx.id(), e)); } } From 087db9bf80b2b9bae3cd4567683084701c7cb135 Mon Sep 17 00:00:00 2001 From: D-Stacks <78099568+D-Stacks@users.noreply.github.com> Date: Tue, 3 Sep 2024 03:53:11 +0200 Subject: [PATCH 3/3] lints --- .../body_validation_in_context.rs | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/consensus/src/pipeline/body_processor/body_validation_in_context.rs b/consensus/src/pipeline/body_processor/body_validation_in_context.rs index 953bf18d43..371eda53a7 100644 --- a/consensus/src/pipeline/body_processor/body_validation_in_context.rs +++ b/consensus/src/pipeline/body_processor/body_validation_in_context.rs @@ -31,20 +31,25 @@ impl BlockBodyProcessor { if tx.lock_time < LOCK_TIME_THRESHOLD { // will only check daa score if let Err(e) = self.transaction_validator.utxo_free_tx_validation( - tx, - block.header.daa_score, - pmt // we don't need the past median time for this case + tx, + block.header.daa_score, + pmt, // we don't need the past median time for this case ) { return Err(RuleError::TxInContextFailed(tx.id(), e)); - } else if let Err(e) = self.transaction_validator.utxo_free_tx_validation( - tx, - block.header.daa_score, - // we most intialize the pmt value to the past median time of the block - if pmt > 0 { pmt } else { self.window_manager.calc_past_median_time(&self.ghostdag_store.get_data(block.hash()).unwrap())?.0 }) { - return Err(RuleError::TxInContextFailed(tx.id(), e)); + } else if let Err(e) = self.transaction_validator.utxo_free_tx_validation( + tx, + block.header.daa_score, + // we most intialize the pmt value to the past median time of the block + if pmt > 0 { + pmt + } else { + self.window_manager.calc_past_median_time(&self.ghostdag_store.get_data(block.hash()).unwrap())?.0 + }, + ) { + return Err(RuleError::TxInContextFailed(tx.id(), e)); + } } } - } Ok(()) }