Skip to content

Commit bc7da95

Browse files
jkczyzTheBlueMatt
authored andcommitted
Use a tolerance when estimating remote fees
The interactive-tx construction protocol uses an agreed upon fee rate. Since the bitcoind coin selection algorithm may underpay fees when no change output is needed, providing a tolerance when checking if the remote's fee contribution could avoid some unexpected failures. This commit introduces a 95% tolerance similar to Eclair. Backport of 3aa4574
1 parent 7147775 commit bc7da95

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

lightning/src/ln/interactivetxs.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ impl_writeable_tlv_based!(ConstructedTransaction, {
257257
(11, shared_output_index, required),
258258
});
259259

260+
/// The percent tolerance given to the remote when estimating if they paid enough fees.
261+
const REMOTE_FEE_TOLERANCE_PERCENT: u64 = 95;
262+
260263
impl ConstructedTransaction {
261264
fn new(context: NegotiationContext) -> Result<Self, AbortReason> {
262265
let remote_inputs_value = context.remote_inputs_value();
@@ -315,8 +318,10 @@ impl ConstructedTransaction {
315318

316319
// - the peer's paid feerate does not meet or exceed the agreed feerate (based on the minimum fee).
317320
let remote_fees_contributed = remote_inputs_value.saturating_sub(remote_outputs_value);
318-
let required_remote_contribution_fee =
319-
fee_for_weight(context.feerate_sat_per_kw, remote_weight_contributed);
321+
let required_remote_contribution_fee = fee_for_weight(
322+
(context.feerate_sat_per_kw as u64 * REMOTE_FEE_TOLERANCE_PERCENT / 100) as u32,
323+
remote_weight_contributed,
324+
);
320325
if remote_fees_contributed < required_remote_contribution_fee {
321326
return Err(AbortReason::InsufficientFees);
322327
}
@@ -2377,7 +2382,7 @@ mod tests {
23772382
use super::{
23782383
get_output_weight, ConstructedTransaction, InteractiveTxSigningSession, TxInMetadata,
23792384
P2TR_INPUT_WEIGHT_LOWER_BOUND, P2WPKH_INPUT_WEIGHT_LOWER_BOUND,
2380-
P2WSH_INPUT_WEIGHT_LOWER_BOUND, TX_COMMON_FIELDS_WEIGHT,
2385+
P2WSH_INPUT_WEIGHT_LOWER_BOUND, REMOTE_FEE_TOLERANCE_PERCENT, TX_COMMON_FIELDS_WEIGHT,
23812386
};
23822387

23832388
const TEST_FEERATE_SATS_PER_KW: u32 = FEERATE_FLOOR_SATS_PER_KW * 10;
@@ -2842,7 +2847,7 @@ mod tests {
28422847
let outputs_weight = get_output_weight(&generate_p2wsh_script_pubkey()).to_wu();
28432848
let amount_adjusted_with_p2wpkh_fee = 1_000_000
28442849
- fee_for_weight(
2845-
TEST_FEERATE_SATS_PER_KW,
2850+
(TEST_FEERATE_SATS_PER_KW as u64 * REMOTE_FEE_TOLERANCE_PERCENT / 100) as u32,
28462851
P2WPKH_INPUT_WEIGHT_LOWER_BOUND + TX_COMMON_FIELDS_WEIGHT + outputs_weight,
28472852
);
28482853
do_test_interactive_tx_constructor(TestSession {
@@ -2878,7 +2883,7 @@ mod tests {
28782883
});
28792884
let amount_adjusted_with_p2wsh_fee = 1_000_000
28802885
- fee_for_weight(
2881-
TEST_FEERATE_SATS_PER_KW,
2886+
(TEST_FEERATE_SATS_PER_KW as u64 * REMOTE_FEE_TOLERANCE_PERCENT / 100) as u32,
28822887
P2WSH_INPUT_WEIGHT_LOWER_BOUND + TX_COMMON_FIELDS_WEIGHT + outputs_weight,
28832888
);
28842889
do_test_interactive_tx_constructor(TestSession {
@@ -2914,7 +2919,7 @@ mod tests {
29142919
});
29152920
let amount_adjusted_with_p2tr_fee = 1_000_000
29162921
- fee_for_weight(
2917-
TEST_FEERATE_SATS_PER_KW,
2922+
(TEST_FEERATE_SATS_PER_KW as u64 * REMOTE_FEE_TOLERANCE_PERCENT / 100) as u32,
29182923
P2TR_INPUT_WEIGHT_LOWER_BOUND + TX_COMMON_FIELDS_WEIGHT + outputs_weight,
29192924
);
29202925
do_test_interactive_tx_constructor(TestSession {

0 commit comments

Comments
 (0)