Skip to content

Commit a39a294

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 c8ebee5 commit a39a294

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
@@ -10811,11 +10811,21 @@ where
1081110811
)));
1081210812
}
1081310813

10814+
debug_assert_eq!(our_funding_contribution, SignedAmount::ZERO);
10815+
1081410816
// TODO(splicing): Move this check once user-provided contributions are supported for
1081510817
// counterparty-initiated splices.
1081610818
if our_funding_contribution > SignedAmount::MAX_MONEY {
1081710819
return Err(ChannelError::WarnAndDisconnect(format!(
10818-
"Channel {} cannot be spliced; our contribution exceeds total bitcoin supply: {}",
10820+
"Channel {} cannot be spliced in; our {} contribution exceeds the total bitcoin supply",
10821+
self.context.channel_id(),
10822+
our_funding_contribution,
10823+
)));
10824+
}
10825+
10826+
if our_funding_contribution < -SignedAmount::MAX_MONEY {
10827+
return Err(ChannelError::WarnAndDisconnect(format!(
10828+
"Channel {} cannot be spliced out; our {} contribution exhausts the total bitcoin supply",
1081910829
self.context.channel_id(),
1082010830
our_funding_contribution,
1082110831
)));
@@ -10824,22 +10834,38 @@ where
1082410834
let their_funding_contribution = SignedAmount::from_sat(msg.funding_contribution_satoshis);
1082510835
if their_funding_contribution > SignedAmount::MAX_MONEY {
1082610836
return Err(ChannelError::WarnAndDisconnect(format!(
10827-
"Channel {} cannot be spliced; their contribution exceeds total bitcoin supply: {}",
10837+
"Channel {} cannot be spliced in; their {} contribution exceeds the total bitcoin supply",
1082810838
self.context.channel_id(),
1082910839
their_funding_contribution,
1083010840
)));
1083110841
}
1083210842

10833-
debug_assert_eq!(our_funding_contribution, SignedAmount::ZERO);
10834-
if their_funding_contribution < SignedAmount::ZERO {
10843+
if their_funding_contribution < -SignedAmount::MAX_MONEY {
1083510844
return Err(ChannelError::WarnAndDisconnect(format!(
10836-
"Splice-out not supported, only splice in, contribution is {} ({} + {})",
10837-
their_funding_contribution + our_funding_contribution,
10845+
"Channel {} cannot be spliced out; their {} contribution exhausts the total bitcoin supply",
10846+
self.context.channel_id(),
1083810847
their_funding_contribution,
10839-
our_funding_contribution,
1084010848
)));
1084110849
}
1084210850

10851+
let their_channel_balance = Amount::from_sat(self.funding.get_value_satoshis())
10852+
- Amount::from_sat(self.funding.get_value_to_self_msat() / 1000);
10853+
let post_channel_balance = AddSigned::checked_add_signed(
10854+
their_channel_balance.to_sat(),
10855+
their_funding_contribution.to_sat(),
10856+
);
10857+
10858+
if post_channel_balance.is_none() {
10859+
return Err(ChannelError::WarnAndDisconnect(format!(
10860+
"Channel {} cannot be spliced out; their {} contribution exhausts their channel balance: {}",
10861+
self.context.channel_id(),
10862+
their_funding_contribution,
10863+
their_channel_balance,
10864+
)));
10865+
}
10866+
10867+
// TODO(splicing): Check that channel balance does not go below the channel reserve
10868+
1084310869
let splice_funding = FundingScope::for_splice(
1084410870
&self.funding,
1084510871
&self.context,

0 commit comments

Comments
 (0)