Skip to content

Commit fff548e

Browse files
committed
Remove conversion from or to u64
1 parent 03b7bad commit fff548e

File tree

8 files changed

+73
-100
lines changed

8 files changed

+73
-100
lines changed

internal/mithril-persistence/src/database/query/block_range_root/delete_block_range_root.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,13 @@ impl Query for DeleteBlockRangeRootQuery {
3030
}
3131

3232
impl DeleteBlockRangeRootQuery {
33-
pub fn contains_or_above_block_number_threshold<T: Into<BlockNumber>>(
34-
block_number_threshold: T,
33+
pub fn contains_or_above_block_number_threshold(
34+
block_number_threshold: BlockNumber,
3535
) -> StdResult<Self> {
36-
let threshold = block_number_threshold.into();
37-
let block_range = BlockRange::from_block_number(threshold);
38-
let threshold = Value::Integer(
39-
block_range
40-
.start
41-
.try_into()
42-
.with_context(|| format!("Failed to convert threshold `{threshold}` to i64"))?,
43-
);
36+
let block_range = BlockRange::from_block_number(block_number_threshold);
37+
let threshold = Value::Integer(block_range.start.try_into().with_context(|| {
38+
format!("Failed to convert threshold `{block_number_threshold}` to i64")
39+
})?);
4440

4541
Ok(Self {
4642
condition: WhereCondition::new("start >= ?*", vec![threshold]),
@@ -86,7 +82,10 @@ mod tests {
8682

8783
let cursor = connection
8884
.fetch(
89-
DeleteBlockRangeRootQuery::contains_or_above_block_number_threshold(100).unwrap(),
85+
DeleteBlockRangeRootQuery::contains_or_above_block_number_threshold(BlockNumber(
86+
100,
87+
))
88+
.unwrap(),
9089
)
9190
.expect("pruning shouldn't crash without block range root stored");
9291
assert_eq!(0, cursor.count());

internal/mithril-persistence/src/database/query/block_range_root/get_block_range_root.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ impl GetBlockRangeRootQuery {
1717
}
1818
}
1919

20-
pub fn contains_or_below_block_number<T: Into<BlockNumber>>(block_number: T) -> Self {
20+
pub fn contains_or_below_block_number(block_number: BlockNumber) -> Self {
2121
Self {
2222
condition: WhereCondition::new(
2323
"start < ?*",
24-
vec![Value::Integer(*block_number.into() as i64)],
24+
vec![Value::Integer(*block_number as i64)],
2525
),
2626
}
2727
}
@@ -85,7 +85,9 @@ mod tests {
8585
let connection = cardano_tx_db_connection().unwrap();
8686

8787
let cursor: Vec<BlockRangeRootRecord> = connection
88-
.fetch_collect(GetBlockRangeRootQuery::contains_or_below_block_number(100))
88+
.fetch_collect(GetBlockRangeRootQuery::contains_or_below_block_number(
89+
BlockNumber(100),
90+
))
8991
.unwrap();
9092
assert_eq!(Vec::<BlockRangeRootRecord>::new(), cursor);
9193
}
@@ -98,7 +100,7 @@ mod tests {
98100

99101
let cursor: Vec<BlockRangeRootRecord> = connection
100102
.fetch_collect(GetBlockRangeRootQuery::contains_or_below_block_number(
101-
10_000,
103+
BlockNumber(10_000),
102104
))
103105
.unwrap();
104106

@@ -112,7 +114,9 @@ mod tests {
112114
insert_block_range_roots(&connection, dataset.clone());
113115

114116
let cursor: Vec<BlockRangeRootRecord> = connection
115-
.fetch_collect(GetBlockRangeRootQuery::contains_or_below_block_number(44))
117+
.fetch_collect(GetBlockRangeRootQuery::contains_or_below_block_number(
118+
BlockNumber(44),
119+
))
116120
.unwrap();
117121

118122
assert_eq!(&dataset[0..2], &cursor);
@@ -125,7 +129,9 @@ mod tests {
125129
insert_block_range_roots(&connection, dataset.clone());
126130

127131
let cursor: Vec<BlockRangeRootRecord> = connection
128-
.fetch_collect(GetBlockRangeRootQuery::contains_or_below_block_number(45))
132+
.fetch_collect(GetBlockRangeRootQuery::contains_or_below_block_number(
133+
BlockNumber(45),
134+
))
129135
.unwrap();
130136

131137
assert_eq!(&dataset[0..2], &cursor);
@@ -138,7 +144,9 @@ mod tests {
138144
insert_block_range_roots(&connection, dataset.clone());
139145

140146
let cursor: Vec<BlockRangeRootRecord> = connection
141-
.fetch_collect(GetBlockRangeRootQuery::contains_or_below_block_number(46))
147+
.fetch_collect(GetBlockRangeRootQuery::contains_or_below_block_number(
148+
BlockNumber(46),
149+
))
142150
.unwrap();
143151

144152
assert_eq!(dataset, cursor);

internal/mithril-persistence/src/database/query/cardano_transaction/delete_cardano_transaction.rs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,20 @@ impl Query for DeleteCardanoTransactionQuery {
3030
}
3131

3232
impl DeleteCardanoTransactionQuery {
33-
pub fn below_block_number_threshold<T: Into<BlockNumber>>(
34-
block_number_threshold: T,
35-
) -> StdResult<Self> {
36-
let threshold = block_number_threshold.into();
37-
let threshold = Value::Integer(
38-
threshold
39-
.try_into()
40-
.with_context(|| format!("Failed to convert threshold `{threshold}` to i64"))?,
41-
);
33+
pub fn below_block_number_threshold(block_number_threshold: BlockNumber) -> StdResult<Self> {
34+
let threshold = Value::Integer(block_number_threshold.try_into().with_context(|| {
35+
format!("Failed to convert threshold `{block_number_threshold}` to i64")
36+
})?);
4237

4338
Ok(Self {
4439
condition: WhereCondition::new("block_number < ?*", vec![threshold]),
4540
})
4641
}
4742

48-
pub fn above_block_number_threshold<T: Into<BlockNumber>>(
49-
block_number_threshold: T,
50-
) -> StdResult<Self> {
51-
let threshold = block_number_threshold.into();
52-
let threshold = Value::Integer(
53-
threshold
54-
.try_into()
55-
.with_context(|| format!("Failed to convert threshold `{threshold}` to i64"))?,
56-
);
43+
pub fn above_block_number_threshold(block_number_threshold: BlockNumber) -> StdResult<Self> {
44+
let threshold = Value::Integer(block_number_threshold.try_into().with_context(|| {
45+
format!("Failed to convert threshold `{block_number_threshold}` to i64")
46+
})?);
5747

5848
Ok(Self {
5949
condition: WhereCondition::new("block_number > ?*", vec![threshold]),
@@ -94,7 +84,10 @@ mod tests {
9484
let connection = cardano_tx_db_connection().unwrap();
9585

9686
let cursor = connection
97-
.fetch(DeleteCardanoTransactionQuery::below_block_number_threshold(100).unwrap())
87+
.fetch(
88+
DeleteCardanoTransactionQuery::below_block_number_threshold(BlockNumber(100))
89+
.unwrap(),
90+
)
9891
.expect("pruning shouldn't crash without transactions stored");
9992
assert_eq!(0, cursor.count());
10093
}
@@ -105,7 +98,8 @@ mod tests {
10598
insert_transactions(&connection, test_transaction_set());
10699

107100
let query =
108-
DeleteCardanoTransactionQuery::below_block_number_threshold(100_000).unwrap();
101+
DeleteCardanoTransactionQuery::below_block_number_threshold(BlockNumber(100_000))
102+
.unwrap();
109103
let cursor = connection.fetch(query).unwrap();
110104
assert_eq!(test_transaction_set().len(), cursor.count());
111105

@@ -118,7 +112,8 @@ mod tests {
118112
let connection = cardano_tx_db_connection().unwrap();
119113
insert_transactions(&connection, test_transaction_set());
120114

121-
let query = DeleteCardanoTransactionQuery::below_block_number_threshold(0).unwrap();
115+
let query = DeleteCardanoTransactionQuery::below_block_number_threshold(BlockNumber(0))
116+
.unwrap();
122117
let cursor = connection.fetch(query).unwrap();
123118
assert_eq!(0, cursor.count());
124119

@@ -131,7 +126,9 @@ mod tests {
131126
let connection = cardano_tx_db_connection().unwrap();
132127
insert_transactions(&connection, test_transaction_set());
133128

134-
let query = DeleteCardanoTransactionQuery::below_block_number_threshold(12).unwrap();
129+
let query =
130+
DeleteCardanoTransactionQuery::below_block_number_threshold(BlockNumber(12))
131+
.unwrap();
135132
let cursor = connection.fetch(query).unwrap();
136133
assert_eq!(4, cursor.count());
137134

@@ -148,7 +145,10 @@ mod tests {
148145
let connection = cardano_tx_db_connection().unwrap();
149146

150147
let cursor = connection
151-
.fetch(DeleteCardanoTransactionQuery::above_block_number_threshold(100).unwrap())
148+
.fetch(
149+
DeleteCardanoTransactionQuery::above_block_number_threshold(BlockNumber(100))
150+
.unwrap(),
151+
)
152152
.expect("pruning shouldn't crash without transactions stored");
153153
assert_eq!(0, cursor.count());
154154
}
@@ -158,7 +158,8 @@ mod tests {
158158
let connection = cardano_tx_db_connection().unwrap();
159159
insert_transactions(&connection, test_transaction_set());
160160

161-
let query = DeleteCardanoTransactionQuery::above_block_number_threshold(0).unwrap();
161+
let query = DeleteCardanoTransactionQuery::above_block_number_threshold(BlockNumber(0))
162+
.unwrap();
162163
let cursor = connection.fetch(query).unwrap();
163164
assert_eq!(test_transaction_set().len(), cursor.count());
164165

@@ -173,7 +174,8 @@ mod tests {
173174
insert_transactions(&connection, test_transaction_set());
174175

175176
let query =
176-
DeleteCardanoTransactionQuery::above_block_number_threshold(100_000).unwrap();
177+
DeleteCardanoTransactionQuery::above_block_number_threshold(BlockNumber(100_000))
178+
.unwrap();
177179
let cursor = connection.fetch(query).unwrap();
178180
assert_eq!(0, cursor.count());
179181

@@ -186,7 +188,9 @@ mod tests {
186188
let connection = cardano_tx_db_connection().unwrap();
187189
insert_transactions(&connection, test_transaction_set());
188190

189-
let query = DeleteCardanoTransactionQuery::above_block_number_threshold(10).unwrap();
191+
let query =
192+
DeleteCardanoTransactionQuery::above_block_number_threshold(BlockNumber(10))
193+
.unwrap();
190194
let cursor = connection.fetch(query).unwrap();
191195
assert_eq!(4, cursor.count());
192196

mithril-aggregator/src/services/cardano_transactions_importer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,8 +659,8 @@ mod tests {
659659
.expect_get_transactions_in_range()
660660
// Lower bound should be the end block number of the last known block range
661661
// Upper bound should be the block number provided to `import_block_ranges`
662-
.withf(|range| {
663-
BlockRangesSequence::new(HIGHEST_BLOCK_RANGE_START..=(BlockRange::LENGTH * 5))
662+
.withf(move |range| {
663+
BlockRangesSequence::new(HIGHEST_BLOCK_RANGE_START..=up_to_block_number)
664664
.contains(range)
665665
})
666666
.returning(transactions_for_block);

mithril-common/benches/merkle_map.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ fn generate_block_ranges_nodes_iterator(
3737
"total_block_ranges should be strictly greater than 0"
3838
);
3939
(0..total_block_ranges).map(move |block_range_index| {
40-
let block_range = BlockRange::new(
41-
block_range_index * total_transactions_per_block_range,
42-
(block_range_index + 1) * total_transactions_per_block_range,
43-
);
40+
let start = block_range_index * total_transactions_per_block_range;
41+
let end = (block_range_index + 1) * total_transactions_per_block_range;
42+
let block_range = BlockRange::from(start..end);
4443
let mk_map_node = if block_range_index <= total_block_ranges - max_uncompressed_block_ranges
4544
{
4645
let leaves = (*block_range.start..*block_range.end)

mithril-common/src/entities/block_number.rs

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::cmp::Ordering;
21
use std::fmt::{Display, Formatter};
32
use std::num::TryFromIntError;
43
use std::ops::{Deref, DerefMut};
@@ -16,7 +15,7 @@ use wasm_bindgen::prelude::*;
1615

1716
/// BlockNumber is the block number of a Cardano transaction.
1817
#[derive(
19-
Debug, Copy, Clone, Default, PartialEq, Serialize, Deserialize, Hash, Eq, PartialOrd, Ord,
18+
Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Hash,
2019
)]
2120
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
2221
pub struct BlockNumber(pub u64);
@@ -43,18 +42,6 @@ impl DerefMut for BlockNumber {
4342
}
4443
}
4544

46-
impl From<u64> for BlockNumber {
47-
fn from(value: u64) -> Self {
48-
Self(value)
49-
}
50-
}
51-
52-
impl From<&u64> for BlockNumber {
53-
fn from(value: &u64) -> Self {
54-
(*value).into()
55-
}
56-
}
57-
5845
// Useful for conversion to sqlite number (that use i64)
5946
impl TryFrom<BlockNumber> for i64 {
6047
type Error = TryFromIntError;
@@ -64,30 +51,6 @@ impl TryFrom<BlockNumber> for i64 {
6451
}
6552
}
6653

67-
impl PartialOrd<u64> for BlockNumber {
68-
fn partial_cmp(&self, other: &u64) -> Option<Ordering> {
69-
self.0.partial_cmp(other)
70-
}
71-
}
72-
73-
impl PartialOrd<&u64> for BlockNumber {
74-
fn partial_cmp(&self, other: &&u64) -> Option<Ordering> {
75-
self.0.partial_cmp(*other)
76-
}
77-
}
78-
79-
impl PartialOrd<BlockNumber> for u64 {
80-
fn partial_cmp(&self, other: &BlockNumber) -> Option<Ordering> {
81-
self.partial_cmp(&other.0)
82-
}
83-
}
84-
85-
impl PartialOrd<&BlockNumber> for u64 {
86-
fn partial_cmp(&self, other: &&BlockNumber) -> Option<Ordering> {
87-
self.partial_cmp(&other.0)
88-
}
89-
}
90-
9154
impl_add_to_wrapper!(BlockNumber, u64);
9255
impl_sub_to_wrapper!(BlockNumber, u64);
9356
impl_mul_to_wrapper!(BlockNumber, u64);

mithril-common/src/entities/block_range.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ impl BlockRange {
2727
/// Important: this value should be updated with extreme care (probably with an era change) in order to avoid signing disruptions.
2828
pub const LENGTH: BlockRangeLength = BlockNumber(15);
2929

30-
/// BlockRange factory
31-
pub fn new<T1: Into<BlockNumber>, T2: Into<BlockNumber>>(start: T1, end: T2) -> Self {
32-
Self {
33-
inner_range: start.into()..end.into(),
30+
cfg_test_tools! {
31+
/// BlockRange factory
32+
pub fn new(start: u64, end: u64) -> Self {
33+
Self {
34+
inner_range: BlockNumber(start)..BlockNumber(end),
35+
}
3436
}
35-
}
3637

37-
cfg_test_tools! {
3838
/// Try to add two BlockRanges
3939
pub fn try_add(&self, other: &BlockRange) -> StdResult<BlockRange> {
4040
if self.inner_range.end.max(other.inner_range.end)
@@ -82,7 +82,7 @@ impl BlockRange {
8282
}
8383
let block_range_start = Self::start_with_length(number, length);
8484
let block_range_end = block_range_start + length;
85-
Ok(Self::new(*block_range_start, *block_range_end))
85+
Ok(Self::from(*block_range_start..*block_range_end))
8686
}
8787

8888
/// Get the start of the block range of given length that contains the given block number
@@ -238,11 +238,11 @@ mod tests {
238238
fn test_block_range_contains() {
239239
let block_range = BlockRange::new(1, 10);
240240

241-
assert!(block_range.contains(&1));
242-
assert!(block_range.contains(&6));
241+
assert!(block_range.contains(&BlockNumber(1)));
242+
assert!(block_range.contains(&BlockNumber(6)));
243243
assert!(block_range.contains(&BlockNumber(9)));
244244

245-
assert!(block_range.contains(&0).not());
245+
assert!(block_range.contains(&BlockNumber(0)).not());
246246
// The end of the range is exclusive
247247
assert!(block_range.contains(&BlockNumber(10)).not());
248248
}

mithril-signer/src/cardano_transactions_importer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,8 +659,8 @@ mod tests {
659659
.expect_get_transactions_in_range()
660660
// Lower bound should be the end block number of the last known block range
661661
// Upper bound should be the block number provided to `import_block_ranges`
662-
.withf(|range| {
663-
BlockRangesSequence::new(HIGHEST_BLOCK_RANGE_START..=(BlockRange::LENGTH * 5))
662+
.withf(move |range| {
663+
BlockRangesSequence::new(HIGHEST_BLOCK_RANGE_START..=up_to_block_number)
664664
.contains(range)
665665
})
666666
.returning(transactions_for_block);

0 commit comments

Comments
 (0)