@@ -71,7 +71,7 @@ impl PruningSeed {
71
71
///
72
72
/// See: [`DecompressedPruningSeed::new`]
73
73
pub fn new_pruned ( stripe : u32 , log_stripes : u32 ) -> Result < Self , PruningError > {
74
- Ok ( PruningSeed :: Pruned ( DecompressedPruningSeed :: new (
74
+ Ok ( Self :: Pruned ( DecompressedPruningSeed :: new (
75
75
stripe,
76
76
log_stripes,
77
77
) ?) )
@@ -81,9 +81,7 @@ impl PruningSeed {
81
81
///
82
82
/// An error means the pruning seed was invalid.
83
83
pub fn decompress ( seed : u32 ) -> Result < Self , PruningError > {
84
- Ok ( DecompressedPruningSeed :: decompress ( seed) ?
85
- . map ( PruningSeed :: Pruned )
86
- . unwrap_or ( PruningSeed :: NotPruned ) )
84
+ Ok ( DecompressedPruningSeed :: decompress ( seed) ?. map_or ( Self :: NotPruned , Self :: Pruned ) )
87
85
}
88
86
89
87
/// Decompresses the seed, performing the same checks as [`PruningSeed::decompress`] and some more according to
@@ -103,34 +101,34 @@ impl PruningSeed {
103
101
}
104
102
105
103
/// Compresses this pruning seed to a u32.
106
- pub fn compress ( & self ) -> u32 {
104
+ pub const fn compress ( & self ) -> u32 {
107
105
match self {
108
- PruningSeed :: NotPruned => 0 ,
109
- PruningSeed :: Pruned ( seed) => seed. compress ( ) ,
106
+ Self :: NotPruned => 0 ,
107
+ Self :: Pruned ( seed) => seed. compress ( ) ,
110
108
}
111
109
}
112
110
113
111
/// Returns the `log_stripes` for this seed, if this seed is pruned otherwise [`None`] is returned.
114
- pub fn get_log_stripes ( & self ) -> Option < u32 > {
112
+ pub const fn get_log_stripes ( & self ) -> Option < u32 > {
115
113
match self {
116
- PruningSeed :: NotPruned => None ,
117
- PruningSeed :: Pruned ( seed) => Some ( seed. log_stripes ) ,
114
+ Self :: NotPruned => None ,
115
+ Self :: Pruned ( seed) => Some ( seed. log_stripes ) ,
118
116
}
119
117
}
120
118
121
119
/// Returns the `stripe` for this seed, if this seed is pruned otherwise [`None`] is returned.
122
- pub fn get_stripe ( & self ) -> Option < u32 > {
120
+ pub const fn get_stripe ( & self ) -> Option < u32 > {
123
121
match self {
124
- PruningSeed :: NotPruned => None ,
125
- PruningSeed :: Pruned ( seed) => Some ( seed. stripe ) ,
122
+ Self :: NotPruned => None ,
123
+ Self :: Pruned ( seed) => Some ( seed. stripe ) ,
126
124
}
127
125
}
128
126
129
127
/// Returns `true` if a peer with this pruning seed should have a non-pruned version of a block.
130
- pub fn has_full_block ( & self , height : usize , blockchain_height : usize ) -> bool {
128
+ pub const fn has_full_block ( & self , height : usize , blockchain_height : usize ) -> bool {
131
129
match self {
132
- PruningSeed :: NotPruned => true ,
133
- PruningSeed :: Pruned ( seed) => seed. has_full_block ( height, blockchain_height) ,
130
+ Self :: NotPruned => true ,
131
+ Self :: Pruned ( seed) => seed. has_full_block ( height, blockchain_height) ,
134
132
}
135
133
}
136
134
@@ -155,10 +153,8 @@ impl PruningSeed {
155
153
blockchain_height : usize ,
156
154
) -> Result < Option < usize > , PruningError > {
157
155
Ok ( match self {
158
- PruningSeed :: NotPruned => None ,
159
- PruningSeed :: Pruned ( seed) => {
160
- seed. get_next_pruned_block ( block_height, blockchain_height) ?
161
- }
156
+ Self :: NotPruned => None ,
157
+ Self :: Pruned ( seed) => seed. get_next_pruned_block ( block_height, blockchain_height) ?,
162
158
} )
163
159
}
164
160
@@ -181,10 +177,8 @@ impl PruningSeed {
181
177
blockchain_height : usize ,
182
178
) -> Result < usize , PruningError > {
183
179
Ok ( match self {
184
- PruningSeed :: NotPruned => block_height,
185
- PruningSeed :: Pruned ( seed) => {
186
- seed. get_next_unpruned_block ( block_height, blockchain_height) ?
187
- }
180
+ Self :: NotPruned => block_height,
181
+ Self :: Pruned ( seed) => seed. get_next_unpruned_block ( block_height, blockchain_height) ?,
188
182
} )
189
183
}
190
184
}
@@ -199,11 +193,11 @@ impl Ord for PruningSeed {
199
193
fn cmp ( & self , other : & Self ) -> Ordering {
200
194
match ( self , other) {
201
195
// Make sure pruning seeds storing more blocks are greater.
202
- ( PruningSeed :: NotPruned , PruningSeed :: NotPruned ) => Ordering :: Equal ,
203
- ( PruningSeed :: NotPruned , PruningSeed :: Pruned ( _) ) => Ordering :: Greater ,
204
- ( PruningSeed :: Pruned ( _) , PruningSeed :: NotPruned ) => Ordering :: Less ,
196
+ ( Self :: NotPruned , Self :: NotPruned ) => Ordering :: Equal ,
197
+ ( Self :: NotPruned , Self :: Pruned ( _) ) => Ordering :: Greater ,
198
+ ( Self :: Pruned ( _) , Self :: NotPruned ) => Ordering :: Less ,
205
199
206
- ( PruningSeed :: Pruned ( seed1) , PruningSeed :: Pruned ( seed2) ) => seed1. cmp ( seed2) ,
200
+ ( Self :: Pruned ( seed1) , Self :: Pruned ( seed2) ) => seed1. cmp ( seed2) ,
207
201
}
208
202
}
209
203
}
@@ -222,7 +216,7 @@ pub struct DecompressedPruningSeed {
222
216
log_stripes : u32 ,
223
217
/// The specific portion this peer keeps.
224
218
///
225
- /// *MUST* be between 1..=2^log_stripes
219
+ /// *MUST* be between ` 1..=2^log_stripes`
226
220
stripe : u32 ,
227
221
}
228
222
@@ -268,13 +262,13 @@ impl DecompressedPruningSeed {
268
262
/// a valid seed you currently MUST pass in a number 1 to 8 for `stripe`
269
263
/// and 3 for `log_stripes`.*
270
264
///
271
- pub fn new ( stripe : u32 , log_stripes : u32 ) -> Result < Self , PruningError > {
265
+ pub const fn new ( stripe : u32 , log_stripes : u32 ) -> Result < Self , PruningError > {
272
266
if log_stripes > PRUNING_SEED_LOG_STRIPES_MASK {
273
267
Err ( PruningError :: LogStripesOutOfRange )
274
268
} else if !( stripe > 0 && stripe <= ( 1 << log_stripes) ) {
275
269
Err ( PruningError :: StripeOutOfRange )
276
270
} else {
277
- Ok ( DecompressedPruningSeed {
271
+ Ok ( Self {
278
272
log_stripes,
279
273
stripe,
280
274
} )
@@ -286,7 +280,7 @@ impl DecompressedPruningSeed {
286
280
/// Will return Ok(None) if the pruning seed means no pruning.
287
281
///
288
282
/// An error means the pruning seed was invalid.
289
- pub fn decompress ( seed : u32 ) -> Result < Option < Self > , PruningError > {
283
+ pub const fn decompress ( seed : u32 ) -> Result < Option < Self > , PruningError > {
290
284
if seed == 0 {
291
285
// No pruning.
292
286
return Ok ( None ) ;
@@ -299,20 +293,20 @@ impl DecompressedPruningSeed {
299
293
return Err ( PruningError :: StripeOutOfRange ) ;
300
294
}
301
295
302
- Ok ( Some ( DecompressedPruningSeed {
296
+ Ok ( Some ( Self {
303
297
log_stripes,
304
298
stripe,
305
299
} ) )
306
300
}
307
301
308
302
/// Compresses the pruning seed into a u32.
309
- pub fn compress ( & self ) -> u32 {
303
+ pub const fn compress ( & self ) -> u32 {
310
304
( self . log_stripes << PRUNING_SEED_LOG_STRIPES_SHIFT )
311
305
| ( ( self . stripe - 1 ) << PRUNING_SEED_STRIPE_SHIFT )
312
306
}
313
307
314
308
/// Returns `true` if a peer with this pruning seed should have a non-pruned version of a block.
315
- pub fn has_full_block ( & self , height : usize , blockchain_height : usize ) -> bool {
309
+ pub const fn has_full_block ( & self , height : usize , blockchain_height : usize ) -> bool {
316
310
match get_block_pruning_stripe ( height, blockchain_height, self . log_stripes ) {
317
311
Some ( block_stripe) => self . stripe == block_stripe,
318
312
None => true ,
@@ -419,7 +413,7 @@ impl DecompressedPruningSeed {
419
413
// We can get the end of our "non-pruning" cycle by getting the next stripe's first un-pruned block height.
420
414
// So we calculate the next un-pruned block for the next stripe and return it as our next pruned block
421
415
let next_stripe = 1 + ( self . stripe & ( ( 1 << self . log_stripes ) - 1 ) ) ;
422
- let seed = DecompressedPruningSeed :: new ( next_stripe, self . log_stripes )
416
+ let seed = Self :: new ( next_stripe, self . log_stripes )
423
417
. expect ( "We just made sure this stripe is in range for this log_stripe" ) ;
424
418
425
419
let calculated_height = seed. get_next_unpruned_block ( block_height, blockchain_height) ?;
@@ -433,17 +427,22 @@ impl DecompressedPruningSeed {
433
427
}
434
428
}
435
429
436
- fn get_block_pruning_stripe (
430
+ const fn get_block_pruning_stripe (
437
431
block_height : usize ,
438
432
blockchain_height : usize ,
439
433
log_stripe : u32 ,
440
434
) -> Option < u32 > {
441
435
if block_height + CRYPTONOTE_PRUNING_TIP_BLOCKS >= blockchain_height {
442
436
None
443
437
} else {
438
+ #[ expect(
439
+ clippy:: cast_possible_truncation,
440
+ clippy:: cast_sign_loss,
441
+ reason = "it's trivial to prove it's ok to us `as` here"
442
+ ) ]
444
443
Some (
445
444
( ( ( block_height / CRYPTONOTE_PRUNING_STRIPE_SIZE ) & ( ( 1 << log_stripe) as usize - 1 ) )
446
- + 1 ) as u32 , // it's trivial to prove it's ok to us `as` here
445
+ + 1 ) as u32 ,
447
446
)
448
447
}
449
448
}
@@ -483,16 +482,17 @@ mod tests {
483
482
#[ test]
484
483
fn get_pruning_log_stripe ( ) {
485
484
let all_valid_seeds = make_all_pruning_seeds ( ) ;
486
- for seed in all_valid_seeds. iter ( ) {
487
- assert_eq ! ( seed. get_log_stripes( ) . unwrap( ) , 3 )
485
+ for seed in & all_valid_seeds {
486
+ assert_eq ! ( seed. get_log_stripes( ) . unwrap( ) , 3 ) ;
488
487
}
489
488
}
490
489
491
490
#[ test]
492
491
fn get_pruning_stripe ( ) {
493
492
let all_valid_seeds = make_all_pruning_seeds ( ) ;
493
+ #[ expect( clippy:: cast_possible_truncation) ]
494
494
for ( i, seed) in all_valid_seeds. iter ( ) . enumerate ( ) {
495
- assert_eq ! ( seed. get_stripe( ) . unwrap( ) , i as u32 + 1 )
495
+ assert_eq ! ( seed. get_stripe( ) . unwrap( ) , i as u32 + 1 ) ;
496
496
}
497
497
}
498
498
@@ -554,31 +554,31 @@ mod tests {
554
554
assert_eq ! (
555
555
seed. get_next_unpruned_block( 0 , blockchain_height) . unwrap( ) ,
556
556
i * 4096
557
- )
557
+ ) ;
558
558
}
559
559
560
560
for ( i, seed) in all_valid_seeds. iter ( ) . enumerate ( ) {
561
561
assert_eq ! (
562
562
seed. get_next_unpruned_block( ( i + 1 ) * 4096 , blockchain_height)
563
563
. unwrap( ) ,
564
564
i * 4096 + 32768
565
- )
565
+ ) ;
566
566
}
567
567
568
568
for ( i, seed) in all_valid_seeds. iter ( ) . enumerate ( ) {
569
569
assert_eq ! (
570
570
seed. get_next_unpruned_block( ( i + 8 ) * 4096 , blockchain_height)
571
571
. unwrap( ) ,
572
572
i * 4096 + 32768
573
- )
573
+ ) ;
574
574
}
575
575
576
- for seed in all_valid_seeds. iter ( ) {
576
+ for seed in & all_valid_seeds {
577
577
assert_eq ! (
578
578
seed. get_next_unpruned_block( 76437863 - 1 , blockchain_height)
579
579
. unwrap( ) ,
580
580
76437863 - 1
581
- )
581
+ ) ;
582
582
}
583
583
584
584
let zero_seed = PruningSeed :: NotPruned ;
@@ -591,7 +591,7 @@ mod tests {
591
591
let seed = PruningSeed :: decompress ( 384 ) . unwrap ( ) ;
592
592
593
593
// the next unpruned block is the first tip block
594
- assert_eq ! ( seed. get_next_unpruned_block( 5000 , 11000 ) . unwrap( ) , 5500 )
594
+ assert_eq ! ( seed. get_next_unpruned_block( 5000 , 11000 ) . unwrap( ) , 5500 ) ;
595
595
}
596
596
597
597
#[ test]
@@ -605,7 +605,7 @@ mod tests {
605
605
. unwrap( )
606
606
. unwrap( ) ,
607
607
0
608
- )
608
+ ) ;
609
609
}
610
610
611
611
for ( i, seed) in all_valid_seeds. iter ( ) . enumerate ( ) {
@@ -614,7 +614,7 @@ mod tests {
614
614
. unwrap( )
615
615
. unwrap( ) ,
616
616
( i + 1 ) * 4096
617
- )
617
+ ) ;
618
618
}
619
619
620
620
for ( i, seed) in all_valid_seeds. iter ( ) . enumerate ( ) {
@@ -623,15 +623,15 @@ mod tests {
623
623
. unwrap( )
624
624
. unwrap( ) ,
625
625
( i + 9 ) * 4096
626
- )
626
+ ) ;
627
627
}
628
628
629
- for seed in all_valid_seeds. iter ( ) {
629
+ for seed in & all_valid_seeds {
630
630
assert_eq ! (
631
631
seed. get_next_pruned_block( 76437863 - 1 , blockchain_height)
632
632
. unwrap( ) ,
633
633
None
634
- )
634
+ ) ;
635
635
}
636
636
637
637
let zero_seed = PruningSeed :: NotPruned ;
@@ -644,6 +644,6 @@ mod tests {
644
644
let seed = PruningSeed :: decompress ( 384 ) . unwrap ( ) ;
645
645
646
646
// there is no next pruned block
647
- assert_eq ! ( seed. get_next_pruned_block( 5000 , 10000 ) . unwrap( ) , None )
647
+ assert_eq ! ( seed. get_next_pruned_block( 5000 , 10000 ) . unwrap( ) , None ) ;
648
648
}
649
649
}
0 commit comments