Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallelize MuHash calculations #575

Merged
merged 7 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions consensus/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ use kaspa_database::prelude::StoreResultExtensions;
use kaspa_hashes::Hash;
use kaspa_muhash::MuHash;
use kaspa_txscript::caches::TxScriptCacheCounters;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

use std::{
cmp::Reverse,
Expand Down Expand Up @@ -771,9 +772,21 @@ impl ConsensusApi for Consensus {
fn append_imported_pruning_point_utxos(&self, utxoset_chunk: &[(TransactionOutpoint, UtxoEntry)], current_multiset: &mut MuHash) {
let mut pruning_utxoset_write = self.pruning_utxoset_stores.write();
pruning_utxoset_write.utxo_set.write_many(utxoset_chunk).unwrap();
for (outpoint, entry) in utxoset_chunk {
current_multiset.add_utxo(outpoint, entry);
}

// Parallelize processing
let inner_multiset = utxoset_chunk
.par_iter()
.map(|(outpoint, entry)| {
let mut inner_multiset = MuHash::new();
inner_multiset.add_utxo(outpoint, entry);
inner_multiset
})
.reduce(MuHash::new, |mut a, b| {
a.combine(&b);
a
});

current_multiset.combine(&inner_multiset);
}

fn import_pruning_point_utxo_set(&self, new_pruning_point: Hash, imported_utxo_multiset: MuHash) -> PruningImportResult<()> {
Expand Down
2 changes: 1 addition & 1 deletion consensus/src/pipeline/virtual_processor/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ impl VirtualStateProcessor {

// Validate transactions of the pruning point itself
let new_pruning_point_transactions = self.block_transactions_store.get(new_pruning_point).unwrap();
let validated_transactions = self.validate_transactions_in_parallel(
let (validated_transactions, _) = self.validate_transactions_in_parallel(
&new_pruning_point_transactions,
&virtual_read.utxo_set,
new_pruning_point_header.daa_score,
Expand Down
26 changes: 20 additions & 6 deletions consensus/src/pipeline/virtual_processor/utxo_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,14 @@ impl VirtualStateProcessor {
// No need to fully validate selected parent transactions since selected parent txs were already validated
// as part of selected parent UTXO state verification with the exact same UTXO context.
let validation_flags = if is_selected_parent { TxValidationFlags::SkipScriptChecks } else { TxValidationFlags::Full };
let validated_transactions = self.validate_transactions_in_parallel(&txs, &composed_view, pov_daa_score, validation_flags);
let (validated_transactions, inner_multiset) =
self.validate_transactions_in_parallel(&txs, &composed_view, pov_daa_score, validation_flags);

ctx.multiset_hash.combine(&inner_multiset);

let mut block_fee = 0u64;
for (validated_tx, _) in validated_transactions.iter() {
ctx.mergeset_diff.add_transaction(validated_tx, pov_daa_score).unwrap();
ctx.multiset_hash.add_transaction(validated_tx, pov_daa_score);
ctx.accepted_tx_ids.push(validated_tx.id());
block_fee += validated_tx.calculated_fee;
}
Expand Down Expand Up @@ -177,7 +179,7 @@ impl VirtualStateProcessor {

// Verify all transactions are valid in context
let current_utxo_view = selected_parent_utxo_view.compose(&ctx.mergeset_diff);
let validated_transactions =
let (validated_transactions, _) =
coderofstuff marked this conversation as resolved.
Show resolved Hide resolved
self.validate_transactions_in_parallel(&txs, &current_utxo_view, header.daa_score, TxValidationFlags::Full);
if validated_transactions.len() < txs.len() - 1 {
// Some non-coinbase transactions are invalid
Expand Down Expand Up @@ -217,15 +219,27 @@ impl VirtualStateProcessor {
utxo_view: &V,
pov_daa_score: u64,
flags: TxValidationFlags,
) -> Vec<(ValidatedTransaction<'a>, u32)> {
) -> (Vec<(ValidatedTransaction<'a>, u32)>, MuHash) {
self.thread_pool.install(|| {
txs
.par_iter() // We can do this in parallel without complications since block body validation already ensured
// that all txs within each block are independent
.enumerate()
.skip(1) // Skip the coinbase tx.
.filter_map(|(i, tx)| self.validate_transaction_in_utxo_context(tx, &utxo_view, pov_daa_score, flags).ok().map(|vtx| (vtx, i as u32)))
.collect()
.filter_map(|(i, tx)| self.validate_transaction_in_utxo_context(tx, &utxo_view, pov_daa_score, flags).ok().map(|vtx| {
let mut mh = MuHash::new();
mh.add_transaction(&vtx, pov_daa_score);
(vec![(vtx, i as u32)], mh)
}
))
.reduce(
|| (vec![], MuHash::new()),
|mut a, b| {
a.0.extend(b.0);
a.1.combine(&b.1);
a
},
)
})
}

Expand Down