use bdk_chain::ConfirmationTime;
use bitcoin::blockdata::transaction::{OutPoint, Sequence, TxOut};
-use bitcoin::psbt;
+use bitcoin::{psbt, Weight};
use serde::{Deserialize, Serialize};
/// properly maintain the feerate when adding this input to a transaction during coin selection.
///
/// [weight units]: https://en.bitcoin.it/wiki/Weight_units
- pub satisfaction_weight: usize,
+ pub satisfaction_weight: Weight,
/// The UTXO
pub utxo: Utxo,
}
//! (&mut selected_amount, &mut additional_weight),
//! |(selected_amount, additional_weight), weighted_utxo| {
//! **selected_amount += weighted_utxo.utxo.txout().value.to_sat();
-//! **additional_weight += Weight::from_wu(
-//! (TxIn::default().segwit_weight().to_wu()
-//! + weighted_utxo.satisfaction_weight as u64)
-//! as u64,
-//! );
+//! **additional_weight += TxIn::default()
+//! .segwit_weight()
+//! .checked_add(weighted_utxo.satisfaction_weight)
+//! .expect("`Weight` addition should not cause an integer overflow");
//! Some(weighted_utxo.utxo)
//! },
//! )
|(selected_amount, fee_amount), (must_use, weighted_utxo)| {
if must_use || **selected_amount < target_amount + **fee_amount {
**fee_amount += (fee_rate
- * Weight::from_wu(
- TxIn::default().segwit_weight().to_wu()
- + weighted_utxo.satisfaction_weight as u64,
- ))
+ * (TxIn::default()
+ .segwit_weight()
+ .checked_add(weighted_utxo.satisfaction_weight)
+ .expect("`Weight` addition should not cause an integer overflow")))
.to_sat();
**selected_amount += weighted_utxo.utxo.txout().value.to_sat();
Some(weighted_utxo.utxo)
impl OutputGroup {
fn new(weighted_utxo: WeightedUtxo, fee_rate: FeeRate) -> Self {
let fee = (fee_rate
- * Weight::from_wu(
- TxIn::default().segwit_weight().to_wu() + weighted_utxo.satisfaction_weight as u64,
- ))
+ * (TxIn::default()
+ .segwit_weight()
+ .checked_add(weighted_utxo.satisfaction_weight)
+ .expect("`Weight` addition should not cause an integer overflow")))
.to_sat();
let effective_value = weighted_utxo.utxo.txout().value.to_sat() as i64 - fee as i64;
OutputGroup {
))
.unwrap();
WeightedUtxo {
- satisfaction_weight: P2WPKH_SATISFACTION_SIZE,
+ satisfaction_weight: Weight::from_wu_usize(P2WPKH_SATISFACTION_SIZE),
utxo: Utxo::Local(LocalOutput {
outpoint,
txout: TxOut {
let mut res = Vec::new();
for i in 0..utxos_number {
res.push(WeightedUtxo {
- satisfaction_weight: P2WPKH_SATISFACTION_SIZE,
+ satisfaction_weight: Weight::from_wu_usize(P2WPKH_SATISFACTION_SIZE),
utxo: Utxo::Local(LocalOutput {
outpoint: OutPoint::from_str(&format!(
"ebd9813ecebc57ff8f30797de7c205e3c7498ca950ea4341ee51a685ff2fa30a:{}",
fn generate_same_value_utxos(utxos_value: u64, utxos_number: usize) -> Vec<WeightedUtxo> {
(0..utxos_number)
.map(|i| WeightedUtxo {
- satisfaction_weight: P2WPKH_SATISFACTION_SIZE,
+ satisfaction_weight: Weight::from_wu_usize(P2WPKH_SATISFACTION_SIZE),
utxo: Utxo::Local(LocalOutput {
outpoint: OutPoint::from_str(&format!(
"ebd9813ecebc57ff8f30797de7c205e3c7498ca950ea4341ee51a685ff2fa30a:{}",
fn test_filter_duplicates() {
fn utxo(txid: &str, value: u64) -> WeightedUtxo {
WeightedUtxo {
- satisfaction_weight: 0,
+ satisfaction_weight: Weight::ZERO,
utxo: Utxo::Local(LocalOutput {
outpoint: OutPoint::new(bitcoin::hashes::Hash::hash(txid.as_bytes()), 0),
txout: TxOut {
Append, BlockId, ChainPosition, ConfirmationTime, ConfirmationTimeHeightAnchor, FullTxOut,
Indexed, IndexedTxGraph,
};
-use bitcoin::secp256k1::{All, Secp256k1};
use bitcoin::sighash::{EcdsaSighashType, TapSighashType};
use bitcoin::{
absolute, psbt, Address, Block, FeeRate, Network, OutPoint, Script, ScriptBuf, Sequence,
};
use bitcoin::{consensus::encode::serialize, transaction, BlockHash, Psbt};
use bitcoin::{constants::genesis_block, Amount};
+use bitcoin::{
+ secp256k1::{All, Secp256k1},
+ Weight,
+};
use core::fmt;
use core::mem;
use core::ops::Deref;
let satisfaction_weight = self
.get_descriptor_for_keychain(keychain)
.max_weight_to_satisfy()
- .unwrap()
- .to_wu() as usize;
+ .unwrap();
WeightedUtxo {
utxo: Utxo::Local(LocalOutput {
outpoint: txin.previous_output,
}
}
None => {
- let satisfaction_weight =
- serialize(&txin.script_sig).len() * 4 + serialize(&txin.witness).len();
+ let satisfaction_weight = Weight::from_wu_usize(
+ serialize(&txin.script_sig).len() * 4 + serialize(&txin.witness).len(),
+ );
WeightedUtxo {
utxo: Utxo::Foreign {
outpoint: txin.previous_output,
descriptor.at_derivation_index(child).ok()
}
- fn get_available_utxos(&self) -> Vec<(LocalOutput, usize)> {
+ fn get_available_utxos(&self) -> Vec<(LocalOutput, Weight)> {
self.list_unspent()
.map(|utxo| {
let keychain = utxo.keychain;
self.get_descriptor_for_keychain(keychain)
.max_weight_to_satisfy()
.unwrap()
- .to_wu() as usize
})
})
.collect()
use bitcoin::psbt::{self, Psbt};
use bitcoin::script::PushBytes;
-use bitcoin::{absolute, Amount, FeeRate, OutPoint, ScriptBuf, Sequence, Transaction, Txid};
+use bitcoin::{
+ absolute, Amount, FeeRate, OutPoint, ScriptBuf, Sequence, Transaction, Txid, Weight,
+};
use rand_core::RngCore;
use super::coin_selection::CoinSelectionAlgorithm;
for utxo in utxos {
let descriptor = wallet.get_descriptor_for_keychain(utxo.keychain);
-
- let satisfaction_weight =
- descriptor.max_weight_to_satisfy().unwrap().to_wu() as usize;
+ let satisfaction_weight = descriptor.max_weight_to_satisfy().unwrap();
self.params.utxos.push(WeightedUtxo {
satisfaction_weight,
utxo: Utxo::Local(utxo),
&mut self,
outpoint: OutPoint,
psbt_input: psbt::Input,
- satisfaction_weight: usize,
+ satisfaction_weight: Weight,
) -> Result<&mut Self, AddForeignUtxoError> {
self.add_foreign_utxo_with_sequence(
outpoint,
&mut self,
outpoint: OutPoint,
psbt_input: psbt::Input,
- satisfaction_weight: usize,
+ satisfaction_weight: Weight,
sequence: Sequence,
) -> Result<&mut Self, AddForeignUtxoError> {
if psbt_input.witness_utxo.is_none() {
builder
.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000))
.only_witness_utxo()
- .add_foreign_utxo(
- utxo.outpoint,
- psbt_input,
- foreign_utxo_satisfaction.to_wu() as usize,
- )
+ .add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction)
.unwrap();
let mut psbt = builder.finish().unwrap();
wallet1.insert_txout(utxo.outpoint, utxo.txout);
builder
.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000))
.only_witness_utxo()
- .add_foreign_utxo(
- utxo.outpoint,
- psbt_input,
- foreign_utxo_satisfaction.to_wu() as usize,
- )
+ .add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction)
.unwrap();
let psbt = builder.finish().unwrap();
let tx = psbt.extract_tx().expect("failed to extract tx");
.unwrap();
let mut builder = wallet.build_tx();
- let result = builder.add_foreign_utxo(
- outpoint,
- psbt::Input::default(),
- foreign_utxo_satisfaction.to_wu() as usize,
- );
+ let result =
+ builder.add_foreign_utxo(outpoint, psbt::Input::default(), foreign_utxo_satisfaction);
assert!(matches!(result, Err(AddForeignUtxoError::MissingUtxo)));
}
non_witness_utxo: Some(tx1.as_ref().clone()),
..Default::default()
},
- satisfaction_weight.to_wu() as usize
+ satisfaction_weight
)
.is_err(),
"should fail when outpoint doesn't match psbt_input"
non_witness_utxo: Some(tx2.as_ref().clone()),
..Default::default()
},
- satisfaction_weight.to_wu() as usize
+ satisfaction_weight
)
.is_ok(),
"should be ok when outpoint does match psbt_input"
..Default::default()
};
builder
- .add_foreign_utxo(
- utxo2.outpoint,
- psbt_input,
- satisfaction_weight.to_wu() as usize,
- )
+ .add_foreign_utxo(utxo2.outpoint, psbt_input, satisfaction_weight)
.unwrap();
assert!(
builder.finish().is_err(),
};
builder
.only_witness_utxo()
- .add_foreign_utxo(
- utxo2.outpoint,
- psbt_input,
- satisfaction_weight.to_wu() as usize,
- )
+ .add_foreign_utxo(utxo2.outpoint, psbt_input, satisfaction_weight)
.unwrap();
assert!(
builder.finish().is_ok(),
..Default::default()
};
builder
- .add_foreign_utxo(
- utxo2.outpoint,
- psbt_input,
- satisfaction_weight.to_wu() as usize,
- )
+ .add_foreign_utxo(utxo2.outpoint, psbt_input, satisfaction_weight)
.unwrap();
assert!(
builder.finish().is_ok(),
let mut builder = wallet1.build_tx();
builder
.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000))
- .add_foreign_utxo(
- utxo.outpoint,
- psbt_input,
- foreign_utxo_satisfaction.to_wu() as usize,
- )
+ .add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction)
.unwrap();
let psbt = builder.finish().unwrap();
let sent_received =