Skip to content

Commit

Permalink
parallelize anchors checks for unmined_tx
Browse files Browse the repository at this point in the history
  • Loading branch information
arya2 committed Nov 29, 2022
1 parent efe9d9f commit 18f3d33
Showing 1 changed file with 57 additions and 29 deletions.
86 changes: 57 additions & 29 deletions zebra-state/src/service/check/anchors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,39 +454,67 @@ pub(crate) fn tx_anchors_refer_to_final_treestates(
parent_chain: Option<&Arc<Chain>>,
unmined_tx: &UnminedTx,
) -> Result<(), ValidateContextError> {
sapling_orchard_anchors_refer_to_final_treestates(
finalized_state,
parent_chain,
&unmined_tx.transaction,
unmined_tx.id.mined_id(),
None,
None,
)?;
if unmined_tx.transaction.has_sprout_joinsplit_data() {
let mut sprout_anchors_result = None;
let mut sapling_orchard_anchors_result = None;
rayon::in_place_scope_fifo(|s| {
// This check is expensive, because it updates a note commitment tree for each sprout JoinSplit.
// Since we could be processing attacker-controlled mempool transactions, we need to run each one
// in its own thread, separately from tokio's blocking I/O threads. And if we are under heavy load,
// we want verification to finish in order, so that later transactions can't delay earlier ones.
s.spawn_fifo(|_s| {
let mut sprout_final_treestates = HashMap::new();

fetch_sprout_final_treestates(
&mut sprout_final_treestates,
finalized_state,
parent_chain,
&unmined_tx.transaction,
None,
None,
);

let mut sprout_final_treestates = HashMap::new();
tracing::trace!(
sprout_final_treestate_count = ?sprout_final_treestates.len(),
?sprout_final_treestates,
"received sprout final treestate anchors",
);

fetch_sprout_final_treestates(
&mut sprout_final_treestates,
finalized_state,
parent_chain,
&unmined_tx.transaction,
None,
None,
);
sprout_anchors_result = Some(sprout_anchors_refer_to_treestates(
&sprout_final_treestates,
&unmined_tx.transaction,
unmined_tx.id.mined_id(),
None,
None,
));
});

tracing::trace!(
sprout_final_treestate_count = ?sprout_final_treestates.len(),
?sprout_final_treestates,
"received sprout final treestate anchors",
);
s.spawn_fifo(|_s| {
sapling_orchard_anchors_result =
Some(sapling_orchard_anchors_refer_to_final_treestates(
finalized_state,
parent_chain,
&unmined_tx.transaction,
unmined_tx.id.mined_id(),
None,
None,
));
});
});

sprout_anchors_refer_to_treestates(
&sprout_final_treestates,
&unmined_tx.transaction,
unmined_tx.id.mined_id(),
None,
None,
)?;
sprout_anchors_result.expect("scope has finished")?;
sapling_orchard_anchors_result.expect("scope has finished")?;
} else {
// If there are no sprout transactions in the block, avoid running a rayon scope
sapling_orchard_anchors_refer_to_final_treestates(
finalized_state,
parent_chain,
&unmined_tx.transaction,
unmined_tx.id.mined_id(),
None,
None,
)?;
}

Ok(())
}

0 comments on commit 18f3d33

Please sign in to comment.