Skip to content

Commit f12c993

Browse files
committed
Support accepting splice-out
When a counterparty sends splice_init with a negative contribution, they are requesting to remove funds from a channel. Remove conditions guarding against this and check that they have enough channel balance to cover the removed funds.
1 parent e3c1a6b commit f12c993

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

lightning/src/ln/channel.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10824,9 +10824,19 @@ where
1082410824
)));
1082510825
}
1082610826

10827+
debug_assert_eq!(our_funding_contribution, SignedAmount::ZERO);
10828+
1082710829
if our_funding_contribution > SignedAmount::MAX_MONEY {
1082810830
return Err(ChannelError::WarnAndDisconnect(format!(
10829-
"Channel {} cannot be spliced; our contribution exceeds total bitcoin supply: {}",
10831+
"Channel {} cannot be spliced in; our {} contribution exceeds the total bitcoin supply",
10832+
self.context.channel_id(),
10833+
our_funding_contribution,
10834+
)));
10835+
}
10836+
10837+
if our_funding_contribution < -SignedAmount::MAX_MONEY {
10838+
return Err(ChannelError::WarnAndDisconnect(format!(
10839+
"Channel {} cannot be spliced out; our {} contribution exhausts the total bitcoin supply",
1083010840
self.context.channel_id(),
1083110841
our_funding_contribution,
1083210842
)));
@@ -10835,22 +10845,38 @@ where
1083510845
let their_funding_contribution = SignedAmount::from_sat(msg.funding_contribution_satoshis);
1083610846
if their_funding_contribution > SignedAmount::MAX_MONEY {
1083710847
return Err(ChannelError::WarnAndDisconnect(format!(
10838-
"Channel {} cannot be spliced; their contribution exceeds total bitcoin supply: {}",
10848+
"Channel {} cannot be spliced in; their {} contribution exceeds the total bitcoin supply",
1083910849
self.context.channel_id(),
1084010850
their_funding_contribution,
1084110851
)));
1084210852
}
1084310853

10844-
debug_assert_eq!(our_funding_contribution, SignedAmount::ZERO);
10845-
if their_funding_contribution < SignedAmount::ZERO {
10854+
if their_funding_contribution < -SignedAmount::MAX_MONEY {
1084610855
return Err(ChannelError::WarnAndDisconnect(format!(
10847-
"Splice-out not supported, only splice in, contribution is {} ({} + {})",
10848-
their_funding_contribution + our_funding_contribution,
10856+
"Channel {} cannot be spliced out; their {} contribution exhausts the total bitcoin supply",
10857+
self.context.channel_id(),
1084910858
their_funding_contribution,
10850-
our_funding_contribution,
1085110859
)));
1085210860
}
1085310861

10862+
let their_channel_balance = Amount::from_sat(self.funding.get_value_satoshis())
10863+
- Amount::from_sat(self.funding.get_value_to_self_msat() / 1000);
10864+
let post_channel_balance = AddSigned::checked_add_signed(
10865+
their_channel_balance.to_sat(),
10866+
their_funding_contribution.to_sat(),
10867+
);
10868+
10869+
if post_channel_balance.is_none() {
10870+
return Err(ChannelError::WarnAndDisconnect(format!(
10871+
"Channel {} cannot be spliced out; their {} contribution exhausts their channel balance: {}",
10872+
self.context.channel_id(),
10873+
their_funding_contribution,
10874+
their_channel_balance,
10875+
)));
10876+
}
10877+
10878+
// TODO(splicing): Check that channel balance does not go below the channel reserve
10879+
1085410880
let splice_funding = FundingScope::for_splice(
1085510881
&self.funding,
1085610882
&self.context,

0 commit comments

Comments
 (0)