Skip to content

Commit

Permalink
commit in lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Stacks committed Nov 17, 2024
1 parent a064e6d commit 089bfee
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions consensus/src/pipeline/body_processor/body_validation_in_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,23 @@ impl BlockBodyProcessor {

// Use lazy evaluation to avoid unnecessary work, as most of the time we expect the txs not to have lock time.
let lazy_pmt_res =
Lazy::new(|| self.window_manager.calc_past_median_time(self.ghostdag_store.get_data(block.hash()).unwrap().deref()));
Lazy::new(|| match self.window_manager.calc_past_median_time(&self.ghostdag_store.get_data(block.hash()).unwrap()) {
Ok((pmt, pmt_window)) => {
if !self.block_window_cache_for_past_median_time.contains_key(&block.hash()) {
self.block_window_cache_for_past_median_time.insert(block.hash(), pmt_window);
};
Ok(pmt)
}
Err(e) => Err(e),
});

for tx in block.transactions.iter() {
// Quick check to avoid the expensive Lazy eval during ibd (in most cases).
// TODO: refactor this and avoid classifying the tx lock outside of the transaction validator.
if tx.lock_time != 0 {
// Extract the past median time from the Lazy.
let (pmt, pmt_window) = (*lazy_pmt_res).clone()?;

// Commit the past median time to the cache, if not already there.
if !self.block_window_cache_for_past_median_time.contains_key(&block.hash()) {
self.block_window_cache_for_past_median_time.insert(block.hash(), pmt_window);
};

if let Err(e) = self.transaction_validator.utxo_free_tx_validation(tx, block.header.daa_score, pmt) {
if let Err(e) =
self.transaction_validator.utxo_free_tx_validation(tx, block.header.daa_score, (*lazy_pmt_res).clone()?)
{
return Err(RuleError::TxInContextFailed(tx.id(), e));
};
};
Expand Down

0 comments on commit 089bfee

Please sign in to comment.