Skip to content

Commit a4566b8

Browse files
authored
Merge pull request #5630 from stacks-network/fix/clippy-ci-stacks-lib-collapsible-else-if
Fix collapsible_else_if clippy warnings in stackslib
2 parents 403aefc + 0835819 commit a4566b8

File tree

29 files changed

+793
-905
lines changed

29 files changed

+793
-905
lines changed

stackslib/src/burnchains/bitcoin/address.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,10 @@ impl BitcoinAddress {
591591
} else {
592592
BitcoinNetworkType::Testnet
593593
};
594-
if let Some(addr) = BitcoinAddress::from_scriptpubkey(network_id, scriptpubkey) {
595-
if let BitcoinAddress::Segwit(sw) = addr {
596-
return Some(BitcoinAddress::Segwit(sw));
597-
}
594+
if let Some(BitcoinAddress::Segwit(sw)) =
595+
BitcoinAddress::from_scriptpubkey(network_id, scriptpubkey)
596+
{
597+
return Some(BitcoinAddress::Segwit(sw));
598598
}
599599
return None;
600600
}

stackslib/src/burnchains/bitcoin/indexer.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -503,13 +503,11 @@ impl BitcoinIndexer {
503503
start_block: u64,
504504
remove_old: bool,
505505
) -> Result<SpvClient, btc_error> {
506-
if remove_old {
507-
if PathBuf::from(&reorg_headers_path).exists() {
508-
fs::remove_file(&reorg_headers_path).map_err(|e| {
509-
error!("Failed to remove {}", reorg_headers_path);
510-
btc_error::Io(e)
511-
})?;
512-
}
506+
if remove_old && PathBuf::from(&reorg_headers_path).exists() {
507+
fs::remove_file(&reorg_headers_path).map_err(|e| {
508+
error!("Failed to remove {}", reorg_headers_path);
509+
btc_error::Io(e)
510+
})?;
513511
}
514512

515513
// bootstrap reorg client

stackslib/src/burnchains/bitcoin/spv.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,11 @@ impl SpvClient {
328328
} else {
329329
return Err(btc_error::DBError(db_error::NoDBError));
330330
}
331-
} else {
331+
} else if readwrite {
332332
// can just open
333-
if readwrite {
334-
OpenFlags::SQLITE_OPEN_READ_WRITE
335-
} else {
336-
OpenFlags::SQLITE_OPEN_READ_ONLY
337-
}
333+
OpenFlags::SQLITE_OPEN_READ_WRITE
334+
} else {
335+
OpenFlags::SQLITE_OPEN_READ_ONLY
338336
};
339337

340338
let mut conn = sqlite_open(headers_path, open_flags, false)

stackslib/src/burnchains/tests/affirmation.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -351,29 +351,27 @@ pub fn make_reward_cycle_with_vote(
351351
let append = if !burnchain.is_in_prepare_phase(block_commit.block_height) {
352352
// non-prepare-phase commits always confirm their parent
353353
true
354+
} else if confirm_anchor_block {
355+
// all block-commits confirm anchor block
356+
true
354357
} else {
355-
if confirm_anchor_block {
356-
// all block-commits confirm anchor block
358+
// fewer than anchor_threshold commits confirm anchor block
359+
let next_rc_start = burnchain.reward_cycle_to_block_height(
360+
burnchain
361+
.block_height_to_reward_cycle(block_commit.block_height)
362+
.unwrap()
363+
+ 1,
364+
);
365+
if block_commit.block_height
366+
+ (burnchain.pox_constants.anchor_threshold as u64)
367+
+ 1
368+
< next_rc_start
369+
{
370+
// in first half of prepare phase, so confirm
357371
true
358372
} else {
359-
// fewer than anchor_threshold commits confirm anchor block
360-
let next_rc_start = burnchain.reward_cycle_to_block_height(
361-
burnchain
362-
.block_height_to_reward_cycle(block_commit.block_height)
363-
.unwrap()
364-
+ 1,
365-
);
366-
if block_commit.block_height
367-
+ (burnchain.pox_constants.anchor_threshold as u64)
368-
+ 1
369-
< next_rc_start
370-
{
371-
// in first half of prepare phase, so confirm
372-
true
373-
} else {
374-
// in second half of prepare phase, so don't confirm
375-
false
376-
}
373+
// in second half of prepare phase, so don't confirm
374+
false
377375
}
378376
};
379377

stackslib/src/chainstate/burn/db/sortdb.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,21 +1849,19 @@ impl SortitionHandleTx<'_> {
18491849
true
18501850
} else if cur_height > stacks_block_height {
18511851
false
1852+
} else if &cur_ch == consensus_hash {
1853+
// same sortition (i.e. nakamoto block)
1854+
// no replacement
1855+
false
18521856
} else {
1853-
if &cur_ch == consensus_hash {
1854-
// same sortition (i.e. nakamoto block)
1855-
// no replacement
1856-
false
1857-
} else {
1858-
// tips come from different sortitions
1859-
// break ties by going with the latter-signed block
1860-
let sn_current = SortitionDB::get_block_snapshot_consensus(self, &cur_ch)?
1857+
// tips come from different sortitions
1858+
// break ties by going with the latter-signed block
1859+
let sn_current = SortitionDB::get_block_snapshot_consensus(self, &cur_ch)?
1860+
.ok_or(db_error::NotFoundError)?;
1861+
let sn_accepted =
1862+
SortitionDB::get_block_snapshot_consensus(self, &consensus_hash)?
18611863
.ok_or(db_error::NotFoundError)?;
1862-
let sn_accepted =
1863-
SortitionDB::get_block_snapshot_consensus(self, &consensus_hash)?
1864-
.ok_or(db_error::NotFoundError)?;
1865-
sn_current.block_height < sn_accepted.block_height
1866-
}
1864+
sn_current.block_height < sn_accepted.block_height
18671865
};
18681866

18691867
debug!("Setting Stacks tip as accepted";
@@ -5774,10 +5772,9 @@ impl SortitionHandleTx<'_> {
57745772
.map(|parent_commit_sn| parent_commit_sn.sortition_id)
57755773
.unwrap_or(SortitionId([0x00; 32]));
57765774

5777-
if !cfg!(test) {
5778-
if block_commit.parent_block_ptr != 0 || block_commit.parent_vtxindex != 0 {
5779-
assert!(parent_sortition_id != SortitionId([0x00; 32]));
5780-
}
5775+
if !cfg!(test) && (block_commit.parent_block_ptr != 0 || block_commit.parent_vtxindex != 0)
5776+
{
5777+
assert!(parent_sortition_id != SortitionId([0x00; 32]));
57815778
}
57825779

57835780
let args = params![

stackslib/src/chainstate/burn/operations/leader_block_commit.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,11 @@ impl LeaderBlockCommitOp {
313313
})?;
314314

315315
// basic sanity checks
316-
if data.parent_block_ptr == 0 {
317-
if data.parent_vtxindex != 0 {
318-
warn!("Invalid tx: parent block back-pointer must be positive");
319-
return Err(op_error::ParseError);
320-
}
321-
// if parent block ptr and parent vtxindex are both 0, then this block's parent is
322-
// the genesis block.
316+
// if parent block ptr and parent vtxindex are both 0, then this block's parent is
317+
// the genesis block.
318+
if data.parent_block_ptr == 0 && data.parent_vtxindex != 0 {
319+
warn!("Invalid tx: parent block back-pointer must be positive");
320+
return Err(op_error::ParseError);
323321
}
324322

325323
if u64::from(data.parent_block_ptr) >= block_height {

stackslib/src/chainstate/coordinator/mod.rs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,11 @@ impl<T: BlockEventDispatcher> RewardSetProvider for OnChainRewardSetProvider<'_,
374374
cur_epoch,
375375
)?;
376376

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

384384
Ok(reward_set)
@@ -918,12 +918,10 @@ pub fn calculate_paid_rewards(ops: &[BlockstackOperationType]) -> PaidRewards {
918918
for addr in commit.commit_outs.iter() {
919919
if addr.is_burn() {
920920
burn_amt += amt_per_address;
921+
} else if let Some(prior_amt) = reward_recipients.get_mut(addr) {
922+
*prior_amt += amt_per_address;
921923
} else {
922-
if let Some(prior_amt) = reward_recipients.get_mut(addr) {
923-
*prior_amt += amt_per_address;
924-
} else {
925-
reward_recipients.insert(addr.clone(), amt_per_address);
926-
}
924+
reward_recipients.insert(addr.clone(), amt_per_address);
927925
}
928926
}
929927
}
@@ -1402,21 +1400,20 @@ impl<
14021400
}
14031401
};
14041402

1405-
if sortition_changed_reward_cycle_opt.is_none() {
1406-
if sortition_tip_affirmation_map.len() >= heaviest_am.len()
1407-
&& sortition_tip_affirmation_map.len() <= canonical_affirmation_map.len()
1403+
if sortition_changed_reward_cycle_opt.is_none()
1404+
&& sortition_tip_affirmation_map.len() >= heaviest_am.len()
1405+
&& sortition_tip_affirmation_map.len() <= canonical_affirmation_map.len()
1406+
{
1407+
if let Some(divergence_rc) =
1408+
canonical_affirmation_map.find_divergence(&sortition_tip_affirmation_map)
14081409
{
1409-
if let Some(divergence_rc) =
1410-
canonical_affirmation_map.find_divergence(&sortition_tip_affirmation_map)
1411-
{
1412-
if divergence_rc + 1 >= (heaviest_am.len() as u64) {
1413-
// this can arise if there are unaffirmed PoX anchor blocks that are not
1414-
// reflected in the sortiiton affirmation map
1415-
debug!("Update sortition-changed reward cycle to {} from canonical affirmation map `{}` (sortition AM is `{}`)",
1416-
divergence_rc, &canonical_affirmation_map, &sortition_tip_affirmation_map);
1410+
if divergence_rc + 1 >= (heaviest_am.len() as u64) {
1411+
// this can arise if there are unaffirmed PoX anchor blocks that are not
1412+
// reflected in the sortiiton affirmation map
1413+
debug!("Update sortition-changed reward cycle to {} from canonical affirmation map `{}` (sortition AM is `{}`)",
1414+
divergence_rc, &canonical_affirmation_map, &sortition_tip_affirmation_map);
14171415

1418-
sortition_changed_reward_cycle_opt = Some(divergence_rc);
1419-
}
1416+
sortition_changed_reward_cycle_opt = Some(divergence_rc);
14201417
}
14211418
}
14221419
}

stackslib/src/chainstate/coordinator/tests.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,12 +1223,10 @@ fn missed_block_commits_2_05() {
12231223
// how many commit do we expect to see counted in the current window?
12241224
let expected_window_commits = if ix >= (MINING_COMMITMENT_WINDOW as usize) {
12251225
(MINING_COMMITMENT_WINDOW - 1) as usize
1226+
} else if ix >= 3 {
1227+
ix
12261228
} else {
1227-
if ix >= 3 {
1228-
ix
1229-
} else {
1230-
ix + 1
1231-
}
1229+
ix + 1
12321230
};
12331231
// there were 2 burn blocks before we started mining
12341232
let expected_window_size = cmp::min(MINING_COMMITMENT_WINDOW as usize, ix + 3);
@@ -1552,12 +1550,10 @@ fn missed_block_commits_2_1() {
15521550
// how many commits do we expect to see counted in the current window?
15531551
let mut expected_window_commits = if ix >= (MINING_COMMITMENT_WINDOW as usize) {
15541552
(MINING_COMMITMENT_WINDOW - 1) as usize
1553+
} else if ix >= 3 {
1554+
ix
15551555
} else {
1556-
if ix >= 3 {
1557-
ix
1558-
} else {
1559-
ix + 1
1560-
}
1556+
ix + 1
15611557
};
15621558
// there were 2 burn blocks before we started mining
15631559
let expected_window_size = cmp::min(MINING_COMMITMENT_WINDOW as usize, ix + 3);
@@ -1895,12 +1891,10 @@ fn late_block_commits_2_1() {
18951891
// how many commit do we expect to see counted in the current window?
18961892
let mut expected_window_commits = if ix >= (MINING_COMMITMENT_WINDOW as usize) {
18971893
(MINING_COMMITMENT_WINDOW - 1) as usize
1894+
} else if ix >= 3 {
1895+
ix
18981896
} else {
1899-
if ix >= 3 {
1900-
ix
1901-
} else {
1902-
ix + 1
1903-
}
1897+
ix + 1
19041898
};
19051899
// there were 2 burn blocks before we started mining
19061900
let expected_window_size = cmp::min(MINING_COMMITMENT_WINDOW as usize, ix + 3);

stackslib/src/chainstate/nakamoto/miner.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -572,20 +572,18 @@ impl NakamotoBlockBuilder {
572572
);
573573
let mut remaining_limit = block_limit.clone();
574574
let cost_so_far = tenure_tx.cost_so_far();
575-
if remaining_limit.sub(&cost_so_far).is_ok() {
576-
if remaining_limit.divide(100).is_ok() {
577-
remaining_limit.multiply(percentage.into()).expect(
578-
"BUG: failed to multiply by {percentage} when previously divided by 100",
579-
);
580-
remaining_limit.add(&cost_so_far).expect("BUG: unexpected overflow when adding cost_so_far, which was previously checked");
581-
debug!(
582-
"Setting soft limit for clarity cost to {percentage}% of remaining block limit";
583-
"remaining_limit" => %remaining_limit,
584-
"cost_so_far" => %cost_so_far,
585-
"block_limit" => %block_limit,
586-
);
587-
soft_limit = Some(remaining_limit);
588-
}
575+
if remaining_limit.sub(&cost_so_far).is_ok() && remaining_limit.divide(100).is_ok() {
576+
remaining_limit.multiply(percentage.into()).expect(
577+
"BUG: failed to multiply by {percentage} when previously divided by 100",
578+
);
579+
remaining_limit.add(&cost_so_far).expect("BUG: unexpected overflow when adding cost_so_far, which was previously checked");
580+
debug!(
581+
"Setting soft limit for clarity cost to {percentage}% of remaining block limit";
582+
"remaining_limit" => %remaining_limit,
583+
"cost_so_far" => %cost_so_far,
584+
"block_limit" => %block_limit,
585+
);
586+
soft_limit = Some(remaining_limit);
589587
};
590588
}
591589

stackslib/src/chainstate/nakamoto/mod.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4174,17 +4174,15 @@ impl NakamotoChainState {
41744174
"Bitvec does not match the block commit's PoX handling".into(),
41754175
));
41764176
}
4177-
} else if all_0 {
4178-
if treated_addr.is_reward() {
4179-
warn!(
4180-
"Invalid Nakamoto block: rewarded PoX address when bitvec contained 0s for the address";
4181-
"reward_address" => %treated_addr.deref(),
4182-
"bitvec_values" => ?bitvec_values,
4183-
);
4184-
return Err(ChainstateError::InvalidStacksBlock(
4185-
"Bitvec does not match the block commit's PoX handling".into(),
4186-
));
4187-
}
4177+
} else if all_0 && treated_addr.is_reward() {
4178+
warn!(
4179+
"Invalid Nakamoto block: rewarded PoX address when bitvec contained 0s for the address";
4180+
"reward_address" => %treated_addr.deref(),
4181+
"bitvec_values" => ?bitvec_values,
4182+
);
4183+
return Err(ChainstateError::InvalidStacksBlock(
4184+
"Bitvec does not match the block commit's PoX handling".into(),
4185+
));
41884186
}
41894187
}
41904188

@@ -4426,13 +4424,11 @@ impl NakamotoChainState {
44264424
"Could not advance tenure, even though tenure changed".into(),
44274425
));
44284426
}
4429-
} else {
4430-
if coinbase_height != parent_coinbase_height {
4431-
// this should be unreachable
4432-
return Err(ChainstateError::InvalidStacksBlock(
4433-
"Advanced tenure even though a new tenure did not happen".into(),
4434-
));
4435-
}
4427+
} else if coinbase_height != parent_coinbase_height {
4428+
// this should be unreachable
4429+
return Err(ChainstateError::InvalidStacksBlock(
4430+
"Advanced tenure even though a new tenure did not happen".into(),
4431+
));
44364432
}
44374433

44384434
// begin processing this block

0 commit comments

Comments
 (0)