Skip to content

Commit 1b04400

Browse files
committed
Prover retrieve the transactions to prove directly from db
Instead of finding them by walking the entire transactions list used to create the merkle tree for the proof.
1 parent 8210fa7 commit 1b04400

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

mithril-aggregator/src/services/prover.rs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ impl MithrilProverService {
5151
}
5252
}
5353

54+
async fn get_transactions_by_hashes_with_block_range(
55+
&self,
56+
hashes: Vec<TransactionHash>,
57+
) -> StdResult<Vec<(BlockRange, CardanoTransaction)>> {
58+
let transactions = self.transaction_retriever.get_by_hashes(hashes).await?;
59+
let transactions_with_block_range = transactions
60+
.into_iter()
61+
.map(|transaction| {
62+
let block_range = BlockRange::from_block_number(transaction.block_number);
63+
(block_range, transaction)
64+
})
65+
.collect::<Vec<_>>();
66+
Ok(transactions_with_block_range)
67+
}
68+
5469
fn compute_merkle_map_from_transactions(
5570
&self,
5671
transactions: Vec<CardanoTransaction>,
@@ -89,20 +104,13 @@ impl ProverService for MithrilProverService {
89104
up_to: &CardanoDbBeacon,
90105
transaction_hashes: &[TransactionHash],
91106
) -> StdResult<Vec<CardanoTransactionsSetProof>> {
92-
let transactions = self.transaction_retriever.get_up_to(up_to).await?;
93-
94107
// 1 - Get transactions to prove per block range
95-
let transactions_to_prove = transactions
96-
.iter()
97-
.filter_map(|transaction| {
98-
let block_range = BlockRange::from_block_number(transaction.block_number);
99-
transaction_hashes
100-
.contains(&transaction.transaction_hash)
101-
.then_some((block_range, transaction.clone()))
102-
})
103-
.collect::<Vec<_>>();
108+
let transactions_to_prove = self
109+
.get_transactions_by_hashes_with_block_range(transaction_hashes.to_vec())
110+
.await?;
104111

105112
// 2 - Compute Transactions Merkle Tree
113+
let transactions = self.transaction_retriever.get_up_to(up_to).await?;
106114
let mk_map = self.compute_merkle_map_from_transactions(transactions)?;
107115

108116
// 3 - Compute proof for each transaction to prove
@@ -178,6 +186,11 @@ mod tests {
178186
async fn compute_proof_for_one_set_with_multiple_transactions() {
179187
let (transaction_hashes, transactions) = generate_transactions(3);
180188
let prover = build_prover(|retriever_mock| {
189+
let transactions_by_hashes_res = transactions.clone();
190+
retriever_mock
191+
.expect_get_by_hashes()
192+
.with(eq(transaction_hashes.clone()))
193+
.return_once(move |_| Ok(transactions_by_hashes_res));
181194
retriever_mock
182195
.expect_get_up_to()
183196
.with(eq(fake_data::beacon()))
@@ -199,8 +212,12 @@ mod tests {
199212

200213
#[tokio::test]
201214
async fn cant_compute_proof_for_unknown_transaction() {
202-
let (transaction_hashes, _transactions) = generate_transactions(3);
215+
let (transaction_hashes, transactions) = generate_transactions(3);
203216
let prover = build_prover(|retriever_mock| {
217+
retriever_mock
218+
.expect_get_by_hashes()
219+
.with(eq(transaction_hashes.clone()))
220+
.return_once(move |_| Ok(transactions));
204221
retriever_mock
205222
.expect_get_up_to()
206223
.with(eq(fake_data::beacon()))
@@ -221,6 +238,11 @@ mod tests {
221238
let prover = build_prover(|retriever_mock| {
222239
// The last two are not in the "store"
223240
let transactions = transactions[0..=2].to_vec();
241+
let transactions_by_hashes_res = transactions.clone();
242+
retriever_mock
243+
.expect_get_by_hashes()
244+
.with(eq(transaction_hashes.clone()))
245+
.return_once(move |_| Ok(transactions_by_hashes_res));
224246
retriever_mock
225247
.expect_get_up_to()
226248
.with(eq(fake_data::beacon()))
@@ -245,8 +267,8 @@ mod tests {
245267
let (transaction_hashes, _transactions) = generate_transactions(3);
246268
let prover = build_prover(|retriever_mock| {
247269
retriever_mock
248-
.expect_get_up_to()
249-
.with(eq(fake_data::beacon()))
270+
.expect_get_by_hashes()
271+
.with(eq(transaction_hashes.clone()))
250272
.returning(|_| Err(anyhow!("Error")));
251273
});
252274

0 commit comments

Comments
 (0)