@@ -44,6 +44,7 @@ pub struct ChannelValueStat {
44
44
pub pending_inbound_htlcs_amount_msat : u64 ,
45
45
pub holding_cell_outbound_amount_msat : u64 ,
46
46
pub their_max_htlc_value_in_flight_msat : u64 , // outgoing
47
+ pub their_dust_limit_msat : u64 ,
47
48
}
48
49
49
50
enum InboundHTLCRemovalReason {
@@ -1663,6 +1664,73 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1663
1664
cmp:: min ( self . value_to_self_msat as i64 - self . get_outbound_pending_htlc_stats ( ) . 1 as i64 , 0 ) as u64 )
1664
1665
}
1665
1666
1667
+ // Get the fee cost of a commitment tx with a given number of HTLC outputs.
1668
+ // Note that num_htlcs should not include dust HTLCs.
1669
+ fn commit_tx_fee_msat ( & self , num_htlcs : usize ) -> u64 {
1670
+ // Note that we need to divide before multiplying to round properly,
1671
+ // since the lowest denomination of bitcoin on-chain is the satoshi.
1672
+ ( COMMITMENT_TX_BASE_WEIGHT + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC ) * self . feerate_per_kw / 1000 * 1000
1673
+ }
1674
+
1675
+ // Get the commitment tx fee for the local (i.e our) next commitment transaction
1676
+ // based on the number of pending HTLCs that are on track to be in our next
1677
+ // commitment tx. `addl_htcs` is an optional parameter allowing the caller
1678
+ // to add a number of additional HTLCs to the calculation. Note that dust
1679
+ // HTLCs are excluded.
1680
+ fn next_local_commit_tx_fee_msat ( & self , addl_htlcs : usize ) -> u64 {
1681
+ assert ! ( self . channel_outbound) ;
1682
+
1683
+ let mut their_acked_htlcs = self . pending_inbound_htlcs . len ( ) ;
1684
+ for ref htlc in self . pending_outbound_htlcs . iter ( ) {
1685
+ if htlc. amount_msat / 1000 <= self . our_dust_limit_satoshis {
1686
+ continue
1687
+ }
1688
+ match htlc. state {
1689
+ OutboundHTLCState :: Committed => their_acked_htlcs += 1 ,
1690
+ OutboundHTLCState :: RemoteRemoved { ..} => their_acked_htlcs += 1 ,
1691
+ OutboundHTLCState :: LocalAnnounced { ..} => their_acked_htlcs += 1 ,
1692
+ _ => { } ,
1693
+ }
1694
+ }
1695
+
1696
+ for htlc in self . holding_cell_htlc_updates . iter ( ) {
1697
+ match htlc {
1698
+ & HTLCUpdateAwaitingACK :: AddHTLC { .. } => their_acked_htlcs += 1 ,
1699
+ _ => { } ,
1700
+ }
1701
+ }
1702
+
1703
+ self . commit_tx_fee_msat ( their_acked_htlcs + addl_htlcs)
1704
+ }
1705
+
1706
+ // Get the commitment tx fee for the remote's next commitment transaction
1707
+ // based on the number of pending HTLCs that are on track to be in their
1708
+ // next commitment tx. `addl_htcs` is an optional parameter allowing the caller
1709
+ // to add a number of additional HTLCs to the calculation. Note that dust HTLCs
1710
+ // are excluded.
1711
+ fn next_remote_commit_tx_fee_msat ( & self , addl_htlcs : usize ) -> u64 {
1712
+ assert ! ( !self . channel_outbound) ;
1713
+
1714
+ // When calculating the set of HTLCs which will be included in their next
1715
+ // commitment_signed, all inbound HTLCs are included (as all states imply it will be
1716
+ // included) and only committed outbound HTLCs, see below.
1717
+ let mut their_acked_htlcs = self . pending_inbound_htlcs . len ( ) ;
1718
+ for ref htlc in self . pending_outbound_htlcs . iter ( ) {
1719
+ if htlc. amount_msat / 1000 <= self . their_dust_limit_satoshis {
1720
+ continue
1721
+ }
1722
+ // We only include outbound HTLCs if it will not be included in their next
1723
+ // commitment_signed, i.e. if they've responded to us with an RAA after announcement.
1724
+ match htlc. state {
1725
+ OutboundHTLCState :: Committed => their_acked_htlcs += 1 ,
1726
+ OutboundHTLCState :: RemoteRemoved { ..} => their_acked_htlcs += 1 ,
1727
+ _ => { } ,
1728
+ }
1729
+ }
1730
+
1731
+ self . commit_tx_fee_msat ( their_acked_htlcs + addl_htlcs)
1732
+ }
1733
+
1666
1734
pub fn update_add_htlc < F > ( & mut self , msg : & msgs:: UpdateAddHTLC , mut pending_forward_status : PendingHTLCStatus , create_pending_htlc_status : F ) -> Result < ( ) , ChannelError >
1667
1735
where F : for < ' a > Fn ( & ' a Self , PendingHTLCStatus , u16 ) -> PendingHTLCStatus {
1668
1736
// We can't accept HTLCs sent after we've sent a shutdown.
@@ -1716,9 +1784,54 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1716
1784
removed_outbound_total_msat += htlc. amount_msat ;
1717
1785
}
1718
1786
}
1719
- if htlc_inbound_value_msat + msg. amount_msat + self . value_to_self_msat > ( self . channel_value_satoshis - Channel :: < ChanSigner > :: get_remote_channel_reserve_satoshis ( self . channel_value_satoshis ) ) * 1000 + removed_outbound_total_msat {
1720
- return Err ( ChannelError :: Close ( "Remote HTLC add would put them under their reserve value" ) ) ;
1787
+
1788
+ let pending_value_to_self_msat =
1789
+ self . value_to_self_msat + htlc_inbound_value_msat - removed_outbound_total_msat;
1790
+ let pending_remote_value_msat =
1791
+ self . channel_value_satoshis * 1000 - pending_value_to_self_msat;
1792
+ if pending_remote_value_msat < msg. amount_msat {
1793
+ return Err ( ChannelError :: Close ( "Remote HTLC add would overdraw remaining funds" ) ) ;
1794
+ }
1795
+
1796
+ // Check that the remote can afford to pay for this HTLC on-chain at the current
1797
+ // feerate_per_kw, while maintaining their channel reserve (as required by the spec).
1798
+ let remote_commit_tx_fee_msat = if self . channel_outbound { 0 } else {
1799
+ // +1 for this HTLC.
1800
+ self . next_remote_commit_tx_fee_msat ( 1 )
1801
+ } ;
1802
+ if pending_remote_value_msat - msg. amount_msat < remote_commit_tx_fee_msat {
1803
+ return Err ( ChannelError :: Close ( "Remote HTLC add would not leave enough to pay for fees" ) ) ;
1804
+ } ;
1805
+
1806
+ let chan_reserve_msat =
1807
+ Channel :: < ChanSigner > :: get_remote_channel_reserve_satoshis ( self . channel_value_satoshis ) * 1000 ;
1808
+ if pending_remote_value_msat - msg. amount_msat - remote_commit_tx_fee_msat < chan_reserve_msat {
1809
+ return Err ( ChannelError :: Close ( "Remote HTLC add would put them under remote reserve value" ) ) ;
1810
+ }
1811
+
1812
+ if !self . channel_outbound {
1813
+ // `+1` for this HTLC, `2 *` and `+1` fee spike buffer we keep for the remote (this deviates from the spec
1814
+ // but should help protect us from stuck channels).
1815
+ // Note that when we eventually remove support for fee updates and switch to anchor output fees,
1816
+ // we will drop the `2 *`, since we no longer be as sensitive to fee spikes. But, keep the extra +1
1817
+ // as we should still be able to afford adding this HTLC plus one more future HTLC, regardless of
1818
+ // being sensitive to fee spikes.
1819
+ let remote_fee_cost_incl_stuck_buffer_msat = 2 * self . next_remote_commit_tx_fee_msat ( 1 + 1 ) ;
1820
+ if pending_remote_value_msat - msg. amount_msat - chan_reserve_msat < remote_fee_cost_incl_stuck_buffer_msat {
1821
+ // Note that if the pending_forward_status is not updated here, then it's because we're already failing
1822
+ // the HTLC, i.e. its status is already set to failing.
1823
+ pending_forward_status = create_pending_htlc_status ( self , pending_forward_status, 0x1000 |7 ) ;
1824
+ }
1825
+ } else {
1826
+ // Check that they won't violate our local required channel reserve by adding this HTLC.
1827
+
1828
+ // +1 for this HTLC.
1829
+ let local_commit_tx_fee_msat = self . next_local_commit_tx_fee_msat ( 1 ) ;
1830
+ if self . value_to_self_msat < self . local_channel_reserve_satoshis * 1000 + local_commit_tx_fee_msat {
1831
+ return Err ( ChannelError :: Close ( "Cannot receive value that would put us under local channel reserve value" ) ) ;
1832
+ }
1721
1833
}
1834
+
1722
1835
if self . next_remote_htlc_id != msg. htlc_id {
1723
1836
return Err ( ChannelError :: Close ( "Remote skipped HTLC ID" ) ) ;
1724
1837
}
@@ -3044,6 +3157,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3044
3157
res
3045
3158
} ,
3046
3159
their_max_htlc_value_in_flight_msat : self . their_max_htlc_value_in_flight_msat ,
3160
+ their_dust_limit_msat : self . their_dust_limit_satoshis * 1000 ,
3047
3161
}
3048
3162
}
3049
3163
@@ -3554,40 +3668,67 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3554
3668
return Err ( ChannelError :: Ignore ( "Cannot send value that would put us over the max HTLC value in flight our peer will accept" ) ) ;
3555
3669
}
3556
3670
3671
+ if !self . channel_outbound {
3672
+ // Check that we won't violate the remote channel reserve by adding this HTLC.
3673
+
3674
+ let remote_balance_msat = self . channel_value_satoshis * 1000 - self . value_to_self_msat ;
3675
+ let remote_chan_reserve_msat = Channel :: < ChanSigner > :: get_remote_channel_reserve_satoshis ( self . channel_value_satoshis ) ;
3676
+ // 1 additional HTLC corresponding to this HTLC.
3677
+ let remote_commit_tx_fee_msat = self . next_remote_commit_tx_fee_msat ( 1 ) ;
3678
+ if remote_balance_msat < remote_chan_reserve_msat + remote_commit_tx_fee_msat {
3679
+ return Err ( ChannelError :: Ignore ( "Cannot send value that would put them under remote channel reserve value" ) ) ;
3680
+ }
3681
+ }
3682
+
3683
+ let pending_value_to_self_msat = self . value_to_self_msat - htlc_outbound_value_msat;
3684
+ if pending_value_to_self_msat < amount_msat {
3685
+ return Err ( ChannelError :: Ignore ( "Cannot send value that would overdraw remaining funds" ) ) ;
3686
+ }
3687
+
3688
+ // The `+1` is for the HTLC currently being added to the commitment tx and
3689
+ // the `2 *` and `+1` are for the fee spike buffer.
3690
+ let local_commit_tx_fee_msat = if self . channel_outbound {
3691
+ 2 * self . next_local_commit_tx_fee_msat ( 1 + 1 )
3692
+ } else { 0 } ;
3693
+ if pending_value_to_self_msat - amount_msat < local_commit_tx_fee_msat {
3694
+ return Err ( ChannelError :: Ignore ( "Cannot send value that would not leave enough to pay for fees" ) ) ;
3695
+ }
3696
+
3557
3697
// Check self.local_channel_reserve_satoshis (the amount we must keep as
3558
3698
// reserve for the remote to have something to claim if we misbehave)
3559
- if self . value_to_self_msat < self . local_channel_reserve_satoshis * 1000 + amount_msat + htlc_outbound_value_msat {
3699
+ let chan_reserve_msat = self . local_channel_reserve_satoshis * 1000 ;
3700
+ if pending_value_to_self_msat - amount_msat - local_commit_tx_fee_msat < chan_reserve_msat {
3560
3701
return Err ( ChannelError :: Ignore ( "Cannot send value that would put us under local channel reserve value" ) ) ;
3561
3702
}
3562
3703
3563
3704
// Now update local state:
3564
3705
if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) == ( ChannelState :: AwaitingRemoteRevoke as u32 ) {
3565
3706
self . holding_cell_htlc_updates . push ( HTLCUpdateAwaitingACK :: AddHTLC {
3566
- amount_msat : amount_msat ,
3567
- payment_hash : payment_hash ,
3568
- cltv_expiry : cltv_expiry ,
3707
+ amount_msat,
3708
+ payment_hash,
3709
+ cltv_expiry,
3569
3710
source,
3570
- onion_routing_packet : onion_routing_packet ,
3711
+ onion_routing_packet,
3571
3712
} ) ;
3572
3713
return Ok ( None ) ;
3573
3714
}
3574
3715
3575
3716
self . pending_outbound_htlcs . push ( OutboundHTLCOutput {
3576
3717
htlc_id : self . next_local_htlc_id ,
3577
- amount_msat : amount_msat ,
3718
+ amount_msat,
3578
3719
payment_hash : payment_hash. clone ( ) ,
3579
- cltv_expiry : cltv_expiry ,
3720
+ cltv_expiry,
3580
3721
state : OutboundHTLCState :: LocalAnnounced ( Box :: new ( onion_routing_packet. clone ( ) ) ) ,
3581
3722
source,
3582
3723
} ) ;
3583
3724
3584
3725
let res = msgs:: UpdateAddHTLC {
3585
3726
channel_id : self . channel_id ,
3586
3727
htlc_id : self . next_local_htlc_id ,
3587
- amount_msat : amount_msat ,
3588
- payment_hash : payment_hash ,
3589
- cltv_expiry : cltv_expiry ,
3590
- onion_routing_packet : onion_routing_packet ,
3728
+ amount_msat,
3729
+ payment_hash,
3730
+ cltv_expiry,
3731
+ onion_routing_packet,
3591
3732
} ;
3592
3733
self . next_local_htlc_id += 1 ;
3593
3734
0 commit comments