Skip to content

Commit 9c0605c

Browse files
dlachaumejpraynaud
authored andcommitted
Add per_immutable_file for CardanoTransactionsBuilder
1 parent 4ed01ea commit 9c0605c

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

mithril-common/src/test_utils/cardano_transactions_builder.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ use crate::entities::{BlockRange, CardanoTransaction};
66
pub struct CardanoTransactionsBuilder {
77
max_transactions_per_block: usize,
88
max_blocks_per_block_range: usize,
9+
max_transactions_per_immutable_file: usize,
910
}
1011

1112
impl CardanoTransactionsBuilder {
1213
pub fn new() -> Self {
1314
Self {
1415
max_transactions_per_block: 1,
1516
max_blocks_per_block_range: 1,
17+
max_transactions_per_immutable_file: 1,
1618
}
1719
}
1820

@@ -22,6 +24,12 @@ impl CardanoTransactionsBuilder {
2224
self
2325
}
2426

27+
/// Define how many transactions we generate for one immutable_file.
28+
pub fn per_immutable_file(mut self, transactions_per_immutable_file: usize) -> Self {
29+
self.max_transactions_per_immutable_file = transactions_per_immutable_file;
30+
self
31+
}
32+
2533
/// Define how many blocks we generate in each block_range.
2634
/// If we set too many blocks for a block_range, this function panic.
2735
pub fn blocks_per_block_range(mut self, blocks_per_block_range: usize) -> Self {
@@ -53,8 +61,11 @@ impl CardanoTransactionsBuilder {
5361
let first_transaction_number = 100;
5462
for tx_index in 0..transactions_count {
5563
let block_number = self.block_number_from_transaction_index(tx_index);
56-
transactions
57-
.push(self.create_transaction(tx_index + first_transaction_number, block_number))
64+
transactions.push(self.create_transaction(
65+
tx_index as u64 + first_transaction_number,
66+
block_number,
67+
tx_index as u64 / self.max_transactions_per_immutable_file as u64,
68+
))
5869
}
5970

6071
transactions
@@ -82,15 +93,16 @@ impl CardanoTransactionsBuilder {
8293
/// Create a transaction with a given index and block number.
8394
fn create_transaction(
8495
&self,
85-
transaction_index: usize,
96+
transaction_id: u64,
8697
block_number: u64,
98+
immutable_file_number: u64,
8799
) -> CardanoTransaction {
88100
CardanoTransaction::new(
89-
format!("tx-hash-{}-{}", block_number, transaction_index),
101+
format!("tx-hash-{}-{}", block_number, transaction_id),
90102
block_number,
91-
transaction_index as u64,
103+
transaction_id,
92104
format!("block-hash-{block_number}"),
93-
transaction_index as u64,
105+
immutable_file_number,
94106
)
95107
}
96108

@@ -368,4 +380,18 @@ mod test {
368380
fn should_panic_when_too_many_blocks_per_block_range() {
369381
CardanoTransactionsBuilder::new().blocks_per_block_range(BlockRange::LENGTH as usize + 1);
370382
}
383+
384+
#[test]
385+
fn build_transactions_with_many_transactions_per_immutable_file() {
386+
let transactions = CardanoTransactionsBuilder::new()
387+
.per_immutable_file(5)
388+
.build_transactions(18);
389+
390+
assert_eq!(transactions.len(), 18);
391+
392+
assert_eq!(
393+
4,
394+
count_distinct_values(&transactions, &|t| t.immutable_file_number)
395+
);
396+
}
371397
}

0 commit comments

Comments
 (0)