pruning: enable workspace lints (#284)

pruning: enable/fix workspace lints
This commit is contained in:
hinto-janai 2024-09-18 17:44:23 -04:00 committed by GitHub
parent 6502729d8c
commit 8b4b403c5c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 56 additions and 53 deletions

View file

@ -13,3 +13,6 @@ borsh = ["dep:borsh"]
thiserror = { workspace = true } thiserror = { workspace = true }
borsh = { workspace = true, features = ["derive", "std"], optional = true } borsh = { workspace = true, features = ["derive", "std"], optional = true }
[lints]
workspace = true

View file

@ -71,7 +71,7 @@ impl PruningSeed {
/// ///
/// See: [`DecompressedPruningSeed::new`] /// See: [`DecompressedPruningSeed::new`]
pub fn new_pruned(stripe: u32, log_stripes: u32) -> Result<Self, PruningError> { pub fn new_pruned(stripe: u32, log_stripes: u32) -> Result<Self, PruningError> {
Ok(PruningSeed::Pruned(DecompressedPruningSeed::new( Ok(Self::Pruned(DecompressedPruningSeed::new(
stripe, stripe,
log_stripes, log_stripes,
)?)) )?))
@ -81,9 +81,7 @@ impl PruningSeed {
/// ///
/// An error means the pruning seed was invalid. /// An error means the pruning seed was invalid.
pub fn decompress(seed: u32) -> Result<Self, PruningError> { pub fn decompress(seed: u32) -> Result<Self, PruningError> {
Ok(DecompressedPruningSeed::decompress(seed)? Ok(DecompressedPruningSeed::decompress(seed)?.map_or(Self::NotPruned, Self::Pruned))
.map(PruningSeed::Pruned)
.unwrap_or(PruningSeed::NotPruned))
} }
/// Decompresses the seed, performing the same checks as [`PruningSeed::decompress`] and some more according to /// Decompresses the seed, performing the same checks as [`PruningSeed::decompress`] and some more according to
@ -103,34 +101,34 @@ impl PruningSeed {
} }
/// Compresses this pruning seed to a u32. /// Compresses this pruning seed to a u32.
pub fn compress(&self) -> u32 { pub const fn compress(&self) -> u32 {
match self { match self {
PruningSeed::NotPruned => 0, Self::NotPruned => 0,
PruningSeed::Pruned(seed) => seed.compress(), Self::Pruned(seed) => seed.compress(),
} }
} }
/// Returns the `log_stripes` for this seed, if this seed is pruned otherwise [`None`] is returned. /// Returns the `log_stripes` for this seed, if this seed is pruned otherwise [`None`] is returned.
pub fn get_log_stripes(&self) -> Option<u32> { pub const fn get_log_stripes(&self) -> Option<u32> {
match self { match self {
PruningSeed::NotPruned => None, Self::NotPruned => None,
PruningSeed::Pruned(seed) => Some(seed.log_stripes), Self::Pruned(seed) => Some(seed.log_stripes),
} }
} }
/// Returns the `stripe` for this seed, if this seed is pruned otherwise [`None`] is returned. /// Returns the `stripe` for this seed, if this seed is pruned otherwise [`None`] is returned.
pub fn get_stripe(&self) -> Option<u32> { pub const fn get_stripe(&self) -> Option<u32> {
match self { match self {
PruningSeed::NotPruned => None, Self::NotPruned => None,
PruningSeed::Pruned(seed) => Some(seed.stripe), Self::Pruned(seed) => Some(seed.stripe),
} }
} }
/// Returns `true` if a peer with this pruning seed should have a non-pruned version of a block. /// Returns `true` if a peer with this pruning seed should have a non-pruned version of a block.
pub fn has_full_block(&self, height: usize, blockchain_height: usize) -> bool { pub const fn has_full_block(&self, height: usize, blockchain_height: usize) -> bool {
match self { match self {
PruningSeed::NotPruned => true, Self::NotPruned => true,
PruningSeed::Pruned(seed) => seed.has_full_block(height, blockchain_height), Self::Pruned(seed) => seed.has_full_block(height, blockchain_height),
} }
} }
@ -155,10 +153,8 @@ impl PruningSeed {
blockchain_height: usize, blockchain_height: usize,
) -> Result<Option<usize>, PruningError> { ) -> Result<Option<usize>, PruningError> {
Ok(match self { Ok(match self {
PruningSeed::NotPruned => None, Self::NotPruned => None,
PruningSeed::Pruned(seed) => { Self::Pruned(seed) => seed.get_next_pruned_block(block_height, blockchain_height)?,
seed.get_next_pruned_block(block_height, blockchain_height)?
}
}) })
} }
@ -181,10 +177,8 @@ impl PruningSeed {
blockchain_height: usize, blockchain_height: usize,
) -> Result<usize, PruningError> { ) -> Result<usize, PruningError> {
Ok(match self { Ok(match self {
PruningSeed::NotPruned => block_height, Self::NotPruned => block_height,
PruningSeed::Pruned(seed) => { Self::Pruned(seed) => seed.get_next_unpruned_block(block_height, blockchain_height)?,
seed.get_next_unpruned_block(block_height, blockchain_height)?
}
}) })
} }
} }
@ -199,11 +193,11 @@ impl Ord for PruningSeed {
fn cmp(&self, other: &Self) -> Ordering { fn cmp(&self, other: &Self) -> Ordering {
match (self, other) { match (self, other) {
// Make sure pruning seeds storing more blocks are greater. // Make sure pruning seeds storing more blocks are greater.
(PruningSeed::NotPruned, PruningSeed::NotPruned) => Ordering::Equal, (Self::NotPruned, Self::NotPruned) => Ordering::Equal,
(PruningSeed::NotPruned, PruningSeed::Pruned(_)) => Ordering::Greater, (Self::NotPruned, Self::Pruned(_)) => Ordering::Greater,
(PruningSeed::Pruned(_), PruningSeed::NotPruned) => Ordering::Less, (Self::Pruned(_), Self::NotPruned) => Ordering::Less,
(PruningSeed::Pruned(seed1), PruningSeed::Pruned(seed2)) => seed1.cmp(seed2), (Self::Pruned(seed1), Self::Pruned(seed2)) => seed1.cmp(seed2),
} }
} }
} }
@ -222,7 +216,7 @@ pub struct DecompressedPruningSeed {
log_stripes: u32, log_stripes: u32,
/// The specific portion this peer keeps. /// The specific portion this peer keeps.
/// ///
/// *MUST* be between 1..=2^log_stripes /// *MUST* be between `1..=2^log_stripes`
stripe: u32, stripe: u32,
} }
@ -268,13 +262,13 @@ impl DecompressedPruningSeed {
/// a valid seed you currently MUST pass in a number 1 to 8 for `stripe` /// a valid seed you currently MUST pass in a number 1 to 8 for `stripe`
/// and 3 for `log_stripes`.* /// and 3 for `log_stripes`.*
/// ///
pub fn new(stripe: u32, log_stripes: u32) -> Result<Self, PruningError> { pub const fn new(stripe: u32, log_stripes: u32) -> Result<Self, PruningError> {
if log_stripes > PRUNING_SEED_LOG_STRIPES_MASK { if log_stripes > PRUNING_SEED_LOG_STRIPES_MASK {
Err(PruningError::LogStripesOutOfRange) Err(PruningError::LogStripesOutOfRange)
} else if !(stripe > 0 && stripe <= (1 << log_stripes)) { } else if !(stripe > 0 && stripe <= (1 << log_stripes)) {
Err(PruningError::StripeOutOfRange) Err(PruningError::StripeOutOfRange)
} else { } else {
Ok(DecompressedPruningSeed { Ok(Self {
log_stripes, log_stripes,
stripe, stripe,
}) })
@ -286,7 +280,7 @@ impl DecompressedPruningSeed {
/// Will return Ok(None) if the pruning seed means no pruning. /// Will return Ok(None) if the pruning seed means no pruning.
/// ///
/// An error means the pruning seed was invalid. /// An error means the pruning seed was invalid.
pub fn decompress(seed: u32) -> Result<Option<Self>, PruningError> { pub const fn decompress(seed: u32) -> Result<Option<Self>, PruningError> {
if seed == 0 { if seed == 0 {
// No pruning. // No pruning.
return Ok(None); return Ok(None);
@ -299,20 +293,20 @@ impl DecompressedPruningSeed {
return Err(PruningError::StripeOutOfRange); return Err(PruningError::StripeOutOfRange);
} }
Ok(Some(DecompressedPruningSeed { Ok(Some(Self {
log_stripes, log_stripes,
stripe, stripe,
})) }))
} }
/// Compresses the pruning seed into a u32. /// Compresses the pruning seed into a u32.
pub fn compress(&self) -> u32 { pub const fn compress(&self) -> u32 {
(self.log_stripes << PRUNING_SEED_LOG_STRIPES_SHIFT) (self.log_stripes << PRUNING_SEED_LOG_STRIPES_SHIFT)
| ((self.stripe - 1) << PRUNING_SEED_STRIPE_SHIFT) | ((self.stripe - 1) << PRUNING_SEED_STRIPE_SHIFT)
} }
/// Returns `true` if a peer with this pruning seed should have a non-pruned version of a block. /// Returns `true` if a peer with this pruning seed should have a non-pruned version of a block.
pub fn has_full_block(&self, height: usize, blockchain_height: usize) -> bool { pub const fn has_full_block(&self, height: usize, blockchain_height: usize) -> bool {
match get_block_pruning_stripe(height, blockchain_height, self.log_stripes) { match get_block_pruning_stripe(height, blockchain_height, self.log_stripes) {
Some(block_stripe) => self.stripe == block_stripe, Some(block_stripe) => self.stripe == block_stripe,
None => true, None => true,
@ -419,7 +413,7 @@ impl DecompressedPruningSeed {
// We can get the end of our "non-pruning" cycle by getting the next stripe's first un-pruned block height. // We can get the end of our "non-pruning" cycle by getting the next stripe's first un-pruned block height.
// So we calculate the next un-pruned block for the next stripe and return it as our next pruned block // So we calculate the next un-pruned block for the next stripe and return it as our next pruned block
let next_stripe = 1 + (self.stripe & ((1 << self.log_stripes) - 1)); let next_stripe = 1 + (self.stripe & ((1 << self.log_stripes) - 1));
let seed = DecompressedPruningSeed::new(next_stripe, self.log_stripes) let seed = Self::new(next_stripe, self.log_stripes)
.expect("We just made sure this stripe is in range for this log_stripe"); .expect("We just made sure this stripe is in range for this log_stripe");
let calculated_height = seed.get_next_unpruned_block(block_height, blockchain_height)?; let calculated_height = seed.get_next_unpruned_block(block_height, blockchain_height)?;
@ -433,7 +427,7 @@ impl DecompressedPruningSeed {
} }
} }
fn get_block_pruning_stripe( const fn get_block_pruning_stripe(
block_height: usize, block_height: usize,
blockchain_height: usize, blockchain_height: usize,
log_stripe: u32, log_stripe: u32,
@ -441,9 +435,14 @@ fn get_block_pruning_stripe(
if block_height + CRYPTONOTE_PRUNING_TIP_BLOCKS >= blockchain_height { if block_height + CRYPTONOTE_PRUNING_TIP_BLOCKS >= blockchain_height {
None None
} else { } else {
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "it's trivial to prove it's ok to us `as` here"
)]
Some( Some(
(((block_height / CRYPTONOTE_PRUNING_STRIPE_SIZE) & ((1 << log_stripe) as usize - 1)) (((block_height / CRYPTONOTE_PRUNING_STRIPE_SIZE) & ((1 << log_stripe) as usize - 1))
+ 1) as u32, // it's trivial to prove it's ok to us `as` here + 1) as u32,
) )
} }
} }
@ -483,16 +482,17 @@ mod tests {
#[test] #[test]
fn get_pruning_log_stripe() { fn get_pruning_log_stripe() {
let all_valid_seeds = make_all_pruning_seeds(); let all_valid_seeds = make_all_pruning_seeds();
for seed in all_valid_seeds.iter() { for seed in &all_valid_seeds {
assert_eq!(seed.get_log_stripes().unwrap(), 3) assert_eq!(seed.get_log_stripes().unwrap(), 3);
} }
} }
#[test] #[test]
fn get_pruning_stripe() { fn get_pruning_stripe() {
let all_valid_seeds = make_all_pruning_seeds(); let all_valid_seeds = make_all_pruning_seeds();
#[expect(clippy::cast_possible_truncation)]
for (i, seed) in all_valid_seeds.iter().enumerate() { for (i, seed) in all_valid_seeds.iter().enumerate() {
assert_eq!(seed.get_stripe().unwrap(), i as u32 + 1) assert_eq!(seed.get_stripe().unwrap(), i as u32 + 1);
} }
} }
@ -554,7 +554,7 @@ mod tests {
assert_eq!( assert_eq!(
seed.get_next_unpruned_block(0, blockchain_height).unwrap(), seed.get_next_unpruned_block(0, blockchain_height).unwrap(),
i * 4096 i * 4096
) );
} }
for (i, seed) in all_valid_seeds.iter().enumerate() { for (i, seed) in all_valid_seeds.iter().enumerate() {
@ -562,7 +562,7 @@ mod tests {
seed.get_next_unpruned_block((i + 1) * 4096, blockchain_height) seed.get_next_unpruned_block((i + 1) * 4096, blockchain_height)
.unwrap(), .unwrap(),
i * 4096 + 32768 i * 4096 + 32768
) );
} }
for (i, seed) in all_valid_seeds.iter().enumerate() { for (i, seed) in all_valid_seeds.iter().enumerate() {
@ -570,15 +570,15 @@ mod tests {
seed.get_next_unpruned_block((i + 8) * 4096, blockchain_height) seed.get_next_unpruned_block((i + 8) * 4096, blockchain_height)
.unwrap(), .unwrap(),
i * 4096 + 32768 i * 4096 + 32768
) );
} }
for seed in all_valid_seeds.iter() { for seed in &all_valid_seeds {
assert_eq!( assert_eq!(
seed.get_next_unpruned_block(76437863 - 1, blockchain_height) seed.get_next_unpruned_block(76437863 - 1, blockchain_height)
.unwrap(), .unwrap(),
76437863 - 1 76437863 - 1
) );
} }
let zero_seed = PruningSeed::NotPruned; let zero_seed = PruningSeed::NotPruned;
@ -591,7 +591,7 @@ mod tests {
let seed = PruningSeed::decompress(384).unwrap(); let seed = PruningSeed::decompress(384).unwrap();
// the next unpruned block is the first tip block // the next unpruned block is the first tip block
assert_eq!(seed.get_next_unpruned_block(5000, 11000).unwrap(), 5500) assert_eq!(seed.get_next_unpruned_block(5000, 11000).unwrap(), 5500);
} }
#[test] #[test]
@ -605,7 +605,7 @@ mod tests {
.unwrap() .unwrap()
.unwrap(), .unwrap(),
0 0
) );
} }
for (i, seed) in all_valid_seeds.iter().enumerate() { for (i, seed) in all_valid_seeds.iter().enumerate() {
@ -614,7 +614,7 @@ mod tests {
.unwrap() .unwrap()
.unwrap(), .unwrap(),
(i + 1) * 4096 (i + 1) * 4096
) );
} }
for (i, seed) in all_valid_seeds.iter().enumerate() { for (i, seed) in all_valid_seeds.iter().enumerate() {
@ -623,15 +623,15 @@ mod tests {
.unwrap() .unwrap()
.unwrap(), .unwrap(),
(i + 9) * 4096 (i + 9) * 4096
) );
} }
for seed in all_valid_seeds.iter() { for seed in &all_valid_seeds {
assert_eq!( assert_eq!(
seed.get_next_pruned_block(76437863 - 1, blockchain_height) seed.get_next_pruned_block(76437863 - 1, blockchain_height)
.unwrap(), .unwrap(),
None None
) );
} }
let zero_seed = PruningSeed::NotPruned; let zero_seed = PruningSeed::NotPruned;
@ -644,6 +644,6 @@ mod tests {
let seed = PruningSeed::decompress(384).unwrap(); let seed = PruningSeed::decompress(384).unwrap();
// there is no next pruned block // there is no next pruned block
assert_eq!(seed.get_next_pruned_block(5000, 10000).unwrap(), None) assert_eq!(seed.get_next_pruned_block(5000, 10000).unwrap(), None);
} }
} }