Skip to content

Commit

Permalink
spend: add warning about fee for ancestor
Browse files Browse the repository at this point in the history
  • Loading branch information
jp1ac4 committed Jan 18, 2024
1 parent 948a75a commit db52c83
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/spend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ pub enum SpendTxFees {
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum CreateSpendWarning {
ChangeAddedToFee(u64),
AddtionalFeeForAncestors(u64),
}

impl fmt::Display for CreateSpendWarning {
Expand All @@ -554,6 +555,12 @@ impl fmt::Display for CreateSpendWarning {
amt,
if *amt > 1 {"s"} else {""},
),
CreateSpendWarning::AddtionalFeeForAncestors(amt) => write!(
f,
"An additional fee of {} sat{} has been added to pay for ancestors at the target feerate.",
amt,
if *amt > 1 {"s"} else {""},
),
}
}
}
Expand Down Expand Up @@ -673,7 +680,7 @@ pub fn create_spend(
selected,
change_amount,
max_change_amount,
..
fee_for_ancestors,
} = {
// At this point the transaction still has no input and no change output, as expected
// by the coins selection helper function.
Expand Down Expand Up @@ -735,6 +742,12 @@ pub fn create_spend(
));
}

if fee_for_ancestors.to_sat() > 0 {
warnings.push(CreateSpendWarning::AddtionalFeeForAncestors(
fee_for_ancestors.to_sat(),
));
}

// Iterate through selected coins and add necessary information to the PSBT inputs.
let mut psbt_ins = Vec::with_capacity(selected.len());
for cand in &selected {
Expand Down
11 changes: 10 additions & 1 deletion tests/test_spend.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,16 @@ def sign_and_broadcast(psbt):
psbt = PSBT.from_base64(res["psbt"])
sign_and_broadcast(psbt)
assert len(psbt.o) == 4
assert len(res["warnings"]) == 0
assert bitcoind.rpc.getmempoolentry(deposit_d)["ancestorsize"] == 165
assert bitcoind.rpc.getmempoolentry(deposit_d)["fees"]["ancestor"] * COIN == 165
# ancestor vsize at feerate 2 sat/vb = ancestor_fee / 2 = 165 / 2 = 82
# extra_weight <= (extra vsize * witness factor) = (165 - 82) * 4 = 332
# additional fee at 2 sat/vb (0.5 sat/wu) = 332 * 0.5 = 166
assert len(res["warnings"]) == 1
assert (
res["warnings"][0]
== "An additional fee of 166 sats has been added to pay for ancestors at the target feerate."
)

# All the spent coins must have been detected as such
all_deposits = (deposit_a, deposit_b, deposit_c, deposit_d)
Expand Down

0 comments on commit db52c83

Please sign in to comment.