1
- use anyhow:: anyhow;
2
- use serde:: { Deserialize , Serialize } ;
3
1
use std:: {
4
2
cmp:: Ordering ,
5
3
fmt:: { Display , Formatter , Result } ,
6
- ops:: { Deref , Range } ,
4
+ ops:: { Deref , Range , RangeInclusive } ,
7
5
} ;
8
6
7
+ use anyhow:: anyhow;
8
+ use serde:: { Deserialize , Serialize } ;
9
+
9
10
use crate :: {
10
11
crypto_helper:: { MKMapKey , MKTreeNode } ,
11
12
entities:: BlockNumber ,
@@ -59,7 +60,7 @@ impl BlockRange {
59
60
}
60
61
61
62
/// Get all [BlockRange] strictly contained in the given interval
62
- pub fn all_block_ranges_in ( interval : Range < BlockNumber > ) -> BlockRangesSequence {
63
+ pub fn all_block_ranges_in ( interval : RangeInclusive < BlockNumber > ) -> BlockRangesSequence {
63
64
BlockRangesSequence :: new ( interval)
64
65
}
65
66
@@ -158,13 +159,14 @@ impl BlockRangesSequence {
158
159
/// Build the [BlockRangesSequence] strictly contained in the given interval.
159
160
///
160
161
/// The interval bounds will be corrected to be multiples of [BlockRange::LENGTH].
161
- pub fn new ( interval : Range < BlockNumber > ) -> Self {
162
- let start = if ( interval. start % BlockRange :: LENGTH ) == 0 {
163
- interval. start
162
+ pub fn new ( interval : RangeInclusive < BlockNumber > ) -> Self {
163
+ let start = if ( interval. start ( ) % BlockRange :: LENGTH ) == 0 {
164
+ * interval. start ( )
164
165
} else {
165
- BlockRange :: start ( interval. start ) + BlockRange :: LENGTH
166
+ BlockRange :: start ( * interval. start ( ) ) + BlockRange :: LENGTH
166
167
} ;
167
- let end = BlockRange :: start ( interval. end ) ;
168
+ // End is inclusive, so we need to add 1
169
+ let end = BlockRange :: start ( * interval. end ( ) + 1 ) ;
168
170
169
171
Self { start, end }
170
172
}
@@ -284,28 +286,28 @@ mod tests {
284
286
285
287
#[ test]
286
288
fn test_block_range_all_block_ranges_in ( ) {
287
- assert_eq ! ( BlockRange :: all_block_ranges_in( 0 ..0 ) . into_vec( ) , vec![ ] ) ;
288
- assert_eq ! ( BlockRange :: all_block_ranges_in( 0 ..1 ) . into_vec( ) , vec![ ] ) ;
289
- assert_eq ! ( BlockRange :: all_block_ranges_in( 0 ..14 ) . into_vec( ) , vec![ ] ) ;
290
- assert_eq ! ( BlockRange :: all_block_ranges_in( 1 ..15 ) . into_vec( ) , vec![ ] ) ;
289
+ assert_eq ! ( BlockRange :: all_block_ranges_in( 0 ..= 0 ) . into_vec( ) , vec![ ] ) ;
290
+ assert_eq ! ( BlockRange :: all_block_ranges_in( 0 ..= 1 ) . into_vec( ) , vec![ ] ) ;
291
+ assert_eq ! ( BlockRange :: all_block_ranges_in( 0 ..= 13 ) . into_vec( ) , vec![ ] ) ;
292
+ assert_eq ! ( BlockRange :: all_block_ranges_in( 1 ..= 14 ) . into_vec( ) , vec![ ] ) ;
291
293
assert_eq ! (
292
- BlockRange :: all_block_ranges_in( 0 ..15 ) . into_vec( ) ,
294
+ BlockRange :: all_block_ranges_in( 0 ..= 14 ) . into_vec( ) ,
293
295
vec![ BlockRange :: new( 0 , 15 ) ]
294
296
) ;
295
297
assert_eq ! (
296
- BlockRange :: all_block_ranges_in( 0 ..16 ) . into_vec( ) ,
298
+ BlockRange :: all_block_ranges_in( 0 ..= 15 ) . into_vec( ) ,
297
299
vec![ BlockRange :: new( 0 , 15 ) ]
298
300
) ;
299
301
assert_eq ! (
300
- BlockRange :: all_block_ranges_in( 14 ..30 ) . into_vec( ) ,
302
+ BlockRange :: all_block_ranges_in( 14 ..= 29 ) . into_vec( ) ,
301
303
vec![ BlockRange :: new( 15 , 30 ) ]
302
304
) ;
303
305
assert_eq ! (
304
- BlockRange :: all_block_ranges_in( 14 ..31 ) . into_vec( ) ,
306
+ BlockRange :: all_block_ranges_in( 14 ..= 30 ) . into_vec( ) ,
305
307
vec![ BlockRange :: new( 15 , 30 ) ]
306
308
) ;
307
309
assert_eq ! (
308
- BlockRange :: all_block_ranges_in( 14 ..61 ) . into_vec( ) ,
310
+ BlockRange :: all_block_ranges_in( 14 ..= 60 ) . into_vec( ) ,
309
311
vec![
310
312
BlockRange :: new( 15 , 30 ) ,
311
313
BlockRange :: new( 30 , 45 ) ,
@@ -316,45 +318,45 @@ mod tests {
316
318
317
319
#[ test]
318
320
fn test_block_ranges_sequence_is_empty ( ) {
319
- assert ! ( BlockRange :: all_block_ranges_in( 0 ..0 ) . is_empty( ) ) ;
320
- assert ! ( BlockRange :: all_block_ranges_in( 0 ..1 ) . is_empty( ) ) ;
321
- assert ! ( BlockRange :: all_block_ranges_in( 0 ..14 ) . is_empty( ) ) ;
322
- assert ! ( BlockRange :: all_block_ranges_in( 1 ..15 ) . is_empty( ) ) ;
323
- assert ! ( BlockRange :: all_block_ranges_in( 0 ..15 ) . is_empty( ) . not( ) ) ;
324
- assert ! ( BlockRange :: all_block_ranges_in( 0 ..16 ) . is_empty( ) . not( ) ) ;
325
- assert ! ( BlockRange :: all_block_ranges_in( 14 ..30 ) . is_empty( ) . not( ) ) ;
326
- assert ! ( BlockRange :: all_block_ranges_in( 14 ..31 ) . is_empty( ) . not( ) ) ;
327
- assert ! ( BlockRange :: all_block_ranges_in( 14 ..61 ) . is_empty( ) . not( ) ) ;
321
+ assert ! ( BlockRange :: all_block_ranges_in( 0 ..= 0 ) . is_empty( ) ) ;
322
+ assert ! ( BlockRange :: all_block_ranges_in( 0 ..= 1 ) . is_empty( ) ) ;
323
+ assert ! ( BlockRange :: all_block_ranges_in( 0 ..= 13 ) . is_empty( ) ) ;
324
+ assert ! ( BlockRange :: all_block_ranges_in( 1 ..= 14 ) . is_empty( ) ) ;
325
+ assert ! ( BlockRange :: all_block_ranges_in( 0 ..= 14 ) . is_empty( ) . not( ) ) ;
326
+ assert ! ( BlockRange :: all_block_ranges_in( 0 ..= 15 ) . is_empty( ) . not( ) ) ;
327
+ assert ! ( BlockRange :: all_block_ranges_in( 14 ..= 29 ) . is_empty( ) . not( ) ) ;
328
+ assert ! ( BlockRange :: all_block_ranges_in( 14 ..= 30 ) . is_empty( ) . not( ) ) ;
329
+ assert ! ( BlockRange :: all_block_ranges_in( 14 ..= 60 ) . is_empty( ) . not( ) ) ;
328
330
}
329
331
330
332
#[ test]
331
333
fn test_block_ranges_sequence_len ( ) {
332
334
assert_eq ! (
333
- BlockRange :: all_block_ranges_in( 0 ..( BlockRange :: LENGTH - 1 ) ) . len( ) ,
335
+ BlockRange :: all_block_ranges_in( 0 ..= ( BlockRange :: LENGTH - 2 ) ) . len( ) ,
334
336
0
335
337
) ;
336
338
assert_eq ! (
337
- BlockRange :: all_block_ranges_in( 0 ..( BlockRange :: LENGTH ) ) . len( ) ,
339
+ BlockRange :: all_block_ranges_in( 0 ..= ( BlockRange :: LENGTH - 1 ) ) . len( ) ,
338
340
1
339
341
) ;
340
342
assert_eq ! (
341
- BlockRange :: all_block_ranges_in( 0 ..( BlockRange :: LENGTH * 15 ) ) . len( ) ,
343
+ BlockRange :: all_block_ranges_in( 0 ..= ( BlockRange :: LENGTH * 15 ) ) . len( ) ,
342
344
15
343
345
) ;
344
346
}
345
347
346
348
#[ test]
347
349
fn test_block_ranges_sequence_contains ( ) {
348
350
let block_range = BlockRange :: new ( 15 , 30 ) ;
349
- assert ! ( BlockRange :: all_block_ranges_in( 0 ..15 )
351
+ assert ! ( BlockRange :: all_block_ranges_in( 0 ..= 14 )
350
352
. contains( & block_range)
351
353
. not( ) ) ;
352
- assert ! ( BlockRange :: all_block_ranges_in( 30 ..60 )
354
+ assert ! ( BlockRange :: all_block_ranges_in( 30 ..= 59 )
353
355
. contains( & block_range)
354
356
. 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) ) ;
357
+ assert ! ( BlockRange :: all_block_ranges_in( 0 ..= 29 ) . contains( & block_range) ) ;
358
+ assert ! ( BlockRange :: all_block_ranges_in( 15 ..= 29 ) . contains( & block_range) ) ;
359
+ assert ! ( BlockRange :: all_block_ranges_in( 15 ..= 44 ) . contains( & block_range) ) ;
358
360
}
359
361
360
362
#[ test]
0 commit comments