Skip to content

Commit 8b4b403

Browse files
authored
pruning: enable workspace lints (#284)
pruning: enable/fix workspace lints
1 parent 6502729 commit 8b4b403

File tree

2 files changed

+56
-53
lines changed

2 files changed

+56
-53
lines changed

pruning/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ borsh = ["dep:borsh"]
1313
thiserror = { workspace = true }
1414

1515
borsh = { workspace = true, features = ["derive", "std"], optional = true }
16+
17+
[lints]
18+
workspace = true

pruning/src/lib.rs

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl PruningSeed {
7171
///
7272
/// See: [`DecompressedPruningSeed::new`]
7373
pub fn new_pruned(stripe: u32, log_stripes: u32) -> Result<Self, PruningError> {
74-
Ok(PruningSeed::Pruned(DecompressedPruningSeed::new(
74+
Ok(Self::Pruned(DecompressedPruningSeed::new(
7575
stripe,
7676
log_stripes,
7777
)?))
@@ -81,9 +81,7 @@ impl PruningSeed {
8181
///
8282
/// An error means the pruning seed was invalid.
8383
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))
8785
}
8886

8987
/// Decompresses the seed, performing the same checks as [`PruningSeed::decompress`] and some more according to
@@ -103,34 +101,34 @@ impl PruningSeed {
103101
}
104102

105103
/// Compresses this pruning seed to a u32.
106-
pub fn compress(&self) -> u32 {
104+
pub const fn compress(&self) -> u32 {
107105
match self {
108-
PruningSeed::NotPruned => 0,
109-
PruningSeed::Pruned(seed) => seed.compress(),
106+
Self::NotPruned => 0,
107+
Self::Pruned(seed) => seed.compress(),
110108
}
111109
}
112110

113111
/// 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> {
115113
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),
118116
}
119117
}
120118

121119
/// 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> {
123121
match self {
124-
PruningSeed::NotPruned => None,
125-
PruningSeed::Pruned(seed) => Some(seed.stripe),
122+
Self::NotPruned => None,
123+
Self::Pruned(seed) => Some(seed.stripe),
126124
}
127125
}
128126

129127
/// 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 {
131129
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),
134132
}
135133
}
136134

@@ -155,10 +153,8 @@ impl PruningSeed {
155153
blockchain_height: usize,
156154
) -> Result<Option<usize>, PruningError> {
157155
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)?,
162158
})
163159
}
164160

@@ -181,10 +177,8 @@ impl PruningSeed {
181177
blockchain_height: usize,
182178
) -> Result<usize, PruningError> {
183179
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)?,
188182
})
189183
}
190184
}
@@ -199,11 +193,11 @@ impl Ord for PruningSeed {
199193
fn cmp(&self, other: &Self) -> Ordering {
200194
match (self, other) {
201195
// 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,
205199

206-
(PruningSeed::Pruned(seed1), PruningSeed::Pruned(seed2)) => seed1.cmp(seed2),
200+
(Self::Pruned(seed1), Self::Pruned(seed2)) => seed1.cmp(seed2),
207201
}
208202
}
209203
}
@@ -222,7 +216,7 @@ pub struct DecompressedPruningSeed {
222216
log_stripes: u32,
223217
/// The specific portion this peer keeps.
224218
///
225-
/// *MUST* be between 1..=2^log_stripes
219+
/// *MUST* be between `1..=2^log_stripes`
226220
stripe: u32,
227221
}
228222

@@ -268,13 +262,13 @@ impl DecompressedPruningSeed {
268262
/// a valid seed you currently MUST pass in a number 1 to 8 for `stripe`
269263
/// and 3 for `log_stripes`.*
270264
///
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> {
272266
if log_stripes > PRUNING_SEED_LOG_STRIPES_MASK {
273267
Err(PruningError::LogStripesOutOfRange)
274268
} else if !(stripe > 0 && stripe <= (1 << log_stripes)) {
275269
Err(PruningError::StripeOutOfRange)
276270
} else {
277-
Ok(DecompressedPruningSeed {
271+
Ok(Self {
278272
log_stripes,
279273
stripe,
280274
})
@@ -286,7 +280,7 @@ impl DecompressedPruningSeed {
286280
/// Will return Ok(None) if the pruning seed means no pruning.
287281
///
288282
/// 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> {
290284
if seed == 0 {
291285
// No pruning.
292286
return Ok(None);
@@ -299,20 +293,20 @@ impl DecompressedPruningSeed {
299293
return Err(PruningError::StripeOutOfRange);
300294
}
301295

302-
Ok(Some(DecompressedPruningSeed {
296+
Ok(Some(Self {
303297
log_stripes,
304298
stripe,
305299
}))
306300
}
307301

308302
/// Compresses the pruning seed into a u32.
309-
pub fn compress(&self) -> u32 {
303+
pub const fn compress(&self) -> u32 {
310304
(self.log_stripes << PRUNING_SEED_LOG_STRIPES_SHIFT)
311305
| ((self.stripe - 1) << PRUNING_SEED_STRIPE_SHIFT)
312306
}
313307

314308
/// 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 {
316310
match get_block_pruning_stripe(height, blockchain_height, self.log_stripes) {
317311
Some(block_stripe) => self.stripe == block_stripe,
318312
None => true,
@@ -419,7 +413,7 @@ impl DecompressedPruningSeed {
419413
// We can get the end of our "non-pruning" cycle by getting the next stripe's first un-pruned block height.
420414
// So we calculate the next un-pruned block for the next stripe and return it as our next pruned block
421415
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)
423417
.expect("We just made sure this stripe is in range for this log_stripe");
424418

425419
let calculated_height = seed.get_next_unpruned_block(block_height, blockchain_height)?;
@@ -433,17 +427,22 @@ impl DecompressedPruningSeed {
433427
}
434428
}
435429

436-
fn get_block_pruning_stripe(
430+
const fn get_block_pruning_stripe(
437431
block_height: usize,
438432
blockchain_height: usize,
439433
log_stripe: u32,
440434
) -> Option<u32> {
441435
if block_height + CRYPTONOTE_PRUNING_TIP_BLOCKS >= blockchain_height {
442436
None
443437
} 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+
)]
444443
Some(
445444
(((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,
447446
)
448447
}
449448
}
@@ -483,16 +482,17 @@ mod tests {
483482
#[test]
484483
fn get_pruning_log_stripe() {
485484
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);
488487
}
489488
}
490489

491490
#[test]
492491
fn get_pruning_stripe() {
493492
let all_valid_seeds = make_all_pruning_seeds();
493+
#[expect(clippy::cast_possible_truncation)]
494494
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);
496496
}
497497
}
498498

@@ -554,31 +554,31 @@ mod tests {
554554
assert_eq!(
555555
seed.get_next_unpruned_block(0, blockchain_height).unwrap(),
556556
i * 4096
557-
)
557+
);
558558
}
559559

560560
for (i, seed) in all_valid_seeds.iter().enumerate() {
561561
assert_eq!(
562562
seed.get_next_unpruned_block((i + 1) * 4096, blockchain_height)
563563
.unwrap(),
564564
i * 4096 + 32768
565-
)
565+
);
566566
}
567567

568568
for (i, seed) in all_valid_seeds.iter().enumerate() {
569569
assert_eq!(
570570
seed.get_next_unpruned_block((i + 8) * 4096, blockchain_height)
571571
.unwrap(),
572572
i * 4096 + 32768
573-
)
573+
);
574574
}
575575

576-
for seed in all_valid_seeds.iter() {
576+
for seed in &all_valid_seeds {
577577
assert_eq!(
578578
seed.get_next_unpruned_block(76437863 - 1, blockchain_height)
579579
.unwrap(),
580580
76437863 - 1
581-
)
581+
);
582582
}
583583

584584
let zero_seed = PruningSeed::NotPruned;
@@ -591,7 +591,7 @@ mod tests {
591591
let seed = PruningSeed::decompress(384).unwrap();
592592

593593
// 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);
595595
}
596596

597597
#[test]
@@ -605,7 +605,7 @@ mod tests {
605605
.unwrap()
606606
.unwrap(),
607607
0
608-
)
608+
);
609609
}
610610

611611
for (i, seed) in all_valid_seeds.iter().enumerate() {
@@ -614,7 +614,7 @@ mod tests {
614614
.unwrap()
615615
.unwrap(),
616616
(i + 1) * 4096
617-
)
617+
);
618618
}
619619

620620
for (i, seed) in all_valid_seeds.iter().enumerate() {
@@ -623,15 +623,15 @@ mod tests {
623623
.unwrap()
624624
.unwrap(),
625625
(i + 9) * 4096
626-
)
626+
);
627627
}
628628

629-
for seed in all_valid_seeds.iter() {
629+
for seed in &all_valid_seeds {
630630
assert_eq!(
631631
seed.get_next_pruned_block(76437863 - 1, blockchain_height)
632632
.unwrap(),
633633
None
634-
)
634+
);
635635
}
636636

637637
let zero_seed = PruningSeed::NotPruned;
@@ -644,6 +644,6 @@ mod tests {
644644
let seed = PruningSeed::decompress(384).unwrap();
645645

646646
// 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);
648648
}
649649
}

0 commit comments

Comments
 (0)