Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 41 additions & 24 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6484,6 +6484,19 @@ fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satos
cmp::min(channel_value_satoshis, cmp::max(q, dust_limit_satoshis))
}

/// Returns the minimum feerate for our own RBF attempts given a previous feerate.
///
/// The spec (tx_init_rbf) requires the new feerate to be >= 25/24 of the previous feerate.
/// However, at low feerates that multiplier doesn't always satisfy BIP125's relay requirement of
/// an absolute fee increase, so we take the max of a flat +25 sat/kwu (0.1 sat/vB) increment
/// and the spec's multiplicative rule. We still accept the bare 25/24 rule from counterparties
/// in [`FundedChannel::validate_tx_init_rbf`].
fn min_rbf_feerate(prev_feerate: u32) -> FeeRate {
let flat_increment = (prev_feerate as u64).saturating_add(25);
let spec_increment = ((prev_feerate as u64) * 25).div_ceil(24);
FeeRate::from_sat_per_kwu(cmp::max(flat_increment, spec_increment))
}

/// Context for negotiating channels (dual-funded V2 open, splicing)
#[derive(Debug)]
pub(super) struct FundingNegotiationContext {
Expand Down Expand Up @@ -12019,10 +12032,7 @@ where
prev_feerate.is_some(),
"pending_splice should have last_funding_feerate or funding_negotiation",
);
let min_rbf_feerate = prev_feerate.map(|f| {
let min_feerate_kwu = ((f as u64) * 25).div_ceil(24);
FeeRate::from_sat_per_kwu(min_feerate_kwu)
});
let min_rbf_feerate = prev_feerate.map(min_rbf_feerate);
let prior = if pending_splice.last_funding_feerate_sat_per_1000_weight.is_some() {
self.build_prior_contribution()
} else {
Expand Down Expand Up @@ -12114,10 +12124,7 @@ where
}

match pending_splice.last_funding_feerate_sat_per_1000_weight {
Some(prev_feerate) => {
let min_feerate_kwu = ((prev_feerate as u64) * 25).div_ceil(24);
Ok(FeeRate::from_sat_per_kwu(min_feerate_kwu))
},
Some(prev_feerate) => Ok(min_rbf_feerate(prev_feerate)),
None => Err(format!(
"Channel {} has no prior feerate to compute RBF minimum",
self.context.channel_id(),
Expand Down Expand Up @@ -12588,13 +12595,15 @@ where
pub(crate) fn splice_init<ES: EntropySource, L: Logger>(
&mut self, msg: &msgs::SpliceInit, entropy_source: &ES, holder_node_id: &PublicKey,
logger: &L,
) -> Result<msgs::SpliceAck, ChannelError> {
) -> Result<msgs::SpliceAck, InteractiveTxMsgError> {
let feerate = FeeRate::from_sat_per_kwu(msg.funding_feerate_per_kw as u64);
let (our_funding_contribution, holder_balance) =
self.resolve_queued_contribution(feerate, logger)?;
let (our_funding_contribution, holder_balance) = self
.resolve_queued_contribution(feerate, logger)
.map_err(|e| self.quiescent_negotiation_err(e))?;

let splice_funding =
self.validate_splice_init(msg, our_funding_contribution.unwrap_or(SignedAmount::ZERO))?;
let splice_funding = self
.validate_splice_init(msg, our_funding_contribution.unwrap_or(SignedAmount::ZERO))
.map_err(|e| self.quiescent_negotiation_err(e))?;

// Adjust for the feerate and clone so we can store it for future RBF re-use.
let (adjusted_contribution, our_funding_inputs, our_funding_outputs) =
Expand Down Expand Up @@ -12746,10 +12755,11 @@ where
pub(crate) fn tx_init_rbf<ES: EntropySource, F: FeeEstimator, L: Logger>(
&mut self, msg: &msgs::TxInitRbf, entropy_source: &ES, holder_node_id: &PublicKey,
fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
) -> Result<msgs::TxAckRbf, ChannelError> {
) -> Result<msgs::TxAckRbf, InteractiveTxMsgError> {
let feerate = FeeRate::from_sat_per_kwu(msg.feerate_sat_per_1000_weight as u64);
let (queued_net_value, holder_balance) =
self.resolve_queued_contribution(feerate, logger)?;
let (queued_net_value, holder_balance) = self
.resolve_queued_contribution(feerate, logger)
.map_err(|e| self.quiescent_negotiation_err(e))?;

// If no queued contribution, try prior contribution from previous negotiation.
// Failing here means the RBF would erase our splice — reject it.
Expand All @@ -12766,19 +12776,22 @@ where
prior
.net_value_for_acceptor_at_feerate(feerate, holder_balance)
.map_err(|_| ChannelError::Abort(AbortReason::InsufficientRbfFeerate))
})?;
})
.map_err(|e| self.quiescent_negotiation_err(e))?;
Some(net_value)
} else {
None
};

let our_funding_contribution = queued_net_value.or(prior_net_value);

let rbf_funding = self.validate_tx_init_rbf(
msg,
our_funding_contribution.unwrap_or(SignedAmount::ZERO),
fee_estimator,
)?;
let rbf_funding = self
.validate_tx_init_rbf(
msg,
our_funding_contribution.unwrap_or(SignedAmount::ZERO),
fee_estimator,
)
.map_err(|e| self.quiescent_negotiation_err(e))?;

// Consume the appropriate contribution source.
let (our_funding_inputs, our_funding_outputs) = if queued_net_value.is_some() {
Expand Down Expand Up @@ -13942,8 +13955,6 @@ where
Some(msgs::Stfu { channel_id: self.context.channel_id, initiator })
}

#[cfg(any(test, fuzzing, feature = "_test_utils"))]
#[rustfmt::skip]
pub fn exit_quiescence(&mut self) -> bool {
// Make sure we either finished the quiescence handshake and are quiescent, or we never
// attempted to initiate quiescence at all.
Expand All @@ -13956,6 +13967,12 @@ where
was_quiescent
}

fn quiescent_negotiation_err(&mut self, err: ChannelError) -> InteractiveTxMsgError {
let exited_quiescence =
if matches!(err, ChannelError::Abort(_)) { self.exit_quiescence() } else { false };
InteractiveTxMsgError { err, splice_funding_failed: None, exited_quiescence }
}

pub fn remove_legacy_scids_before_block(&mut self, height: u32) -> alloc::vec::Drain<'_, u64> {
let end = self
.funding
Expand Down
129 changes: 81 additions & 48 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11673,6 +11673,39 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
}
}

fn handle_interactive_tx_msg_err(
&self, err: InteractiveTxMsgError, channel_id: ChannelId, counterparty_node_id: &PublicKey,
user_channel_id: u128,
) -> MsgHandleErrInternal {
if let Some(splice_funding_failed) = err.splice_funding_failed {
let pending_events = &mut self.pending_events.lock().unwrap();
pending_events.push_back((
events::Event::SpliceFailed {
channel_id,
counterparty_node_id: *counterparty_node_id,
user_channel_id,
abandoned_funding_txo: splice_funding_failed.funding_txo,
channel_type: splice_funding_failed.channel_type.clone(),
},
None,
));
pending_events.push_back((
events::Event::DiscardFunding {
channel_id,
funding_info: FundingInfo::Contribution {
inputs: splice_funding_failed.contributed_inputs,
outputs: splice_funding_failed.contributed_outputs,
},
},
None,
));
}
debug_assert!(!err.exited_quiescence || matches!(err.err, ChannelError::Abort(_)));

MsgHandleErrInternal::from_chan_no_close(err.err, channel_id)
.with_exited_quiescence(err.exited_quiescence)
}

fn internal_tx_msg<
HandleTxMsgFn: Fn(&mut Channel<SP>) -> Result<InteractiveTxMessageSend, InteractiveTxMsgError>,
>(
Expand All @@ -11694,38 +11727,14 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
peer_state.pending_msg_events.push(msg_send_event);
Ok(())
},
Err(InteractiveTxMsgError {
err,
splice_funding_failed,
exited_quiescence,
}) => {
if let Some(splice_funding_failed) = splice_funding_failed {
let pending_events = &mut self.pending_events.lock().unwrap();
pending_events.push_back((
events::Event::SpliceFailed {
channel_id,
counterparty_node_id: *counterparty_node_id,
user_channel_id: channel.context().get_user_id(),
abandoned_funding_txo: splice_funding_failed.funding_txo,
channel_type: splice_funding_failed.channel_type.clone(),
},
None,
));
pending_events.push_back((
events::Event::DiscardFunding {
channel_id,
funding_info: FundingInfo::Contribution {
inputs: splice_funding_failed.contributed_inputs,
outputs: splice_funding_failed.contributed_outputs,
},
},
None,
));
}
debug_assert!(!exited_quiescence || matches!(err, ChannelError::Abort(_)));

Err(MsgHandleErrInternal::from_chan_no_close(err, channel_id)
.with_exited_quiescence(exited_quiescence))
Err(err) => {
let user_channel_id = channel.context().get_user_id();
Err(self.handle_interactive_tx_msg_err(
err,
channel_id,
counterparty_node_id,
user_channel_id,
))
},
}
},
Expand Down Expand Up @@ -13100,18 +13109,30 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
}

if let Some(ref mut funded_channel) = chan_entry.get_mut().as_funded_mut() {
let init_res = funded_channel.splice_init(
let user_channel_id = funded_channel.context.get_user_id();
match funded_channel.splice_init(
msg,
&self.entropy_source,
&self.get_our_node_id(),
&self.logger,
);
let splice_ack_msg = try_channel_entry!(self, peer_state, init_res, chan_entry);
peer_state.pending_msg_events.push(MessageSendEvent::SendSpliceAck {
node_id: *counterparty_node_id,
msg: splice_ack_msg,
});
Ok(())
) {
Ok(splice_ack_msg) => {
peer_state.pending_msg_events.push(MessageSendEvent::SendSpliceAck {
node_id: *counterparty_node_id,
msg: splice_ack_msg,
});
Ok(())
},
Err(err) => {
debug_assert!(err.splice_funding_failed.is_none());
Err(self.handle_interactive_tx_msg_err(
err,
msg.channel_id,
counterparty_node_id,
user_channel_id,
))
},
}
} else {
try_channel_entry!(
self,
Expand Down Expand Up @@ -13144,19 +13165,31 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
},
hash_map::Entry::Occupied(mut chan_entry) => {
if let Some(ref mut funded_channel) = chan_entry.get_mut().as_funded_mut() {
let init_res = funded_channel.tx_init_rbf(
let user_channel_id = funded_channel.context.get_user_id();
match funded_channel.tx_init_rbf(
msg,
&self.entropy_source,
&self.get_our_node_id(),
&self.fee_estimator,
&self.logger,
);
let tx_ack_rbf_msg = try_channel_entry!(self, peer_state, init_res, chan_entry);
peer_state.pending_msg_events.push(MessageSendEvent::SendTxAckRbf {
node_id: *counterparty_node_id,
msg: tx_ack_rbf_msg,
});
Ok(())
) {
Ok(tx_ack_rbf_msg) => {
peer_state.pending_msg_events.push(MessageSendEvent::SendTxAckRbf {
node_id: *counterparty_node_id,
msg: tx_ack_rbf_msg,
});
Ok(())
},
Err(err) => {
debug_assert!(err.splice_funding_failed.is_none());
Err(self.handle_interactive_tx_msg_err(
err,
msg.channel_id,
counterparty_node_id,
user_channel_id,
))
},
}
} else {
try_channel_entry!(
self,
Expand Down
26 changes: 14 additions & 12 deletions lightning/src/ln/funding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,9 @@ impl PriorContribution {
/// prior contribution logic internally — reusing an adjusted prior when possible, re-running
/// coin selection when needed, or creating a fee-bump-only contribution.
///
/// Check [`FundingTemplate::min_rbf_feerate`] for the minimum feerate required (25/24 of
/// the previous feerate). Use [`FundingTemplate::prior_contribution`] to inspect the prior
/// Check [`FundingTemplate::min_rbf_feerate`] for the minimum feerate required (the greater of
/// the previous feerate + 25 sat/kwu and the spec's 25/24 rule). Use
/// [`FundingTemplate::prior_contribution`] to inspect the prior
/// contribution's parameters (e.g., [`FundingContribution::value_added`],
/// [`FundingContribution::outputs`]) before deciding whether to reuse it via the RBF methods
/// or build a fresh contribution with different parameters using the splice methods above.
Expand All @@ -232,8 +233,9 @@ pub struct FundingTemplate {
/// transaction.
shared_input: Option<Input>,

/// The minimum RBF feerate (25/24 of the previous feerate), if this template is for an
/// RBF attempt. `None` for fresh splices with no pending splice candidates.
/// The minimum RBF feerate (the greater of previous feerate + 25 sat/kwu and the spec's
/// 25/24 rule), if this template is for an RBF attempt. `None` for fresh splices with no
/// pending splice candidates.
min_rbf_feerate: Option<FeeRate>,

/// The user's prior contribution from a previous splice negotiation, if available.
Expand Down Expand Up @@ -2262,8 +2264,8 @@ mod tests {
// When the caller's max_feerate is below the minimum RBF feerate, rbf_sync should
// return Err(()).
let prior_feerate = FeeRate::from_sat_per_kwu(2000);
let min_rbf_feerate = FeeRate::from_sat_per_kwu(5000);
let max_feerate = FeeRate::from_sat_per_kwu(3000);
let min_rbf_feerate = FeeRate::from_sat_per_kwu(2025);
let max_feerate = FeeRate::from_sat_per_kwu(2020);

let prior = FundingContribution {
value_added: Amount::from_sat(50_000),
Expand All @@ -2276,7 +2278,7 @@ mod tests {
is_splice: true,
};

// max_feerate (3000) < min_rbf_feerate (5000).
// max_feerate (2020) < min_rbf_feerate (2025).
let template = FundingTemplate::new(
None,
Some(min_rbf_feerate),
Expand Down Expand Up @@ -2359,8 +2361,8 @@ mod tests {
// When the prior contribution's feerate is below the minimum RBF feerate and no
// holder balance is available, rbf_sync should run coin selection to add inputs that
// cover the higher RBF fee.
let min_rbf_feerate = FeeRate::from_sat_per_kwu(5000);
let prior_feerate = FeeRate::from_sat_per_kwu(2000);
let min_rbf_feerate = FeeRate::from_sat_per_kwu(2025);
let withdrawal = funding_output_sats(20_000);

let prior = FundingContribution {
Expand Down Expand Up @@ -2397,7 +2399,7 @@ mod tests {
fn test_rbf_sync_no_prior_fee_bump_only_runs_coin_selection() {
// When there is no prior contribution (e.g., acceptor), rbf_sync should run coin
// selection to add inputs for a fee-bump-only contribution.
let min_rbf_feerate = FeeRate::from_sat_per_kwu(5000);
let min_rbf_feerate = FeeRate::from_sat_per_kwu(2025);

let template =
FundingTemplate::new(Some(shared_input(100_000)), Some(min_rbf_feerate), None);
Expand All @@ -2419,7 +2421,7 @@ mod tests {
// When the prior contribution's feerate is below the minimum RBF feerate and no
// holder balance is available, rbf_sync should use the caller's max_feerate (not the
// prior's) for the resulting contribution.
let min_rbf_feerate = FeeRate::from_sat_per_kwu(5000);
let min_rbf_feerate = FeeRate::from_sat_per_kwu(2025);
let prior_max_feerate = FeeRate::from_sat_per_kwu(50_000);
let callers_max_feerate = FeeRate::from_sat_per_kwu(10_000);
let withdrawal = funding_output_sats(20_000);
Expand Down Expand Up @@ -2458,8 +2460,8 @@ mod tests {
// When splice_out_sync is called on a template with min_rbf_feerate set (user
// choosing a fresh splice-out instead of rbf_sync), coin selection should NOT run.
// Fees come from the channel balance.
let min_rbf_feerate = FeeRate::from_sat_per_kwu(5000);
let feerate = FeeRate::from_sat_per_kwu(5000);
let min_rbf_feerate = FeeRate::from_sat_per_kwu(2025);
let feerate = FeeRate::from_sat_per_kwu(2025);
let withdrawal = funding_output_sats(20_000);

let template =
Expand Down
Loading
Loading