Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Apply Clippy lint comparison_chain #5771

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 58 additions & 48 deletions stackslib/src/chainstate/nakamoto/coordinator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::sync::Mutex;

Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion stackslib/src/chainstate/stacks/boot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand Down
34 changes: 20 additions & 14 deletions stackslib/src/chainstate/stacks/index/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

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};
Expand Down Expand Up @@ -1042,11 +1043,8 @@ impl<T: MarfTrieId> TrieRAM<T> {
}

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;
Expand All @@ -1059,16 +1057,24 @@ impl<T: MarfTrieId> TrieRAM<T> {
}
}

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)
}
}
}

Expand Down
25 changes: 11 additions & 14 deletions stackslib/src/chainstate/stacks/tests/accounting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 6 additions & 12 deletions stackslib/src/core/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 19 additions & 18 deletions stackslib/src/net/inv/epoch2x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::cmp;
use std::cmp::{self, Ordering};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::io::{Read, Write};
use std::net::SocketAddr;
Expand Down Expand Up @@ -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),
))
}
}
}

Expand Down
10 changes: 1 addition & 9 deletions stackslib/src/net/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![];
Expand Down
Loading
Loading