Skip to content

Commit ba3901a

Browse files
committed
fix: make 'ProverService' compute proof on the correct range of blocks in aggregator
1 parent 0dfb936 commit ba3901a

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

mithril-aggregator/src/services/prover.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashMap, sync::Arc};
1+
use std::{collections::BTreeMap, sync::Arc};
22

33
use anyhow::Context;
44
use async_trait::async_trait;
@@ -70,25 +70,37 @@ impl MithrilProverService {
7070
&self,
7171
transactions: Vec<CardanoTransaction>,
7272
) -> StdResult<MKMap<BlockRange, MKMapNode<BlockRange>>> {
73-
let mut transactions_by_block_ranges: HashMap<BlockRange, Vec<TransactionHash>> =
74-
HashMap::new();
73+
let mut transactions_by_block_ranges: BTreeMap<BlockRange, Vec<TransactionHash>> =
74+
BTreeMap::new();
75+
let mut last_transaction: Option<CardanoTransaction> = None;
7576
for transaction in transactions {
7677
let block_range = BlockRange::from_block_number(transaction.block_number);
78+
last_transaction = Some(transaction.clone());
7779
transactions_by_block_ranges
7880
.entry(block_range)
7981
.or_default()
8082
.push(transaction.transaction_hash);
8183
}
84+
let mut block_ranges = transactions_by_block_ranges.into_iter().try_fold(
85+
vec![],
86+
|mut acc, (block_range, transactions)| -> StdResult<Vec<(_, MKMapNode<_>)>> {
87+
acc.push((block_range, MKTree::new(&transactions)?.into()));
88+
Ok(acc)
89+
},
90+
)?;
91+
92+
// This is a temporary fix to avoid including an incomplete block ranges in the computation of the prover.
93+
// This will be swiftly replaced by the new way of computing proof relying on the block range roots stored in database.
94+
if let Some(transaction) = last_transaction {
95+
if let Some((last_block_range, _)) = block_ranges.last() {
96+
if transaction.block_number < last_block_range.end - 1 {
97+
block_ranges.pop();
98+
}
99+
}
100+
}
101+
82102
let mk_hash_map = MKMap::new_from_iter(
83-
transactions_by_block_ranges
84-
.into_iter()
85-
.try_fold(
86-
vec![],
87-
|mut acc, (block_range, transactions)| -> StdResult<Vec<(_, MKMapNode<_>)>> {
88-
acc.push((block_range, MKTree::new(&transactions)?.into()));
89-
Ok(acc)
90-
},
91-
)?,
103+
block_ranges
92104
)
93105
.with_context(|| "ProverService failed to compute the merkelized structure that proves ownership of the transaction")?;
94106

@@ -143,6 +155,8 @@ impl ProverService for MithrilProverService {
143155

144156
#[cfg(test)]
145157
mod tests {
158+
use std::cmp::max;
159+
146160
use anyhow::anyhow;
147161
use mithril_common::entities::CardanoTransaction;
148162
use mithril_common::test_utils::fake_data;
@@ -160,7 +174,7 @@ mod tests {
160174
let hash = format!("tx-{i}");
161175
transactions.push(CardanoTransaction::new(
162176
&hash,
163-
10 * i as u64,
177+
max(0, 10 * i - 1) as u64,
164178
100 * i as u64,
165179
format!("block_hash-{i}"),
166180
i as u64,
@@ -200,7 +214,6 @@ mod tests {
200214
.compute_transactions_proofs(&fake_data::beacon(), &transaction_hashes)
201215
.await
202216
.unwrap();
203-
204217
assert_eq!(transactions_set_proof.len(), 1);
205218
assert_eq!(
206219
transactions_set_proof[0].transactions_hashes(),

0 commit comments

Comments
 (0)