Skip to content

Commit 44e446f

Browse files
committed
wip(feat): use Weight type instead of usize
1 parent 9cc5066 commit 44e446f

File tree

5 files changed

+46
-69
lines changed

5 files changed

+46
-69
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,
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 & 8 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;
@@ -1700,11 +1703,11 @@ impl Wallet {
17001703

17011704
let weighted_utxo = match txout_index.index_of_spk(&txout.script_pubkey) {
17021705
Some((keychain, derivation_index)) => {
1706+
// TODO: (@leonardo) remove unwrap() use here, use expect or proper error!
17031707
let satisfaction_weight = self
17041708
.get_descriptor_for_keychain(keychain)
17051709
.max_weight_to_satisfy()
1706-
.unwrap()
1707-
.to_wu() as usize;
1710+
.unwrap();
17081711
WeightedUtxo {
17091712
utxo: Utxo::Local(LocalOutput {
17101713
outpoint: txin.previous_output,
@@ -1718,8 +1721,9 @@ impl Wallet {
17181721
}
17191722
}
17201723
None => {
1721-
let satisfaction_weight =
1722-
serialize(&txin.script_sig).len() * 4 + serialize(&txin.witness).len();
1724+
let satisfaction_weight = Weight::from_wu_usize(
1725+
serialize(&txin.script_sig).len() * 4 + serialize(&txin.witness).len(),
1726+
);
17231727
WeightedUtxo {
17241728
utxo: Utxo::Foreign {
17251729
outpoint: txin.previous_output,
@@ -2029,15 +2033,14 @@ impl Wallet {
20292033
descriptor.at_derivation_index(child).ok()
20302034
}
20312035

2032-
fn get_available_utxos(&self) -> Vec<(LocalOutput, usize)> {
2036+
fn get_available_utxos(&self) -> Vec<(LocalOutput, Weight)> {
20332037
self.list_unspent()
20342038
.map(|utxo| {
20352039
let keychain = utxo.keychain;
20362040
(utxo, {
20372041
self.get_descriptor_for_keychain(keychain)
20382042
.max_weight_to_satisfy()
2039-
.unwrap()
2040-
.to_wu() as usize
2043+
.unwrap() // TODO: (@leonardo) remove unwrap() use here, use expect or proper error!
20412044
})
20422045
})
20432046
.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-
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
@@ -1346,11 +1346,7 @@ fn test_add_foreign_utxo() {
13461346
builder
13471347
.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000))
13481348
.only_witness_utxo()
1349-
.add_foreign_utxo(
1350-
utxo.outpoint,
1351-
psbt_input,
1352-
foreign_utxo_satisfaction.to_wu() as usize,
1353-
)
1349+
.add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction)
13541350
.unwrap();
13551351
let mut psbt = builder.finish().unwrap();
13561352
wallet1.insert_txout(utxo.outpoint, utxo.txout);
@@ -1426,11 +1422,7 @@ fn test_calculate_fee_with_missing_foreign_utxo() {
14261422
builder
14271423
.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000))
14281424
.only_witness_utxo()
1429-
.add_foreign_utxo(
1430-
utxo.outpoint,
1431-
psbt_input,
1432-
foreign_utxo_satisfaction.to_wu() as usize,
1433-
)
1425+
.add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction)
14341426
.unwrap();
14351427
let psbt = builder.finish().unwrap();
14361428
let tx = psbt.extract_tx().expect("failed to extract tx");
@@ -1447,11 +1439,8 @@ fn test_add_foreign_utxo_invalid_psbt_input() {
14471439
.unwrap();
14481440

14491441
let mut builder = wallet.build_tx();
1450-
let result = builder.add_foreign_utxo(
1451-
outpoint,
1452-
psbt::Input::default(),
1453-
foreign_utxo_satisfaction.to_wu() as usize,
1454-
);
1442+
let result =
1443+
builder.add_foreign_utxo(outpoint, psbt::Input::default(), foreign_utxo_satisfaction);
14551444
assert!(matches!(result, Err(AddForeignUtxoError::MissingUtxo)));
14561445
}
14571446

@@ -1479,7 +1468,7 @@ fn test_add_foreign_utxo_where_outpoint_doesnt_match_psbt_input() {
14791468
non_witness_utxo: Some(tx1.as_ref().clone()),
14801469
..Default::default()
14811470
},
1482-
satisfaction_weight.to_wu() as usize
1471+
satisfaction_weight
14831472
)
14841473
.is_err(),
14851474
"should fail when outpoint doesn't match psbt_input"
@@ -1492,7 +1481,7 @@ fn test_add_foreign_utxo_where_outpoint_doesnt_match_psbt_input() {
14921481
non_witness_utxo: Some(tx2.as_ref().clone()),
14931482
..Default::default()
14941483
},
1495-
satisfaction_weight.to_wu() as usize
1484+
satisfaction_weight
14961485
)
14971486
.is_ok(),
14981487
"should be ok when outpoint does match psbt_input"
@@ -1524,11 +1513,7 @@ fn test_add_foreign_utxo_only_witness_utxo() {
15241513
..Default::default()
15251514
};
15261515
builder
1527-
.add_foreign_utxo(
1528-
utxo2.outpoint,
1529-
psbt_input,
1530-
satisfaction_weight.to_wu() as usize,
1531-
)
1516+
.add_foreign_utxo(utxo2.outpoint, psbt_input, satisfaction_weight)
15321517
.unwrap();
15331518
assert!(
15341519
builder.finish().is_err(),
@@ -1544,11 +1529,7 @@ fn test_add_foreign_utxo_only_witness_utxo() {
15441529
};
15451530
builder
15461531
.only_witness_utxo()
1547-
.add_foreign_utxo(
1548-
utxo2.outpoint,
1549-
psbt_input,
1550-
satisfaction_weight.to_wu() as usize,
1551-
)
1532+
.add_foreign_utxo(utxo2.outpoint, psbt_input, satisfaction_weight)
15521533
.unwrap();
15531534
assert!(
15541535
builder.finish().is_ok(),
@@ -1564,11 +1545,7 @@ fn test_add_foreign_utxo_only_witness_utxo() {
15641545
..Default::default()
15651546
};
15661547
builder
1567-
.add_foreign_utxo(
1568-
utxo2.outpoint,
1569-
psbt_input,
1570-
satisfaction_weight.to_wu() as usize,
1571-
)
1548+
.add_foreign_utxo(utxo2.outpoint, psbt_input, satisfaction_weight)
15721549
.unwrap();
15731550
assert!(
15741551
builder.finish().is_ok(),
@@ -3405,11 +3382,7 @@ fn test_taproot_foreign_utxo() {
34053382
let mut builder = wallet1.build_tx();
34063383
builder
34073384
.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000))
3408-
.add_foreign_utxo(
3409-
utxo.outpoint,
3410-
psbt_input,
3411-
foreign_utxo_satisfaction.to_wu() as usize,
3412-
)
3385+
.add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction)
34133386
.unwrap();
34143387
let psbt = builder.finish().unwrap();
34153388
let sent_received =

0 commit comments

Comments
 (0)