Skip to content

Commit 4a02574

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 b79be98 commit 4a02574

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
@@ -10809,11 +10809,21 @@ where
1080910809
)));
1081010810
}
1081110811

10812+
debug_assert_eq!(our_funding_contribution, SignedAmount::ZERO);
10813+
1081210814
// TODO(splicing): Move this check once user-provided contributions are supported for
1081310815
// counterparty-initiated splices.
1081410816
if our_funding_contribution > SignedAmount::MAX_MONEY {
1081510817
return Err(ChannelError::WarnAndDisconnect(format!(
10816-
"Channel {} cannot be spliced; our contribution exceeds total bitcoin supply: {}",
10818+
"Channel {} cannot be spliced in; our {} contribution exceeds the total bitcoin supply",
10819+
self.context.channel_id(),
10820+
our_funding_contribution,
10821+
)));
10822+
}
10823+
10824+
if our_funding_contribution < -SignedAmount::MAX_MONEY {
10825+
return Err(ChannelError::WarnAndDisconnect(format!(
10826+
"Channel {} cannot be spliced out; our {} contribution exhausts the total bitcoin supply",
1081710827
self.context.channel_id(),
1081810828
our_funding_contribution,
1081910829
)));
@@ -10822,22 +10832,38 @@ where
1082210832
let their_funding_contribution = SignedAmount::from_sat(msg.funding_contribution_satoshis);
1082310833
if their_funding_contribution > SignedAmount::MAX_MONEY {
1082410834
return Err(ChannelError::WarnAndDisconnect(format!(
10825-
"Channel {} cannot be spliced; their contribution exceeds total bitcoin supply: {}",
10835+
"Channel {} cannot be spliced in; their {} contribution exceeds the total bitcoin supply",
1082610836
self.context.channel_id(),
1082710837
their_funding_contribution,
1082810838
)));
1082910839
}
1083010840

10831-
debug_assert_eq!(our_funding_contribution, SignedAmount::ZERO);
10832-
if their_funding_contribution < SignedAmount::ZERO {
10841+
if their_funding_contribution < -SignedAmount::MAX_MONEY {
1083310842
return Err(ChannelError::WarnAndDisconnect(format!(
10834-
"Splice-out not supported, only splice in, contribution is {} ({} + {})",
10835-
their_funding_contribution + our_funding_contribution,
10843+
"Channel {} cannot be spliced out; their {} contribution exhausts the total bitcoin supply",
10844+
self.context.channel_id(),
1083610845
their_funding_contribution,
10837-
our_funding_contribution,
1083810846
)));
1083910847
}
1084010848

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

0 commit comments

Comments
 (0)