diff --git a/stackslib/src/chainstate/nakamoto/coordinator/tests.rs b/stackslib/src/chainstate/nakamoto/coordinator/tests.rs index 72ebc8ff07..385641bde5 100644 --- a/stackslib/src/chainstate/nakamoto/coordinator/tests.rs +++ b/stackslib/src/chainstate/nakamoto/coordinator/tests.rs @@ -14,6 +14,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +use std::cmp::Ordering; use std::collections::{HashMap, HashSet}; use std::sync::Mutex; @@ -2310,30 +2311,34 @@ pub fn simple_nakamoto_coordinator_10_tenures_10_sortitions<'a>() -> TestPeer<'a assert_eq!(matured_reward.parent_miner.coinbase, 1_000_000_000); } - if i < 11 { - // epoch2 - assert_eq!( - matured_reward.parent_miner.tx_fees, - MinerPaymentTxFees::Epoch2 { - anchored: 0, - streamed: 0, - } - ); - } else if i == 11 { - // transition - assert_eq!( - matured_reward.parent_miner.tx_fees, - MinerPaymentTxFees::Nakamoto { parent_fees: 0 } - ); - } else { - // nakamoto - assert_eq!( - matured_reward.parent_miner.tx_fees, - MinerPaymentTxFees::Nakamoto { - parent_fees: fee_counts[i - 12] - } - ) - } + match i.cmp(&11) { + Ordering::Less => { + // epoch2 + assert_eq!( + matured_reward.parent_miner.tx_fees, + MinerPaymentTxFees::Epoch2 { + anchored: 0, + streamed: 0, + } + ); + } + Ordering::Equal => { + // transition + assert_eq!( + matured_reward.parent_miner.tx_fees, + MinerPaymentTxFees::Nakamoto { parent_fees: 0 } + ); + } + Ordering::Greater => { + // nakamoto + assert_eq!( + matured_reward.parent_miner.tx_fees, + MinerPaymentTxFees::Nakamoto { + parent_fees: fee_counts[i - 12] + } + ) + } + }; assert_eq!(matured_reward.latest_miners.len(), 1); @@ -2344,30 +2349,35 @@ pub fn simple_nakamoto_coordinator_10_tenures_10_sortitions<'a>() -> TestPeer<'a } else { assert_eq!(miner_reward.coinbase, 1_000_000_000); } - if i < 10 { - // epoch2 - assert_eq!( - miner_reward.tx_fees, - MinerPaymentTxFees::Epoch2 { - anchored: 0, - streamed: 0, - } - ); - } else if i == 10 { - // transition - assert_eq!( - miner_reward.tx_fees, - MinerPaymentTxFees::Nakamoto { parent_fees: 0 } - ) - } else { - // nakamoto - assert_eq!( - miner_reward.tx_fees, - MinerPaymentTxFees::Nakamoto { - parent_fees: fee_counts[i - 11] - } - ) - } + + match i.cmp(&10) { + Ordering::Less => { + // epoch2 + assert_eq!( + miner_reward.tx_fees, + MinerPaymentTxFees::Epoch2 { + anchored: 0, + streamed: 0, + } + ) + } + Ordering::Equal => { + // transition + assert_eq!( + miner_reward.tx_fees, + MinerPaymentTxFees::Nakamoto { parent_fees: 0 } + ) + } + Ordering::Greater => { + // nakamoto + assert_eq!( + miner_reward.tx_fees, + MinerPaymentTxFees::Nakamoto { + parent_fees: fee_counts[i - 11] + } + ) + } + }; } // replay the blocks and sortitions in random order, and verify that we still reach the chain diff --git a/stackslib/src/chainstate/stacks/boot/mod.rs b/stackslib/src/chainstate/stacks/boot/mod.rs index 0277ceb586..c0f26dcf4a 100644 --- a/stackslib/src/chainstate/stacks/boot/mod.rs +++ b/stackslib/src/chainstate/stacks/boot/mod.rs @@ -4875,6 +4875,7 @@ pub mod test { } if first_reward_cycle > 0 && second_reward_cycle == 0 { + #[allow(clippy::comparison_chain)] if cur_reward_cycle == first_reward_cycle { test_in_first_reward_cycle = true; @@ -5002,6 +5003,7 @@ pub mod test { assert_eq!(charlie_account.stx_balance.unlock_height() as u128, 0); } } else if second_reward_cycle > 0 { + #[allow(clippy::comparison_chain)] if cur_reward_cycle == second_reward_cycle { test_in_second_reward_cycle = true; @@ -5484,6 +5486,7 @@ pub mod test { } if reward_cycle > 0 { + #[allow(clippy::comparison_chain)] if cur_reward_cycle == reward_cycle { test_in_first_reward_cycle = true; @@ -5510,7 +5513,7 @@ pub mod test { for addr in stacker_addrs.iter() { let (amount_ustx, pox_addr, lock_period, pox_reward_cycle) = get_stacker_info(&mut peer, addr).unwrap(); - eprintln!("\naddr {}: {} uSTX stacked for {} cycle(s); addr is {:?}; first reward cycle is {}\n", addr, amount_ustx, lock_period, &pox_addr, reward_cycle); + eprintln!("\naddr {addr}: {amount_ustx} uSTX stacked for {lock_period} cycle(s); addr is {pox_addr:?}; first reward cycle is {reward_cycle}\n"); assert_eq!(pox_reward_cycle, reward_cycle); assert_eq!(lock_period, 1); diff --git a/stackslib/src/chainstate/stacks/index/storage.rs b/stackslib/src/chainstate/stacks/index/storage.rs index db99f8004e..f473e7e3da 100644 --- a/stackslib/src/chainstate/stacks/index/storage.rs +++ b/stackslib/src/chainstate/stacks/index/storage.rs @@ -15,6 +15,7 @@ // along with this program. If not, see . use std::char::from_digit; +use std::cmp::Ordering; use std::collections::{HashMap, HashSet, VecDeque}; use std::hash::{Hash, Hasher}; use std::io::{BufWriter, Cursor, Read, Seek, SeekFrom, Write}; @@ -1042,11 +1043,8 @@ impl TrieRAM { } trace!( - "TrieRAM: write_nodetype({:?}): at {}: {:?} {:?}", + "TrieRAM: write_nodetype({:?}): at {node_array_ptr}: {hash:?} {node:?}", &self.block_header, - node_array_ptr, - &hash, - node ); self.write_count += 1; @@ -1059,16 +1057,24 @@ impl TrieRAM { } } - if node_array_ptr < (self.data.len() as u32) { - self.data[node_array_ptr as usize] = (node.clone(), hash); - Ok(()) - } else if node_array_ptr == (self.data.len() as u32) { - self.data.push((node.clone(), hash)); - self.total_bytes += get_node_byte_len(node); - Ok(()) - } else { - error!("Failed to write node bytes: off the end of the buffer"); - Err(Error::NotFoundError) + let node_array_ptr = usize::try_from(node_array_ptr).map_err(|e| { + error!("Conversion to usize failed: {e}"); + Error::NotFoundError + })?; + match node_array_ptr.cmp(&self.data.len()) { + Ordering::Less => { + self.data[node_array_ptr] = (node.clone(), hash); + Ok(()) + } + Ordering::Equal => { + self.data.push((node.clone(), hash)); + self.total_bytes += get_node_byte_len(node); + Ok(()) + } + Ordering::Greater => { + error!("Failed to write node bytes: off the end of the buffer"); + Err(Error::NotFoundError) + } } } diff --git a/stackslib/src/chainstate/stacks/tests/accounting.rs b/stackslib/src/chainstate/stacks/tests/accounting.rs index 80df67d592..c5504149cb 100644 --- a/stackslib/src/chainstate/stacks/tests/accounting.rs +++ b/stackslib/src/chainstate/stacks/tests/accounting.rs @@ -18,6 +18,7 @@ /// various conditions, such as ensuring that the right principals get paid and ensuring that fees /// are appropriately distributed. use std::cell::RefCell; +use std::cmp::Ordering; use std::collections::{HashMap, HashSet, VecDeque}; use std::path::{Path, PathBuf}; use std::{fs, io}; @@ -706,23 +707,19 @@ fn test_bad_microblock_fees_fix_transition() { coinbase + tx_fees_anchored + tx_fees_streamed_produced + tx_fees_streamed_confirmed }; - let expected_reward = if i < 5 { - bad_expected_rewards[i - 1] - } else if i == 5 { - // epoch transition boundary, so no microblock reward at all - 3600000000 + (2000 + 1000 * i as u128) - } else { - good_expected_reward + let expected_reward = match i.cmp(&5) { + Ordering::Less => bad_expected_rewards[i - 1], + Ordering::Equal => { + // epoch transition boundary, so no microblock reward at all + 3600000000 + (2000 + 1000 * i as u128) + } + Ordering::Greater => good_expected_reward, }; eprintln!( - "i = {}, {}, total = {}, good = {}, bad = {}, expected = {}", - i, - &block_id, - matured_reward.total(), - good_expected_reward, - bad_expected_rewards[i - 1], - expected_reward + "i = {i}, {block_id}, total = {t}, good = {good_expected_reward}, bad = {b}, expected = {expected_reward}", + t=matured_reward.total(), + b=bad_expected_rewards[i - 1], ); assert_eq!(expected_reward, matured_reward.total()); } else { diff --git a/stackslib/src/core/mempool.rs b/stackslib/src/core/mempool.rs index e7ff24db78..c3fe20e68f 100644 --- a/stackslib/src/core/mempool.rs +++ b/stackslib/src/core/mempool.rs @@ -1217,19 +1217,13 @@ fn order_nonces( sponsor_actual: u64, sponsor_expected: u64, ) -> Ordering { - if origin_actual < origin_expected { - return Ordering::Less; - } else if origin_actual > origin_expected { - return Ordering::Greater; + if let o @ (Ordering::Greater | Ordering::Less) = origin_actual.cmp(&origin_expected) { + o + } else if let o @ (Ordering::Greater | Ordering::Less) = sponsor_actual.cmp(&sponsor_expected) { + o + } else { + Ordering::Equal } - - if sponsor_actual < sponsor_expected { - return Ordering::Less; - } else if sponsor_actual > sponsor_expected { - return Ordering::Greater; - } - - Ordering::Equal } impl MemPoolDB { diff --git a/stackslib/src/net/inv/epoch2x.rs b/stackslib/src/net/inv/epoch2x.rs index 758ffe3758..7984b9bc34 100644 --- a/stackslib/src/net/inv/epoch2x.rs +++ b/stackslib/src/net/inv/epoch2x.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use std::cmp; +use std::cmp::{self, Ordering}; use std::collections::{BTreeMap, HashMap, HashSet}; use std::io::{Read, Write}; use std::net::SocketAddr; @@ -445,23 +445,24 @@ impl PeerBlocksInv { return Some((i, my_bit, pox_bit)); } } - if (pox_id.len() as u64) - 1 == self.num_reward_cycles { - // all agreed - None - } else if (pox_id.len() as u64) - 1 < self.num_reward_cycles { - // pox inv is longer - Some(( - (pox_id.len() as u64) - 1, - self.has_ith_anchor_block((pox_id.len() as u64) - 1), - false, - )) - } else { - // our inv is longer - Some(( - self.num_reward_cycles, - false, - pox_id.has_ith_anchor_block(self.num_reward_cycles as usize), - )) + let reward_cycle = (pox_id.len() as u64) - 1; + match reward_cycle.cmp(&self.num_reward_cycles) { + Ordering::Less => { + // pox inv is longer + Some((reward_cycle, self.has_ith_anchor_block(reward_cycle), false)) + } + Ordering::Equal => { + // all agreed + None + } + Ordering::Greater => { + // our inv is longer + Some(( + self.num_reward_cycles, + false, + pox_id.has_ith_anchor_block(self.num_reward_cycles as usize), + )) + } } } diff --git a/stackslib/src/net/prune.rs b/stackslib/src/net/prune.rs index 1ad6b4ffc8..9ac5a3ba66 100644 --- a/stackslib/src/net/prune.rs +++ b/stackslib/src/net/prune.rs @@ -339,15 +339,7 @@ impl PeerNetwork { // sort in order by first-contact time (oldest first) for (_, stats_list) in ip_neighbor.iter_mut() { - stats_list.sort_by(|(_e1, _nk1, stats1), (_e2, _nk2, stats2)| { - if stats1.first_contact_time < stats2.first_contact_time { - Ordering::Less - } else if stats1.first_contact_time > stats2.first_contact_time { - Ordering::Greater - } else { - Ordering::Equal - } - }); + stats_list.sort_by_key(|(_e, _nk, stats)| stats.first_contact_time); } let mut to_remove = vec![]; diff --git a/stackslib/src/net/stackerdb/sync.rs b/stackslib/src/net/stackerdb/sync.rs index 7c20bb9930..772a49c23c 100644 --- a/stackslib/src/net/stackerdb/sync.rs +++ b/stackslib/src/net/stackerdb/sync.rs @@ -14,6 +14,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +use std::cmp::Ordering; use std::collections::{HashMap, HashSet}; use std::mem; @@ -380,21 +381,25 @@ impl StackerDBSync { continue; }; - if request.slot_version < chunk_inv.slot_versions[i] { - // this peer has a newer view - available.clear(); - available.push(naddr.clone()); - *request = StackerDBGetChunkData { - contract_id: self.smart_contract_id.clone(), - rc_consensus_hash, - slot_id: i as u32, - slot_version: chunk_inv.slot_versions[i], - }; - } else if request.slot_version == chunk_inv.slot_versions[i] { - // this peer has the same view as a prior peer. - // just track how many times we see this - available.push(naddr.clone()); - } + match request.slot_version.cmp(&chunk_inv.slot_versions[i]) { + Ordering::Less => { + // this peer has a newer view + available.clear(); + available.push(naddr.clone()); + *request = StackerDBGetChunkData { + contract_id: self.smart_contract_id.clone(), + rc_consensus_hash, + slot_id: i as u32, + slot_version: chunk_inv.slot_versions[i], + }; + } + Ordering::Equal => { + // this peer has the same view as a prior peer. + // just track how many times we see this + available.push(naddr.clone()); + } + Ordering::Greater => {} + }; } }