@@ -2,17 +2,16 @@ use std::path::{Path, PathBuf};
2
2
use std:: sync:: Arc ;
3
3
4
4
use async_trait:: async_trait;
5
- #[ cfg( test) ]
6
- use mockall:: automock;
7
5
use slog:: { debug, Logger } ;
8
6
9
7
use mithril_common:: cardano_block_scanner:: BlockScanner ;
10
- use mithril_common:: entities:: { CardanoTransaction , ImmutableFileNumber } ;
8
+ use mithril_common:: crypto_helper:: MKTreeNode ;
9
+ use mithril_common:: entities:: { BlockRange , CardanoTransaction , ImmutableFileNumber } ;
11
10
use mithril_common:: signable_builder:: TransactionsImporter ;
12
11
use mithril_common:: StdResult ;
13
12
14
13
/// Cardano transactions store
15
- #[ cfg_attr( test, automock) ]
14
+ #[ cfg_attr( test, mockall :: automock) ]
16
15
#[ async_trait]
17
16
pub trait TransactionStore : Send + Sync {
18
17
/// Get the highest known transaction beacon
@@ -26,6 +25,12 @@ pub trait TransactionStore: Send + Sync {
26
25
27
26
/// Store list of transactions
28
27
async fn store_transactions ( & self , transactions : Vec < CardanoTransaction > ) -> StdResult < ( ) > ;
28
+
29
+ /// Store list of block ranges with their corresponding merkle root
30
+ async fn store_block_ranges (
31
+ & self ,
32
+ block_ranges : Vec < ( BlockRange , MKTreeNode ) > ,
33
+ ) -> StdResult < ( ) > ;
29
34
}
30
35
31
36
/// Import and store [CardanoTransaction].
@@ -121,6 +126,8 @@ mod tests {
121
126
use mockall:: predicate:: eq;
122
127
123
128
use mithril_common:: cardano_block_scanner:: ScannedBlock ;
129
+ use mithril_common:: crypto_helper:: MKTree ;
130
+ use mithril_common:: entities:: BlockNumber ;
124
131
125
132
use crate :: database:: repository:: CardanoTransactionRepository ;
126
133
use crate :: database:: test_utils:: cardano_tx_db_connection;
@@ -166,8 +173,18 @@ mod tests {
166
173
)
167
174
}
168
175
176
+ fn block_range_with_merkle_root < T : Into < MKTreeNode > + Clone > (
177
+ block_number : BlockNumber ,
178
+ hashes : & [ T ] ,
179
+ ) -> ( BlockRange , MKTreeNode ) {
180
+ (
181
+ BlockRange :: from_block_number ( block_number) ,
182
+ MKTree :: new ( hashes) . unwrap ( ) . compute_root ( ) . unwrap ( ) ,
183
+ )
184
+ }
185
+
169
186
#[ tokio:: test]
170
- async fn if_nothing_stored_parse_and_store_all_transactions ( ) {
187
+ async fn if_nothing_stored_parse_and_store_all_transactions_and_block_ranges ( ) {
171
188
let blocks = vec ! [
172
189
ScannedBlock :: new( "block_hash-1" , 10 , 15 , 11 , vec![ "tx_hash-1" , "tx_hash-2" ] ) ,
173
190
ScannedBlock :: new( "block_hash-2" , 20 , 25 , 12 , vec![ "tx_hash-3" , "tx_hash-4" ] ) ,
@@ -195,6 +212,14 @@ mod tests {
195
212
. with ( eq ( expected_stored_transactions) )
196
213
. returning ( |_| Ok ( ( ) ) )
197
214
. once ( ) ;
215
+ store_mock
216
+ . expect_store_block_ranges ( )
217
+ . with ( eq ( vec ! [
218
+ block_range_with_merkle_root( 10 , & [ "tx_hash-1" , "tx_hash-2" ] ) ,
219
+ block_range_with_merkle_root( 20 , & [ "tx_hash-3" , "tx_hash-4" ] ) ,
220
+ ] ) )
221
+ . returning ( |_| Ok ( ( ) ) )
222
+ . once ( ) ;
198
223
} ,
199
224
) ;
200
225
@@ -217,6 +242,7 @@ mod tests {
217
242
. expect_get_highest_beacon ( )
218
243
. returning ( || Ok ( Some ( 12 ) ) ) ;
219
244
store_mock. expect_store_transactions ( ) . never ( ) ;
245
+ store_mock. expect_store_block_ranges ( ) . never ( ) ;
220
246
} ,
221
247
) ;
222
248
@@ -256,6 +282,14 @@ mod tests {
256
282
. with ( eq ( expected_to_store_transactions) )
257
283
. returning ( |_| Ok ( ( ) ) )
258
284
. once ( ) ;
285
+ store_mock
286
+ . expect_store_block_ranges ( )
287
+ . with ( eq ( vec ! [ block_range_with_merkle_root(
288
+ 20 ,
289
+ & [ "tx_hash-3" , "tx_hash-4" ] ,
290
+ ) ] ) )
291
+ . returning ( |_| Ok ( ( ) ) )
292
+ . once ( ) ;
259
293
} ,
260
294
) ;
261
295
@@ -266,7 +300,7 @@ mod tests {
266
300
}
267
301
268
302
#[ tokio:: test]
269
- async fn importing_twice_starting_with_nothing_in_a_real_db_should_yield_the_transactions_in_same_order (
303
+ async fn importing_twice_starting_with_nothing_in_a_real_db_should_yield_transactions_and_block_ranges_in_same_order (
270
304
) {
271
305
let blocks = vec ! [
272
306
ScannedBlock :: new( "block_hash-1" , 10 , 15 , 11 , vec![ "tx_hash-1" , "tx_hash-2" ] ) ,
0 commit comments