Skip to content

Commit d1bf6a1

Browse files
f Add reserve checks for outbound channels when we're receiving, and inbound channels when we're sending
TODO: update tests TODO: I left an open question I have in the comments
1 parent 25aa688 commit d1bf6a1

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

lightning/src/ln/channel.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,7 +1765,21 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
17651765
}
17661766
}
17671767

1768-
// The + 1 is for the HTLC that is currently being added to the commitment tx.
1768+
if self.channel_outbound {
1769+
// Check that they won't violate our channel reserve by adding this HTLC.
1770+
1771+
// One of the +1's is for the HTLC that is currently being added to the commitment tx
1772+
// and the other is a fee spike buffer we're keeping for the remote (this deviates
1773+
// from the spec but should help protect us from stuck channels).
1774+
// TODO: is the reason this added HTLC's amount can't count toward's the receiver's
1775+
// fee spike buffer because that doesn't work with existing HTLC output spend scripts?
1776+
let local_fee_cost_msat = self.commit_tx_fee_msat(self.htlc_count_next_local_commit_tx() + 1 + 1);
1777+
if self.value_to_self_msat < self.their_channel_reserve_satoshis * 1000 + local_fee_cost_msat {
1778+
return Err(ChannelError::Ignore("Cannot receive value that would put us over their reserve value"));
1779+
}
1780+
}
1781+
1782+
// The +1 is for the HTLC that is currently being added to the commitment tx.
17691783
let remote_fee_cost_msat = if self.channel_outbound { 0 } else {
17701784
self.commit_tx_fee_msat(self.htlc_count_next_remote_commit_tx() + 1)
17711785
};
@@ -3606,10 +3620,22 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
36063620
return Err(ChannelError::Ignore("Cannot send value that would put us over the max HTLC value in flight our peer will accept"));
36073621
}
36083622

3609-
// Add additional reserve that avoids stuck channels in the case of fee spikes.
3623+
if !self.channel_outbound {
3624+
// Check that we won't violate their channel reserve by adding this HTLC.
3625+
3626+
let their_balance = self.channel_value_satoshis * 1000 - self.value_to_self_msat;
3627+
let chan_reserve_we_require_msat = Channel::<ChanSigner>::get_our_channel_reserve_satoshis(self.channel_value_satoshis);
3628+
// +1 for this HTLC, +2 for their fee spike buffer
3629+
// TODO: is the reason this added HTLC's amount can't count toward's the receiver's
3630+
// fee spike buffer because that doesn't work with existing HTLC output spend scripts?
3631+
let remote_commit_tx_fee_msat = self.commit_tx_fee_msat(self.htlc_count_next_remote_commit_tx() + 1 + 2);
3632+
if their_balance < chan_reserve_we_require_msat + remote_commit_tx_fee_msat {
3633+
return Err(ChannelError::Ignore("Cannot send value that would put them over our reserve value"));
3634+
}
3635+
}
36103636

36113637
// The +1 is for the HTLC currently being added to the commitment tx and
3612-
// the +2 is for the fee spike reserve.
3638+
// the +2 is for the fee spike buffer.
36133639
let local_fee_cost_msat = if self.channel_outbound {
36143640
self.commit_tx_fee_msat(self.htlc_count_next_local_commit_tx() + 1 + 2)
36153641
} else { 0 };

0 commit comments

Comments
 (0)