Skip to content

Commit

Permalink
test(bdk): initialize all feerates from u64
Browse files Browse the repository at this point in the history
This makes the helper `feerate_unchecked` now redundant but
still usable.
  • Loading branch information
ValuedMammal committed Jan 31, 2024
1 parent c685276 commit d928fd3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
11 changes: 6 additions & 5 deletions crates/bdk/tests/psbt.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bdk::bitcoin::FeeRate;
use bdk::bitcoin::TxIn;
use bdk::wallet::AddressIndex;
use bdk::wallet::AddressIndex::New;
Expand All @@ -10,10 +11,10 @@ use common::*;
// from bip 174
const PSBT_STR: &str = "cHNidP8BAKACAAAAAqsJSaCMWvfEm4IS9Bfi8Vqz9cM9zxU4IagTn4d6W3vkAAAAAAD+////qwlJoIxa98SbghL0F+LxWrP1wz3PFTghqBOfh3pbe+QBAAAAAP7///8CYDvqCwAAAAAZdqkUdopAu9dAy+gdmI5x3ipNXHE5ax2IrI4kAAAAAAAAGXapFG9GILVT+glechue4O/p+gOcykWXiKwAAAAAAAEHakcwRAIgR1lmF5fAGwNrJZKJSGhiGDR9iYZLcZ4ff89X0eURZYcCIFMJ6r9Wqk2Ikf/REf3xM286KdqGbX+EhtdVRs7tr5MZASEDXNxh/HupccC1AaZGoqg7ECy0OIEhfKaC3Ibi1z+ogpIAAQEgAOH1BQAAAAAXqRQ1RebjO4MsRwUPJNPuuTycA5SLx4cBBBYAFIXRNTfy4mVAWjTbr6nj3aAfuCMIAAAA";

fn feerate_unchecked(sat_vb: f64) -> bitcoin::FeeRate {
fn _feerate_unchecked(sat_vb: f64) -> FeeRate {
// 1 sat_vb / 4wu_vb * 1000kwu_wu = 250 sat_kwu
let sat_kwu = (sat_vb * 250.0).ceil() as u64;
bitcoin::FeeRate::from_sat_per_kwu(sat_kwu)
FeeRate::from_sat_per_kwu(sat_kwu)
}

#[test]
Expand Down Expand Up @@ -88,7 +89,7 @@ fn test_psbt_sign_with_finalized() {
fn test_psbt_fee_rate_with_witness_utxo() {
use psbt::PsbtUtils;

let expected_fee_rate = feerate_unchecked(1.2345);
let expected_fee_rate = FeeRate::from_sat_per_kwu(310);

let (mut wallet, _) = get_funded_wallet("wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)");
let addr = wallet.get_address(New);
Expand All @@ -113,7 +114,7 @@ fn test_psbt_fee_rate_with_witness_utxo() {
fn test_psbt_fee_rate_with_nonwitness_utxo() {
use psbt::PsbtUtils;

let expected_fee_rate = feerate_unchecked(1.2345);
let expected_fee_rate = FeeRate::from_sat_per_kwu(310);

let (mut wallet, _) = get_funded_wallet("pkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)");
let addr = wallet.get_address(New);
Expand All @@ -137,7 +138,7 @@ fn test_psbt_fee_rate_with_nonwitness_utxo() {
fn test_psbt_fee_rate_with_missing_txout() {
use psbt::PsbtUtils;

let expected_fee_rate = feerate_unchecked(1.2345);
let expected_fee_rate = FeeRate::from_sat_per_kwu(310);

let (mut wpkh_wallet, _) = get_funded_wallet("wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)");
let addr = wpkh_wallet.get_address(New);
Expand Down
14 changes: 8 additions & 6 deletions crates/bdk/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn receive_output_in_latest_block(wallet: &mut Wallet, value: u64) -> OutPoint {
receive_output(wallet, value, anchor)
}

fn feerate_unchecked(sat_vb: f64) -> FeeRate {
fn _feerate_unchecked(sat_vb: f64) -> FeeRate {
// 1 sat_vb / 4wu_vb * 1000kwu_wu = 250 sat_kwu
let sat_kwu = (sat_vb * 250.0).ceil() as u64;
FeeRate::from_sat_per_kwu(sat_kwu)
Expand Down Expand Up @@ -1581,8 +1581,9 @@ fn test_bump_fee_reduce_change() {
.insert_tx(tx, ConfirmationTime::Unconfirmed { last_seen: 0 })
.unwrap();

let feerate = FeeRate::from_sat_per_kwu(625); // 2.5 sat/vb
let mut builder = wallet.build_fee_bump(txid).unwrap();
builder.fee_rate(feerate_unchecked(2.5)).enable_rbf();
builder.fee_rate(feerate).enable_rbf();
let psbt = builder.finish().unwrap();
let sent_received = wallet.sent_and_received(&psbt.clone().extract_tx());
let fee = check_fee!(wallet, psbt);
Expand Down Expand Up @@ -1613,7 +1614,7 @@ fn test_bump_fee_reduce_change() {
sent_received.1
);

assert_fee_rate!(psbt, fee.unwrap_or(0), feerate_unchecked(2.5), @add_signature);
assert_fee_rate!(psbt, fee.unwrap_or(0), feerate, @add_signature);

let mut builder = wallet.build_fee_bump(txid).unwrap();
builder.fee_absolute(200);
Expand Down Expand Up @@ -1676,9 +1677,10 @@ fn test_bump_fee_reduce_single_recipient() {
.insert_tx(tx, ConfirmationTime::Unconfirmed { last_seen: 0 })
.unwrap();

let feerate = FeeRate::from_sat_per_kwu(625); // 2.5 sat/vb
let mut builder = wallet.build_fee_bump(txid).unwrap();
builder
.fee_rate(feerate_unchecked(2.5))
.fee_rate(feerate)
.allow_shrinking(addr.script_pubkey())
.unwrap();
let psbt = builder.finish().unwrap();
Expand All @@ -1692,7 +1694,7 @@ fn test_bump_fee_reduce_single_recipient() {
assert_eq!(tx.output.len(), 1);
assert_eq!(tx.output[0].value + fee.unwrap_or(0), sent_received.0);

assert_fee_rate!(psbt, fee.unwrap_or(0), feerate_unchecked(2.5), @add_signature);
assert_fee_rate!(psbt, fee.unwrap_or(0), feerate, @add_signature);
}

#[test]
Expand Down Expand Up @@ -2311,7 +2313,7 @@ fn test_fee_amount_negative_drain_val() {
let send_to = Address::from_str("tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt")
.unwrap()
.assume_checked();
let fee_rate = feerate_unchecked(2.01);
let fee_rate = FeeRate::from_sat_per_kwu(500);
let incoming_op = receive_output_in_latest_block(&mut wallet, 8859);

let mut builder = wallet.build_tx();
Expand Down

0 comments on commit d928fd3

Please sign in to comment.