From 9992bee2bde59b61577f60a461696ae164ae0cf1 Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Thu, 15 Jun 2023 17:04:13 +0000 Subject: [PATCH] more clippy lol: bitcoind-tests --- bitcoind-tests/tests/setup/mod.rs | 6 ++-- bitcoind-tests/tests/setup/test_util.rs | 38 ++++++++++++------------- bitcoind-tests/tests/test_arith.rs | 10 +++---- bitcoind-tests/tests/test_cpp.rs | 5 ++-- bitcoind-tests/tests/test_csfs.rs | 12 ++++---- bitcoind-tests/tests/test_desc.rs | 24 ++++++++-------- bitcoind-tests/tests/test_introspect.rs | 10 +++---- 7 files changed, 51 insertions(+), 54 deletions(-) diff --git a/bitcoind-tests/tests/setup/mod.rs b/bitcoind-tests/tests/setup/mod.rs index 135af444..af7cb92b 100644 --- a/bitcoind-tests/tests/setup/mod.rs +++ b/bitcoind-tests/tests/setup/mod.rs @@ -18,8 +18,8 @@ pub mod test_util; pub fn setup(validate_pegin: bool) -> (ElementsD, Option, elements::BlockHash) { // Lookup bitcoind binary path let curr_dir = std::env::current_dir().unwrap(); - let bitcoind_path = curr_dir.clone().join("bin/bitcoind"); - let elementsd_path = curr_dir.clone().join("bin/elementsd"); + let bitcoind_path = curr_dir.join("bin/bitcoind"); + let elementsd_path = curr_dir.join("bin/elementsd"); std::env::set_var("BITCOIND_EXE", bitcoind_path); std::env::set_var("ELEMENTSD_EXE", elementsd_path); @@ -28,7 +28,7 @@ pub fn setup(validate_pegin: bool) -> (ElementsD, Option, elements::Bl if validate_pegin { let bitcoind_exe = bitcoind::exe_path().unwrap(); let bitcoind_conf = bitcoind::Conf::default(); - bitcoind = Some(bitcoind::BitcoinD::with_conf(&bitcoind_exe, &bitcoind_conf).unwrap()); + bitcoind = Some(bitcoind::BitcoinD::with_conf(bitcoind_exe, &bitcoind_conf).unwrap()); } let mut conf = elementsd::Conf::new(bitcoind.as_ref()); diff --git a/bitcoind-tests/tests/setup/test_util.rs b/bitcoind-tests/tests/setup/test_util.rs index 848e8f18..f73820e9 100644 --- a/bitcoind-tests/tests/setup/test_util.rs +++ b/bitcoind-tests/tests/setup/test_util.rs @@ -101,8 +101,8 @@ fn setup_keys( let mut x_only_keypairs = vec![]; let mut x_only_pks = vec![]; - for i in 0..n { - let keypair = bitcoin::key::KeyPair::from_secret_key(&secp_sign, &sks[i]); + for sk in &sks { + let keypair = bitcoin::key::KeyPair::from_secret_key(&secp_sign, sk); let (xpk, _parity) = bitcoin::key::XOnlyPublicKey::from_keypair(&keypair); x_only_keypairs.push(keypair); x_only_pks.push(xpk); @@ -114,13 +114,13 @@ impl TestData { // generate a fixed data for n keys pub(crate) fn new_fixed_data(n: usize, genesis_hash: BlockHash) -> Self { let (sks, pks, x_only_keypairs, x_only_pks) = setup_keys(n); - let sha256_pre = [0x12 as u8; 32]; + let sha256_pre = [0x12; 32]; let sha256 = sha256::Hash::hash(&sha256_pre); - let hash256_pre = [0x34 as u8; 32]; + let hash256_pre = [0x34; 32]; let hash256 = hash256::Hash::hash(&hash256_pre); - let hash160_pre = [0x56 as u8; 32]; + let hash160_pre = [0x56; 32]; let hash160 = hash160::Hash::hash(&hash160_pre); - let ripemd160_pre = [0x78 as u8; 32]; + let ripemd160_pre = [0x78; 32]; let ripemd160 = ripemd160::Hash::hash(&ripemd160_pre); let msg = CsfsMsg::from_slice(&[0x9a; 32]).unwrap(); @@ -182,8 +182,7 @@ pub fn parse_insane_ms( let ms = Miniscript::::from_str_insane(&ms).expect("only parsing valid minsicripts"); let mut translator = StrTranslatorLoose(0, pubdata); - let ms = ms.translate_pk(&mut translator).unwrap(); - ms + ms.translate_pk(&mut translator).unwrap() } /// Translate Abstract Str to Consensus Extensions @@ -193,9 +192,9 @@ impl<'a> ExtParamTranslator for StrExtTranslator<'a> { fn ext(&mut self, e: &String) -> Result { if e.starts_with("msg") { Ok(CovExtArgs::CsfsMsg(self.1.msg.clone())) - } else if e.starts_with("X") { + } else if e.starts_with('X') { let csfs_pk = CovExtArgs::XOnlyKey(CsfsKey(self.1.x_only_pks[self.0])); - self.0 = self.0 + 1; + self.0 += 1; Ok(csfs_pk) } else if e.starts_with("spk") { let default = elements::Script::from_str( @@ -233,15 +232,15 @@ struct StrDescPubKeyTranslator<'a>(usize, &'a PubData); impl<'a> Translator for StrDescPubKeyTranslator<'a> { fn pk(&mut self, pk_str: &String) -> Result { - let avail = !pk_str.ends_with("!"); + let avail = !pk_str.ends_with('!'); if avail { - self.0 = self.0 + 1; - if pk_str.starts_with("K") { + self.0 += 1; + if pk_str.starts_with('K') { Ok(DescriptorPublicKey::Single(SinglePub { origin: None, key: SinglePubKey::FullKey(self.1.pks[self.0]), })) - } else if pk_str.starts_with("X") { + } else if pk_str.starts_with('X') { Ok(DescriptorPublicKey::Single(SinglePub { origin: None, key: SinglePubKey::XOnly(self.1.x_only_pks[self.0]), @@ -286,15 +285,15 @@ struct StrTranslatorLoose<'a>(usize, &'a PubData); impl<'a> Translator for StrTranslatorLoose<'a> { fn pk(&mut self, pk_str: &String) -> Result { - let avail = !pk_str.ends_with("!"); + let avail = !pk_str.ends_with('!'); if avail { - self.0 = self.0 + 1; - if pk_str.starts_with("K") { + self.0 += 1; + if pk_str.starts_with('K') { Ok(DescriptorPublicKey::Single(SinglePub { origin: None, key: SinglePubKey::FullKey(self.1.pks[self.0]), })) - } else if pk_str.starts_with("X") { + } else if pk_str.starts_with('X') { Ok(DescriptorPublicKey::Single(SinglePub { origin: None, key: SinglePubKey::XOnly(self.1.x_only_pks[self.0]), @@ -381,6 +380,5 @@ fn subs_hash_frag(ms: &str, pubdata: &PubData) -> String { "ripemd160(H!)", &format!("ripemd160({})", rand_hash20.to_hex()), ); - let ms = ms.replace("hash160(H!)", &format!("hash160({})", rand_hash20.to_hex())); - ms + ms.replace("hash160(H!)", &format!("hash160({})", rand_hash20.to_hex())) } diff --git a/bitcoind-tests/tests/test_arith.rs b/bitcoind-tests/tests/test_arith.rs index d232fad9..071c0628 100644 --- a/bitcoind-tests/tests/test_arith.rs +++ b/bitcoind-tests/tests/test_arith.rs @@ -59,7 +59,7 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec // Spend one input and spend one output for simplicity. let mut psbt = Psbt::new_v2(); // figure out the outpoint from the txid - let (outpoint, witness_utxo) = get_vout(&cl, txid, 100_000_000, derived_desc.script_pubkey()); + let (outpoint, witness_utxo) = get_vout(cl, txid, 100_000_000, derived_desc.script_pubkey()); let txin = TxIn { previous_output: outpoint, is_pegin: false, @@ -103,7 +103,7 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec Descriptor::TrExt(ref tr) => { let hash_ty = sighash::SchnorrSigHashType::Default; - let prevouts = [witness_utxo.clone()]; + let prevouts = [witness_utxo]; let prevouts = sighash::Prevouts::All(&prevouts); // ------------------ script spend ------------- let x_only_keypairs_reqd: Vec<(secp256k1::KeyPair, TapLeafHash)> = tr @@ -112,7 +112,7 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec let leaf_hash = TapLeafHash::from_script(&ms.encode(), LeafVersion::default()); ms.iter_pk().filter_map(move |pk| { let i = x_only_pks.iter().position(|&x| x.to_public_key() == pk); - i.map(|idx| (xonly_keypairs[idx].clone(), leaf_hash)) + i.map(|idx| (xonly_keypairs[idx], leaf_hash)) }) }) .collect(); @@ -138,7 +138,7 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec (x_only_pk, leaf_hash), elements::SchnorrSig { sig, - hash_ty: hash_ty, + hash_ty, }, ); } @@ -173,7 +173,7 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec let txid = cl.send_raw_transaction(&tx); // Finally mine the blocks and await confirmations - let _blocks = cl.generate(1); + cl.generate(1); // Get the required transactions from the node mined in the blocks. // Check whether the transaction is mined in blocks // Assert that the confirmations are > 0. diff --git a/bitcoind-tests/tests/test_cpp.rs b/bitcoind-tests/tests/test_cpp.rs index 20e79ac9..330546df 100644 --- a/bitcoind-tests/tests/test_cpp.rs +++ b/bitcoind-tests/tests/test_cpp.rs @@ -92,10 +92,9 @@ pub fn test_from_cpp_ms(cl: &ElementsD, testdata: &TestData) { let mut psbt = Psbt::new_v2(); psbt.global.tx_data.fallback_locktime = Some( LockTime::from_time(1_603_866_330) - .expect("valid timestamp") - .into(), + .expect("valid timestamp"), ); // 10/28/2020 @ 6:25am (UTC) - let (outpoint, witness_utxo) = get_vout(&cl, txid, 100_000_000); + let (outpoint, witness_utxo) = get_vout(cl, txid, 100_000_000); let txin = TxIn { previous_output: outpoint, is_pegin: false, diff --git a/bitcoind-tests/tests/test_csfs.rs b/bitcoind-tests/tests/test_csfs.rs index 2258e69a..e414bf3f 100644 --- a/bitcoind-tests/tests/test_csfs.rs +++ b/bitcoind-tests/tests/test_csfs.rs @@ -60,7 +60,7 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec // Spend one input and spend one output for simplicity. let mut psbt = Psbt::new_v2(); // figure out the outpoint from the txid - let (outpoint, witness_utxo) = get_vout(&cl, txid, 100_000_000, derived_desc.script_pubkey()); + let (outpoint, witness_utxo) = get_vout(cl, txid, 100_000_000, derived_desc.script_pubkey()); let txin = TxIn { previous_output: outpoint, is_pegin: false, @@ -113,7 +113,7 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec let leaf_hash = TapLeafHash::from_script(&ms.encode(), LeafVersion::default()); ms.iter_pk().filter_map(move |pk| { let i = x_only_pks.iter().position(|&x| x.to_public_key() == pk); - i.map(|idx| (xonly_keypairs[idx].clone(), leaf_hash)) + i.map(|idx| (xonly_keypairs[idx], leaf_hash)) }) }) .collect(); @@ -139,7 +139,7 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec (x_only_pk, leaf_hash), elements::SchnorrSig { sig, - hash_ty: hash_ty, + hash_ty, }, ); } @@ -171,7 +171,7 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec // Create a signature let keypair = &self.0.secretdata.x_only_keypairs[i]; - let msg = secp256k1::Message::from_slice(&msg.as_inner()[..]).unwrap(); + let msg = secp256k1::Message::from_slice(msg.as_inner()).unwrap(); let mut aux_rand = [0u8; 32]; rand::thread_rng().fill_bytes(&mut aux_rand); @@ -205,7 +205,7 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec } let psbt_sat = PsbtInputSatisfier::new(&psbt, 0); - let csfs_sat = CsfsSatisfier(&testdata); + let csfs_sat = CsfsSatisfier(testdata); let mut tx = psbt.extract_tx().unwrap(); let txouts = vec![psbt.inputs()[0].witness_utxo.clone().unwrap()]; @@ -227,7 +227,7 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec let txid = cl.send_raw_transaction(&tx); // Finally mine the blocks and await confirmations - let _blocks = cl.generate(1); + cl.generate(1); // Get the required transactions from the node mined in the blocks. // Check whether the transaction is mined in blocks // Assert that the confirmations are > 0. diff --git a/bitcoind-tests/tests/test_desc.rs b/bitcoind-tests/tests/test_desc.rs index 5b222819..76357153 100644 --- a/bitcoind-tests/tests/test_desc.rs +++ b/bitcoind-tests/tests/test_desc.rs @@ -93,7 +93,7 @@ pub fn test_desc_satisfy( // Spend one input and spend one output for simplicity. let mut psbt = Psbt::new_v2(); // figure out the outpoint from the txid - let (outpoint, witness_utxo) = get_vout(&cl, txid, 100_000_000, derived_desc.script_pubkey()); + let (outpoint, witness_utxo) = get_vout(cl, txid, 100_000_000, derived_desc.script_pubkey()); let txin = TxIn { previous_output: outpoint, is_pegin: false, @@ -141,7 +141,7 @@ pub fn test_desc_satisfy( let internal_key_present = x_only_pks .iter() .position(|&x| x.to_public_key() == *tr.internal_key()); - let internal_keypair = internal_key_present.map(|idx| xonly_keypairs[idx].clone()); + let internal_keypair = internal_key_present.map(|idx| xonly_keypairs[idx]); let prevouts = [witness_utxo]; let prevouts = sighash::Prevouts::All(&prevouts); @@ -169,7 +169,7 @@ pub fn test_desc_satisfy( secp.sign_schnorr_with_aux_rand(&msg, &internal_keypair, &aux_rand); psbt.inputs_mut()[0].tap_key_sig = Some(SchnorrSig { sig: schnorr_sig, - hash_ty: hash_ty, + hash_ty, }); } else { // No internal key @@ -181,7 +181,7 @@ pub fn test_desc_satisfy( let leaf_hash = TapLeafHash::from_script(&ms.encode(), LeafVersion::default()); ms.iter_pk().filter_map(move |pk| { let i = x_only_pks.iter().position(|&x| x.to_public_key() == pk); - i.map(|idx| (xonly_keypairs[idx].clone(), leaf_hash)) + i.map(|idx| (xonly_keypairs[idx], leaf_hash)) }) }) .collect(); @@ -207,7 +207,7 @@ pub fn test_desc_satisfy( (x_only_pk, leaf_hash), elements::SchnorrSig { sig, - hash_ty: hash_ty, + hash_ty, }, ); } @@ -216,7 +216,7 @@ pub fn test_desc_satisfy( // Non-tr descriptors // Ecdsa sigs let sks_reqd = match derived_desc { - Descriptor::Bare(bare) => find_sks_ms(&bare.as_inner(), testdata), + Descriptor::Bare(bare) => find_sks_ms(bare.as_inner(), testdata), Descriptor::Pkh(pk) => find_sk_single_key(*pk.as_inner(), testdata), Descriptor::Wpkh(pk) => find_sk_single_key(*pk.as_inner(), testdata), Descriptor::Sh(sh) => match sh.as_inner() { @@ -225,7 +225,7 @@ pub fn test_desc_satisfy( let ms = Miniscript::from_ast(smv.sorted_node()).unwrap(); find_sks_ms(&ms, testdata) } - miniscript::descriptor::WshInner::Ms(ref ms) => find_sks_ms(&ms, testdata), + miniscript::descriptor::WshInner::Ms(ref ms) => find_sks_ms(ms, testdata), }, miniscript::descriptor::ShInner::Wpkh(pk) => { find_sk_single_key(*pk.as_inner(), testdata) @@ -234,14 +234,14 @@ pub fn test_desc_satisfy( let ms = Miniscript::from_ast(smv.sorted_node()).unwrap(); find_sks_ms(&ms, testdata) } - miniscript::descriptor::ShInner::Ms(ms) => find_sks_ms(&ms, testdata), + miniscript::descriptor::ShInner::Ms(ms) => find_sks_ms(ms, testdata), }, Descriptor::Wsh(wsh) => match wsh.as_inner() { miniscript::descriptor::WshInner::SortedMulti(ref smv) => { let ms = Miniscript::from_ast(smv.sorted_node()).unwrap(); find_sks_ms(&ms, testdata) } - miniscript::descriptor::WshInner::Ms(ref ms) => find_sks_ms(&ms, testdata), + miniscript::descriptor::WshInner::Ms(ref ms) => find_sks_ms(ms, testdata), }, Descriptor::Tr(_tr) => unreachable!("Tr checked earlier"), Descriptor::TrExt(_tr) => unreachable!("Extensions not tested here"), @@ -286,7 +286,7 @@ pub fn test_desc_satisfy( println!("Testing descriptor: {}", definite_desc); // Finalize the transaction using psbt // Let miniscript do it's magic! - if let Err(_) = psbt.finalize_mut(&secp, testdata.pubdata.genesis_hash) { + if psbt.finalize_mut(&secp, testdata.pubdata.genesis_hash).is_err() { return Err(DescError::PsbtFinalizeError); } let tx = psbt @@ -299,7 +299,7 @@ pub fn test_desc_satisfy( let txid = cl.send_raw_transaction(&tx); // Finally mine the blocks and await confirmations - let _blocks = cl.generate(1); + cl.generate(1); // Get the required transactions from the node mined in the blocks. // Check whether the transaction is mined in blocks // Assert that the confirmations are > 0. @@ -307,7 +307,7 @@ pub fn test_desc_satisfy( .as_u64() .unwrap(); assert!(num_conf > 0); - return Ok(tx.input[0].witness.script_witness.clone()); + Ok(tx.input[0].witness.script_witness.clone()) } // Find all secret corresponding to the known public keys in ms diff --git a/bitcoind-tests/tests/test_introspect.rs b/bitcoind-tests/tests/test_introspect.rs index f95a4d93..e9c78cba 100644 --- a/bitcoind-tests/tests/test_introspect.rs +++ b/bitcoind-tests/tests/test_introspect.rs @@ -59,7 +59,7 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec // Spend one input and spend one output for simplicity. let mut psbt = Psbt::new_v2(); // figure out the outpoint from the txid - let (outpoint, witness_utxo) = get_vout(&cl, txid, 100_000_000, derived_desc.script_pubkey()); + let (outpoint, witness_utxo) = get_vout(cl, txid, 100_000_000, derived_desc.script_pubkey()); let txin = TxIn { previous_output: outpoint, is_pegin: false, @@ -103,7 +103,7 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec Descriptor::TrExt(ref tr) => { let hash_ty = sighash::SchnorrSigHashType::Default; - let prevouts = [witness_utxo.clone()]; + let prevouts = [witness_utxo]; let prevouts = sighash::Prevouts::All(&prevouts); // ------------------ script spend ------------- let x_only_keypairs_reqd: Vec<(secp256k1::KeyPair, TapLeafHash)> = tr @@ -112,7 +112,7 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec let leaf_hash = TapLeafHash::from_script(&ms.encode(), LeafVersion::default()); ms.iter_pk().filter_map(move |pk| { let i = x_only_pks.iter().position(|&x| x.to_public_key() == pk); - i.map(|idx| (xonly_keypairs[idx].clone(), leaf_hash)) + i.map(|idx| (xonly_keypairs[idx], leaf_hash)) }) }) .collect(); @@ -138,7 +138,7 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec (x_only_pk, leaf_hash), elements::SchnorrSig { sig, - hash_ty: hash_ty, + hash_ty, }, ); } @@ -173,7 +173,7 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec let txid = cl.send_raw_transaction(&tx); // Finally mine the blocks and await confirmations - let _blocks = cl.generate(1); + cl.generate(1); // Get the required transactions from the node mined in the blocks. // Check whether the transaction is mined in blocks // Assert that the confirmations are > 0.