Skip to content

Commit 686bdb6

Browse files
committed
address some clippy lints
- make tests readable - derive Clone for UpdateOptions
1 parent 5cd6fd1 commit 686bdb6

File tree

2 files changed

+86
-74
lines changed

2 files changed

+86
-74
lines changed

src/builder.rs

Lines changed: 85 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl Builder {
213213
/// - If `data` exceeds 80 bytes in size.
214214
/// - If this is not the first `OP_RETURN` output being added to this builder.
215215
///
216-
/// Refer to https://github.com/bitcoin/bitcoin/blob/v28.0/src/policy/policy.cpp for more
216+
/// Refer to <https://github.com/bitcoin/bitcoin/blob/v28.0/src/policy/policy.cpp> for more
217217
/// details about transaction standardness.
218218
pub fn add_data<T>(&mut self, data: T) -> Result<&mut Self, Error>
219219
where
@@ -240,6 +240,14 @@ impl Builder {
240240
}
241241

242242
/// Build a PSBT with the given data provider and return a [`PsbtUpdater`].
243+
///
244+
/// # Errors
245+
///
246+
/// - If attempting to mix locktime units
247+
/// - If the tx is illegally constructed or fails one of a number of sanity checks
248+
/// defined by the library.
249+
/// - If a requested locktime or sequence interferes with the locktime constraints
250+
/// of a planned input.
243251
pub fn build_psbt<D>(self, provider: &mut D) -> Result<PsbtUpdater, Error>
244252
where
245253
D: DataProvider,
@@ -334,7 +342,7 @@ impl Builder {
334342
self.sanity_check()?;
335343

336344
if self.is_change_added() {
337-
self._check_fee(&mut unsigned_tx);
345+
self.do_check_fee(&mut unsigned_tx);
338346
}
339347

340348
provider.sort_transaction(&mut unsigned_tx);
@@ -343,6 +351,12 @@ impl Builder {
343351
}
344352

345353
/// Convenience method to build an updated [`Psbt`] and return a [`Finalizer`].
354+
/// Refer to [`build_psbt`](Self::build_psbt) for more.
355+
///
356+
/// # Errors
357+
///
358+
/// This method returns an error if a problem occurs when either building or updating
359+
/// the PSBT.
346360
pub fn build_tx<D>(self, provider: &mut D) -> Result<(Psbt, Finalizer), Error>
347361
where
348362
D: DataProvider,
@@ -393,7 +407,7 @@ impl Builder {
393407
/// to below the dust limit, then no shrinking will occur.
394408
///
395409
/// Panics if `tx` is not a sane tx
396-
fn _check_fee(&self, tx: &mut Transaction) {
410+
fn do_check_fee(&self, tx: &mut Transaction) {
397411
const DUST: u64 = 546;
398412
if !self.is_change_added() {
399413
return;
@@ -538,7 +552,7 @@ pub enum Error {
538552
/// required sequence
539553
required: Sequence,
540554
},
541-
/// too many OP_RETURN in a single tx
555+
/// too many `OP_RETURN` in a single tx
542556
TooManyOpReturn,
543557
/// error when updating a PSBT
544558
Update(UpdatePsbtError),
@@ -753,38 +767,38 @@ mod test {
753767
let mut keymap = KeyMap::new();
754768

755769
let mut index = KeychainTxOutIndex::new(10);
756-
for (k, s) in descriptors.iter().enumerate() {
757-
let (desc, km) = parse_descriptor(s);
770+
for (keychain, desc_str) in descriptors.iter().enumerate() {
771+
let (desc, km) = parse_descriptor(desc_str);
758772
desc.for_each_key(|k| {
759773
keys.push(k.clone());
760774
true
761775
});
762776
keymap.extend(km);
763-
index.insert_descriptor(k, desc).unwrap();
777+
index.insert_descriptor(keychain, desc).unwrap();
764778
}
765779

766780
let mut graph = KeychainTxGraph::new(index);
767781

768782
let genesis_hash = constants::genesis_block(Network::Regtest).block_hash();
769783
let mut cp = CheckPoint::new(block_id!(0, genesis_hash));
770784

771-
for h in 1..11 {
785+
for height in 1..11 {
772786
let ((_, script_pubkey), _) = graph.index.reveal_next_spk(0).unwrap();
773787

774788
let tx = Transaction {
775789
output: vec![TxOut {
776790
value: Amount::from_btc(0.01).unwrap(),
777791
script_pubkey,
778792
}],
779-
..new_tx(h)
793+
..new_tx(height)
780794
};
781795
let txid = tx.compute_txid();
782796
let _ = graph.insert_tx(tx);
783797

784-
let block_id = block_id!(h, Hash::hash(h.to_be_bytes().as_slice()));
798+
let block_id = block_id!(height, Hash::hash(height.to_be_bytes().as_slice()));
785799
let anchor = ConfirmationBlockTime {
786800
block_id,
787-
confirmation_time: h as u64,
801+
confirmation_time: height as u64,
788802
};
789803
let _ = graph.insert_anchor(txid, anchor);
790804

@@ -806,42 +820,34 @@ mod test {
806820
}
807821
}
808822

809-
fn extract(f: Finalizer, mut psbt: Psbt) -> anyhow::Result<Transaction> {
810-
if f.finalize(&mut psbt).is_finalized() {
811-
Ok(psbt.extract_tx()?)
812-
} else {
813-
anyhow::bail!("failed to finalize");
814-
}
815-
}
816-
817823
#[test]
818824
fn test_build_tx_finalize() {
819825
let mut graph = init_graph(&get_single_sig_tr_xprv());
820826
assert_eq!(graph.balance().total().to_btc(), 0.1);
821827

822828
let recip = ScriptBuf::from_hex(SPK).unwrap();
823-
let mut b = Builder::new();
824-
b.add_output(recip, Amount::from_sat(2_500_000));
829+
let mut builder = Builder::new();
830+
builder.add_output(recip, Amount::from_sat(2_500_000));
825831

826832
let selection = graph.planned_utxos().into_iter().take(3);
827-
b.add_inputs(selection);
828-
b.add_change_output(graph.next_internal_spk(), Amount::from_sat(499_500));
833+
builder.add_inputs(selection);
834+
builder.add_change_output(graph.next_internal_spk(), Amount::from_sat(499_500));
829835

830-
let (mut psbt, f) = b.build_tx(&mut graph).unwrap();
836+
let (mut psbt, finalizer) = builder.build_tx(&mut graph).unwrap();
831837
assert_eq!(psbt.unsigned_tx.input.len(), 3);
832838
assert_eq!(psbt.unsigned_tx.output.len(), 2);
833839

834840
graph.sign(&mut psbt);
835-
let _tx = extract(f, psbt).unwrap();
841+
assert!(finalizer.finalize(&mut psbt).is_finalized());
836842
}
837843

838844
#[test]
839845
fn test_build_tx_insane_fee() {
840846
let mut graph = init_graph(&get_single_sig_tr_xprv());
841847

842848
let recip = ScriptBuf::from_hex(SPK).unwrap();
843-
let mut b = Builder::new();
844-
b.add_output(recip, Amount::from_btc(0.01).unwrap());
849+
let mut builder = Builder::new();
850+
builder.add_output(recip, Amount::from_btc(0.01).unwrap());
845851

846852
let selection = graph
847853
.planned_utxos()
@@ -856,10 +862,10 @@ mod test {
856862
.to_btc(),
857863
0.03
858864
);
859-
b.add_inputs(selection);
865+
builder.add_inputs(selection);
860866

861-
let err = b.build_tx(&mut graph).unwrap_err();
862-
assert!(matches!(err, Error::InsaneFee(_)));
867+
let err = builder.build_tx(&mut graph).unwrap_err();
868+
assert!(matches!(err, Error::InsaneFee(..)));
863869
}
864870

865871
#[test]
@@ -868,24 +874,24 @@ mod test {
868874

869875
let recip = ScriptBuf::from_hex(SPK).unwrap();
870876

871-
let mut b = Builder::new();
872-
b.add_output(recip, Amount::from_btc(0.02).unwrap());
873-
b.add_inputs(graph.planned_utxos().into_iter().take(1));
877+
let mut builder = Builder::new();
878+
builder.add_output(recip, Amount::from_btc(0.02).unwrap());
879+
builder.add_inputs(graph.planned_utxos().into_iter().take(1));
874880

875-
let err = b.build_tx(&mut graph).unwrap_err();
876-
assert!(matches!(err, Error::NegativeFee(_)));
881+
let err = builder.build_tx(&mut graph).unwrap_err();
882+
assert!(matches!(err, Error::NegativeFee(..)));
877883
}
878884

879885
#[test]
880886
fn test_build_tx_add_data() {
881887
let mut graph = init_graph(&get_single_sig_tr_xprv());
882888

883-
let mut b = Builder::new();
884-
b.add_inputs(graph.planned_utxos().into_iter().take(1));
885-
b.add_output(graph.next_internal_spk(), Amount::from_sat(999_000));
886-
b.add_data(b"satoshi nakamoto").unwrap();
889+
let mut builder = Builder::new();
890+
builder.add_inputs(graph.planned_utxos().into_iter().take(1));
891+
builder.add_output(graph.next_internal_spk(), Amount::from_sat(999_000));
892+
builder.add_data(b"satoshi nakamoto").unwrap();
887893

888-
let psbt = b.build_tx(&mut graph).unwrap().0;
894+
let psbt = builder.build_tx(&mut graph).unwrap().0;
889895
assert!(psbt
890896
.unsigned_tx
891897
.output
@@ -894,14 +900,20 @@ mod test {
894900

895901
// try to add more than 80 bytes of data
896902
let data = [0x90; 81];
897-
b = Builder::new();
898-
assert!(matches!(b.add_data(data), Err(Error::MaxOpReturnRelay)));
903+
builder = Builder::new();
904+
assert!(matches!(
905+
builder.add_data(data),
906+
Err(Error::MaxOpReturnRelay)
907+
));
899908

900909
// try to add more than 1 op return
901910
let data = [0x90; 80];
902-
b = Builder::new();
903-
b.add_data(data).unwrap();
904-
assert!(matches!(b.add_data(data), Err(Error::TooManyOpReturn)));
911+
builder = Builder::new();
912+
builder.add_data(data).unwrap();
913+
assert!(matches!(
914+
builder.add_data(data),
915+
Err(Error::TooManyOpReturn)
916+
));
905917
}
906918

907919
#[test]
@@ -910,23 +922,23 @@ mod test {
910922
let mut graph = init_graph(&get_single_sig_tr_xprv());
911923

912924
// test default tx version (2)
913-
let mut b = Builder::new();
925+
let mut builder = Builder::new();
914926
let recip = graph.spk_at_index(0).unwrap();
915927
let utxo = graph.planned_utxos().first().unwrap().clone();
916928
let amt = utxo.txout.value - Amount::from_sat(256);
917-
b.add_input(utxo.clone());
918-
b.add_output(recip.clone(), amt);
929+
builder.add_input(utxo.clone());
930+
builder.add_output(recip.clone(), amt);
919931

920-
let psbt = b.build_tx(&mut graph).unwrap().0;
932+
let psbt = builder.build_tx(&mut graph).unwrap().0;
921933
assert_eq!(psbt.unsigned_tx.version, Version::TWO);
922934

923935
// allow any potentially non-standard version
924-
b = Builder::new();
925-
b.version(Version(3));
926-
b.add_input(utxo);
927-
b.add_output(recip, amt);
936+
builder = Builder::new();
937+
builder.version(Version(3));
938+
builder.add_input(utxo);
939+
builder.add_output(recip, amt);
928940

929-
let psbt = b.build_tx(&mut graph).unwrap().0;
941+
let psbt = builder.build_tx(&mut graph).unwrap().0;
930942
assert_eq!(psbt.unsigned_tx.version, Version(3));
931943
}
932944

@@ -943,21 +955,21 @@ mod test {
943955
output: (recip, amount),
944956
} = in_out;
945957

946-
let mut b = Builder::new();
947-
b.add_output(recip, amount);
948-
b.add_input(input);
949-
b.locktime(absolute::LockTime::from_consensus(lt));
958+
let mut builder = Builder::new();
959+
builder.add_output(recip, amount);
960+
builder.add_input(input);
961+
builder.locktime(absolute::LockTime::from_consensus(lt));
950962

951-
let res = b.build_tx(graph);
963+
let res = builder.build_tx(graph);
952964

953965
match res {
954-
Ok((mut psbt, f)) => {
966+
Ok((mut psbt, finalizer)) => {
955967
assert_eq!(
956968
psbt.unsigned_tx.lock_time.to_consensus_u32(),
957969
exp_lt.unwrap()
958970
);
959971
graph.sign(&mut psbt);
960-
assert!(f.finalize(&mut psbt).is_finalized());
972+
assert!(finalizer.finalize(&mut psbt).is_finalized());
961973
}
962974
Err(e) => {
963975
assert!(exp_lt.is_none());
@@ -973,10 +985,10 @@ mod test {
973985
// initial state
974986
let mut graph = init_graph(&[get_single_sig_cltv_timestamp()]);
975987
let mut t = 1735877503;
976-
let lt = absolute::LockTime::from_consensus(t);
988+
let locktime = absolute::LockTime::from_consensus(t);
977989

978990
// supply the assets needed to create plans
979-
graph = graph.after(lt);
991+
graph = graph.after(locktime);
980992

981993
let in_out = InOut {
982994
input: graph.planned_utxos().first().unwrap().clone(),
@@ -1010,21 +1022,21 @@ mod test {
10101022
let utxos = graph.planned_utxos();
10111023

10121024
// case: 1-in/1-out
1013-
let mut b = Builder::new();
1014-
b.add_inputs(utxos.iter().take(1).cloned());
1015-
b.add_output(recip.clone(), Amount::from_sat(1_000_000));
1016-
let psbt = b.build_tx(&mut graph).unwrap().0;
1025+
let mut builder = Builder::new();
1026+
builder.add_inputs(utxos.iter().take(1).cloned());
1027+
builder.add_output(recip.clone(), Amount::from_sat(1_000_000));
1028+
let psbt = builder.build_tx(&mut graph).unwrap().0;
10171029
assert_eq!(psbt.unsigned_tx.output.len(), 1);
10181030
assert_eq!(psbt.unsigned_tx.output[0].value.to_btc(), 0.01);
10191031

10201032
// case: 1-in/2-out
1021-
let mut b = Builder::new();
1022-
b.add_inputs(utxos.iter().take(1).cloned());
1023-
b.add_output(recip, Amount::from_sat(500_000));
1024-
b.add_change_output(graph.next_internal_spk(), Amount::from_sat(500_000));
1025-
b.check_fee(Some(Amount::ZERO), Some(FeeRate::from_sat_per_kwu(0)));
1033+
let mut builder = Builder::new();
1034+
builder.add_inputs(utxos.iter().take(1).cloned());
1035+
builder.add_output(recip, Amount::from_sat(500_000));
1036+
builder.add_change_output(graph.next_internal_spk(), Amount::from_sat(500_000));
1037+
builder.check_fee(Some(Amount::ZERO), Some(FeeRate::from_sat_per_kwu(0)));
10261038

1027-
let psbt = b.build_tx(&mut graph).unwrap().0;
1039+
let psbt = builder.build_tx(&mut graph).unwrap().0;
10281040
assert_eq!(psbt.unsigned_tx.output.len(), 2);
10291041
assert!(psbt
10301042
.unsigned_tx

src/updater.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl PsbtUpdater {
152152
}
153153

154154
/// Options for updating a PSBT
155-
#[derive(Debug, Default)]
155+
#[derive(Debug, Default, Clone)]
156156
pub struct UpdateOptions {
157157
/// Only set the input `witness_utxo` if applicable, i.e. do not set `non_witness_utxo`.
158158
///

0 commit comments

Comments
 (0)