Skip to content

Commit b507ff6

Browse files
committed
Merge #686: bitcoind_test: Fix clippy
b9a207b Use is_err instead of redundant pattern matching (Tobin C. Harding) ef868cc Use unwrap_or_else instead of expect (Tobin C. Harding) a1a5467 Remove clone call from Copy type (Tobin C. Harding) f939367 Remove useless conversion (Tobin C. Harding) c20be39 Remove explicit reference (Tobin C. Harding) 7ed6680 Do not mutate local variable (Tobin C. Harding) e361cfc Remove unneeded return statement (Tobin C. Harding) f9907b0 Use += operator (Tobin C. Harding) 5e0df21 Use single quotes (Tobin C. Harding) 7218e5e Remove redundant field names (Tobin C. Harding) 6a2577f Remove useless let binding (Tobin C. Harding) ccc808a Use iterator instead of array access (Tobin C. Harding) badeb44 Remove unnecessary cast (Tobin C. Harding) Pull request description: All the clippy patches rom #682, only touches `bitcoind_test`. ACKs for top commit: apoelstra: utACK b9a207b Tree-SHA512: ae9451601bc5232f0ce194be1b7c6c1c31c8118ea87951f3eac9c6c4349c7ff963516c8cc34c2a63c9f1b99f03c781e687c74b46dc24624bbfa81a0decb513ef
2 parents 351ffa8 + b9a207b commit b507ff6

File tree

3 files changed

+56
-57
lines changed

3 files changed

+56
-57
lines changed

bitcoind-tests/tests/setup/test_util.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ fn setup_keys(
8686
let mut x_only_keypairs = vec![];
8787
let mut x_only_pks = vec![];
8888

89-
for i in 0..n {
90-
let keypair = bitcoin::secp256k1::Keypair::from_secret_key(&secp_sign, &sks[i]);
89+
for sk in &sks {
90+
let keypair = bitcoin::secp256k1::Keypair::from_secret_key(&secp_sign, &sk);
9191
let (xpk, _parity) = XOnlyPublicKey::from_keypair(&keypair);
9292
x_only_keypairs.push(keypair);
9393
x_only_pks.push(xpk);
@@ -99,13 +99,13 @@ impl TestData {
9999
// generate a fixed data for n keys
100100
pub(crate) fn new_fixed_data(n: usize) -> Self {
101101
let (sks, pks, x_only_keypairs, x_only_pks) = setup_keys(n);
102-
let sha256_pre = [0x12 as u8; 32];
102+
let sha256_pre = [0x12_u8; 32];
103103
let sha256 = sha256::Hash::hash(&sha256_pre);
104-
let hash256_pre = [0x34 as u8; 32];
104+
let hash256_pre = [0x34_u8; 32];
105105
let hash256 = hash256::Hash::hash(&hash256_pre);
106-
let hash160_pre = [0x56 as u8; 32];
106+
let hash160_pre = [0x56_u8; 32];
107107
let hash160 = hash160::Hash::hash(&hash160_pre);
108-
let ripemd160_pre = [0x78 as u8; 32];
108+
let ripemd160_pre = [0x78_u8; 32];
109109
let ripemd160 = ripemd160::Hash::hash(&ripemd160_pre);
110110

111111
let pubdata = PubData { pks, sha256, hash256, ripemd160, hash160, x_only_pks };
@@ -148,8 +148,7 @@ pub fn parse_insane_ms<Ctx: ScriptContext>(
148148
let ms =
149149
Miniscript::<String, Ctx>::from_str_insane(&ms).expect("only parsing valid minsicripts");
150150
let mut translator = StrTranslatorLoose(0, pubdata);
151-
let ms = ms.translate_pk(&mut translator).unwrap();
152-
ms
151+
ms.translate_pk(&mut translator).unwrap()
153152
}
154153

155154
// Translate Str to DescriptorPublicKey
@@ -158,15 +157,15 @@ struct StrDescPubKeyTranslator<'a>(usize, &'a PubData);
158157

159158
impl<'a> Translator<String, DescriptorPublicKey, ()> for StrDescPubKeyTranslator<'a> {
160159
fn pk(&mut self, pk_str: &String) -> Result<DescriptorPublicKey, ()> {
161-
let avail = !pk_str.ends_with("!");
160+
let avail = !pk_str.ends_with('!');
162161
if avail {
163-
self.0 = self.0 + 1;
164-
if pk_str.starts_with("K") {
162+
self.0 += 1;
163+
if pk_str.starts_with('K') {
165164
Ok(DescriptorPublicKey::Single(SinglePub {
166165
origin: None,
167166
key: SinglePubKey::FullKey(self.1.pks[self.0]),
168167
}))
169-
} else if pk_str.starts_with("X") {
168+
} else if pk_str.starts_with('X') {
170169
Ok(DescriptorPublicKey::Single(SinglePub {
171170
origin: None,
172171
key: SinglePubKey::XOnly(self.1.x_only_pks[self.0]),
@@ -211,15 +210,15 @@ struct StrTranslatorLoose<'a>(usize, &'a PubData);
211210

212211
impl<'a> Translator<String, DescriptorPublicKey, ()> for StrTranslatorLoose<'a> {
213212
fn pk(&mut self, pk_str: &String) -> Result<DescriptorPublicKey, ()> {
214-
let avail = !pk_str.ends_with("!");
213+
let avail = !pk_str.ends_with('!');
215214
if avail {
216-
self.0 = self.0 + 1;
217-
if pk_str.starts_with("K") {
215+
self.0 += 1;
216+
if pk_str.starts_with('K') {
218217
Ok(DescriptorPublicKey::Single(SinglePub {
219218
origin: None,
220219
key: SinglePubKey::FullKey(self.1.pks[self.0]),
221220
}))
222-
} else if pk_str.starts_with("X") {
221+
} else if pk_str.starts_with('X') {
223222
Ok(DescriptorPublicKey::Single(SinglePub {
224223
origin: None,
225224
key: SinglePubKey::XOnly(self.1.x_only_pks[self.0]),
@@ -291,6 +290,5 @@ fn subs_hash_frag(ms: &str, pubdata: &PubData) -> String {
291290
let ms = ms.replace("hash256(H!)", &format!("hash256({})", rand_hash32.to_lower_hex_string()));
292291
let ms =
293292
ms.replace("ripemd160(H!)", &format!("ripemd160({})", rand_hash20.to_lower_hex_string()));
294-
let ms = ms.replace("hash160(H!)", &format!("hash160({})", rand_hash20.to_lower_hex_string()));
295-
ms
293+
ms.replace("hash160(H!)", &format!("hash160({})", rand_hash20.to_lower_hex_string()))
296294
}

bitcoind-tests/tests/test_cpp.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
105105
let mut psbt = Psbt {
106106
unsigned_tx: Transaction {
107107
version: transaction::Version::TWO,
108-
lock_time: absolute::LockTime::from_time(1_603_866_330)
109-
.expect("valid timestamp")
110-
.into(), // 10/28/2020 @ 6:25am (UTC)
108+
lock_time: absolute::LockTime::from_time(1_603_866_330).expect("valid timestamp"), // 10/28/2020 @ 6:25am (UTC)
111109
input: vec![],
112110
output: vec![],
113111
},
@@ -119,13 +117,15 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
119117
outputs: vec![],
120118
};
121119
// figure out the outpoint from the txid
122-
let (outpoint, witness_utxo) = get_vout(&cl, txid, btc(1.0));
123-
let mut txin = TxIn::default();
124-
txin.previous_output = outpoint;
125-
// set the sequence to a non-final number for the locktime transactions to be
126-
// processed correctly.
127-
// We waited 50 blocks, keep 49 for safety
128-
txin.sequence = Sequence::from_height(49);
120+
let (outpoint, witness_utxo) = get_vout(cl, txid, btc(1.0));
121+
let txin = TxIn {
122+
previous_output: outpoint,
123+
// set the sequence to a non-final number for the locktime transactions to be
124+
// processed correctly.
125+
// We waited 50 blocks, keep 49 for safety
126+
sequence: Sequence::from_height(49),
127+
..Default::default()
128+
};
129129
psbt.unsigned_tx.input.push(txin);
130130
// Get a new script pubkey from the node so that
131131
// the node wallet tracks the receiving transaction
@@ -138,11 +138,13 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
138138
value: Amount::from_sat(99_999_000),
139139
script_pubkey: addr.script_pubkey(),
140140
});
141-
let mut input = psbt::Input::default();
142-
input.witness_utxo = Some(witness_utxo);
143-
input.witness_script = Some(desc.explicit_script().unwrap());
141+
let input = psbt::Input {
142+
witness_utxo: Some(witness_utxo),
143+
witness_script: Some(desc.explicit_script().unwrap()),
144+
..Default::default()
145+
};
144146
psbt.inputs.push(input);
145-
psbt.update_input_with_descriptor(0, &desc).unwrap();
147+
psbt.update_input_with_descriptor(0, desc).unwrap();
146148
psbt.outputs.push(psbt::Output::default());
147149
psbts.push(psbt);
148150
}
@@ -213,7 +215,7 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
213215
// Check whether the node accepts the transactions
214216
let txid = cl
215217
.send_raw_transaction(&tx)
216-
.expect(&format!("{} send tx failed for ms {}", i, ms));
218+
.unwrap_or_else(|_| panic!("{} send tx failed for ms {}", i, ms));
217219
spend_txids.push(txid);
218220
}
219221
}

bitcoind-tests/tests/test_desc.rs

+23-24
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn test_desc_satisfy(
8181
.unwrap();
8282
assert_eq!(blocks.len(), 1);
8383

84-
let definite_desc = test_util::parse_test_desc(&descriptor, &testdata.pubdata)
84+
let definite_desc = test_util::parse_test_desc(descriptor, &testdata.pubdata)
8585
.map_err(|_| DescError::DescParseError)?
8686
.at_derivation_index(0)
8787
.unwrap();
@@ -104,9 +104,7 @@ pub fn test_desc_satisfy(
104104
let mut psbt = Psbt {
105105
unsigned_tx: Transaction {
106106
version: transaction::Version::TWO,
107-
lock_time: absolute::LockTime::from_time(1_603_866_330)
108-
.expect("valid timestamp")
109-
.into(), // 10/28/2020 @ 6:25am (UTC)
107+
lock_time: absolute::LockTime::from_time(1_603_866_330).expect("valid timestamp"), // 10/28/2020 @ 6:25am (UTC)
110108
input: vec![],
111109
output: vec![],
112110
},
@@ -118,13 +116,15 @@ pub fn test_desc_satisfy(
118116
outputs: vec![],
119117
};
120118
// figure out the outpoint from the txid
121-
let (outpoint, witness_utxo) = get_vout(&cl, txid, btc(1.0), derived_desc.script_pubkey());
122-
let mut txin = TxIn::default();
123-
txin.previous_output = outpoint;
124-
// set the sequence to a non-final number for the locktime transactions to be
125-
// processed correctly.
126-
// We waited 2 blocks, keep 1 for safety
127-
txin.sequence = Sequence::from_height(1);
119+
let (outpoint, witness_utxo) = get_vout(cl, txid, btc(1.0), derived_desc.script_pubkey());
120+
let txin = TxIn {
121+
previous_output: outpoint,
122+
// set the sequence to a non-final number for the locktime transactions to be
123+
// processed correctly.
124+
// We waited 2 blocks, keep 1 for safety
125+
sequence: Sequence::from_height(1),
126+
..Default::default()
127+
};
128128
psbt.unsigned_tx.input.push(txin);
129129
// Get a new script pubkey from the node so that
130130
// the node wallet tracks the receiving transaction
@@ -160,7 +160,7 @@ pub fn test_desc_satisfy(
160160
let internal_key_present = x_only_pks
161161
.iter()
162162
.position(|&x| x.to_public_key() == *tr.internal_key());
163-
let internal_keypair = internal_key_present.map(|idx| xonly_keypairs[idx].clone());
163+
let internal_keypair = internal_key_present.map(|idx| xonly_keypairs[idx]);
164164
let prevouts = [witness_utxo];
165165
let prevouts = sighash::Prevouts::All(&prevouts);
166166

@@ -177,8 +177,7 @@ pub fn test_desc_satisfy(
177177
rand::thread_rng().fill_bytes(&mut aux_rand);
178178
let schnorr_sig =
179179
secp.sign_schnorr_with_aux_rand(&msg, &internal_keypair, &aux_rand);
180-
psbt.inputs[0].tap_key_sig =
181-
Some(taproot::Signature { sig: schnorr_sig, hash_ty: hash_ty });
180+
psbt.inputs[0].tap_key_sig = Some(taproot::Signature { sig: schnorr_sig, hash_ty });
182181
} else {
183182
// No internal key
184183
}
@@ -189,7 +188,7 @@ pub fn test_desc_satisfy(
189188
let leaf_hash = TapLeafHash::from_script(&ms.encode(), LeafVersion::TapScript);
190189
ms.iter_pk().filter_map(move |pk| {
191190
let i = x_only_pks.iter().position(|&x| x.to_public_key() == pk);
192-
i.map(|idx| (xonly_keypairs[idx].clone(), leaf_hash))
191+
i.map(|idx| (xonly_keypairs[idx], leaf_hash))
193192
})
194193
})
195194
.collect();
@@ -205,14 +204,14 @@ pub fn test_desc_satisfy(
205204
x_only_pks[xonly_keypairs.iter().position(|&x| x == keypair).unwrap()];
206205
psbt.inputs[0]
207206
.tap_script_sigs
208-
.insert((x_only_pk, leaf_hash), taproot::Signature { sig, hash_ty: hash_ty });
207+
.insert((x_only_pk, leaf_hash), taproot::Signature { sig, hash_ty });
209208
}
210209
}
211210
_ => {
212211
// Non-tr descriptors
213212
// Ecdsa sigs
214213
let sks_reqd = match derived_desc {
215-
Descriptor::Bare(bare) => find_sks_ms(&bare.as_inner(), testdata),
214+
Descriptor::Bare(bare) => find_sks_ms(bare.as_inner(), testdata),
216215
Descriptor::Pkh(pk) => find_sk_single_key(*pk.as_inner(), testdata),
217216
Descriptor::Wpkh(pk) => find_sk_single_key(*pk.as_inner(), testdata),
218217
Descriptor::Sh(sh) => match sh.as_inner() {
@@ -221,7 +220,7 @@ pub fn test_desc_satisfy(
221220
let ms = Miniscript::from_ast(smv.sorted_node()).unwrap();
222221
find_sks_ms(&ms, testdata)
223222
}
224-
miniscript::descriptor::WshInner::Ms(ref ms) => find_sks_ms(&ms, testdata),
223+
miniscript::descriptor::WshInner::Ms(ref ms) => find_sks_ms(ms, testdata),
225224
},
226225
miniscript::descriptor::ShInner::Wpkh(pk) => {
227226
find_sk_single_key(*pk.as_inner(), testdata)
@@ -230,14 +229,14 @@ pub fn test_desc_satisfy(
230229
let ms = Miniscript::from_ast(smv.sorted_node()).unwrap();
231230
find_sks_ms(&ms, testdata)
232231
}
233-
miniscript::descriptor::ShInner::Ms(ms) => find_sks_ms(&ms, testdata),
232+
miniscript::descriptor::ShInner::Ms(ms) => find_sks_ms(ms, testdata),
234233
},
235234
Descriptor::Wsh(wsh) => match wsh.as_inner() {
236235
miniscript::descriptor::WshInner::SortedMulti(ref smv) => {
237236
let ms = Miniscript::from_ast(smv.sorted_node()).unwrap();
238237
find_sks_ms(&ms, testdata)
239238
}
240-
miniscript::descriptor::WshInner::Ms(ref ms) => find_sks_ms(&ms, testdata),
239+
miniscript::descriptor::WshInner::Ms(ref ms) => find_sks_ms(ms, testdata),
241240
},
242241
Descriptor::Tr(_tr) => unreachable!("Tr checked earlier"),
243242
};
@@ -256,7 +255,7 @@ pub fn test_desc_satisfy(
256255
assert!(secp.verify_ecdsa(&msg, &sig, &pk.inner).is_ok());
257256
psbt.inputs[0]
258257
.partial_sigs
259-
.insert(pk, ecdsa::Signature { sig, hash_ty: hash_ty });
258+
.insert(pk, ecdsa::Signature { sig, hash_ty });
260259
}
261260
}
262261
}
@@ -277,7 +276,7 @@ pub fn test_desc_satisfy(
277276
println!("Testing descriptor: {}", definite_desc);
278277
// Finalize the transaction using psbt
279278
// Let miniscript do it's magic!
280-
if let Err(_) = psbt.finalize_mut(&secp) {
279+
if psbt.finalize_mut(&secp).is_err() {
281280
return Err(DescError::PsbtFinalizeError);
282281
}
283282
let tx = psbt.extract(&secp).expect("Extraction error");
@@ -287,7 +286,7 @@ pub fn test_desc_satisfy(
287286
// Check whether the node accepts the transactions
288287
let txid = cl
289288
.send_raw_transaction(&tx)
290-
.expect(&format!("send tx failed for desc {}", definite_desc));
289+
.unwrap_or_else(|_| panic!("send tx failed for desc {}", definite_desc));
291290

292291
// Finally mine the blocks and await confirmations
293292
let _blocks = cl
@@ -298,7 +297,7 @@ pub fn test_desc_satisfy(
298297
// Assert that the confirmations are > 0.
299298
let num_conf = cl.get_transaction(&txid, None).unwrap().info.confirmations;
300299
assert!(num_conf > 0);
301-
return Ok(tx.input[0].witness.clone());
300+
Ok(tx.input[0].witness.clone())
302301
}
303302

304303
// Find all secret corresponding to the known public keys in ms

0 commit comments

Comments
 (0)