Use common note commitment list code

This commit is contained in:
teor 2023-08-31 15:37:41 +10:00
parent cdb1e62a61
commit 63728dad26
3 changed files with 30 additions and 43 deletions

View File

@ -2,6 +2,8 @@
use std::{collections::HashMap, fmt, ops::Neg, sync::Arc};
use halo2::pasta::pallas;
use crate::{
amount::NegativeAllowed,
block::merkle::AuthDataRoot,
@ -152,16 +154,30 @@ impl Block {
/// Access the [`orchard::Nullifier`]s from all transactions in this block.
pub fn orchard_nullifiers(&self) -> impl Iterator<Item = &orchard::Nullifier> {
// Work around a compiler panic (ICE) with flat_map():
// https://github.com/rust-lang/rust/issues/105044
#[allow(clippy::needless_collect)]
let nullifiers: Vec<_> = self
.transactions
self.transactions
.iter()
.flat_map(|transaction| transaction.orchard_nullifiers())
.collect();
}
nullifiers.into_iter()
/// Access the [`sprout::NoteCommitment`]s from all transactions in this block.
pub fn sprout_note_commitments(&self) -> impl Iterator<Item = &sprout::NoteCommitment> {
self.transactions
.iter()
.flat_map(|transaction| transaction.sprout_note_commitments())
}
/// Access the [sapling note commitments](jubjub::Fq) from all transactions in this block.
pub fn sapling_note_commitments(&self) -> impl Iterator<Item = &jubjub::Fq> {
self.transactions
.iter()
.flat_map(|transaction| transaction.sapling_note_commitments())
}
/// Access the [orchard note commitments](pallas::Base) from all transactions in this block.
pub fn orchard_note_commitments(&self) -> impl Iterator<Item = &pallas::Base> {
self.transactions
.iter()
.flat_map(|transaction| transaction.orchard_note_commitments())
}
/// Count how many Sapling transactions exist in a block,

View File

@ -69,24 +69,9 @@ impl NoteCommitmentTrees {
..
} = self.clone();
let sprout_note_commitments: Vec<_> = block
.transactions
.iter()
.flat_map(|tx| tx.sprout_note_commitments())
.cloned()
.collect();
let sapling_note_commitments: Vec<_> = block
.transactions
.iter()
.flat_map(|tx| tx.sapling_note_commitments())
.cloned()
.collect();
let orchard_note_commitments: Vec<_> = block
.transactions
.iter()
.flat_map(|tx| tx.orchard_note_commitments())
.cloned()
.collect();
let sprout_note_commitments: Vec<_> = block.sprout_note_commitments().cloned().collect();
let sapling_note_commitments: Vec<_> = block.sapling_note_commitments().cloned().collect();
let orchard_note_commitments: Vec<_> = block.orchard_note_commitments().cloned().collect();
let mut sprout_result = None;
let mut sapling_result = None;

View File

@ -51,21 +51,14 @@ pub fn run(
.block(height.into())
.expect("height with note commitment tree should have block");
let sapling_note_commitments: Vec<_> = block
.transactions
.iter()
.flat_map(|tx| tx.sapling_note_commitments())
.cloned()
.collect();
for sapling_note_commitment in sapling_note_commitments {
for sapling_note_commitment in block.sapling_note_commitments() {
// Return early if there is a cancel signal.
if !matches!(cancel_receiver.try_recv(), Err(mpsc::TryRecvError::Empty)) {
return;
}
sapling_nct
.append(sapling_note_commitment)
.append(*sapling_note_commitment)
.expect("finalized notes should append successfully");
// The loop always breaks on this condition,
@ -122,21 +115,14 @@ pub fn run(
.block(height.into())
.expect("height with note commitment tree should have block");
let orchard_note_commitments: Vec<_> = block
.transactions
.iter()
.flat_map(|tx| tx.orchard_note_commitments())
.cloned()
.collect();
for orchard_note_commitment in orchard_note_commitments {
for orchard_note_commitment in block.orchard_note_commitments() {
// Return early if there is a cancel signal.
if !matches!(cancel_receiver.try_recv(), Err(mpsc::TryRecvError::Empty)) {
return;
}
orchard_nct
.append(orchard_note_commitment)
.append(*orchard_note_commitment)
.expect("finalized notes should append successfully");
// The loop always breaks on this condition,