Skip to content

Commit eaa82f1

Browse files
committed
Avoid to copy the entire transactions sets when making proof Merkle Tree
1 parent 5e22115 commit eaa82f1

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

mithril-aggregator/src/services/prover.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl MithrilProverService {
5050

5151
fn compute_merkle_map_from_transactions(
5252
&self,
53-
transactions: &[CardanoTransaction],
53+
transactions: Vec<CardanoTransaction>,
5454
) -> StdResult<MKMap<BlockRange, MKMapNode<BlockRange>>> {
5555
let mut transactions_by_block_ranges: HashMap<BlockRange, Vec<TransactionHash>> =
5656
HashMap::new();
@@ -59,7 +59,7 @@ impl MithrilProverService {
5959
transactions_by_block_ranges
6060
.entry(block_range)
6161
.or_default()
62-
.push(transaction.transaction_hash.to_owned());
62+
.push(transaction.transaction_hash);
6363
}
6464
let mk_hash_map = MKMap::new(
6565
transactions_by_block_ranges
@@ -87,16 +87,22 @@ impl ProverService for MithrilProverService {
8787
transaction_hashes: &[TransactionHash],
8888
) -> StdResult<Vec<CardanoTransactionsSetProof>> {
8989
let transactions = self.transaction_retriever.get_up_to(up_to).await?;
90-
let mk_map = self.compute_merkle_map_from_transactions(&transactions)?;
90+
91+
// 1 - Get transactions to prove per block range
9192
let transactions_to_prove = transactions
9293
.iter()
9394
.filter_map(|transaction| {
9495
let block_range = BlockRange::from_block_number(transaction.block_number);
9596
transaction_hashes
9697
.contains(&transaction.transaction_hash)
97-
.then(|| (block_range, transaction.to_owned()))
98+
.then_some((block_range, transaction.clone()))
9899
})
99100
.collect::<Vec<_>>();
101+
102+
// 2 - Compute Transactions Merkle Tree
103+
let mk_map = self.compute_merkle_map_from_transactions(transactions)?;
104+
105+
// 3 - Compute proof for each transaction to prove
100106
let mut transaction_hashes_certified = vec![];
101107
for (_block_range, transaction) in transactions_to_prove {
102108
let mk_tree_node_transaction_hash: MKTreeNode =
@@ -105,9 +111,10 @@ impl ProverService for MithrilProverService {
105111
.compute_proof(&[mk_tree_node_transaction_hash])
106112
.is_ok()
107113
{
108-
transaction_hashes_certified.push(transaction.transaction_hash.to_string());
114+
transaction_hashes_certified.push(transaction.transaction_hash);
109115
}
110116
}
117+
111118
if !transaction_hashes_certified.is_empty() {
112119
let mk_leaves: Vec<MKTreeNode> = transaction_hashes_certified
113120
.iter()

0 commit comments

Comments
 (0)