1
- use std:: { collections:: HashMap , sync:: Arc } ;
1
+ use std:: { collections:: BTreeMap , sync:: Arc } ;
2
2
3
3
use anyhow:: Context ;
4
4
use async_trait:: async_trait;
@@ -70,25 +70,37 @@ impl MithrilProverService {
70
70
& self ,
71
71
transactions : Vec < CardanoTransaction > ,
72
72
) -> 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 ;
75
76
for transaction in transactions {
76
77
let block_range = BlockRange :: from_block_number ( transaction. block_number ) ;
78
+ last_transaction = Some ( transaction. clone ( ) ) ;
77
79
transactions_by_block_ranges
78
80
. entry ( block_range)
79
81
. or_default ( )
80
82
. push ( transaction. transaction_hash ) ;
81
83
}
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
+
82
102
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
92
104
)
93
105
. with_context ( || "ProverService failed to compute the merkelized structure that proves ownership of the transaction" ) ?;
94
106
@@ -143,6 +155,8 @@ impl ProverService for MithrilProverService {
143
155
144
156
#[ cfg( test) ]
145
157
mod tests {
158
+ use std:: cmp:: max;
159
+
146
160
use anyhow:: anyhow;
147
161
use mithril_common:: entities:: CardanoTransaction ;
148
162
use mithril_common:: test_utils:: fake_data;
@@ -160,7 +174,7 @@ mod tests {
160
174
let hash = format ! ( "tx-{i}" ) ;
161
175
transactions. push ( CardanoTransaction :: new (
162
176
& hash,
163
- 10 * i as u64 ,
177
+ max ( 0 , 10 * i - 1 ) as u64 ,
164
178
100 * i as u64 ,
165
179
format ! ( "block_hash-{i}" ) ,
166
180
i as u64 ,
@@ -200,7 +214,6 @@ mod tests {
200
214
. compute_transactions_proofs ( & fake_data:: beacon ( ) , & transaction_hashes)
201
215
. await
202
216
. unwrap ( ) ;
203
-
204
217
assert_eq ! ( transactions_set_proof. len( ) , 1 ) ;
205
218
assert_eq ! (
206
219
transactions_set_proof[ 0 ] . transactions_hashes( ) ,
0 commit comments