Skip to content

Commit

Permalink
more clippy lol: bitcoind-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
apoelstra committed Jun 15, 2023
1 parent f7e96e5 commit 9992bee
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 54 deletions.
6 changes: 3 additions & 3 deletions bitcoind-tests/tests/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub mod test_util;
pub fn setup(validate_pegin: bool) -> (ElementsD, Option<BitcoinD>, 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);
Expand All @@ -28,7 +28,7 @@ pub fn setup(validate_pegin: bool) -> (ElementsD, Option<BitcoinD>, 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());
Expand Down
38 changes: 18 additions & 20 deletions bitcoind-tests/tests/setup/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -182,8 +182,7 @@ pub fn parse_insane_ms<Ctx: ScriptContext>(
let ms =
Miniscript::<String, Ctx>::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
Expand All @@ -193,9 +192,9 @@ impl<'a> ExtParamTranslator<String, CovExtArgs, ()> for StrExtTranslator<'a> {
fn ext(&mut self, e: &String) -> Result<CovExtArgs, ()> {
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(
Expand Down Expand Up @@ -233,15 +232,15 @@ struct StrDescPubKeyTranslator<'a>(usize, &'a PubData);

impl<'a> Translator<String, DescriptorPublicKey, ()> for StrDescPubKeyTranslator<'a> {
fn pk(&mut self, pk_str: &String) -> Result<DescriptorPublicKey, ()> {
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]),
Expand Down Expand Up @@ -286,15 +285,15 @@ struct StrTranslatorLoose<'a>(usize, &'a PubData);

impl<'a> Translator<String, DescriptorPublicKey, ()> for StrTranslatorLoose<'a> {
fn pk(&mut self, pk_str: &String) -> Result<DescriptorPublicKey, ()> {
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]),
Expand Down Expand Up @@ -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()))
}
10 changes: 5 additions & 5 deletions bitcoind-tests/tests/test_arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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,
},
);
}
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions bitcoind-tests/tests/test_cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions bitcoind-tests/tests/test_csfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -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,
},
);
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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()];
Expand All @@ -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.
Expand Down
24 changes: 12 additions & 12 deletions bitcoind-tests/tests/test_desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -207,7 +207,7 @@ pub fn test_desc_satisfy(
(x_only_pk, leaf_hash),
elements::SchnorrSig {
sig,
hash_ty: hash_ty,
hash_ty,
},
);
}
Expand All @@ -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() {
Expand All @@ -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)
Expand All @@ -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"),
Expand Down Expand Up @@ -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
Expand All @@ -299,15 +299,15 @@ 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.
let num_conf = cl.call("gettransaction", &[txid.to_string().into()])["confirmations"]
.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
Expand Down
10 changes: 5 additions & 5 deletions bitcoind-tests/tests/test_introspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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,
},
);
}
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 9992bee

Please sign in to comment.