Skip to content

Commit fe0f5e5

Browse files
committed
wip(feat): use Weight type instead of usize
1 parent e6a93de commit fe0f5e5

File tree

5 files changed

+46
-71
lines changed

5 files changed

+46
-71
lines changed

crates/wallet/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use core::convert::AsRef;
1414

1515
use bdk_chain::ConfirmationTime;
1616
use bitcoin::blockdata::transaction::{OutPoint, Sequence, TxOut};
17-
use bitcoin::psbt;
17+
use bitcoin::{psbt, Weight};
1818

1919
use serde::{Deserialize, Serialize};
2020

@@ -72,7 +72,7 @@ pub struct WeightedUtxo {
7272
/// properly maintain the feerate when adding this input to a transaction during coin selection.
7373
///
7474
/// [weight units]: https://en.bitcoin.it/wiki/Weight_units
75-
pub satisfaction_weight: usize, // FIXME: (@leonardo) I think it should expect the new bitcoin-units `Weight` type instead of usize.
75+
pub satisfaction_weight: Weight,
7676
/// The UTXO
7777
pub utxo: Utxo,
7878
}

crates/wallet/src/wallet/coin_selection.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@
5353
//! (&mut selected_amount, &mut additional_weight),
5454
//! |(selected_amount, additional_weight), weighted_utxo| {
5555
//! **selected_amount += weighted_utxo.utxo.txout().value.to_sat();
56-
//! **additional_weight += Weight::from_wu(
57-
//! (TxIn::default().segwit_weight().to_wu()
58-
//! + weighted_utxo.satisfaction_weight as u64)
59-
//! as u64,
60-
//! );
56+
//! **additional_weight += TxIn::default()
57+
//! .segwit_weight()
58+
//! .checked_add(weighted_utxo.satisfaction_weight)
59+
//! .expect("valid `Weight`'s");
6160
//! Some(weighted_utxo.utxo)
6261
//! },
6362
//! )
@@ -344,10 +343,10 @@ fn select_sorted_utxos(
344343
|(selected_amount, fee_amount), (must_use, weighted_utxo)| {
345344
if must_use || **selected_amount < target_amount + **fee_amount {
346345
**fee_amount += (fee_rate
347-
* Weight::from_wu(
348-
TxIn::default().segwit_weight().to_wu()
349-
+ weighted_utxo.satisfaction_weight as u64,
350-
))
346+
* (TxIn::default()
347+
.segwit_weight()
348+
.checked_add(weighted_utxo.satisfaction_weight)
349+
.expect("valid `Weight`'s")))
351350
.to_sat();
352351
**selected_amount += weighted_utxo.utxo.txout().value.to_sat();
353352
Some(weighted_utxo.utxo)
@@ -390,9 +389,10 @@ struct OutputGroup {
390389
impl OutputGroup {
391390
fn new(weighted_utxo: WeightedUtxo, fee_rate: FeeRate) -> Self {
392391
let fee = (fee_rate
393-
* Weight::from_wu(
394-
TxIn::default().segwit_weight().to_wu() + weighted_utxo.satisfaction_weight as u64,
395-
))
392+
* (TxIn::default()
393+
.segwit_weight()
394+
.checked_add(weighted_utxo.satisfaction_weight)
395+
.expect("valid `Weight`'s")))
396396
.to_sat();
397397
let effective_value = weighted_utxo.utxo.txout().value.to_sat() as i64 - fee as i64;
398398
OutputGroup {
@@ -767,7 +767,7 @@ mod test {
767767
))
768768
.unwrap();
769769
WeightedUtxo {
770-
satisfaction_weight: P2WPKH_SATISFACTION_SIZE,
770+
satisfaction_weight: Weight::from_wu_usize(P2WPKH_SATISFACTION_SIZE),
771771
utxo: Utxo::Local(LocalOutput {
772772
outpoint,
773773
txout: TxOut {
@@ -827,7 +827,7 @@ mod test {
827827
let mut res = Vec::new();
828828
for i in 0..utxos_number {
829829
res.push(WeightedUtxo {
830-
satisfaction_weight: P2WPKH_SATISFACTION_SIZE,
830+
satisfaction_weight: Weight::from_wu_usize(P2WPKH_SATISFACTION_SIZE),
831831
utxo: Utxo::Local(LocalOutput {
832832
outpoint: OutPoint::from_str(&format!(
833833
"ebd9813ecebc57ff8f30797de7c205e3c7498ca950ea4341ee51a685ff2fa30a:{}",
@@ -858,7 +858,7 @@ mod test {
858858
fn generate_same_value_utxos(utxos_value: u64, utxos_number: usize) -> Vec<WeightedUtxo> {
859859
(0..utxos_number)
860860
.map(|i| WeightedUtxo {
861-
satisfaction_weight: P2WPKH_SATISFACTION_SIZE,
861+
satisfaction_weight: Weight::from_wu_usize(P2WPKH_SATISFACTION_SIZE),
862862
utxo: Utxo::Local(LocalOutput {
863863
outpoint: OutPoint::from_str(&format!(
864864
"ebd9813ecebc57ff8f30797de7c205e3c7498ca950ea4341ee51a685ff2fa30a:{}",
@@ -1513,7 +1513,7 @@ mod test {
15131513
fn test_filter_duplicates() {
15141514
fn utxo(txid: &str, value: u64) -> WeightedUtxo {
15151515
WeightedUtxo {
1516-
satisfaction_weight: 0,
1516+
satisfaction_weight: Weight::ZERO,
15171517
utxo: Utxo::Local(LocalOutput {
15181518
outpoint: OutPoint::new(bitcoin::hashes::Hash::hash(txid.as_bytes()), 0),
15191519
txout: TxOut {

crates/wallet/src/wallet/mod.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@ use bdk_chain::{
3232
IndexedTxGraph,
3333
};
3434
use bdk_persist::{Persist, PersistBackend};
35-
use bitcoin::secp256k1::{All, Secp256k1};
3635
use bitcoin::sighash::{EcdsaSighashType, TapSighashType};
3736
use bitcoin::{
3837
absolute, psbt, Address, Block, FeeRate, Network, OutPoint, Script, ScriptBuf, Sequence,
3938
Transaction, TxOut, Txid, Witness,
4039
};
4140
use bitcoin::{consensus::encode::serialize, transaction, BlockHash, Psbt};
4241
use bitcoin::{constants::genesis_block, Amount};
42+
use bitcoin::{
43+
secp256k1::{All, Secp256k1},
44+
Weight,
45+
};
4346
use core::fmt;
4447
use core::ops::Deref;
4548
use descriptor::error::Error as DescriptorError;
@@ -1736,12 +1739,11 @@ impl Wallet {
17361739

17371740
let weighted_utxo = match txout_index.index_of_spk(&txout.script_pubkey) {
17381741
Some((keychain, derivation_index)) => {
1739-
// FIXME: (@leonardo) I think it should expect the new bitcoin-units `Weight` type instead of usize.
1742+
// TODO: (@leonardo) remove unwrap() use here, use expect or proper error!
17401743
let satisfaction_weight = self
17411744
.get_descriptor_for_keychain(keychain)
17421745
.max_weight_to_satisfy()
1743-
.unwrap()
1744-
.to_wu() as usize;
1746+
.unwrap();
17451747
WeightedUtxo {
17461748
utxo: Utxo::Local(LocalOutput {
17471749
outpoint: txin.previous_output,
@@ -1755,8 +1757,9 @@ impl Wallet {
17551757
}
17561758
}
17571759
None => {
1758-
let satisfaction_weight =
1759-
serialize(&txin.script_sig).len() * 4 + serialize(&txin.witness).len();
1760+
let satisfaction_weight = Weight::from_wu_usize(
1761+
serialize(&txin.script_sig).len() * 4 + serialize(&txin.witness).len(),
1762+
);
17601763
WeightedUtxo {
17611764
utxo: Utxo::Foreign {
17621765
outpoint: txin.previous_output,
@@ -2074,16 +2077,14 @@ impl Wallet {
20742077
descriptor.at_derivation_index(child).ok()
20752078
}
20762079

2077-
fn get_available_utxos(&self) -> Vec<(LocalOutput, usize)> {
2080+
fn get_available_utxos(&self) -> Vec<(LocalOutput, Weight)> {
20782081
self.list_unspent()
20792082
.map(|utxo| {
20802083
let keychain = utxo.keychain;
20812084
(utxo, {
2082-
// FIXME: (@leonardo) I think it should expect the new bitcoin-units `Weight` type instead of usize.
20832085
self.get_descriptor_for_keychain(keychain)
20842086
.max_weight_to_satisfy()
2085-
.unwrap()
2086-
.to_wu() as usize
2087+
.unwrap() // TODO: (@leonardo) remove unwrap() use here, use expect or proper error!
20872088
})
20882089
})
20892090
.collect()

crates/wallet/src/wallet/tx_builder.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ use core::fmt;
4545

4646
use bitcoin::psbt::{self, Psbt};
4747
use bitcoin::script::PushBytes;
48-
use bitcoin::{absolute, Amount, FeeRate, OutPoint, ScriptBuf, Sequence, Transaction, Txid};
48+
use bitcoin::{
49+
absolute, Amount, FeeRate, OutPoint, ScriptBuf, Sequence, Transaction, Txid, Weight,
50+
};
4951

5052
use super::coin_selection::CoinSelectionAlgorithm;
5153
use super::{CreateTxError, Wallet};
@@ -295,9 +297,8 @@ impl<'a, Cs> TxBuilder<'a, Cs> {
295297

296298
for utxo in utxos {
297299
let descriptor = wallet.get_descriptor_for_keychain(utxo.keychain);
298-
// FIXME: (@leonardo) I think it should expect the new bitcoin-units `Weight` type instead of usize.
299-
let satisfaction_weight =
300-
descriptor.max_weight_to_satisfy().unwrap().to_wu() as usize;
300+
// TODO: (@leonardo) remove unwrap() use here, use expect or proper error!
301+
let satisfaction_weight = descriptor.max_weight_to_satisfy().unwrap();
301302
self.params.utxos.push(WeightedUtxo {
302303
satisfaction_weight,
303304
utxo: Utxo::Local(utxo),
@@ -366,7 +367,7 @@ impl<'a, Cs> TxBuilder<'a, Cs> {
366367
&mut self,
367368
outpoint: OutPoint,
368369
psbt_input: psbt::Input,
369-
satisfaction_weight: usize,
370+
satisfaction_weight: Weight,
370371
) -> Result<&mut Self, AddForeignUtxoError> {
371372
self.add_foreign_utxo_with_sequence(
372373
outpoint,
@@ -381,7 +382,7 @@ impl<'a, Cs> TxBuilder<'a, Cs> {
381382
&mut self,
382383
outpoint: OutPoint,
383384
psbt_input: psbt::Input,
384-
satisfaction_weight: usize,
385+
satisfaction_weight: Weight,
385386
sequence: Sequence,
386387
) -> Result<&mut Self, AddForeignUtxoError> {
387388
if psbt_input.witness_utxo.is_none() {

crates/wallet/tests/wallet.rs

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,11 +1310,7 @@ fn test_add_foreign_utxo() {
13101310
builder
13111311
.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000))
13121312
.only_witness_utxo()
1313-
.add_foreign_utxo(
1314-
utxo.outpoint,
1315-
psbt_input,
1316-
foreign_utxo_satisfaction.to_wu() as usize,
1317-
)
1313+
.add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction)
13181314
.unwrap();
13191315
let mut psbt = builder.finish().unwrap();
13201316
wallet1.insert_txout(utxo.outpoint, utxo.txout);
@@ -1390,11 +1386,7 @@ fn test_calculate_fee_with_missing_foreign_utxo() {
13901386
builder
13911387
.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000))
13921388
.only_witness_utxo()
1393-
.add_foreign_utxo(
1394-
utxo.outpoint,
1395-
psbt_input,
1396-
foreign_utxo_satisfaction.to_wu() as usize,
1397-
)
1389+
.add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction)
13981390
.unwrap();
13991391
let psbt = builder.finish().unwrap();
14001392
let tx = psbt.extract_tx().expect("failed to extract tx");
@@ -1411,11 +1403,8 @@ fn test_add_foreign_utxo_invalid_psbt_input() {
14111403
.unwrap();
14121404

14131405
let mut builder = wallet.build_tx();
1414-
let result = builder.add_foreign_utxo(
1415-
outpoint,
1416-
psbt::Input::default(),
1417-
foreign_utxo_satisfaction.to_wu() as usize,
1418-
);
1406+
let result =
1407+
builder.add_foreign_utxo(outpoint, psbt::Input::default(), foreign_utxo_satisfaction);
14191408
assert!(matches!(result, Err(AddForeignUtxoError::MissingUtxo)));
14201409
}
14211410

@@ -1443,7 +1432,7 @@ fn test_add_foreign_utxo_where_outpoint_doesnt_match_psbt_input() {
14431432
non_witness_utxo: Some(tx1.as_ref().clone()),
14441433
..Default::default()
14451434
},
1446-
satisfaction_weight.to_wu() as usize
1435+
satisfaction_weight
14471436
)
14481437
.is_err(),
14491438
"should fail when outpoint doesn't match psbt_input"
@@ -1456,7 +1445,7 @@ fn test_add_foreign_utxo_where_outpoint_doesnt_match_psbt_input() {
14561445
non_witness_utxo: Some(tx2.as_ref().clone()),
14571446
..Default::default()
14581447
},
1459-
satisfaction_weight.to_wu() as usize
1448+
satisfaction_weight
14601449
)
14611450
.is_ok(),
14621451
"should be ok when outpoint does match psbt_input"
@@ -1488,11 +1477,7 @@ fn test_add_foreign_utxo_only_witness_utxo() {
14881477
..Default::default()
14891478
};
14901479
builder
1491-
.add_foreign_utxo(
1492-
utxo2.outpoint,
1493-
psbt_input,
1494-
satisfaction_weight.to_wu() as usize,
1495-
)
1480+
.add_foreign_utxo(utxo2.outpoint, psbt_input, satisfaction_weight)
14961481
.unwrap();
14971482
assert!(
14981483
builder.finish().is_err(),
@@ -1508,11 +1493,7 @@ fn test_add_foreign_utxo_only_witness_utxo() {
15081493
};
15091494
builder
15101495
.only_witness_utxo()
1511-
.add_foreign_utxo(
1512-
utxo2.outpoint,
1513-
psbt_input,
1514-
satisfaction_weight.to_wu() as usize,
1515-
)
1496+
.add_foreign_utxo(utxo2.outpoint, psbt_input, satisfaction_weight)
15161497
.unwrap();
15171498
assert!(
15181499
builder.finish().is_ok(),
@@ -1528,11 +1509,7 @@ fn test_add_foreign_utxo_only_witness_utxo() {
15281509
..Default::default()
15291510
};
15301511
builder
1531-
.add_foreign_utxo(
1532-
utxo2.outpoint,
1533-
psbt_input,
1534-
satisfaction_weight.to_wu() as usize,
1535-
)
1512+
.add_foreign_utxo(utxo2.outpoint, psbt_input, satisfaction_weight)
15361513
.unwrap();
15371514
assert!(
15381515
builder.finish().is_ok(),
@@ -3372,11 +3349,7 @@ fn test_taproot_foreign_utxo() {
33723349
let mut builder = wallet1.build_tx();
33733350
builder
33743351
.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000))
3375-
.add_foreign_utxo(
3376-
utxo.outpoint,
3377-
psbt_input,
3378-
foreign_utxo_satisfaction.to_wu() as usize,
3379-
)
3352+
.add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction)
33803353
.unwrap();
33813354
let psbt = builder.finish().unwrap();
33823355
let sent_received =

0 commit comments

Comments
 (0)