Skip to content

Commit fde2f61

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 56ed8ba commit fde2f61

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
@@ -10832,9 +10832,19 @@ where
1083210832
)));
1083310833
}
1083410834

10835+
debug_assert_eq!(our_funding_contribution, SignedAmount::ZERO);
10836+
1083510837
if our_funding_contribution > SignedAmount::MAX_MONEY {
1083610838
return Err(ChannelError::WarnAndDisconnect(format!(
10837-
"Channel {} cannot be spliced; our contribution exceeds total bitcoin supply: {}",
10839+
"Channel {} cannot be spliced in; our {} contribution exceeds the total bitcoin supply",
10840+
self.context.channel_id(),
10841+
our_funding_contribution,
10842+
)));
10843+
}
10844+
10845+
if our_funding_contribution < -SignedAmount::MAX_MONEY {
10846+
return Err(ChannelError::WarnAndDisconnect(format!(
10847+
"Channel {} cannot be spliced out; our {} contribution exhausts the total bitcoin supply",
1083810848
self.context.channel_id(),
1083910849
our_funding_contribution,
1084010850
)));
@@ -10843,22 +10853,38 @@ where
1084310853
let their_funding_contribution = SignedAmount::from_sat(msg.funding_contribution_satoshis);
1084410854
if their_funding_contribution > SignedAmount::MAX_MONEY {
1084510855
return Err(ChannelError::WarnAndDisconnect(format!(
10846-
"Channel {} cannot be spliced; their contribution exceeds total bitcoin supply: {}",
10856+
"Channel {} cannot be spliced in; their {} contribution exceeds the total bitcoin supply",
1084710857
self.context.channel_id(),
1084810858
their_funding_contribution,
1084910859
)));
1085010860
}
1085110861

10852-
debug_assert_eq!(our_funding_contribution, SignedAmount::ZERO);
10853-
if their_funding_contribution < SignedAmount::ZERO {
10862+
if their_funding_contribution < -SignedAmount::MAX_MONEY {
1085410863
return Err(ChannelError::WarnAndDisconnect(format!(
10855-
"Splice-out not supported, only splice in, contribution is {} ({} + {})",
10856-
their_funding_contribution + our_funding_contribution,
10864+
"Channel {} cannot be spliced out; their {} contribution exhausts the total bitcoin supply",
10865+
self.context.channel_id(),
1085710866
their_funding_contribution,
10858-
our_funding_contribution,
1085910867
)));
1086010868
}
1086110869

10870+
let their_channel_balance = Amount::from_sat(self.funding.get_value_satoshis())
10871+
- Amount::from_sat(self.funding.get_value_to_self_msat() / 1000);
10872+
let post_channel_balance = AddSigned::checked_add_signed(
10873+
their_channel_balance.to_sat(),
10874+
their_funding_contribution.to_sat(),
10875+
);
10876+
10877+
if post_channel_balance.is_none() {
10878+
return Err(ChannelError::WarnAndDisconnect(format!(
10879+
"Channel {} cannot be spliced out; their {} contribution exhausts their channel balance: {}",
10880+
self.context.channel_id(),
10881+
their_funding_contribution,
10882+
their_channel_balance,
10883+
)));
10884+
}
10885+
10886+
// TODO(splicing): Check that channel balance does not go below the channel reserve
10887+
1086210888
let splice_funding = FundingScope::for_splice(
1086310889
&self.funding,
1086410890
&self.context,

0 commit comments

Comments
 (0)