@@ -1766,7 +1766,21 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1766
1766
}
1767
1767
}
1768
1768
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.
1770
1784
let remote_fee_cost_msat = if self . channel_outbound { 0 } else {
1771
1785
self . commit_tx_fee_msat ( self . htlc_count_next_remote_commit_tx ( ) + 1 )
1772
1786
} ;
@@ -3589,10 +3603,22 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3589
3603
return Err ( ChannelError :: Ignore ( "Cannot send value that would put us over the max HTLC value in flight our peer will accept" ) ) ;
3590
3604
}
3591
3605
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
+ }
3593
3619
3594
3620
// 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 .
3596
3622
let local_fee_cost_msat = if self . channel_outbound {
3597
3623
self . commit_tx_fee_msat ( self . htlc_count_next_local_commit_tx ( ) + 1 + 2 )
3598
3624
} else { 0 } ;
0 commit comments