@@ -1765,7 +1765,21 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1765
1765
}
1766
1766
}
1767
1767
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.
1769
1783
let remote_fee_cost_msat = if self . channel_outbound { 0 } else {
1770
1784
self . commit_tx_fee_msat ( self . htlc_count_next_remote_commit_tx ( ) + 1 )
1771
1785
} ;
@@ -3606,10 +3620,22 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3606
3620
return Err ( ChannelError :: Ignore ( "Cannot send value that would put us over the max HTLC value in flight our peer will accept" ) ) ;
3607
3621
}
3608
3622
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
+ }
3610
3636
3611
3637
// 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 .
3613
3639
let local_fee_cost_msat = if self . channel_outbound {
3614
3640
self . commit_tx_fee_msat ( self . htlc_count_next_local_commit_tx ( ) + 1 + 2 )
3615
3641
} else { 0 } ;
0 commit comments