Skip to content

Commit 2bcb1a7

Browse files
committed
PR review: avoid using RangeInclusive
To be consistent with all usages or ranges for blocks, this remove the last usage of a range inclusive to only use non-inclusive range. The last case of RangeInclusive was a test that checked that an inverval is contained in a block ranges sequence. To do so it add a `contains(range)` to `BlockRangesSequence`.
1 parent 2905703 commit 2bcb1a7

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

mithril-aggregator/src/services/cardano_transactions_importer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ mod tests {
182182

183183
use mithril_common::cardano_block_scanner::{DumbBlockScanner, ScannedBlock};
184184
use mithril_common::crypto_helper::MKTree;
185-
use mithril_common::entities::BlockNumber;
185+
use mithril_common::entities::{BlockNumber, BlockRangesSequence};
186186

187187
use crate::database::repository::CardanoTransactionRepository;
188188
use crate::database::test_helper::cardano_tx_db_connection;
@@ -534,8 +534,8 @@ mod tests {
534534
// Upper bound should be the block number of the highest transaction in a db that can be
535535
// included in a block range
536536
.withf(|range| {
537-
let expected_range = BlockRange::LENGTH..=(BlockRange::LENGTH * 5);
538-
expected_range.contains(&range.start) && expected_range.contains(&range.end)
537+
BlockRangesSequence::new(BlockRange::LENGTH..(BlockRange::LENGTH * 5))
538+
.contains(range)
539539
})
540540
.returning(transactions_for_block);
541541
store_mock

mithril-common/src/entities/block_range.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ impl BlockRangesSequence {
179179
self.end
180180
}
181181

182+
/// Returns `true` if range is contained in the sequence.
183+
pub fn contains(&self, range: &Range<BlockNumber>) -> bool {
184+
self.start <= range.start && range.end <= self.end
185+
}
186+
182187
/// Returns `true` if the block ranges sequence contains no elements.
183188
pub fn is_empty(&self) -> bool {
184189
self.len() == 0
@@ -338,6 +343,20 @@ mod tests {
338343
);
339344
}
340345

346+
#[test]
347+
fn test_block_ranges_sequence_contains() {
348+
let block_range = BlockRange::new(15, 30);
349+
assert!(BlockRange::all_block_ranges_in(0..15)
350+
.contains(&block_range)
351+
.not());
352+
assert!(BlockRange::all_block_ranges_in(30..60)
353+
.contains(&block_range)
354+
.not());
355+
assert!(BlockRange::all_block_ranges_in(0..30).contains(&block_range));
356+
assert!(BlockRange::all_block_ranges_in(15..30).contains(&block_range));
357+
assert!(BlockRange::all_block_ranges_in(15..45).contains(&block_range));
358+
}
359+
341360
#[test]
342361
fn test_block_range_from_number() {
343362
assert_eq!(BlockRange::from_block_number(0), BlockRange::new(0, 15));

mithril-signer/src/cardano_transactions_importer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ mod tests {
182182

183183
use mithril_common::cardano_block_scanner::{DumbBlockScanner, ScannedBlock};
184184
use mithril_common::crypto_helper::MKTree;
185-
use mithril_common::entities::BlockNumber;
185+
use mithril_common::entities::{BlockNumber, BlockRangesSequence};
186186

187187
use crate::database::repository::CardanoTransactionRepository;
188188
use crate::database::test_utils::cardano_tx_db_connection;
@@ -534,8 +534,8 @@ mod tests {
534534
// Upper bound should be the block number of the highest transaction in a db that can be
535535
// included in a block range
536536
.withf(|range| {
537-
let expected_range = BlockRange::LENGTH..=(BlockRange::LENGTH * 5);
538-
expected_range.contains(&range.start) && expected_range.contains(&range.end)
537+
BlockRangesSequence::new(BlockRange::LENGTH..(BlockRange::LENGTH * 5))
538+
.contains(range)
539539
})
540540
.returning(transactions_for_block);
541541
store_mock

0 commit comments

Comments
 (0)