This commit is contained in:
Arya 2024-04-26 09:09:08 +00:00 committed by GitHub
commit c193263e48
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 23 additions and 12 deletions

View File

@ -79,8 +79,19 @@ impl NoteCommitmentTrees {
let mut sapling_result = None;
let mut orchard_result = None;
// Note: Only updating the note commitment trees when there are more notes that
// need to be appended to the tree prevents unnecessarily duplicating note commitment
// tree data in the non-finalized state.
let has_sprout_notes = !sprout_note_commitments.is_empty();
let has_sapling_notes = !sapling_note_commitments.is_empty();
let has_orchard_notes = !orchard_note_commitments.is_empty();
if !(has_sprout_notes || has_sapling_notes || has_orchard_notes) {
return Ok(());
}
rayon::in_place_scope_fifo(|scope| {
if !sprout_note_commitments.is_empty() {
if has_sprout_notes {
scope.spawn_fifo(|_scope| {
sprout_result = Some(Self::update_sprout_note_commitment_tree(
sprout,
@ -89,7 +100,7 @@ impl NoteCommitmentTrees {
});
}
if !sapling_note_commitments.is_empty() {
if has_sapling_notes {
scope.spawn_fifo(|_scope| {
sapling_result = Some(Self::update_sapling_note_commitment_tree(
sapling,
@ -98,7 +109,7 @@ impl NoteCommitmentTrees {
});
}
if !orchard_note_commitments.is_empty() {
if has_orchard_notes {
scope.spawn_fifo(|_scope| {
orchard_result = Some(Self::update_orchard_note_commitment_tree(
orchard,
@ -132,10 +143,10 @@ impl NoteCommitmentTrees {
/// Update the sprout note commitment tree.
/// This method modifies the tree inside the `Arc`, if the `Arc` only has one reference.
fn update_sprout_note_commitment_tree(
mut sprout: Arc<sprout::tree::NoteCommitmentTree>,
sprout: Arc<sprout::tree::NoteCommitmentTree>,
sprout_note_commitments: Vec<sprout::NoteCommitment>,
) -> Result<Arc<sprout::tree::NoteCommitmentTree>, NoteCommitmentTreeError> {
let sprout_nct = Arc::make_mut(&mut sprout);
let mut sprout_nct = Arc::unwrap_or_clone(sprout);
for sprout_note_commitment in sprout_note_commitments {
sprout_nct.append(sprout_note_commitment)?;
@ -144,14 +155,14 @@ impl NoteCommitmentTrees {
// Re-calculate and cache the tree root.
let _ = sprout_nct.root();
Ok(sprout)
Ok(Arc::new(sprout_nct))
}
/// Update the sapling note commitment tree.
/// This method modifies the tree inside the `Arc`, if the `Arc` only has one reference.
#[allow(clippy::unwrap_in_result)]
pub fn update_sapling_note_commitment_tree(
mut sapling: Arc<sapling::tree::NoteCommitmentTree>,
sapling: Arc<sapling::tree::NoteCommitmentTree>,
sapling_note_commitments: Vec<sapling::tree::NoteCommitmentUpdate>,
) -> Result<
(
@ -160,7 +171,7 @@ impl NoteCommitmentTrees {
),
NoteCommitmentTreeError,
> {
let sapling_nct = Arc::make_mut(&mut sapling);
let mut sapling_nct = Arc::unwrap_or_clone(sapling);
// It is impossible for blocks to contain more than one level 16 sapling root:
// > [NU5 onward] nSpendsSapling, nOutputsSapling, and nActionsOrchard MUST all be less than 2^16.
@ -187,14 +198,14 @@ impl NoteCommitmentTrees {
// Re-calculate and cache the tree root.
let _ = sapling_nct.root();
Ok((sapling, subtree_root))
Ok((Arc::new(sapling_nct), subtree_root))
}
/// Update the orchard note commitment tree.
/// This method modifies the tree inside the `Arc`, if the `Arc` only has one reference.
#[allow(clippy::unwrap_in_result)]
pub fn update_orchard_note_commitment_tree(
mut orchard: Arc<orchard::tree::NoteCommitmentTree>,
orchard: Arc<orchard::tree::NoteCommitmentTree>,
orchard_note_commitments: Vec<orchard::tree::NoteCommitmentUpdate>,
) -> Result<
(
@ -203,7 +214,7 @@ impl NoteCommitmentTrees {
),
NoteCommitmentTreeError,
> {
let orchard_nct = Arc::make_mut(&mut orchard);
let mut orchard_nct = Arc::unwrap_or_clone(orchard);
// It is impossible for blocks to contain more than one level 16 orchard root:
// > [NU5 onward] nSpendsSapling, nOutputsSapling, and nActionsOrchard MUST all be less than 2^16.
@ -224,6 +235,6 @@ impl NoteCommitmentTrees {
// Re-calculate and cache the tree root.
let _ = orchard_nct.root();
Ok((orchard, subtree_root))
Ok((Arc::new(orchard_nct), subtree_root))
}
}