use super::*;
pub(super) const TRANSACTIONS_DEPTH: u8 = 16;
type TransactionsTree<N> = BHPMerkleTree<N, TRANSACTIONS_DEPTH>;
pub type TransactionsPath<N> = MerklePath<N, TRANSACTIONS_DEPTH>;
impl<N: Network> Transactions<N> {
pub fn to_root(&self) -> Result<Field<N>> {
Ok(*self.to_tree()?.root())
}
pub fn to_path(&self, index: usize, leaf: impl ToBits) -> Result<TransactionsPath<N>> {
self.to_tree()?.prove(index, &leaf.to_bits_le())
}
pub fn to_tree(&self) -> Result<TransactionsTree<N>> {
Self::transactions_tree(&self.transactions)
}
fn transactions_tree(transactions: &IndexMap<N::TransactionID, Transaction<N>>) -> Result<TransactionsTree<N>> {
ensure!(
transactions.len() <= Self::MAX_TRANSACTIONS,
"Block cannot exceed {} transactions, found {}",
Self::MAX_TRANSACTIONS,
transactions.len()
);
let leaves = transactions.values().map(|transaction| transaction.id().to_bits_le());
N::merkle_tree_bhp::<TRANSACTIONS_DEPTH>(&leaves.collect::<Vec<_>>())
}
}
#[cfg(test)]
mod tests {
use super::*;
use console::network::Testnet3;
type CurrentNetwork = Testnet3;
#[test]
fn test_transactions_depth() {
assert_eq!(2usize.pow(TRANSACTIONS_DEPTH as u32), Transactions::<CurrentNetwork>::MAX_TRANSACTIONS);
}
}