From 6f8b024fa5b0e7dc6999c5988a8fc513d45c16da Mon Sep 17 00:00:00 2001 From: Tiram <18632023+tiram88@users.noreply.github.com> Date: Sun, 24 Sep 2023 16:05:11 +0300 Subject: [PATCH] Ease atomic lock ordering & enhance counter updates --- mining/src/lib.rs | 4 ++-- .../mempool/handle_new_block_transactions.rs | 18 +++++++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/mining/src/lib.rs b/mining/src/lib.rs index d1469e96b..45b49ab93 100644 --- a/mining/src/lib.rs +++ b/mining/src/lib.rs @@ -41,10 +41,10 @@ impl MiningCounters { pub fn increase_tx_counts(&self, value: u64, priority: Priority) { match priority { Priority::Low => { - self.low_priority_tx_counts.fetch_add(value, Ordering::SeqCst); + self.low_priority_tx_counts.fetch_add(value, Ordering::Relaxed); } Priority::High => { - self.high_priority_tx_counts.fetch_add(value, Ordering::SeqCst); + self.high_priority_tx_counts.fetch_add(value, Ordering::Relaxed); } } } diff --git a/mining/src/mempool/handle_new_block_transactions.rs b/mining/src/mempool/handle_new_block_transactions.rs index 0d5c81864..c234fcc7e 100644 --- a/mining/src/mempool/handle_new_block_transactions.rs +++ b/mining/src/mempool/handle_new_block_transactions.rs @@ -21,6 +21,9 @@ impl Mempool { ) -> RuleResult> { let _sw = Stopwatch::<400>::with_threshold("handle_new_block_transactions op"); let mut unorphaned_transactions = vec![]; + let mut tx_accepted_counts = 0; + let mut input_counts = 0; + let mut output_counts = 0; for transaction in block_transactions[1..].iter() { let transaction_id = transaction.id(); // Rust rewrite: This behavior does differ from golang implementation. @@ -32,17 +35,18 @@ impl Mempool { } self.remove_double_spends(transaction)?; self.orphan_pool.remove_orphan(&transaction_id, false, TxRemovalReason::Accepted, "")?; - self.counters.block_tx_counts.fetch_add(1, Ordering::SeqCst); if self.accepted_transactions.add(transaction_id, block_daa_score) { - self.counters.tx_accepted_counts.fetch_add(1, Ordering::SeqCst); - self.counters.input_counts.fetch_add(transaction.inputs.len() as u64, Ordering::SeqCst); - self.counters.output_counts.fetch_add(transaction.outputs.len() as u64, Ordering::SeqCst); + tx_accepted_counts += 1; + input_counts += transaction.inputs.len(); + output_counts += transaction.outputs.len(); } unorphaned_transactions.extend(self.get_unorphaned_transactions_after_accepted_transaction(transaction)); } - - // Update the sample of number of ready transactions in the mempool and log the stats - self.counters.ready_txs_sample.store(self.transaction_pool.ready_transaction_count() as u64, Ordering::SeqCst); + self.counters.block_tx_counts.fetch_add(block_transactions.len() as u64 - 1, Ordering::Relaxed); + self.counters.tx_accepted_counts.fetch_add(tx_accepted_counts, Ordering::Relaxed); + self.counters.input_counts.fetch_add(input_counts as u64, Ordering::Relaxed); + self.counters.output_counts.fetch_add(output_counts as u64, Ordering::Relaxed); + self.counters.ready_txs_sample.store(self.transaction_pool.ready_transaction_count() as u64, Ordering::Relaxed); self.log_stats(); Ok(unorphaned_transactions)