Skip to content

Commit ba6ffbf

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 cdb822a commit ba6ffbf

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
@@ -1766,7 +1766,21 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
17661766
}
17671767
}
17681768

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

3592-
// Add additional reserve that avoids stuck channels in the case of fee spikes.
3606+
if !self.channel_outbound {
3607+
// Check that we won't violate their channel reserve by adding this HTLC.
3608+
3609+
let their_balance = self.channel_value_satoshis * 1000 - self.value_to_self_msat;
3610+
let chan_reserve_we_require_msat = Channel::<ChanSigner>::get_our_channel_reserve_satoshis(self.channel_value_satoshis);
3611+
// +1 for this HTLC, +2 for their fee spike buffer
3612+
// TODO: is the reason this added HTLC's amount can't count toward's the receiver's
3613+
// fee spike buffer because that doesn't work with existing HTLC output spend scripts?
3614+
let remote_commit_tx_fee_msat = self.commit_tx_fee_msat(self.htlc_count_next_remote_commit_tx() + 1 + 2);
3615+
if their_balance < chan_reserve_we_require_msat + remote_commit_tx_fee_msat {
3616+
return Err(ChannelError::Ignore("Cannot send value that would put them over our reserve value"));
3617+
}
3618+
}
35933619

35943620
// The +1 is for the HTLC currently being added to the commitment tx and
3595-
// the +2 is for the fee spike reserve.
3621+
// the +2 is for the fee spike buffer.
35963622
let local_fee_cost_msat = if self.channel_outbound {
35973623
self.commit_tx_fee_msat(self.htlc_count_next_local_commit_tx() + 1 + 2)
35983624
} else { 0 };

0 commit comments

Comments
 (0)