Skip to content

Commit

Permalink
Fix clippy::needless_borrow throughout stackslib
Browse files Browse the repository at this point in the history
Signed-off-by: Jacinta Ferrant <[email protected]>
  • Loading branch information
jferrant committed Jan 4, 2025
1 parent 8c23c0e commit 541ec42
Show file tree
Hide file tree
Showing 132 changed files with 1,532 additions and 1,603 deletions.
4 changes: 2 additions & 2 deletions stackslib/src/burnchains/affirmation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ pub fn read_prepare_phase_commits<B: BurnchainHeaderReader>(

let mut ret = vec![];
for header in headers.into_iter() {
let blk = BurnchainDB::get_burnchain_block(&burnchain_tx.conn(), &header.block_hash)
let blk = BurnchainDB::get_burnchain_block(burnchain_tx.conn(), &header.block_hash)
.unwrap_or_else(|_| {
panic!(
"BUG: failed to load prepare-phase block {} ({})",
Expand Down Expand Up @@ -1126,7 +1126,7 @@ pub fn find_pox_anchor_block<B: BurnchainHeaderReader>(
let prepare_ops_valid =
inner_find_valid_prepare_phase_commits(burnchain_tx, reward_cycle, indexer, burnchain)?;
let anchor_block_and_descendancy_opt = find_heaviest_block_commit(
&burnchain_tx,
burnchain_tx,
indexer,
&prepare_ops_valid,
burnchain.pox_constants.anchor_threshold,
Expand Down
2 changes: 1 addition & 1 deletion stackslib/src/burnchains/bitcoin/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ impl SegwitBitcoinAddress {
let mut bytes_u5: Vec<u5> = vec![u5::try_from_u8(self.witness_version())
.expect("FATAL: bad witness version does not fit into a u5")];
bytes_u5.extend_from_slice(&bytes.to_base32());
let addr = bech32::encode(&hrp, bytes_u5, self.bech32_variant())
let addr = bech32::encode(hrp, bytes_u5, self.bech32_variant())
.expect("FATAL: could not encode segwit address");
addr
}
Expand Down
6 changes: 3 additions & 3 deletions stackslib/src/burnchains/bitcoin/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl BitcoinTxInputStructured {
let i2 = &instructions[1];

match (i1, i2) {
(Instruction::PushBytes(ref _data1), Instruction::PushBytes(ref data2)) => {
(Instruction::PushBytes(_data1), Instruction::PushBytes(data2)) => {
// data2 is a pubkey?
match BitcoinPublicKey::from_slice(data2) {
Ok(pubkey) => {
Expand Down Expand Up @@ -1277,7 +1277,7 @@ mod tests {
let raw_in = BitcoinTxInputRaw::from_bitcoin_witness_script_sig(
&txin.script_sig,
txin.witness.clone(),
to_txid(&txin),
to_txid(txin),
);
assert_eq!(raw_in, inputs[i]);
}
Expand All @@ -1290,7 +1290,7 @@ mod tests {
}

let segwit_out =
BitcoinTxOutput::from_bitcoin_txout(BitcoinNetworkType::Mainnet, &txout)
BitcoinTxOutput::from_bitcoin_txout(BitcoinNetworkType::Mainnet, txout)
.unwrap();
assert_eq!(segwit_out, outputs[j]);
j += 1;
Expand Down
19 changes: 9 additions & 10 deletions stackslib/src/burnchains/bitcoin/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,7 @@ impl BitcoinBlockParser {
}

// block transactions must match header merkle root
let tx_merkle_root =
bitcoin_merkle_root(block.txdata.iter().map(|ref tx| tx.txid()).collect());
let tx_merkle_root = bitcoin_merkle_root(block.txdata.iter().map(|tx| tx.txid()).collect());

if block.header.merkle_root != tx_merkle_root {
return false;
Expand All @@ -275,15 +274,15 @@ impl BitcoinBlockParser {
return None;
}

let script_pieces = bits::parse_script(&data_output);
let script_pieces = bits::parse_script(data_output);
if script_pieces.len() != 2 {
// not OP_RETURN <data>
test_debug!("Data output does not encode a valid OP_RETURN");
return None;
}

match (&script_pieces[0], &script_pieces[1]) {
(Instruction::Op(ref opcode), Instruction::PushBytes(ref data)) => {
(Instruction::Op(ref opcode), Instruction::PushBytes(data)) => {
if *opcode != btc_opcodes::OP_RETURN {
test_debug!("Data output does not use a standard OP_RETURN");
return None;
Expand Down Expand Up @@ -351,7 +350,7 @@ impl BitcoinBlockParser {
fn parse_inputs_structured(tx: &Transaction) -> Option<Vec<BitcoinTxInput>> {
let mut ret = vec![];
for inp in &tx.input {
match BitcoinTxInput::from_bitcoin_txin_structured(&inp) {
match BitcoinTxInput::from_bitcoin_txin_structured(inp) {
None => {
test_debug!("Failed to parse input");
return None;
Expand All @@ -369,7 +368,7 @@ impl BitcoinBlockParser {
fn parse_inputs_raw(tx: &Transaction) -> Vec<BitcoinTxInput> {
let mut ret = vec![];
for inp in &tx.input {
ret.push(BitcoinTxInput::from_bitcoin_txin_raw(&inp));
ret.push(BitcoinTxInput::from_bitcoin_txin_raw(inp));
}
ret
}
Expand All @@ -388,9 +387,9 @@ impl BitcoinBlockParser {
let mut ret = vec![];
for outp in &tx.output[1..tx.output.len()] {
let out_opt = if BitcoinBlockParser::allow_segwit_outputs(epoch_id) {
BitcoinTxOutput::from_bitcoin_txout(self.network_id, &outp)
BitcoinTxOutput::from_bitcoin_txout(self.network_id, outp)
} else {
BitcoinTxOutput::from_bitcoin_txout_legacy(self.network_id, &outp)
BitcoinTxOutput::from_bitcoin_txout_legacy(self.network_id, outp)
};
match out_opt {
None => {
Expand Down Expand Up @@ -509,7 +508,7 @@ impl BitcoinBlockParser {
}

// parse it
let burn_block = self.parse_block(&block, height, epoch_id);
let burn_block = self.parse_block(block, height, epoch_id);
Some(burn_block)
}
}
Expand All @@ -525,7 +524,7 @@ impl BurnchainBlockParser for BitcoinBlockParser {
match ipc_block.block_message {
btc_message::NetworkMessage::Block(ref block) => {
match self.process_block(
&block,
block,
&ipc_block.header_data.block_header,
ipc_block.header_data.block_height,
epoch_id,
Expand Down
6 changes: 3 additions & 3 deletions stackslib/src/burnchains/bitcoin/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ impl BitcoinIndexer {
network_id: BitcoinNetworkType,
) -> Result<SpvClient, btc_error> {
SpvClient::new_without_migration(
&reorg_headers_path,
reorg_headers_path,
start_block,
end_block,
network_id,
Expand All @@ -486,7 +486,7 @@ impl BitcoinIndexer {
network_id: BitcoinNetworkType,
) -> Result<SpvClient, btc_error> {
SpvClient::new(
&reorg_headers_path,
reorg_headers_path,
start_block,
end_block,
network_id,
Expand Down Expand Up @@ -3476,7 +3476,7 @@ mod test {

// set up SPV client so we don't have chain work at first
let mut spv_client = SpvClient::new_without_migration(
&db_path,
db_path,
0,
None,
BitcoinNetworkType::Regtest,
Expand Down
4 changes: 2 additions & 2 deletions stackslib/src/burnchains/bitcoin/spv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,13 +724,13 @@ impl SpvClient {
.next()
.map_err(|e| btc_error::DBError(db_error::SqliteError(e)))?
{
let height: u64 = u64::from_column(&row, "height")?;
let height: u64 = u64::from_column(row, "height")?;
if height != next_height {
break;
}
next_height += 1;

let next_header = BlockHeader::from_row(&row)?;
let next_header = BlockHeader::from_row(row)?;
headers.push(LoneBlockHeader {
header: next_header,
tx_count: VarInt(0),
Expand Down
20 changes: 10 additions & 10 deletions stackslib/src/burnchains/burnchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl BurnchainStateTransition {

/// Get the transaction IDs of all accepted burnchain operations in this block
pub fn txids(&self) -> Vec<Txid> {
self.accepted_ops.iter().map(|ref op| op.txid()).collect()
self.accepted_ops.iter().map(|op| op.txid()).collect()
}

/// Get the sum of all burnchain tokens spent in this burnchain block's accepted operations
Expand Down Expand Up @@ -196,7 +196,7 @@ impl BurnchainStateTransition {

// find all VRF leader keys that were consumed by the block commits of this block
let consumed_leader_keys =
sort_tx.get_consumed_leader_keys(&parent_snapshot, &block_commits)?;
sort_tx.get_consumed_leader_keys(parent_snapshot, &block_commits)?;

// assemble the commit windows
let mut windowed_block_commits = vec![block_commits];
Expand Down Expand Up @@ -355,7 +355,7 @@ impl BurnchainStateTransition {
);
}

accepted_ops.sort_by(|ref a, ref b| a.vtxindex().partial_cmp(&b.vtxindex()).unwrap());
accepted_ops.sort_by(|a, b| a.vtxindex().partial_cmp(&b.vtxindex()).unwrap());

Ok(BurnchainStateTransition {
burn_dist,
Expand Down Expand Up @@ -425,7 +425,7 @@ impl BurnchainBlock {
BurnchainBlock::Bitcoin(ref data) => data
.txs
.iter()
.map(|ref tx| BurnchainTransaction::Bitcoin((*tx).clone()))
.map(|tx| BurnchainTransaction::Bitcoin((*tx).clone()))
.collect(),
}
}
Expand Down Expand Up @@ -850,7 +850,7 @@ impl Burnchain {
}
x if x == Opcodes::TransferStx as u8 => {
let pre_stx_txid = TransferStxOp::get_sender_txid(burn_tx).ok()?;
let pre_stx_tx = match pre_stx_op_map.get(&pre_stx_txid) {
let pre_stx_tx = match pre_stx_op_map.get(pre_stx_txid) {
Some(tx_ref) => Some(BlockstackOperationType::PreStx(tx_ref.clone())),
None => burnchain_db.find_burnchain_op(indexer, pre_stx_txid),
};
Expand Down Expand Up @@ -879,7 +879,7 @@ impl Burnchain {
}
x if x == Opcodes::StackStx as u8 => {
let pre_stx_txid = StackStxOp::get_sender_txid(burn_tx).ok()?;
let pre_stx_tx = match pre_stx_op_map.get(&pre_stx_txid) {
let pre_stx_tx = match pre_stx_op_map.get(pre_stx_txid) {
Some(tx_ref) => Some(BlockstackOperationType::PreStx(tx_ref.clone())),
None => burnchain_db.find_burnchain_op(indexer, pre_stx_txid),
};
Expand Down Expand Up @@ -914,7 +914,7 @@ impl Burnchain {
}
x if x == Opcodes::DelegateStx as u8 => {
let pre_stx_txid = DelegateStxOp::get_sender_txid(burn_tx).ok()?;
let pre_stx_tx = match pre_stx_op_map.get(&pre_stx_txid) {
let pre_stx_tx = match pre_stx_op_map.get(pre_stx_txid) {
Some(tx_ref) => Some(BlockstackOperationType::PreStx(tx_ref.clone())),
None => burnchain_db.find_burnchain_op(indexer, pre_stx_txid),
};
Expand Down Expand Up @@ -943,7 +943,7 @@ impl Burnchain {
}
x if x == Opcodes::VoteForAggregateKey as u8 => {
let pre_stx_txid = VoteForAggregateKeyOp::get_sender_txid(burn_tx).ok()?;
let pre_stx_tx = match pre_stx_op_map.get(&pre_stx_txid) {
let pre_stx_tx = match pre_stx_op_map.get(pre_stx_txid) {
Some(tx_ref) => Some(BlockstackOperationType::PreStx(tx_ref.clone())),
None => burnchain_db.find_burnchain_op(indexer, pre_stx_txid),
};
Expand Down Expand Up @@ -1039,7 +1039,7 @@ impl Burnchain {
);

let _blockstack_txs =
burnchain_db.store_new_burnchain_block(burnchain, indexer, &block, epoch_id)?;
burnchain_db.store_new_burnchain_block(burnchain, indexer, block, epoch_id)?;
Burnchain::process_affirmation_maps(
burnchain,
burnchain_db,
Expand Down Expand Up @@ -1111,7 +1111,7 @@ impl Burnchain {
let blockstack_txs = burnchain_db.store_new_burnchain_block(
burnchain,
indexer,
&block,
block,
cur_epoch.epoch_id,
)?;

Expand Down
10 changes: 5 additions & 5 deletions stackslib/src/burnchains/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub(crate) fn apply_blockstack_txs_safety_checks(
);

// safety -- make sure these are in order
blockstack_txs.sort_by(|ref a, ref b| a.vtxindex().partial_cmp(&b.vtxindex()).unwrap());
blockstack_txs.sort_by(|a, b| a.vtxindex().partial_cmp(&b.vtxindex()).unwrap());

// safety -- no duplicate vtxindex (shouldn't happen but crash if so)
if blockstack_txs.len() > 1 {
Expand Down Expand Up @@ -349,7 +349,7 @@ impl BurnchainDBTransaction<'_> {
let args = params![affirmation_map.encode(), u64_to_sql(weight)?];
match self.sql_tx.execute(sql, args) {
Ok(_) => {
let am_id = BurnchainDB::get_affirmation_map_id(&self.sql_tx, &affirmation_map)?
let am_id = BurnchainDB::get_affirmation_map_id(&self.sql_tx, affirmation_map)?
.expect("BUG: no affirmation ID for affirmation map we just inserted");
Ok(am_id)
}
Expand Down Expand Up @@ -1231,7 +1231,7 @@ impl BurnchainDB {
self,
block_header,
epoch_id,
&tx,
tx,
&pre_stx_ops,
);
if let Some(classified_tx) = result {
Expand Down Expand Up @@ -1409,7 +1409,7 @@ impl BurnchainDB {
blockstack_ops.len()
);
db_tx.store_burnchain_db_entry(block_header)?;
db_tx.store_blockstack_ops(burnchain, indexer, &block_header, blockstack_ops)?;
db_tx.store_blockstack_ops(burnchain, indexer, block_header, blockstack_ops)?;

db_tx.commit()?;
Ok(())
Expand Down Expand Up @@ -1459,7 +1459,7 @@ impl BurnchainDB {
) -> Result<Option<LeaderBlockCommitOp>, DBError> {
let qry = "SELECT txid FROM block_commit_metadata WHERE block_height = ?1 AND vtxindex = ?2 AND burn_block_hash = ?3";
let args = params![block_ptr, vtxindex, header_hash];
let txid = match query_row(&conn, qry, args) {
let txid = match query_row(conn, qry, args) {
Ok(Some(txid)) => txid,
Ok(None) => {
test_debug!(
Expand Down
2 changes: 1 addition & 1 deletion stackslib/src/burnchains/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl BurnchainTransaction {
BurnchainTransaction::Bitcoin(ref btc) => btc
.outputs
.iter()
.map(|ref o| BurnchainRecipient::try_from_bitcoin_output(o))
.map(BurnchainRecipient::try_from_bitcoin_output)
.collect(),
}
}
Expand Down
4 changes: 2 additions & 2 deletions stackslib/src/burnchains/tests/affirmation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ pub fn make_reward_cycle_with_vote(
let mut commits = vec![];
for i in 0..parent_commits.len() {
let mut block_commit = make_simple_block_commit(
&burnchain,
burnchain,
parent_commits[i].as_ref(),
&block_header,
next_block_hash(),
Expand Down Expand Up @@ -388,7 +388,7 @@ pub fn make_reward_cycle_with_vote(
block_commit.parent_vtxindex
);

if let Some(ref parent_commit) = parent_commits[i].as_ref() {
if let Some(parent_commit) = parent_commits[i].as_ref() {
assert!(parent_commit.block_height != block_commit.block_height);
assert!(
parent_commit.block_height == u64::from(block_commit.parent_block_ptr)
Expand Down
20 changes: 8 additions & 12 deletions stackslib/src/burnchains/tests/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ pub fn make_simple_block_commit(
new_op.commit_outs = vec![PoxAddress::standard_burn_address(false)];
}

if let Some(ref op) = parent {
if let Some(op) = parent {
new_op.parent_block_ptr = op.block_height as u32;
new_op.parent_vtxindex = op.vtxindex as u16;
};
Expand Down Expand Up @@ -639,18 +639,14 @@ fn test_get_commit_at() {
}

for i in 0..5 {
let cmt = BurnchainDB::get_commit_at(
&burnchain_db.conn(),
&headers,
(first_height + i) as u32,
0,
)
.unwrap()
.unwrap();
let cmt =
BurnchainDB::get_commit_at(burnchain_db.conn(), &headers, (first_height + i) as u32, 0)
.unwrap()
.unwrap();
assert_eq!(cmt, cmts[i as usize]);
}

let cmt = BurnchainDB::get_commit_at(&burnchain_db.conn(), &headers, 5, 0)
let cmt = BurnchainDB::get_commit_at(burnchain_db.conn(), &headers, 5, 0)
.unwrap()
.unwrap();
assert_eq!(cmt, cmts[4]);
Expand Down Expand Up @@ -681,12 +677,12 @@ fn test_get_commit_at() {
)
.unwrap();

let cmt = BurnchainDB::get_commit_at(&burnchain_db.conn(), &headers, 5, 0)
let cmt = BurnchainDB::get_commit_at(burnchain_db.conn(), &headers, 5, 0)
.unwrap()
.unwrap();
assert_eq!(cmt, cmts[4]);

let cmt = BurnchainDB::get_commit_at(&burnchain_db.conn(), &fork_headers, 5, 1)
let cmt = BurnchainDB::get_commit_at(burnchain_db.conn(), &fork_headers, 5, 1)
.unwrap()
.unwrap();
assert_eq!(cmt, fork_cmt);
Expand Down
Loading

0 comments on commit 541ec42

Please sign in to comment.