Skip to content

Commit e949261

Browse files
Alenarsfauvel
authored andcommitted
Use new BlockNumber type in mithril-common
Only common compile, other crates still needs some work. In order to do so some more capabilities add to be added to the `BlockNumber`: * Convertion from u64: to use with 'Into<BlockNumber>' generics, should be reserved to specific cases when it's clear that the number written is a Block Number and not something magic. * PartialOrd from/to `u64`: so it can be passed to Range::contains method.
1 parent f1980ab commit e949261

31 files changed

+458
-298
lines changed

mithril-common/benches/block_range.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
22

3-
use mithril_common::entities::BlockRange;
3+
use mithril_common::entities::{BlockNumber, BlockRange};
44

55
fn all_block_ranges_in(c: &mut Criterion) {
66
let mut group = c.benchmark_group("all_block_ranges_in");
@@ -14,7 +14,7 @@ fn all_block_ranges_in(c: &mut Criterion) {
1414
BenchmarkId::from_parameter(format!("0..{end_bound}")),
1515
&end_bound,
1616
|b, &end_bound| {
17-
b.iter(|| BlockRange::all_block_ranges_in(0..=end_bound));
17+
b.iter(|| BlockRange::all_block_ranges_in(BlockNumber(0)..=end_bound));
1818
},
1919
);
2020
}

mithril-common/benches/merkle_map.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::ops::Range;
2-
31
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
42
use mithril_common::{
53
crypto_helper::{MKMap, MKMapNode, MKMapValue, MKTree, MKTreeNode},
@@ -45,7 +43,7 @@ fn generate_block_ranges_nodes_iterator(
4543
);
4644
let mk_map_node = if block_range_index <= total_block_ranges - max_uncompressed_block_ranges
4745
{
48-
let leaves = <Range<u64> as Clone>::clone(&block_range)
46+
let leaves = (*block_range.start..*block_range.end)
4947
.map(|leaf_index| leaf_index.to_string())
5048
.collect::<Vec<_>>();
5149
let merkle_tree_block_range = MKTree::new(&leaves).unwrap();

mithril-common/src/cardano_block_scanner/chain_reader_block_streamer.rs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ mod tests {
166166

167167
#[tokio::test]
168168
async fn test_parse_expected_nothing_strictly_above_block_number_threshold() {
169-
let until_block_number = 10;
169+
let until_block_number = BlockNumber(10);
170170
let chain_reader = Arc::new(Mutex::new(FakeChainReader::new(vec![
171171
ChainBlockNextAction::RollForward {
172172
parsed_block: ScannedBlock::new(
@@ -224,16 +224,16 @@ mod tests {
224224
async fn test_parse_expected_multiple_rollforwards_below_block_number_threshold() {
225225
let chain_reader = Arc::new(Mutex::new(FakeChainReader::new(vec![
226226
ChainBlockNextAction::RollForward {
227-
parsed_block: ScannedBlock::new("hash-1", 1, 10, Vec::<&str>::new()),
227+
parsed_block: ScannedBlock::new("hash-1", BlockNumber(1), 10, Vec::<&str>::new()),
228228
},
229229
ChainBlockNextAction::RollForward {
230-
parsed_block: ScannedBlock::new("hash-2", 2, 20, Vec::<&str>::new()),
230+
parsed_block: ScannedBlock::new("hash-2", BlockNumber(2), 20, Vec::<&str>::new()),
231231
},
232232
])));
233233
let mut block_streamer = ChainReaderBlockStreamer::try_new(
234234
chain_reader,
235235
None,
236-
100,
236+
BlockNumber(100),
237237
MAX_ROLL_FORWARDS_PER_POLL,
238238
TestLogger::stdout(),
239239
)
@@ -244,8 +244,8 @@ mod tests {
244244

245245
assert_eq!(
246246
Some(ChainScannedBlocks::RollForwards(vec![
247-
ScannedBlock::new("hash-1", 1, 10, Vec::<&str>::new()),
248-
ScannedBlock::new("hash-2", 2, 20, Vec::<&str>::new())
247+
ScannedBlock::new("hash-1", BlockNumber(1), 10, Vec::<&str>::new()),
248+
ScannedBlock::new("hash-2", BlockNumber(2), 20, Vec::<&str>::new())
249249
])),
250250
scanned_blocks,
251251
);
@@ -255,19 +255,19 @@ mod tests {
255255
async fn test_parse_expected_maximum_rollforwards_retrieved_per_poll() {
256256
let chain_reader = Arc::new(Mutex::new(FakeChainReader::new(vec![
257257
ChainBlockNextAction::RollForward {
258-
parsed_block: ScannedBlock::new("hash-1", 1, 10, Vec::<&str>::new()),
258+
parsed_block: ScannedBlock::new("hash-1", BlockNumber(1), 10, Vec::<&str>::new()),
259259
},
260260
ChainBlockNextAction::RollForward {
261-
parsed_block: ScannedBlock::new("hash-2", 2, 20, Vec::<&str>::new()),
261+
parsed_block: ScannedBlock::new("hash-2", BlockNumber(2), 20, Vec::<&str>::new()),
262262
},
263263
ChainBlockNextAction::RollForward {
264-
parsed_block: ScannedBlock::new("hash-3", 3, 30, Vec::<&str>::new()),
264+
parsed_block: ScannedBlock::new("hash-3", BlockNumber(3), 30, Vec::<&str>::new()),
265265
},
266266
])));
267267
let mut block_streamer = ChainReaderBlockStreamer::try_new(
268268
chain_reader,
269269
None,
270-
100,
270+
BlockNumber(100),
271271
MAX_ROLL_FORWARDS_PER_POLL,
272272
TestLogger::stdout(),
273273
)
@@ -278,8 +278,8 @@ mod tests {
278278
let scanned_blocks = block_streamer.poll_next().await.expect("poll_next failed");
279279
assert_eq!(
280280
Some(ChainScannedBlocks::RollForwards(vec![
281-
ScannedBlock::new("hash-1", 1, 10, Vec::<&str>::new()),
282-
ScannedBlock::new("hash-2", 2, 20, Vec::<&str>::new())
281+
ScannedBlock::new("hash-1", BlockNumber(1), 10, Vec::<&str>::new()),
282+
ScannedBlock::new("hash-2", BlockNumber(2), 20, Vec::<&str>::new())
283283
])),
284284
scanned_blocks,
285285
);
@@ -288,7 +288,7 @@ mod tests {
288288
assert_eq!(
289289
Some(ChainScannedBlocks::RollForwards(vec![ScannedBlock::new(
290290
"hash-3",
291-
3,
291+
BlockNumber(3),
292292
30,
293293
Vec::<&str>::new()
294294
),])),
@@ -306,8 +306,8 @@ mod tests {
306306
])));
307307
let mut block_streamer = ChainReaderBlockStreamer::try_new(
308308
chain_reader,
309-
Some(ChainPoint::new(100, 10, "hash-123")),
310-
1,
309+
Some(ChainPoint::new(100, BlockNumber(10), "hash-123")),
310+
BlockNumber(1),
311311
MAX_ROLL_FORWARDS_PER_POLL,
312312
TestLogger::stdout(),
313313
)
@@ -327,7 +327,7 @@ mod tests {
327327
let mut block_streamer = ChainReaderBlockStreamer::try_new(
328328
chain_reader,
329329
None,
330-
1,
330+
BlockNumber(1),
331331
MAX_ROLL_FORWARDS_PER_POLL,
332332
TestLogger::stdout(),
333333
)
@@ -347,20 +347,25 @@ mod tests {
347347
) {
348348
let chain_reader = Arc::new(Mutex::new(FakeChainReader::new(vec![
349349
ChainBlockNextAction::RollForward {
350-
parsed_block: ScannedBlock::new("hash-8", 80, 8, Vec::<&str>::new()),
350+
parsed_block: ScannedBlock::new("hash-8", BlockNumber(80), 8, Vec::<&str>::new()),
351351
},
352352
ChainBlockNextAction::RollForward {
353-
parsed_block: ScannedBlock::new("hash-9", 90, 9, Vec::<&str>::new()),
353+
parsed_block: ScannedBlock::new("hash-9", BlockNumber(90), 9, Vec::<&str>::new()),
354354
},
355355
ChainBlockNextAction::RollForward {
356-
parsed_block: ScannedBlock::new("hash-10", 100, 10, Vec::<&str>::new()),
356+
parsed_block: ScannedBlock::new(
357+
"hash-10",
358+
BlockNumber(100),
359+
10,
360+
Vec::<&str>::new(),
361+
),
357362
},
358363
ChainBlockNextAction::RollBackward { slot_number: 9 },
359364
])));
360365
let mut block_streamer = ChainReaderBlockStreamer::try_new(
361366
chain_reader,
362367
None,
363-
1000,
368+
BlockNumber(1000),
364369
MAX_ROLL_FORWARDS_PER_POLL,
365370
TestLogger::stdout(),
366371
)
@@ -371,8 +376,8 @@ mod tests {
371376

372377
assert_eq!(
373378
Some(ChainScannedBlocks::RollForwards(vec![
374-
ScannedBlock::new("hash-8", 80, 8, Vec::<&str>::new()),
375-
ScannedBlock::new("hash-9", 90, 9, Vec::<&str>::new())
379+
ScannedBlock::new("hash-8", BlockNumber(80), 8, Vec::<&str>::new()),
380+
ScannedBlock::new("hash-9", BlockNumber(90), 9, Vec::<&str>::new())
376381
])),
377382
scanned_blocks,
378383
);
@@ -383,17 +388,17 @@ mod tests {
383388
) {
384389
let chain_reader = Arc::new(Mutex::new(FakeChainReader::new(vec![
385390
ChainBlockNextAction::RollForward {
386-
parsed_block: ScannedBlock::new("hash-8", 80, 8, Vec::<&str>::new()),
391+
parsed_block: ScannedBlock::new("hash-8", BlockNumber(80), 8, Vec::<&str>::new()),
387392
},
388393
ChainBlockNextAction::RollForward {
389-
parsed_block: ScannedBlock::new("hash-9", 90, 9, Vec::<&str>::new()),
394+
parsed_block: ScannedBlock::new("hash-9", BlockNumber(90), 9, Vec::<&str>::new()),
390395
},
391396
ChainBlockNextAction::RollBackward { slot_number: 3 },
392397
])));
393398
let mut block_streamer = ChainReaderBlockStreamer::try_new(
394399
chain_reader,
395400
None,
396-
1000,
401+
BlockNumber(1000),
397402
MAX_ROLL_FORWARDS_PER_POLL,
398403
TestLogger::stdout(),
399404
)
@@ -411,7 +416,7 @@ mod tests {
411416
let mut block_streamer = ChainReaderBlockStreamer::try_new(
412417
chain_reader,
413418
None,
414-
1,
419+
BlockNumber(1),
415420
MAX_ROLL_FORWARDS_PER_POLL,
416421
TestLogger::stdout(),
417422
)

mithril-common/src/cardano_block_scanner/dumb_block_scanner.rs

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,12 @@ mod tests {
131131

132132
#[tokio::test]
133133
async fn polling_with_one_set_of_block_returns_some_once() {
134-
let expected_blocks = vec![ScannedBlock::new("hash-1", 1, 10, Vec::<&str>::new())];
134+
let expected_blocks = vec![ScannedBlock::new(
135+
"hash-1",
136+
BlockNumber(1),
137+
10,
138+
Vec::<&str>::new(),
139+
)];
135140
let mut streamer = DumbBlockStreamer::new().forwards(vec![expected_blocks.clone()]);
136141

137142
let blocks = streamer.poll_next().await.unwrap();
@@ -147,12 +152,22 @@ mod tests {
147152
#[tokio::test]
148153
async fn polling_with_multiple_sets_of_blocks_returns_some_once() {
149154
let expected_blocks = vec![
150-
vec![ScannedBlock::new("hash-1", 1, 10, Vec::<&str>::new())],
155+
vec![ScannedBlock::new(
156+
"hash-1",
157+
BlockNumber(1),
158+
10,
159+
Vec::<&str>::new(),
160+
)],
151161
vec![
152-
ScannedBlock::new("hash-2", 2, 11, Vec::<&str>::new()),
153-
ScannedBlock::new("hash-3", 3, 12, Vec::<&str>::new()),
162+
ScannedBlock::new("hash-2", BlockNumber(2), 11, Vec::<&str>::new()),
163+
ScannedBlock::new("hash-3", BlockNumber(3), 12, Vec::<&str>::new()),
154164
],
155-
vec![ScannedBlock::new("hash-4", 4, 13, Vec::<&str>::new())],
165+
vec![ScannedBlock::new(
166+
"hash-4",
167+
BlockNumber(4),
168+
13,
169+
Vec::<&str>::new(),
170+
)],
156171
];
157172
let mut streamer = DumbBlockStreamer::new().forwards(expected_blocks.clone());
158173

@@ -180,24 +195,34 @@ mod tests {
180195

181196
#[tokio::test]
182197
async fn dumb_scanned_construct_a_streamer_based_on_its_stored_blocks() {
183-
let expected_blocks = vec![ScannedBlock::new("hash-1", 1, 10, Vec::<&str>::new())];
198+
let expected_blocks = vec![ScannedBlock::new(
199+
"hash-1",
200+
BlockNumber(1),
201+
10,
202+
Vec::<&str>::new(),
203+
)];
184204

185205
let scanner = DumbBlockScanner::new().forwards(vec![expected_blocks.clone()]);
186-
let mut streamer = scanner.scan(None, 5).await.unwrap();
206+
let mut streamer = scanner.scan(None, BlockNumber(5)).await.unwrap();
187207

188208
let blocks = streamer.poll_all().await.unwrap();
189209
assert_eq!(blocks, expected_blocks);
190210
}
191211

192212
#[tokio::test]
193213
async fn dumb_scanned_construct_a_streamer_based_on_its_stored_chain_scanned_blocks() {
194-
let expected_blocks = vec![ScannedBlock::new("hash-1", 1, 10, Vec::<&str>::new())];
195-
let expected_chain_point = ChainPoint::new(10, 2, "block-hash");
214+
let expected_blocks = vec![ScannedBlock::new(
215+
"hash-1",
216+
BlockNumber(1),
217+
10,
218+
Vec::<&str>::new(),
219+
)];
220+
let expected_chain_point = ChainPoint::new(10, BlockNumber(2), "block-hash");
196221

197222
let scanner = DumbBlockScanner::new()
198223
.forwards(vec![expected_blocks.clone()])
199224
.backward(expected_chain_point.clone());
200-
let mut streamer = scanner.scan(None, 5).await.unwrap();
225+
let mut streamer = scanner.scan(None, BlockNumber(5)).await.unwrap();
201226

202227
let blocks = streamer.poll_next().await.unwrap();
203228
assert_eq!(
@@ -217,11 +242,21 @@ mod tests {
217242
#[tokio::test]
218243
async fn polling_with_can_return_roll_backward() {
219244
let expected_blocks = vec![
220-
vec![ScannedBlock::new("hash-1", 1, 10, Vec::<&str>::new())],
221-
vec![ScannedBlock::new("hash-4", 4, 13, Vec::<&str>::new())],
245+
vec![ScannedBlock::new(
246+
"hash-1",
247+
BlockNumber(1),
248+
10,
249+
Vec::<&str>::new(),
250+
)],
251+
vec![ScannedBlock::new(
252+
"hash-4",
253+
BlockNumber(4),
254+
13,
255+
Vec::<&str>::new(),
256+
)],
222257
];
223258

224-
let expected_chain_point = ChainPoint::new(10, 2, "block-hash");
259+
let expected_chain_point = ChainPoint::new(10, BlockNumber(2), "block-hash");
225260

226261
let mut streamer = DumbBlockStreamer::new()
227262
.forwards(expected_blocks.clone())

mithril-common/src/cardano_block_scanner/scanned_block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl ScannedBlock {
3939

4040
Self::new(
4141
multi_era_block.hash().to_string(),
42-
multi_era_block.number(),
42+
BlockNumber(multi_era_block.number()),
4343
multi_era_block.slot(),
4444
transactions,
4545
)

mithril-common/src/cardano_transactions_preloader.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@ impl CardanoTransactionsPreloader {
116116
.with_context(|| {
117117
"No chain point yielded by the chain observer, is your cardano node ready?"
118118
})?;
119-
let up_to_block_number = chain_point
120-
.block_number
121-
.saturating_sub(self.security_parameter);
119+
let up_to_block_number = chain_point.block_number - self.security_parameter;
122120
self.importer.import(up_to_block_number).await?;
123121

124122
Ok(())
@@ -154,8 +152,8 @@ mod tests {
154152

155153
#[tokio::test]
156154
async fn call_its_inner_importer_when_is_activated() {
157-
let chain_block_number = 5000;
158-
let security_parameter = 542;
155+
let chain_block_number = BlockNumber(5000);
156+
let security_parameter = BlockNumber(542);
159157
let chain_observer = FakeObserver::new(Some(TimePoint {
160158
chain_point: ChainPoint {
161159
block_number: chain_block_number,
@@ -194,7 +192,7 @@ mod tests {
194192
let preloader = CardanoTransactionsPreloader::new(
195193
Arc::new(SignedEntityTypeLock::default()),
196194
Arc::new(importer),
197-
542,
195+
BlockNumber(542),
198196
Arc::new(chain_observer),
199197
TestLogger::stdout(),
200198
Arc::new(CardanoTransactionsPreloaderActivation::new(false)),
@@ -218,7 +216,7 @@ mod tests {
218216
let preloader = CardanoTransactionsPreloader::new(
219217
Arc::new(SignedEntityTypeLock::default()),
220218
Arc::new(importer),
221-
542,
219+
BlockNumber(542),
222220
Arc::new(chain_observer),
223221
TestLogger::stdout(),
224222
Arc::new(preloader_checker),
@@ -239,7 +237,7 @@ mod tests {
239237
let preloader = CardanoTransactionsPreloader::new(
240238
Arc::new(SignedEntityTypeLock::default()),
241239
Arc::new(importer),
242-
0,
240+
BlockNumber(0),
243241
Arc::new(chain_observer),
244242
TestLogger::stdout(),
245243
Arc::new(CardanoTransactionsPreloaderActivation::new(true)),
@@ -260,7 +258,7 @@ mod tests {
260258
Arc::new(ImporterWithSignedEntityTypeLockCheck {
261259
signed_entity_type_lock: signed_entity_type_lock.clone(),
262260
}),
263-
0,
261+
BlockNumber(0),
264262
Arc::new(FakeObserver::new(Some(TimePoint::dummy()))),
265263
TestLogger::stdout(),
266264
Arc::new(CardanoTransactionsPreloaderActivation::new(true)),
@@ -291,7 +289,7 @@ mod tests {
291289
Arc::new(ImporterWithSignedEntityTypeLockCheck {
292290
signed_entity_type_lock: signed_entity_type_lock.clone(),
293291
}),
294-
0,
292+
BlockNumber(0),
295293
Arc::new(chain_observer),
296294
TestLogger::stdout(),
297295
Arc::new(CardanoTransactionsPreloaderActivation::new(true)),

0 commit comments

Comments
 (0)