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

Fix collapsible_else_if clippy warnings in stackslib #5630

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
8 changes: 4 additions & 4 deletions stackslib/src/burnchains/bitcoin/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,10 +591,10 @@ impl BitcoinAddress {
} else {
BitcoinNetworkType::Testnet
};
if let Some(addr) = BitcoinAddress::from_scriptpubkey(network_id, scriptpubkey) {
if let BitcoinAddress::Segwit(sw) = addr {
return Some(BitcoinAddress::Segwit(sw));
}
if let Some(BitcoinAddress::Segwit(sw)) =
BitcoinAddress::from_scriptpubkey(network_id, scriptpubkey)
{
return Some(BitcoinAddress::Segwit(sw));
}
return None;
}
Expand Down
12 changes: 5 additions & 7 deletions stackslib/src/burnchains/bitcoin/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 +503,11 @@ impl BitcoinIndexer {
start_block: u64,
remove_old: bool,
) -> Result<SpvClient, btc_error> {
if remove_old {
if PathBuf::from(&reorg_headers_path).exists() {
fs::remove_file(&reorg_headers_path).map_err(|e| {
error!("Failed to remove {}", reorg_headers_path);
btc_error::Io(e)
})?;
}
if remove_old && PathBuf::from(&reorg_headers_path).exists() {
fs::remove_file(&reorg_headers_path).map_err(|e| {
error!("Failed to remove {}", reorg_headers_path);
btc_error::Io(e)
})?;
}

// bootstrap reorg client
Expand Down
10 changes: 4 additions & 6 deletions stackslib/src/burnchains/bitcoin/spv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,11 @@ impl SpvClient {
} else {
return Err(btc_error::DBError(db_error::NoDBError));
}
} else {
} else if readwrite {
// can just open
if readwrite {
OpenFlags::SQLITE_OPEN_READ_WRITE
} else {
OpenFlags::SQLITE_OPEN_READ_ONLY
}
OpenFlags::SQLITE_OPEN_READ_WRITE
} else {
OpenFlags::SQLITE_OPEN_READ_ONLY
};

let mut conn = sqlite_open(headers_path, open_flags, false)
Expand Down
38 changes: 18 additions & 20 deletions stackslib/src/burnchains/tests/affirmation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,29 +351,27 @@ pub fn make_reward_cycle_with_vote(
let append = if !burnchain.is_in_prepare_phase(block_commit.block_height) {
// non-prepare-phase commits always confirm their parent
true
} else if confirm_anchor_block {
// all block-commits confirm anchor block
true
} else {
if confirm_anchor_block {
// all block-commits confirm anchor block
// fewer than anchor_threshold commits confirm anchor block
let next_rc_start = burnchain.reward_cycle_to_block_height(
burnchain
.block_height_to_reward_cycle(block_commit.block_height)
.unwrap()
+ 1,
);
if block_commit.block_height
+ (burnchain.pox_constants.anchor_threshold as u64)
+ 1
< next_rc_start
{
// in first half of prepare phase, so confirm
true
} else {
// fewer than anchor_threshold commits confirm anchor block
let next_rc_start = burnchain.reward_cycle_to_block_height(
burnchain
.block_height_to_reward_cycle(block_commit.block_height)
.unwrap()
+ 1,
);
if block_commit.block_height
+ (burnchain.pox_constants.anchor_threshold as u64)
+ 1
< next_rc_start
{
// in first half of prepare phase, so confirm
true
} else {
// in second half of prepare phase, so don't confirm
false
}
// in second half of prepare phase, so don't confirm
false
}
};

Expand Down
31 changes: 14 additions & 17 deletions stackslib/src/chainstate/burn/db/sortdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1849,21 +1849,19 @@ impl<'a> SortitionHandleTx<'a> {
true
} else if cur_height > stacks_block_height {
false
} else if &cur_ch == consensus_hash {
// same sortition (i.e. nakamoto block)
// no replacement
false
} else {
if &cur_ch == consensus_hash {
// same sortition (i.e. nakamoto block)
// no replacement
false
} else {
// tips come from different sortitions
// break ties by going with the latter-signed block
let sn_current = SortitionDB::get_block_snapshot_consensus(self, &cur_ch)?
// tips come from different sortitions
// break ties by going with the latter-signed block
let sn_current = SortitionDB::get_block_snapshot_consensus(self, &cur_ch)?
.ok_or(db_error::NotFoundError)?;
let sn_accepted =
SortitionDB::get_block_snapshot_consensus(self, &consensus_hash)?
.ok_or(db_error::NotFoundError)?;
let sn_accepted =
SortitionDB::get_block_snapshot_consensus(self, &consensus_hash)?
.ok_or(db_error::NotFoundError)?;
sn_current.block_height < sn_accepted.block_height
}
sn_current.block_height < sn_accepted.block_height
};

debug!("Setting Stacks tip as accepted";
Expand Down Expand Up @@ -5776,10 +5774,9 @@ impl<'a> SortitionHandleTx<'a> {
.map(|parent_commit_sn| parent_commit_sn.sortition_id)
.unwrap_or(SortitionId([0x00; 32]));

if !cfg!(test) {
if block_commit.parent_block_ptr != 0 || block_commit.parent_vtxindex != 0 {
assert!(parent_sortition_id != SortitionId([0x00; 32]));
}
if !cfg!(test) && (block_commit.parent_block_ptr != 0 || block_commit.parent_vtxindex != 0)
{
assert!(parent_sortition_id != SortitionId([0x00; 32]));
}

let args = params![
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,9 @@ impl LeaderBlockCommitOp {
})?;

// basic sanity checks
if data.parent_block_ptr == 0 {
if data.parent_vtxindex != 0 {
warn!("Invalid tx: parent block back-pointer must be positive");
return Err(op_error::ParseError);
}
if data.parent_block_ptr == 0 && data.parent_vtxindex != 0 {
warn!("Invalid tx: parent block back-pointer must be positive");
return Err(op_error::ParseError);
// if parent block ptr and parent vtxindex are both 0, then this block's parent is
jferrant marked this conversation as resolved.
Show resolved Hide resolved
// the genesis block.
}
Expand Down
43 changes: 20 additions & 23 deletions stackslib/src/chainstate/coordinator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,11 @@ impl<'a, T: BlockEventDispatcher> RewardSetProvider for OnChainRewardSetProvider
cur_epoch,
)?;

if is_nakamoto_reward_set {
if reward_set.signers.is_none() || reward_set.signers == Some(vec![]) {
error!("FATAL: Signer sets are empty in a reward set that will be used in nakamoto"; "reward_set" => ?reward_set);
return Err(Error::PoXAnchorBlockRequired);
}
if is_nakamoto_reward_set
&& (reward_set.signers.is_none() || reward_set.signers == Some(vec![]))
{
error!("FATAL: Signer sets are empty in a reward set that will be used in nakamoto"; "reward_set" => ?reward_set);
return Err(Error::PoXAnchorBlockRequired);
}

Ok(reward_set)
Expand Down Expand Up @@ -917,12 +917,10 @@ pub fn calculate_paid_rewards(ops: &[BlockstackOperationType]) -> PaidRewards {
for addr in commit.commit_outs.iter() {
if addr.is_burn() {
burn_amt += amt_per_address;
} else if let Some(prior_amt) = reward_recipients.get_mut(addr) {
*prior_amt += amt_per_address;
} else {
if let Some(prior_amt) = reward_recipients.get_mut(addr) {
*prior_amt += amt_per_address;
} else {
reward_recipients.insert(addr.clone(), amt_per_address);
}
reward_recipients.insert(addr.clone(), amt_per_address);
}
}
}
Expand Down Expand Up @@ -1400,21 +1398,20 @@ impl<
}
};

if sortition_changed_reward_cycle_opt.is_none() {
if sortition_tip_affirmation_map.len() >= heaviest_am.len()
&& sortition_tip_affirmation_map.len() <= canonical_affirmation_map.len()
if sortition_changed_reward_cycle_opt.is_none()
&& sortition_tip_affirmation_map.len() >= heaviest_am.len()
&& sortition_tip_affirmation_map.len() <= canonical_affirmation_map.len()
{
if let Some(divergence_rc) =
canonical_affirmation_map.find_divergence(&sortition_tip_affirmation_map)
{
if let Some(divergence_rc) =
canonical_affirmation_map.find_divergence(&sortition_tip_affirmation_map)
{
if divergence_rc + 1 >= (heaviest_am.len() as u64) {
// this can arise if there are unaffirmed PoX anchor blocks that are not
// reflected in the sortiiton affirmation map
debug!("Update sortition-changed reward cycle to {} from canonical affirmation map `{}` (sortition AM is `{}`)",
divergence_rc, &canonical_affirmation_map, &sortition_tip_affirmation_map);
if divergence_rc + 1 >= (heaviest_am.len() as u64) {
jferrant marked this conversation as resolved.
Show resolved Hide resolved
// this can arise if there are unaffirmed PoX anchor blocks that are not
// reflected in the sortiiton affirmation map
debug!("Update sortition-changed reward cycle to {} from canonical affirmation map `{}` (sortition AM is `{}`)",
divergence_rc, &canonical_affirmation_map, &sortition_tip_affirmation_map);

sortition_changed_reward_cycle_opt = Some(divergence_rc);
}
sortition_changed_reward_cycle_opt = Some(divergence_rc);
}
}
}
Expand Down
24 changes: 9 additions & 15 deletions stackslib/src/chainstate/coordinator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,12 +1222,10 @@ fn missed_block_commits_2_05() {
// how many commit do we expect to see counted in the current window?
let expected_window_commits = if ix >= (MINING_COMMITMENT_WINDOW as usize) {
(MINING_COMMITMENT_WINDOW - 1) as usize
} else if ix >= 3 {
ix
} else {
if ix >= 3 {
ix
} else {
ix + 1
}
ix + 1
};
// there were 2 burn blocks before we started mining
let expected_window_size = cmp::min(MINING_COMMITMENT_WINDOW as usize, ix + 3);
Expand Down Expand Up @@ -1551,12 +1549,10 @@ fn missed_block_commits_2_1() {
// how many commits do we expect to see counted in the current window?
let mut expected_window_commits = if ix >= (MINING_COMMITMENT_WINDOW as usize) {
(MINING_COMMITMENT_WINDOW - 1) as usize
} else if ix >= 3 {
ix
} else {
if ix >= 3 {
ix
} else {
ix + 1
}
ix + 1
};
// there were 2 burn blocks before we started mining
let expected_window_size = cmp::min(MINING_COMMITMENT_WINDOW as usize, ix + 3);
Expand Down Expand Up @@ -1894,12 +1890,10 @@ fn late_block_commits_2_1() {
// how many commit do we expect to see counted in the current window?
let mut expected_window_commits = if ix >= (MINING_COMMITMENT_WINDOW as usize) {
(MINING_COMMITMENT_WINDOW - 1) as usize
} else if ix >= 3 {
ix
} else {
if ix >= 3 {
ix
} else {
ix + 1
}
ix + 1
};
// there were 2 burn blocks before we started mining
let expected_window_size = cmp::min(MINING_COMMITMENT_WINDOW as usize, ix + 3);
Expand Down
26 changes: 12 additions & 14 deletions stackslib/src/chainstate/nakamoto/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,20 +572,18 @@ impl NakamotoBlockBuilder {
);
let mut remaining_limit = block_limit.clone();
let cost_so_far = tenure_tx.cost_so_far();
if remaining_limit.sub(&cost_so_far).is_ok() {
if remaining_limit.divide(100).is_ok() {
remaining_limit.multiply(percentage.into()).expect(
"BUG: failed to multiply by {percentage} when previously divided by 100",
);
remaining_limit.add(&cost_so_far).expect("BUG: unexpected overflow when adding cost_so_far, which was previously checked");
debug!(
"Setting soft limit for clarity cost to {percentage}% of remaining block limit";
"remaining_limit" => %remaining_limit,
"cost_so_far" => %cost_so_far,
"block_limit" => %block_limit,
);
soft_limit = Some(remaining_limit);
}
if remaining_limit.sub(&cost_so_far).is_ok() && remaining_limit.divide(100).is_ok() {
remaining_limit.multiply(percentage.into()).expect(
"BUG: failed to multiply by {percentage} when previously divided by 100",
);
remaining_limit.add(&cost_so_far).expect("BUG: unexpected overflow when adding cost_so_far, which was previously checked");
debug!(
"Setting soft limit for clarity cost to {percentage}% of remaining block limit";
"remaining_limit" => %remaining_limit,
"cost_so_far" => %cost_so_far,
"block_limit" => %block_limit,
);
soft_limit = Some(remaining_limit);
};
}

Expand Down
32 changes: 14 additions & 18 deletions stackslib/src/chainstate/nakamoto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4179,17 +4179,15 @@ impl NakamotoChainState {
"Bitvec does not match the block commit's PoX handling".into(),
));
}
} else if all_0 {
if treated_addr.is_reward() {
warn!(
"Invalid Nakamoto block: rewarded PoX address when bitvec contained 0s for the address";
"reward_address" => %treated_addr.deref(),
"bitvec_values" => ?bitvec_values,
);
return Err(ChainstateError::InvalidStacksBlock(
"Bitvec does not match the block commit's PoX handling".into(),
));
}
} else if all_0 && treated_addr.is_reward() {
warn!(
"Invalid Nakamoto block: rewarded PoX address when bitvec contained 0s for the address";
"reward_address" => %treated_addr.deref(),
"bitvec_values" => ?bitvec_values,
);
return Err(ChainstateError::InvalidStacksBlock(
"Bitvec does not match the block commit's PoX handling".into(),
));
}
}

Expand Down Expand Up @@ -4431,13 +4429,11 @@ impl NakamotoChainState {
"Could not advance tenure, even though tenure changed".into(),
));
}
} else {
if coinbase_height != parent_coinbase_height {
// this should be unreachable
return Err(ChainstateError::InvalidStacksBlock(
"Advanced tenure even though a new tenure did not happen".into(),
));
}
} else if coinbase_height != parent_coinbase_height {
// this should be unreachable
return Err(ChainstateError::InvalidStacksBlock(
"Advanced tenure even though a new tenure did not happen".into(),
));
}

// begin processing this block
Expand Down
8 changes: 3 additions & 5 deletions stackslib/src/chainstate/nakamoto/staging_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,12 +823,10 @@ impl StacksChainState {
} else {
return Err(DBError::NotFoundError.into());
}
} else if readwrite {
OpenFlags::SQLITE_OPEN_READ_WRITE
} else {
if readwrite {
OpenFlags::SQLITE_OPEN_READ_WRITE
} else {
OpenFlags::SQLITE_OPEN_READ_ONLY
}
OpenFlags::SQLITE_OPEN_READ_ONLY
};
let conn = sqlite_open(path, flags, false)?;
if !exists {
Expand Down
Loading
Loading