Skip to content

Commit

Permalink
chore: add helper functions for generating output proofs (#6694)
Browse files Browse the repository at this point in the history
Description
---
Adds helper function to calculate the hashes for the merkle roots of the
header.
  • Loading branch information
SWvheerden authored Nov 19, 2024
1 parent 10f4077 commit d58c670
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
9 changes: 9 additions & 0 deletions base_layer/core/src/blocks/genesis_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,15 @@ mod test {
block.header().block_output_mr.to_vec().to_hex(),
);
} else {
let coinbases = block.block().body.get_coinbase_outputs().into_iter().cloned().collect();
let normal_output_mr = block.block().body.calculate_header_normal_output_mr().unwrap();
assert_eq!(
AggregateBody::calculate_header_block_output_mr(normal_output_mr, &coinbases)
.unwrap()
.to_vec()
.to_hex(),
block.header().block_output_mr.to_vec().to_hex(),
);
assert_eq!(
block_output_mr_hash_from_pruned_mmr(&block_output_mmr)
.unwrap()
Expand Down
31 changes: 30 additions & 1 deletion base_layer/core/src/transactions/aggregated_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
use std::{
cmp::max,
convert::TryFrom,
fmt::{Display, Error, Formatter},
};

use borsh::{BorshDeserialize, BorshSerialize};
use log::*;
use serde::{Deserialize, Serialize};
use tari_common_types::types::{ComAndPubSignature, Commitment, PrivateKey};
use tari_common_types::types::{ComAndPubSignature, Commitment, FixedHash, PrivateKey};
use tari_crypto::commitment::HomomorphicCommitmentFactory;
#[cfg(feature = "base_node")]
use tari_mmr::pruned_hashset::PrunedHashSet;
use tari_utilities::hex::Hex;

use crate::transactions::{
Expand All @@ -45,6 +48,8 @@ use crate::transactions::{
},
weight::TransactionWeight,
};
#[cfg(feature = "base_node")]
use crate::{block_output_mr_hash_from_pruned_mmr, MrHashError, PrunedOutputMmr};

pub const LOG_TARGET: &str = "c::tx::aggregated_body";

Expand Down Expand Up @@ -446,6 +451,30 @@ impl AggregateBody {
.iter()
.any(|k| k.features.output_type == OutputType::Coinbase)
}

#[cfg(feature = "base_node")]
pub fn calculate_header_block_output_mr(
normal_output_mr: FixedHash,
coinbases: &Vec<TransactionOutput>,
) -> Result<FixedHash, MrHashError> {
let mut block_output_mmr = PrunedOutputMmr::new(PrunedHashSet::default());
for o in coinbases {
block_output_mmr.push(o.hash().to_vec())?;
}
block_output_mmr.push(normal_output_mr.to_vec())?;
block_output_mr_hash_from_pruned_mmr(&block_output_mmr)
}

#[cfg(feature = "base_node")]
pub fn calculate_header_normal_output_mr(&self) -> Result<FixedHash, MrHashError> {
let mut normal_output_mmr = PrunedOutputMmr::new(PrunedHashSet::default());
for o in self.outputs() {
if !o.features.is_coinbase() {
normal_output_mmr.push(o.hash().to_vec())?;
}
}
Ok(FixedHash::try_from(normal_output_mmr.get_merkle_root()?)?)
}
}

impl PartialEq for AggregateBody {
Expand Down

0 comments on commit d58c670

Please sign in to comment.