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

Apply clippy::unnecesary_to_owned and clippy::unwrap_or_default fixes throughout stacks core #5741

Merged
merged 5 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion stackslib/src/burnchains/bitcoin/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ impl BitcoinBlockParser {
match (inputs_opt, outputs_opt) {
(Some(inputs), Some(outputs)) => {
Some(BitcoinTransaction {
txid: Txid::from_vec_be(&tx.txid().as_bytes().to_vec()).unwrap(), // this *should* panic if it fails
txid: Txid::from_vec_be(tx.txid().as_bytes().as_ref()).unwrap(), // this *should* panic if it fails
jferrant marked this conversation as resolved.
Show resolved Hide resolved
vtxindex: vtxindex as u32,
opcode,
data,
Expand Down
4 changes: 2 additions & 2 deletions stackslib/src/burnchains/bitcoin/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl BitcoinIndexer {

// instantiate headers DB
let _ = SpvClient::new(
&working_dir_path.to_str().unwrap().to_string(),
working_dir_path.to_str().unwrap(),
0,
None,
BitcoinNetworkType::Regtest,
Expand All @@ -236,7 +236,7 @@ impl BitcoinIndexer {
)
.expect(&format!(
"Failed to open {:?}",
&working_dir_path.to_str().unwrap().to_string()
working_dir_path.to_str().unwrap()
));

BitcoinIndexer {
Expand Down
16 changes: 9 additions & 7 deletions stackslib/src/burnchains/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,15 @@ impl TestMiner {
);
match self.vrf_key_map.get(vrf_pubkey) {
Some(ref prover_key) => {
let proof = VRF::prove(prover_key, &last_sortition_hash.as_bytes().to_vec());
let valid =
match VRF::verify(vrf_pubkey, &proof, &last_sortition_hash.as_bytes().to_vec())
{
Ok(v) => v,
Err(e) => false,
};
let proof = VRF::prove(prover_key, last_sortition_hash.as_bytes().as_ref());
let valid = match VRF::verify(
vrf_pubkey,
&proof,
last_sortition_hash.as_bytes().as_ref(),
) {
jferrant marked this conversation as resolved.
Show resolved Hide resolved
Ok(v) => v,
Err(e) => false,
};
assert!(valid);
Some(proof)
}
Expand Down
2 changes: 1 addition & 1 deletion stackslib/src/chainstate/coordinator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ pub fn setup_states_with_epochs(
);

let block_limit = ExecutionCost::max_value();
let initial_balances = initial_balances.unwrap_or(vec![]);
let initial_balances = initial_balances.unwrap_or_default();
for path in paths.iter() {
let burnchain = get_burnchain(path, pox_consts.clone());

Expand Down
2 changes: 1 addition & 1 deletion stackslib/src/chainstate/nakamoto/coordinator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pub fn make_replay_peer<'a>(peer: &mut TestPeer<'a>) -> TestPeer<'a> {
replay_config.http_port = 0;
replay_config.test_stackers = peer.config.test_stackers.clone();

let test_stackers = replay_config.test_stackers.clone().unwrap_or(vec![]);
let test_stackers = replay_config.test_stackers.clone().unwrap_or_default();
let mut test_signers = replay_config.test_signers.clone().unwrap();
let mut replay_peer = TestPeer::new(replay_config);
let observer = TestEventObserver::new();
Expand Down
4 changes: 2 additions & 2 deletions stackslib/src/chainstate/nakamoto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4123,7 +4123,7 @@ impl NakamotoChainState {
.iter()
.enumerate()
.fold(HashMap::new(), |mut map, (ix, addr)| {
map.entry(addr).or_insert_with(Vec::new).push(ix);
map.entry(addr).or_default().push(ix);
map
});

Expand Down Expand Up @@ -4524,7 +4524,7 @@ impl NakamotoChainState {
let matured_rewards = matured_miner_rewards_opt
.as_ref()
.map(|matured_miner_rewards| matured_miner_rewards.consolidate())
.unwrap_or(vec![]);
.unwrap_or_default();

let mut lockup_events =
match Self::finish_block(&mut clarity_tx, matured_miner_rewards_opt.as_ref()) {
Expand Down
6 changes: 3 additions & 3 deletions stackslib/src/chainstate/nakamoto/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ pub fn test_nakamoto_first_tenure_block_syntactic_validation() {
};

let proof_bytes = hex_bytes("9275df67a68c8745c0ff97b48201ee6db447f7c93b23ae24cdc2400f52fdb08a1a6ac7ec71bf9c9c76e96ee4675ebff60625af28718501047bfd87b810c2d2139b73c23bd69de66360953a642c2a330a").unwrap();
let proof = VRFProof::from_bytes(&proof_bytes[..].to_vec()).unwrap();
let proof = VRFProof::from_bytes(&proof_bytes[..]).unwrap();

let coinbase_payload =
TransactionPayload::Coinbase(CoinbasePayload([0x12; 32]), None, Some(proof.clone()));
Expand Down Expand Up @@ -591,10 +591,10 @@ pub fn test_load_store_update_nakamoto_blocks() {

let private_key = StacksPrivateKey::new();
let epoch2_proof_bytes = hex_bytes("9275df67a68c8745c0ff97b48201ee6db447f7c93b23ae24cdc2400f52fdb08a1a6ac7ec71bf9c9c76e96ee4675ebff60625af28718501047bfd87b810c2d2139b73c23bd69de66360953a642c2a330a").unwrap();
let epoch2_proof = VRFProof::from_bytes(&epoch2_proof_bytes[..].to_vec()).unwrap();
let epoch2_proof = VRFProof::from_bytes(&epoch2_proof_bytes[..]).unwrap();

let nakamoto_proof_bytes = hex_bytes("973c815ac3e81a4aff3243f3d8310d24ab9783acd6caa4dcfab20a3744584b2f966acf08140e1a7e1e685695d51b1b511f4f19260a21887244a6c47f7637b8bdeaf5eafe85c1975bab75bc0668fe8a0b").unwrap();
let nakamoto_proof = VRFProof::from_bytes(&nakamoto_proof_bytes[..].to_vec()).unwrap();
let nakamoto_proof = VRFProof::from_bytes(&nakamoto_proof_bytes[..]).unwrap();

let coinbase_payload = TransactionPayload::Coinbase(
CoinbasePayload([0x12; 32]),
Expand Down
8 changes: 4 additions & 4 deletions stackslib/src/chainstate/stacks/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ mod test {
#[test]
fn codec_stacks_block_ecvrf_proof() {
let proof_bytes = hex_bytes("9275df67a68c8745c0ff97b48201ee6db447f7c93b23ae24cdc2400f52fdb08a1a6ac7ec71bf9c9c76e96ee4675ebff60625af28718501047bfd87b810c2d2139b73c23bd69de66360953a642c2a330a").unwrap();
let proof = VRFProof::from_bytes(&proof_bytes[..].to_vec()).unwrap();
let proof = VRFProof::from_bytes(&proof_bytes[..]).unwrap();

check_codec_and_corruption::<VRFProof>(&proof, &proof_bytes);
}
Expand All @@ -991,7 +991,7 @@ mod test {
#[test]
fn codec_stacks_block_header() {
let proof_bytes = hex_bytes("9275df67a68c8745c0ff97b48201ee6db447f7c93b23ae24cdc2400f52fdb08a1a6ac7ec71bf9c9c76e96ee4675ebff60625af28718501047bfd87b810c2d2139b73c23bd69de66360953a642c2a330a").unwrap();
let proof = VRFProof::from_bytes(&proof_bytes[..].to_vec()).unwrap();
let proof = VRFProof::from_bytes(&proof_bytes[..]).unwrap();

let header = StacksBlockHeader {
version: 0x12,
Expand Down Expand Up @@ -1710,7 +1710,7 @@ mod test {
tx_merkle_root
};
let mut block_header_dup_tx = header.clone();
block_header_dup_tx.tx_merkle_root = get_tx_root(&txs.to_vec());
block_header_dup_tx.tx_merkle_root = get_tx_root(txs);

let block = StacksBlock {
header: block_header_dup_tx,
Expand Down Expand Up @@ -1957,7 +1957,7 @@ mod test {
);

let proof_bytes = hex_bytes("9275df67a68c8745c0ff97b48201ee6db447f7c93b23ae24cdc2400f52fdb08a1a6ac7ec71bf9c9c76e96ee4675ebff60625af28718501047bfd87b810c2d2139b73c23bd69de66360953a642c2a330a").unwrap();
let proof = VRFProof::from_bytes(&proof_bytes[..].to_vec()).unwrap();
let proof = VRFProof::from_bytes(&proof_bytes[..]).unwrap();
let tx_coinbase_proof = StacksTransaction::new(
TransactionVersion::Testnet,
origin_auth.clone(),
Expand Down
4 changes: 2 additions & 2 deletions stackslib/src/chainstate/stacks/boot/pox_4_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7592,8 +7592,8 @@ fn test_deser_abort() {
";

let tx_payload = TransactionPayload::new_smart_contract(
&format!("hello-world"),
&contract.to_string(),
"hello-world",
contract,
Some(ClarityVersion::Clarity2),
)
.unwrap();
Expand Down
30 changes: 10 additions & 20 deletions stackslib/src/chainstate/stacks/db/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,11 +1209,9 @@ mod test {
fn get_tip_ancestor() {
let mut chainstate = instantiate_chainstate(false, 0x80000000, function_name!());
let miner_1 =
StacksAddress::from_string(&"SP1A2K3ENNA6QQ7G8DVJXM24T6QMBDVS7D0TRTAR5".to_string())
.unwrap();
StacksAddress::from_string("SP1A2K3ENNA6QQ7G8DVJXM24T6QMBDVS7D0TRTAR5").unwrap();
let user_1 =
StacksAddress::from_string(&"SP2837ZMC89J40K4YTS64B00M7065C6X46JX6ARG0".to_string())
.unwrap();
StacksAddress::from_string("SP2837ZMC89J40K4YTS64B00M7065C6X46JX6ARG0").unwrap();
let mut miner_reward = make_dummy_miner_payment_schedule(&miner_1, 500, 0, 0, 1000, 1000);
let user_reward = make_dummy_user_payment_schedule(&user_1, 500, 0, 0, 750, 1000, 1);

Expand Down Expand Up @@ -1276,8 +1274,7 @@ mod test {
fn load_store_miner_payment_schedule() {
let mut chainstate = instantiate_chainstate(false, 0x80000000, function_name!());
let miner_1 =
StacksAddress::from_string(&"SP1A2K3ENNA6QQ7G8DVJXM24T6QMBDVS7D0TRTAR5".to_string())
.unwrap();
StacksAddress::from_string("SP1A2K3ENNA6QQ7G8DVJXM24T6QMBDVS7D0TRTAR5").unwrap();

let mut miner_reward = make_dummy_miner_payment_schedule(&miner_1, 500, 0, 0, 1000, 1000);

Expand Down Expand Up @@ -1322,8 +1319,7 @@ mod test {
fn load_store_miner_payment_schedule_pay_contract() {
let mut chainstate = instantiate_chainstate(false, 0x80000000, function_name!());
let miner_1 =
StacksAddress::from_string(&"SP1A2K3ENNA6QQ7G8DVJXM24T6QMBDVS7D0TRTAR5".to_string())
.unwrap();
StacksAddress::from_string("SP1A2K3ENNA6QQ7G8DVJXM24T6QMBDVS7D0TRTAR5").unwrap();

let mut miner_reward = make_dummy_miner_payment_schedule(&miner_1, 500, 0, 0, 1000, 1000);
miner_reward.recipient = PrincipalData::Contract(QualifiedContractIdentifier::transient());
Expand Down Expand Up @@ -1368,8 +1364,7 @@ mod test {
#[test]
fn miner_reward_one_miner_no_tx_fees_no_users() {
let miner_1 =
StacksAddress::from_string(&"SP1A2K3ENNA6QQ7G8DVJXM24T6QMBDVS7D0TRTAR5".to_string())
.unwrap();
StacksAddress::from_string("SP1A2K3ENNA6QQ7G8DVJXM24T6QMBDVS7D0TRTAR5").unwrap();
let participant = make_dummy_miner_payment_schedule(&miner_1, 500, 0, 0, 1000, 1000);

let (parent_reward, miner_reward) = StacksChainState::calculate_miner_reward(
Expand Down Expand Up @@ -1398,8 +1393,7 @@ mod test {
#[test]
fn miner_reward_one_miner_no_tx_fees_no_users_pay_contract() {
let miner_1 =
StacksAddress::from_string(&"SP1A2K3ENNA6QQ7G8DVJXM24T6QMBDVS7D0TRTAR5".to_string())
.unwrap();
StacksAddress::from_string("SP1A2K3ENNA6QQ7G8DVJXM24T6QMBDVS7D0TRTAR5").unwrap();
let mut participant = make_dummy_miner_payment_schedule(&miner_1, 500, 0, 0, 1000, 1000);
participant.recipient = PrincipalData::Contract(QualifiedContractIdentifier::transient());

Expand Down Expand Up @@ -1437,11 +1431,9 @@ mod test {
#[test]
fn miner_reward_one_miner_one_user_no_tx_fees() {
let miner_1 =
StacksAddress::from_string(&"SP1A2K3ENNA6QQ7G8DVJXM24T6QMBDVS7D0TRTAR5".to_string())
.unwrap();
StacksAddress::from_string("SP1A2K3ENNA6QQ7G8DVJXM24T6QMBDVS7D0TRTAR5").unwrap();
let user_1 =
StacksAddress::from_string(&"SP2837ZMC89J40K4YTS64B00M7065C6X46JX6ARG0".to_string())
.unwrap();
StacksAddress::from_string("SP2837ZMC89J40K4YTS64B00M7065C6X46JX6ARG0").unwrap();

let miner = make_dummy_miner_payment_schedule(&miner_1, 500, 0, 0, 250, 1000);
let user = make_dummy_user_payment_schedule(&user_1, 500, 0, 0, 750, 1000, 1);
Expand Down Expand Up @@ -1485,12 +1477,10 @@ mod test {
#[test]
fn miner_reward_tx_fees() {
let miner_1 =
StacksAddress::from_string(&"SP1A2K3ENNA6QQ7G8DVJXM24T6QMBDVS7D0TRTAR5".to_string())
.unwrap();
StacksAddress::from_string("SP1A2K3ENNA6QQ7G8DVJXM24T6QMBDVS7D0TRTAR5").unwrap();

let parent_miner_1 =
StacksAddress::from_string(&"SP2QDF700V0FWXVNQJJ4XFGBWE6R2Y4APTSFQNBVE".to_string())
.unwrap();
StacksAddress::from_string("SP2QDF700V0FWXVNQJJ4XFGBWE6R2Y4APTSFQNBVE").unwrap();

let participant = make_dummy_miner_payment_schedule(&miner_1, 500, 100, 105, 1000, 1000);
let parent_participant =
Expand Down
12 changes: 6 additions & 6 deletions stackslib/src/chainstate/stacks/db/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ impl StacksChainState {

let _ = StacksChainState::mkdirs(&block_path)?;

block_path.push(to_hex(block_hash_bytes).to_string());
block_path.push(to_hex(block_hash_bytes));
let blocks_path_str = block_path
.to_str()
.ok_or_else(|| Error::DBError(db_error::ParseError))?
Expand Down Expand Up @@ -996,7 +996,7 @@ impl StacksChainState {
// load up associated block data
staging_block.block_data =
StacksChainState::load_block_bytes(blocks_path, consensus_hash, block_hash)?
.unwrap_or(vec![]);
.unwrap_or_default();
Ok(Some(staging_block))
}
_ => {
Expand Down Expand Up @@ -1162,7 +1162,7 @@ impl StacksChainState {
// load associated block data
staging_microblock.block_data =
StacksChainState::load_staging_microblock_bytes(blocks_conn, microblock_hash)?
.unwrap_or(vec![]);
.unwrap_or_default();
Ok(Some(staging_microblock))
}
None => {
Expand Down Expand Up @@ -6980,7 +6980,7 @@ pub mod test {
.unwrap();
let auth = TransactionAuth::from_p2pkh(&privk).unwrap();
let proof_bytes = hex_bytes("9275df67a68c8745c0ff97b48201ee6db447f7c93b23ae24cdc2400f52fdb08a1a6ac7ec71bf9c9c76e96ee4675ebff60625af28718501047bfd87b810c2d2139b73c23bd69de66360953a642c2a330a").unwrap();
let proof = VRFProof::from_bytes(&proof_bytes[..].to_vec()).unwrap();
let proof = VRFProof::from_bytes(&proof_bytes[..]).unwrap();

let mut tx_coinbase = StacksTransaction::new(
TransactionVersion::Testnet,
Expand Down Expand Up @@ -7045,7 +7045,7 @@ pub mod test {
.unwrap();
let auth = TransactionAuth::from_p2pkh(&privk).unwrap();
let proof_bytes = hex_bytes("9275df67a68c8745c0ff97b48201ee6db447f7c93b23ae24cdc2400f52fdb08a1a6ac7ec71bf9c9c76e96ee4675ebff60625af28718501047bfd87b810c2d2139b73c23bd69de66360953a642c2a330a").unwrap();
let proof = VRFProof::from_bytes(&proof_bytes[..].to_vec()).unwrap();
let proof = VRFProof::from_bytes(&proof_bytes[..]).unwrap();

let mut tx_coinbase = StacksTransaction::new(
TransactionVersion::Testnet,
Expand Down Expand Up @@ -8593,7 +8593,7 @@ pub mod test {
let num_mblocks = microblocks.len();

let proof_bytes = hex_bytes("9275df67a68c8745c0ff97b48201ee6db447f7c93b23ae24cdc2400f52fdb08a1a6ac7ec71bf9c9c76e96ee4675ebff60625af28718501047bfd87b810c2d2139b73c23bd69de66360953a642c2a330a").unwrap();
let proof = VRFProof::from_bytes(&proof_bytes[..].to_vec()).unwrap();
let proof = VRFProof::from_bytes(&proof_bytes[..]).unwrap();

let child_block_header = StacksBlockHeader {
version: 0x01,
Expand Down
2 changes: 1 addition & 1 deletion stackslib/src/chainstate/stacks/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2511,7 +2511,7 @@ impl StacksChainState {
Ok(txids)
})
.optional()?
.unwrap_or(vec![]);
.unwrap_or_default();

Ok(txids)
}
Expand Down
Loading
Loading