From 697286c34607682e8252d0638e46d6e5a7f0e849 Mon Sep 17 00:00:00 2001 From: vmammal Date: Thu, 16 Nov 2023 18:13:44 -0500 Subject: [PATCH] refactor(bdk): display CreateTxError::FeeRateTooLow in sat/vb Also modify a unit test `test_bump_fee_low_fee_rate` to additionally assert the expected error message --- crates/bdk/src/wallet/error.rs | 6 ++++-- crates/bdk/src/wallet/mod.rs | 11 +++++++++++ crates/bdk/tests/wallet.rs | 16 +++++++++++++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/crates/bdk/src/wallet/error.rs b/crates/bdk/src/wallet/error.rs index 548dc783fa..46cf8ef3c7 100644 --- a/crates/bdk/src/wallet/error.rs +++ b/crates/bdk/src/wallet/error.rs @@ -168,8 +168,10 @@ where CreateTxError::FeeRateTooLow { required } => { write!( f, - "Fee rate too low: required {} sat/kwu", - required.to_sat_per_kwu() + // Note: alternate fmt as sat/vb (ceil) available in bitcoin-0.31 + //"Fee rate too low: required {required:#}" + "Fee rate too low: required {} sat/vb", + crate::floating_rate!(required) ) } CreateTxError::NoUtxosSelected => { diff --git a/crates/bdk/src/wallet/mod.rs b/crates/bdk/src/wallet/mod.rs index aa35122f1b..1aba1fb704 100644 --- a/crates/bdk/src/wallet/mod.rs +++ b/crates/bdk/src/wallet/mod.rs @@ -2560,6 +2560,17 @@ fn create_signers( Ok((signers, change_signers)) } +/// Transforms a [`FeeRate`] to `f64` with unit of sat/vb. +#[macro_export] +#[doc(hidden)] +macro_rules! floating_rate { + ($rate:expr) => {{ + use $crate::bitcoin::blockdata::constants::WITNESS_SCALE_FACTOR; + // sat_kwu / 250.0 -> sat_vb + $rate.to_sat_per_kwu() as f64 / ((1000 / WITNESS_SCALE_FACTOR) as f64) + }}; +} + #[macro_export] #[doc(hidden)] /// Macro for getting a wallet for use in a doctest diff --git a/crates/bdk/tests/wallet.rs b/crates/bdk/tests/wallet.rs index d16dd3d18e..d54a61231c 100644 --- a/crates/bdk/tests/wallet.rs +++ b/crates/bdk/tests/wallet.rs @@ -1494,7 +1494,6 @@ fn test_bump_fee_confirmed_tx() { } #[test] -#[should_panic(expected = "FeeRateTooLow")] fn test_bump_fee_low_fee_rate() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); let addr = wallet.get_address(New); @@ -1503,6 +1502,7 @@ fn test_bump_fee_low_fee_rate() { .add_recipient(addr.script_pubkey(), 25_000) .enable_rbf(); let psbt = builder.finish().unwrap(); + let feerate = psbt.fee_rate().unwrap(); let tx = psbt.extract_tx(); let txid = tx.txid(); @@ -1512,8 +1512,18 @@ fn test_bump_fee_low_fee_rate() { .unwrap(); let mut builder = wallet.build_fee_bump(txid).unwrap(); - builder.fee_rate(FeeRate::from_sat_per_vb_unchecked(1)); - builder.finish().unwrap(); + builder.fee_rate(FeeRate::BROADCAST_MIN); + let res = builder.finish(); + assert_matches!( + res, + Err(CreateTxError::FeeRateTooLow { .. }), + "expected FeeRateTooLow error" + ); + + let required = feerate.to_sat_per_kwu() + 250; // +1 sat/vb + let sat_vb = required as f64 / 250.0; + let expect = format!("Fee rate too low: required {} sat/vb", sat_vb); + assert_eq!(res.unwrap_err().to_string(), expect); } #[test]