Skip to content

Commit b6a6939

Browse files
committed
Add a test for merkle root computation on Importer
1 parent defda78 commit b6a6939

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

mithril-signer/src/cardano_transactions_importer.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,14 @@ mod tests {
252252
.collect()
253253
}
254254

255+
fn merkle_root_for_blocks(block_ranges: &[ScannedBlock]) -> MKTreeNode {
256+
let tx: Vec<_> = block_ranges
257+
.iter()
258+
.flat_map(|br| br.clone().into_transactions())
259+
.collect();
260+
MKTree::new(&tx).unwrap().compute_root().unwrap()
261+
}
262+
255263
#[tokio::test]
256264
async fn if_nothing_stored_parse_and_store_all_transactions() {
257265
let blocks = vec![
@@ -464,6 +472,52 @@ mod tests {
464472
.expect("Transactions Parser should succeed");
465473
}
466474

475+
#[tokio::test]
476+
async fn compute_block_range_merkle_root() {
477+
// 2 block ranges worth of blocks with one more block that should be ignored for merkle root computation
478+
let blocks = build_blocks(0, BlockRange::LENGTH * 2 + 1);
479+
let transactions: Vec<CardanoTransaction> = blocks
480+
.iter()
481+
.flat_map(|b| b.clone().into_transactions())
482+
.collect();
483+
let expected_merkle_root = vec![
484+
(
485+
BlockRange::from_block_number(0),
486+
merkle_root_for_blocks(&blocks[0..(BlockRange::LENGTH as usize)]),
487+
),
488+
(
489+
BlockRange::from_block_number(BlockRange::LENGTH),
490+
merkle_root_for_blocks(
491+
&blocks[(BlockRange::LENGTH as usize)..((BlockRange::LENGTH * 2) as usize)],
492+
),
493+
),
494+
];
495+
496+
let importer = build_importer(
497+
|_scanner_mock| {},
498+
|store_mock| {
499+
let expected_stored_transactions = transactions.clone();
500+
store_mock
501+
.expect_get_block_interval_without_associated_block_range_and_merkle_root()
502+
.returning(|| Ok(Some((0, BlockRange::LENGTH * 2))));
503+
store_mock
504+
.expect_get_transactions_between()
505+
.return_once(move |_, _| Ok(expected_stored_transactions))
506+
.once();
507+
store_mock
508+
.expect_store_block_ranges()
509+
.withf(move |block_ranges| &expected_merkle_root == block_ranges)
510+
.returning(|_| Ok(()))
511+
.once();
512+
},
513+
);
514+
515+
importer
516+
.import_block_ranges()
517+
.await
518+
.expect("Transactions Parser should succeed");
519+
}
520+
467521
#[tokio::test]
468522
async fn importing_twice_starting_with_nothing_in_a_real_db_should_yield_transactions_in_same_order(
469523
) {

0 commit comments

Comments
 (0)