Skip to content

Commit 3156a94

Browse files
Alenarsfauvel
authored andcommitted
Use new BlockNumber type in mithril-client and mithril-persistence
Aggregator & signer still doesn't compile. In order to do so some more capabilities add to be added to the `BlockNumber`: * Faillible conversion to i64: since it's the target datatype for sqlite integer fields.
1 parent e949261 commit 3156a94

File tree

11 files changed

+224
-142
lines changed

11 files changed

+224
-142
lines changed

internal/mithril-persistence/src/database/hydrator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ mod tests {
9797

9898
#[test]
9999
fn hydrate_cardano_transaction_signed_entity_type() {
100-
let expected = SignedEntityType::CardanoTransactions(Epoch(35), 77);
100+
let expected = SignedEntityType::CardanoTransactions(Epoch(35), BlockNumber(77));
101101
let signed_entity = Hydrator::hydrate_signed_entity_type(
102102
SignedEntityTypeDiscriminants::CardanoTransactions.index(),
103103
&expected.get_json_beacon().unwrap(),

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

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

3232
impl DeleteBlockRangeRootQuery {
33-
pub fn contains_or_above_block_number_threshold(
34-
block_number_threshold: BlockNumber,
33+
pub fn contains_or_above_block_number_threshold<T: Into<BlockNumber>>(
34+
block_number_threshold: T,
3535
) -> StdResult<Self> {
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-
})?);
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+
);
4044

4145
Ok(Self {
4246
condition: WhereCondition::new("start >= ?*", vec![threshold]),
@@ -90,13 +94,13 @@ mod tests {
9094

9195
#[test]
9296
fn test_prune_all_data_if_given_block_number_is_lower_than_stored_number_of_block() {
93-
parameterized_test_prune_block_range(0, block_range_root_dataset().len());
97+
parameterized_test_prune_block_range(BlockNumber(0), block_range_root_dataset().len());
9498
}
9599

96100
#[test]
97101
fn test_prune_keep_all_block_range_root_if_given_number_of_block_is_greater_than_the_highest_one(
98102
) {
99-
parameterized_test_prune_block_range(100_000, 0);
103+
parameterized_test_prune_block_range(BlockNumber(100_000), 0);
100104
}
101105

102106
#[test]

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ impl GetBlockRangeRootQuery {
1717
}
1818
}
1919

20-
pub fn contains_or_below_block_number(block_number: BlockNumber) -> Self {
20+
pub fn contains_or_below_block_number<T: Into<BlockNumber>>(block_number: T) -> Self {
2121
Self {
22-
condition: WhereCondition::new("start < ?*", vec![Value::Integer(block_number as i64)]),
22+
condition: WhereCondition::new(
23+
"start < ?*",
24+
vec![Value::Integer(*block_number.into() as i64)],
25+
),
2326
}
2427
}
2528

@@ -60,15 +63,15 @@ mod tests {
6063
fn block_range_root_dataset() -> Vec<BlockRangeRootRecord> {
6164
[
6265
(
63-
BlockRange::from_block_number(15),
66+
BlockRange::from_block_number(BlockNumber(15)),
6467
MKTreeNode::from_hex("AAAA").unwrap(),
6568
),
6669
(
67-
BlockRange::from_block_number(30),
70+
BlockRange::from_block_number(BlockNumber(30)),
6871
MKTreeNode::from_hex("BBBB").unwrap(),
6972
),
7073
(
71-
BlockRange::from_block_number(45),
74+
BlockRange::from_block_number(BlockNumber(45)),
7275
MKTreeNode::from_hex("CCCC").unwrap(),
7376
),
7477
]

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

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

3232
impl DeleteCardanoTransactionQuery {
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-
})?);
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+
);
3742

3843
Ok(Self {
3944
condition: WhereCondition::new("block_number < ?*", vec![threshold]),
4045
})
4146
}
4247

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-
})?);
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+
);
4757

4858
Ok(Self {
4959
condition: WhereCondition::new("block_number > ?*", vec![threshold]),
@@ -67,12 +77,12 @@ mod tests {
6777

6878
fn test_transaction_set() -> Vec<CardanoTransactionRecord> {
6979
vec![
70-
CardanoTransactionRecord::new("tx-hash-0", 10, 50, "block-hash-10"),
71-
CardanoTransactionRecord::new("tx-hash-1", 10, 51, "block-hash-10"),
72-
CardanoTransactionRecord::new("tx-hash-2", 11, 52, "block-hash-11"),
73-
CardanoTransactionRecord::new("tx-hash-3", 11, 53, "block-hash-11"),
74-
CardanoTransactionRecord::new("tx-hash-4", 12, 54, "block-hash-12"),
75-
CardanoTransactionRecord::new("tx-hash-5", 12, 55, "block-hash-12"),
80+
CardanoTransactionRecord::new("tx-hash-0", BlockNumber(10), 50, "block-hash-10"),
81+
CardanoTransactionRecord::new("tx-hash-1", BlockNumber(10), 51, "block-hash-10"),
82+
CardanoTransactionRecord::new("tx-hash-2", BlockNumber(11), 52, "block-hash-11"),
83+
CardanoTransactionRecord::new("tx-hash-3", BlockNumber(11), 53, "block-hash-11"),
84+
CardanoTransactionRecord::new("tx-hash-4", BlockNumber(12), 54, "block-hash-12"),
85+
CardanoTransactionRecord::new("tx-hash-5", BlockNumber(12), 55, "block-hash-12"),
7686
]
7787
}
7888

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl GetCardanoTransactionQuery {
3737
let condition = WhereCondition::where_in("transaction_hash", hashes_values).and_where(
3838
WhereCondition::new(
3939
"block_number <= ?*",
40-
vec![Value::Integer(up_to_or_equal as i64)],
40+
vec![Value::Integer(*up_to_or_equal as i64)],
4141
),
4242
);
4343

@@ -50,8 +50,8 @@ impl GetCardanoTransactionQuery {
5050
condition = condition.or_where(WhereCondition::new(
5151
"(block_number >= ?* and block_number < ?*)",
5252
vec![
53-
Value::Integer(block_range.start as i64),
54-
Value::Integer(block_range.end as i64),
53+
Value::Integer(*block_range.start as i64),
54+
Value::Integer(*block_range.end as i64),
5555
],
5656
))
5757
}
@@ -62,11 +62,11 @@ impl GetCardanoTransactionQuery {
6262
pub fn between_blocks(range: Range<BlockNumber>) -> Self {
6363
let condition = WhereCondition::new(
6464
"block_number >= ?*",
65-
vec![Value::Integer(range.start as i64)],
65+
vec![Value::Integer(*range.start as i64)],
6666
)
6767
.and_where(WhereCondition::new(
6868
"block_number < ?*",
69-
vec![Value::Integer(range.end as i64)],
69+
vec![Value::Integer(*range.end as i64)],
7070
));
7171

7272
Self { condition }
@@ -132,10 +132,10 @@ mod tests {
132132
insert_transactions(
133133
&connection,
134134
vec![
135-
CardanoTransactionRecord::new("tx-hash-0", 10, 50, "block-hash-10"),
136-
CardanoTransactionRecord::new("tx-hash-1", 10, 51, "block-hash-10"),
137-
CardanoTransactionRecord::new("tx-hash-2", 11, 54, "block-hash-11"),
138-
CardanoTransactionRecord::new("tx-hash-3", 11, 55, "block-hash-11"),
135+
CardanoTransactionRecord::new("tx-hash-0", BlockNumber(10), 50, "block-hash-10"),
136+
CardanoTransactionRecord::new("tx-hash-1", BlockNumber(10), 51, "block-hash-10"),
137+
CardanoTransactionRecord::new("tx-hash-2", BlockNumber(11), 54, "block-hash-11"),
138+
CardanoTransactionRecord::new("tx-hash-3", BlockNumber(11), 55, "block-hash-11"),
139139
],
140140
);
141141

@@ -144,8 +144,8 @@ mod tests {
144144
.unwrap();
145145
assert_eq!(
146146
vec![
147-
CardanoTransactionRecord::new("tx-hash-2", 11, 54, "block-hash-11"),
148-
CardanoTransactionRecord::new("tx-hash-3", 11, 55, "block-hash-11"),
147+
CardanoTransactionRecord::new("tx-hash-2", BlockNumber(11), 54, "block-hash-11"),
148+
CardanoTransactionRecord::new("tx-hash-3", BlockNumber(11), 55, "block-hash-11"),
149149
],
150150
records
151151
);

internal/mithril-persistence/src/database/record/block_range_root.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use sqlite::Row;
22

33
use mithril_common::crypto_helper::MKTreeNode;
4-
use mithril_common::entities::BlockRange;
4+
use mithril_common::entities::{BlockNumber, BlockRange};
55

66
use crate::database::Hydrator;
77
use crate::sqlite::{HydrationError, Projection, SqLiteEntity};
@@ -37,7 +37,7 @@ impl SqLiteEntity for BlockRangeRootRecord {
3737
{
3838
let start = Hydrator::try_to_u64("block_range.start", row.read::<i64, _>(0))?;
3939
let end = Hydrator::try_to_u64("block_range.end", row.read::<i64, _>(1))?;
40-
let range = BlockRange::from_block_number(start);
40+
let range = BlockRange::from_block_number(BlockNumber(start));
4141
let merkle_root = row.read::<&str, _>(2);
4242

4343
if range.start != start || range.end != end {
@@ -71,8 +71,6 @@ impl SqLiteEntity for BlockRangeRootRecord {
7171
mod tests {
7272
use sqlite::Connection;
7373

74-
use mithril_common::entities::BlockNumber;
75-
7674
use super::*;
7775

7876
fn select_block_range_from_db(start: BlockNumber, end: BlockNumber, merkle_root: &str) -> Row {
@@ -86,13 +84,13 @@ mod tests {
8684
fn hydrate_succeed_if_valid_block_range_in_row() {
8785
// A valid block range has both bounds as multiples of block range length and the interval
8886
// size is equal to block range length.
89-
let row = select_block_range_from_db(0, BlockRange::LENGTH, "AAAA");
87+
let row = select_block_range_from_db(BlockNumber(0), BlockRange::LENGTH, "AAAA");
9088
let res = BlockRangeRootRecord::hydrate(row).expect("Expected hydrate to succeed");
9189

9290
assert_eq!(
9391
res,
9492
BlockRangeRootRecord {
95-
range: BlockRange::from_block_number(0),
93+
range: BlockRange::from_block_number(BlockNumber(0)),
9694
merkle_root: MKTreeNode::from_hex("AAAA").unwrap(),
9795
}
9896
);
@@ -102,11 +100,11 @@ mod tests {
102100
fn hydrate_fail_if_invalid_block_range_in_row() {
103101
for invalid_row in [
104102
// Start is not a multiple of block range length
105-
select_block_range_from_db(1, BlockRange::LENGTH, "AAAA"),
103+
select_block_range_from_db(BlockNumber(1), BlockRange::LENGTH, "AAAA"),
106104
// End is not a multiple of block range length
107-
select_block_range_from_db(0, BlockRange::LENGTH - 1, "AAAA"),
105+
select_block_range_from_db(BlockNumber(0), BlockRange::LENGTH - 1, "AAAA"),
108106
// Interval is not equal to block range length
109-
select_block_range_from_db(0, BlockRange::LENGTH * 4, "AAAA"),
107+
select_block_range_from_db(BlockNumber(0), BlockRange::LENGTH * 4, "AAAA"),
110108
] {
111109
let res =
112110
BlockRangeRootRecord::hydrate(invalid_row).expect_err("Expected hydrate to fail");

internal/mithril-persistence/src/database/record/cardano_transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl SqLiteEntity for CardanoTransactionRecord {
7474

7575
Ok(Self {
7676
transaction_hash: transaction_hash.to_string(),
77-
block_number,
77+
block_number: BlockNumber(block_number),
7878
slot_number,
7979
block_hash: block_hash.to_string(),
8080
})

0 commit comments

Comments
 (0)