Skip to content

Commit a59f26b

Browse files
committed
refactor: extract MKMap computation in ProverService
And use better namings for the list of transactions to prove.
1 parent 112b3c7 commit a59f26b

File tree

2 files changed

+48
-25
lines changed

2 files changed

+48
-25
lines changed

mithril-aggregator/src/services/prover.rs

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use async_trait::async_trait;
55

66
use mithril_common::{
77
crypto_helper::{MKMap, MKMapNode, MKTree, MKTreeNode},
8-
entities::{Beacon, BlockRange, CardanoTransactionsSetProof, TransactionHash},
8+
entities::{
9+
Beacon, BlockRange, CardanoTransaction, CardanoTransactionsSetProof, TransactionHash,
10+
},
911
signable_builder::TransactionRetriever,
1012
StdResult,
1113
};
@@ -37,24 +39,15 @@ impl MithrilProverService {
3739
transaction_retriever,
3840
}
3941
}
40-
}
4142

42-
#[async_trait]
43-
impl ProverService for MithrilProverService {
44-
async fn compute_transactions_proofs(
43+
fn compute_merkle_map_from_transactions(
4544
&self,
46-
up_to: &Beacon,
47-
transaction_hashes: &[TransactionHash],
48-
) -> StdResult<Vec<CardanoTransactionsSetProof>> {
49-
let transactions = self.transaction_retriever.get_up_to(up_to).await?;
50-
let mut transactions_to_certify = vec![];
45+
transactions: &[CardanoTransaction],
46+
) -> StdResult<MKMap<BlockRange, MKMapNode<BlockRange>>> {
5147
let mut transactions_by_block_ranges: HashMap<BlockRange, Vec<TransactionHash>> =
5248
HashMap::new();
53-
for transaction in &transactions {
49+
for transaction in transactions {
5450
let block_range = BlockRange::from_block_number(transaction.block_number);
55-
if transaction_hashes.contains(&transaction.transaction_hash) {
56-
transactions_to_certify.push((block_range.clone(), transaction));
57-
}
5851
transactions_by_block_ranges
5952
.entry(block_range)
6053
.or_default()
@@ -72,26 +65,47 @@ impl ProverService for MithrilProverService {
7265
)?
7366
.as_slice(),
7467
)
75-
.with_context(|| "ProverService failed to compute MKHashMap")?;
68+
.with_context(|| "ProverService failed to compute the merkelized structure that proves ownership of the transaction")?;
7669

70+
Ok(mk_hash_map)
71+
}
72+
}
73+
74+
#[async_trait]
75+
impl ProverService for MithrilProverService {
76+
async fn compute_transactions_proofs(
77+
&self,
78+
up_to: &Beacon,
79+
transaction_hashes: &[TransactionHash],
80+
) -> StdResult<Vec<CardanoTransactionsSetProof>> {
81+
let transactions = self.transaction_retriever.get_up_to(up_to).await?;
82+
let mk_map = self.compute_merkle_map_from_transactions(&transactions)?;
83+
let transactions_to_prove = transactions
84+
.iter()
85+
.filter_map(|transaction| {
86+
let block_range = BlockRange::from_block_number(transaction.block_number);
87+
transaction_hashes
88+
.contains(&transaction.transaction_hash)
89+
.then(|| (block_range, transaction.to_owned()))
90+
})
91+
.collect::<Vec<_>>();
7792
let mut transaction_hashes_certified = vec![];
78-
for (_block_range, transaction) in transactions_to_certify {
93+
for (_block_range, transaction) in transactions_to_prove {
7994
let mk_tree_node_transaction_hash: MKTreeNode =
8095
transaction.transaction_hash.to_owned().into();
81-
if mk_hash_map
96+
if mk_map
8297
.compute_proof(&[mk_tree_node_transaction_hash])
8398
.is_ok()
8499
{
85100
transaction_hashes_certified.push(transaction.transaction_hash.to_string());
86101
}
87102
}
88-
89103
if !transaction_hashes_certified.is_empty() {
90104
let mk_leaves: Vec<MKTreeNode> = transaction_hashes_certified
91105
.iter()
92106
.map(|h| h.to_owned().into())
93107
.collect();
94-
let mk_proof = mk_hash_map.compute_proof(&mk_leaves)?;
108+
let mk_proof = mk_map.compute_proof(&mk_leaves)?;
95109
let transactions_set_proof_batch =
96110
CardanoTransactionsSetProof::new(transaction_hashes_certified, mk_proof);
97111

mithril-common/src/signable_builder/cardano_transactions.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ impl CardanoTransactionsSignableBuilder {
6565
}
6666
}
6767

68-
fn compute_merkle_root(&self, transactions: &[CardanoTransaction]) -> StdResult<MKTreeNode> {
68+
// Note: Code duplicated from aggregator Prover service as is.
69+
// This will be not be the case when we use the cached intermediate merkle roots.
70+
fn compute_merkle_map_from_transactions(
71+
&self,
72+
transactions: &[CardanoTransaction],
73+
) -> StdResult<MKMap<BlockRange, MKMapNode<BlockRange>>> {
6974
let mut transactions_by_block_ranges: HashMap<BlockRange, Vec<TransactionHash>> =
7075
HashMap::new();
7176
for transaction in transactions {
@@ -80,18 +85,22 @@ impl CardanoTransactionsSignableBuilder {
8085
.into_iter()
8186
.try_fold(
8287
vec![],
83-
|mut acc,
84-
(block_range, transactions)|
85-
-> StdResult<Vec<(BlockRange, MKMapNode<BlockRange>)>> {
88+
|mut acc, (block_range, transactions)| -> StdResult<Vec<(_, MKMapNode<_>)>> {
8689
acc.push((block_range, MKTree::new(&transactions)?.into()));
8790
Ok(acc)
8891
},
8992
)?
9093
.as_slice(),
9194
)
92-
.with_context(|| "CardanoTransactionsSignableBuilder failed to compute MKHashMap")?;
95+
.with_context(|| "ProverService failed to compute the merkelized structure that proves ownership of the transaction")?;
96+
97+
Ok(mk_hash_map)
98+
}
99+
100+
fn compute_merkle_root(&self, transactions: &[CardanoTransaction]) -> StdResult<MKTreeNode> {
101+
let mk_map = self.compute_merkle_map_from_transactions(transactions)?;
93102

94-
let mk_root = mk_hash_map.compute_root().with_context(|| {
103+
let mk_root = mk_map.compute_root().with_context(|| {
95104
"CardanoTransactionsSignableBuilder failed to compute MKHashMap root"
96105
})?;
97106

0 commit comments

Comments
 (0)