Skip to content

Commit c8e2130

Browse files
committed
Fix collapsible_else_if clippy warnings in stackslib
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 0ee5d7c commit c8e2130

File tree

18 files changed

+189
-236
lines changed

18 files changed

+189
-236
lines changed

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: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,21 +1849,19 @@ impl<'a> SortitionHandleTx<'a> {
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";

stackslib/src/chainstate/coordinator/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -917,12 +917,10 @@ pub fn calculate_paid_rewards(ops: &[BlockstackOperationType]) -> PaidRewards {
917917
for addr in commit.commit_outs.iter() {
918918
if addr.is_burn() {
919919
burn_amt += amt_per_address;
920+
} else if let Some(prior_amt) = reward_recipients.get_mut(addr) {
921+
*prior_amt += amt_per_address;
920922
} else {
921-
if let Some(prior_amt) = reward_recipients.get_mut(addr) {
922-
*prior_amt += amt_per_address;
923-
} else {
924-
reward_recipients.insert(addr.clone(), amt_per_address);
925-
}
923+
reward_recipients.insert(addr.clone(), amt_per_address);
926924
}
927925
}
928926
}

stackslib/src/chainstate/coordinator/tests.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,12 +1222,10 @@ fn missed_block_commits_2_05() {
12221222
// how many commit do we expect to see counted in the current window?
12231223
let expected_window_commits = if ix >= (MINING_COMMITMENT_WINDOW as usize) {
12241224
(MINING_COMMITMENT_WINDOW - 1) as usize
1225+
} else if ix >= 3 {
1226+
ix
12251227
} else {
1226-
if ix >= 3 {
1227-
ix
1228-
} else {
1229-
ix + 1
1230-
}
1228+
ix + 1
12311229
};
12321230
// there were 2 burn blocks before we started mining
12331231
let expected_window_size = cmp::min(MINING_COMMITMENT_WINDOW as usize, ix + 3);
@@ -1551,12 +1549,10 @@ fn missed_block_commits_2_1() {
15511549
// how many commits do we expect to see counted in the current window?
15521550
let mut expected_window_commits = if ix >= (MINING_COMMITMENT_WINDOW as usize) {
15531551
(MINING_COMMITMENT_WINDOW - 1) as usize
1552+
} else if ix >= 3 {
1553+
ix
15541554
} else {
1555-
if ix >= 3 {
1556-
ix
1557-
} else {
1558-
ix + 1
1559-
}
1555+
ix + 1
15601556
};
15611557
// there were 2 burn blocks before we started mining
15621558
let expected_window_size = cmp::min(MINING_COMMITMENT_WINDOW as usize, ix + 3);
@@ -1894,12 +1890,10 @@ fn late_block_commits_2_1() {
18941890
// how many commit do we expect to see counted in the current window?
18951891
let mut expected_window_commits = if ix >= (MINING_COMMITMENT_WINDOW as usize) {
18961892
(MINING_COMMITMENT_WINDOW - 1) as usize
1893+
} else if ix >= 3 {
1894+
ix
18971895
} else {
1898-
if ix >= 3 {
1899-
ix
1900-
} else {
1901-
ix + 1
1902-
}
1896+
ix + 1
19031897
};
19041898
// there were 2 burn blocks before we started mining
19051899
let expected_window_size = cmp::min(MINING_COMMITMENT_WINDOW as usize, ix + 3);

stackslib/src/chainstate/nakamoto/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4431,13 +4431,11 @@ impl NakamotoChainState {
44314431
"Could not advance tenure, even though tenure changed".into(),
44324432
));
44334433
}
4434-
} else {
4435-
if coinbase_height != parent_coinbase_height {
4436-
// this should be unreachable
4437-
return Err(ChainstateError::InvalidStacksBlock(
4438-
"Advanced tenure even though a new tenure did not happen".into(),
4439-
));
4440-
}
4434+
} else if coinbase_height != parent_coinbase_height {
4435+
// this should be unreachable
4436+
return Err(ChainstateError::InvalidStacksBlock(
4437+
"Advanced tenure even though a new tenure did not happen".into(),
4438+
));
44414439
}
44424440

44434441
// begin processing this block

stackslib/src/chainstate/nakamoto/staging_blocks.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -823,12 +823,10 @@ impl StacksChainState {
823823
} else {
824824
return Err(DBError::NotFoundError.into());
825825
}
826+
} else if readwrite {
827+
OpenFlags::SQLITE_OPEN_READ_WRITE
826828
} else {
827-
if readwrite {
828-
OpenFlags::SQLITE_OPEN_READ_WRITE
829-
} else {
830-
OpenFlags::SQLITE_OPEN_READ_ONLY
831-
}
829+
OpenFlags::SQLITE_OPEN_READ_ONLY
832830
};
833831
let conn = sqlite_open(path, flags, false)?;
834832
if !exists {

stackslib/src/chainstate/nakamoto/tests/node.rs

Lines changed: 56 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -961,19 +961,17 @@ impl TestStacksNode {
961961
.expect("FATAL: chain tip is not a Nakamoto block");
962962
assert_eq!(nakamoto_chain_tip, &nakamoto_block.header);
963963
}
964+
} else if try_to_process {
965+
test_debug!(
966+
"Did NOT accept Nakamoto block {}",
967+
&block_to_store.block_id()
968+
);
969+
break;
964970
} else {
965-
if try_to_process {
966-
test_debug!(
967-
"Did NOT accept Nakamoto block {}",
968-
&block_to_store.block_id()
969-
);
970-
break;
971-
} else {
972-
test_debug!(
973-
"Test will NOT process Nakamoto block {}",
974-
&block_to_store.block_id()
975-
);
976-
}
971+
test_debug!(
972+
"Test will NOT process Nakamoto block {}",
973+
&block_to_store.block_id()
974+
);
977975
}
978976

979977
if !malleablize {
@@ -2029,34 +2027,32 @@ impl<'a> TestPeer<'a> {
20292027
.unwrap()
20302028
.is_none());
20312029
}
2032-
} else {
2033-
if parent_block_header
2034-
.anchored_header
2035-
.as_stacks_nakamoto()
2036-
.is_some()
2037-
{
2038-
assert_eq!(
2039-
NakamotoChainState::get_ongoing_tenure(
2040-
&mut chainstate.index_conn(),
2041-
&block.block_id()
2042-
)
2043-
.unwrap()
2044-
.unwrap(),
2045-
NakamotoChainState::get_ongoing_tenure(
2046-
&mut chainstate.index_conn(),
2047-
&parent_block_header.index_block_hash()
2048-
)
2049-
.unwrap()
2050-
.unwrap()
2051-
);
2052-
} else {
2053-
assert!(NakamotoChainState::get_ongoing_tenure(
2030+
} else if parent_block_header
2031+
.anchored_header
2032+
.as_stacks_nakamoto()
2033+
.is_some()
2034+
{
2035+
assert_eq!(
2036+
NakamotoChainState::get_ongoing_tenure(
2037+
&mut chainstate.index_conn(),
2038+
&block.block_id()
2039+
)
2040+
.unwrap()
2041+
.unwrap(),
2042+
NakamotoChainState::get_ongoing_tenure(
20542043
&mut chainstate.index_conn(),
20552044
&parent_block_header.index_block_hash()
20562045
)
20572046
.unwrap()
2058-
.is_none());
2059-
}
2047+
.unwrap()
2048+
);
2049+
} else {
2050+
assert!(NakamotoChainState::get_ongoing_tenure(
2051+
&mut chainstate.index_conn(),
2052+
&parent_block_header.index_block_hash()
2053+
)
2054+
.unwrap()
2055+
.is_none());
20602056
}
20612057

20622058
// get_block_found_tenure
@@ -2093,37 +2089,35 @@ impl<'a> TestPeer<'a> {
20932089
.unwrap()
20942090
.is_none());
20952091
}
2096-
} else {
2097-
if parent_block_header
2098-
.anchored_header
2099-
.as_stacks_nakamoto()
2100-
.is_some()
2101-
{
2102-
assert_eq!(
2103-
NakamotoChainState::get_block_found_tenure(
2104-
&mut chainstate.index_conn(),
2105-
&block.block_id(),
2106-
&block.header.consensus_hash
2107-
)
2108-
.unwrap()
2109-
.unwrap(),
2110-
NakamotoChainState::get_block_found_tenure(
2111-
&mut chainstate.index_conn(),
2112-
&block.block_id(),
2113-
&parent_block_header.consensus_hash
2114-
)
2115-
.unwrap()
2116-
.unwrap()
2117-
);
2118-
} else {
2119-
assert!(NakamotoChainState::get_block_found_tenure(
2092+
} else if parent_block_header
2093+
.anchored_header
2094+
.as_stacks_nakamoto()
2095+
.is_some()
2096+
{
2097+
assert_eq!(
2098+
NakamotoChainState::get_block_found_tenure(
2099+
&mut chainstate.index_conn(),
2100+
&block.block_id(),
2101+
&block.header.consensus_hash
2102+
)
2103+
.unwrap()
2104+
.unwrap(),
2105+
NakamotoChainState::get_block_found_tenure(
21202106
&mut chainstate.index_conn(),
21212107
&block.block_id(),
21222108
&parent_block_header.consensus_hash
21232109
)
21242110
.unwrap()
2125-
.is_none());
2126-
}
2111+
.unwrap()
2112+
);
2113+
} else {
2114+
assert!(NakamotoChainState::get_block_found_tenure(
2115+
&mut chainstate.index_conn(),
2116+
&block.block_id(),
2117+
&parent_block_header.consensus_hash
2118+
)
2119+
.unwrap()
2120+
.is_none());
21272121
}
21282122

21292123
// get_nakamoto_tenure_length

stackslib/src/chainstate/stacks/index/proofs.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -896,14 +896,12 @@ impl<T: MarfTrieId> TrieMerkleProof<T> {
896896
for child_ptr in node.ptrs() {
897897
if child_ptr.id != TrieNodeID::Empty as u8 && child_ptr.chr == chr {
898898
all_hashes.push(hash.clone());
899+
} else if ih >= hashes.len() {
900+
trace!("verify_get_hash: {} >= {}", ih, hashes.len());
901+
return None;
899902
} else {
900-
if ih >= hashes.len() {
901-
trace!("verify_get_hash: {} >= {}", ih, hashes.len());
902-
return None;
903-
} else {
904-
all_hashes.push(hashes[ih].clone());
905-
ih += 1;
906-
}
903+
all_hashes.push(hashes[ih].clone());
904+
ih += 1;
907905
}
908906
}
909907
if all_hashes.len() != count {

0 commit comments

Comments
 (0)