@@ -1577,7 +1577,7 @@ fn do_channel_reserve_test(test_recv: bool) {
1577
1577
// nodes[0]'s wealth
1578
1578
loop {
1579
1579
let amt_msat = recv_value_0 + total_fee_msat;
1580
- if stat01. value_to_self_msat - amt_msat < stat01. channel_reserve_msat {
1580
+ if stat01. value_to_self_msat - amt_msat < stat01. channel_reserve_msat + stat01 . commit_tx_fee_outbound {
1581
1581
break ;
1582
1582
}
1583
1583
send_payment ( & nodes[ 0 ] , & vec ! [ & nodes[ 1 ] , & nodes[ 2 ] ] [ ..] , recv_value_0, recv_value_0) ;
@@ -1598,7 +1598,7 @@ fn do_channel_reserve_test(test_recv: bool) {
1598
1598
}
1599
1599
1600
1600
{
1601
- let recv_value = stat01. value_to_self_msat - stat01. channel_reserve_msat - total_fee_msat;
1601
+ let recv_value = stat01. value_to_self_msat - stat01. channel_reserve_msat - total_fee_msat - stat01 . commit_tx_fee_outbound ;
1602
1602
// attempt to get channel_reserve violation
1603
1603
let ( route, our_payment_hash, _) = get_route_and_payment_hash ! ( recv_value + 1 ) ;
1604
1604
let err = nodes[ 0 ] . node . send_payment ( route. clone ( ) , our_payment_hash) . err ( ) . unwrap ( ) ;
@@ -1611,7 +1611,8 @@ fn do_channel_reserve_test(test_recv: bool) {
1611
1611
}
1612
1612
1613
1613
// adding pending output
1614
- let recv_value_1 = ( stat01. value_to_self_msat - stat01. channel_reserve_msat - total_fee_msat) /2 ;
1614
+ let commit_tx_fee_1_acked_htlc = get_feerate ! ( nodes[ 0 ] , chan_1. 2 ) * COMMITMENT_TX_WEIGHT_PER_HTLC ;
1615
+ let recv_value_1 = ( stat01. value_to_self_msat - stat01. channel_reserve_msat - total_fee_msat - stat01. commit_tx_fee_outbound - commit_tx_fee_1_acked_htlc) /2 ;
1615
1616
let amt_msat_1 = recv_value_1 + total_fee_msat;
1616
1617
1617
1618
let ( route_1, our_payment_hash_1, our_payment_preimage_1) = get_route_and_payment_hash ! ( recv_value_1) ;
@@ -1626,7 +1627,7 @@ fn do_channel_reserve_test(test_recv: bool) {
1626
1627
nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & payment_event_1. msgs [ 0 ] ) ;
1627
1628
1628
1629
// channel reserve test with htlc pending output > 0
1629
- let recv_value_2 = stat01. value_to_self_msat - amt_msat_1 - stat01. channel_reserve_msat - total_fee_msat;
1630
+ let recv_value_2 = stat01. value_to_self_msat - amt_msat_1 - stat01. channel_reserve_msat - total_fee_msat - stat01 . commit_tx_fee_outbound - commit_tx_fee_1_acked_htlc ;
1630
1631
{
1631
1632
let ( route, our_payment_hash, _) = get_route_and_payment_hash ! ( recv_value_2 + 1 ) ;
1632
1633
match nodes[ 0 ] . node . send_payment ( route, our_payment_hash) . err ( ) . unwrap ( ) {
@@ -1638,8 +1639,10 @@ fn do_channel_reserve_test(test_recv: bool) {
1638
1639
}
1639
1640
1640
1641
{
1641
- // test channel_reserve test on nodes[1] side
1642
- let ( route, our_payment_hash, _) = get_route_and_payment_hash ! ( recv_value_2 + 1 ) ;
1642
+ // test channel_reserve test on nodes[1] side. The recv_value_1 HTLC hasn't been promoted to Committed yet,
1643
+ // so it won't be included in the channel reserve check. Therefore, to trigger a channel close we'll have
1644
+ // to add the extra HTLC commit tx fee back in.
1645
+ let ( route, our_payment_hash, _) = get_route_and_payment_hash ! ( recv_value_2 + commit_tx_fee_1_acked_htlc + 1 ) ;
1643
1646
1644
1647
// Need to manually create update_add_htlc message to go around the channel reserve check in send_htlc()
1645
1648
let secp_ctx = Secp256k1 :: new ( ) ;
@@ -1656,7 +1659,7 @@ fn do_channel_reserve_test(test_recv: bool) {
1656
1659
let onion_packet = onion_utils:: construct_onion_packet ( onion_payloads, onion_keys, [ 0 ; 32 ] , & our_payment_hash) ;
1657
1660
let msg = msgs:: UpdateAddHTLC {
1658
1661
channel_id : chan_1. 2 ,
1659
- htlc_id,
1662
+ htlc_id : htlc_id + 1 ,
1660
1663
amount_msat : htlc_msat,
1661
1664
payment_hash : our_payment_hash,
1662
1665
cltv_expiry : htlc_cltv,
@@ -1676,11 +1679,12 @@ fn do_channel_reserve_test(test_recv: bool) {
1676
1679
}
1677
1680
1678
1681
// split the rest to test holding cell
1679
- let recv_value_21 = recv_value_2/2 ;
1680
- let recv_value_22 = recv_value_2 - recv_value_21 - total_fee_msat;
1682
+ let commit_tx_fee_1_holding_cell_htlc = get_feerate ! ( nodes[ 0 ] , chan_1. 2 ) * COMMITMENT_TX_WEIGHT_PER_HTLC ;
1683
+ let recv_value_21 = recv_value_2/2 - commit_tx_fee_1_holding_cell_htlc/2 ;
1684
+ let recv_value_22 = recv_value_2 - recv_value_21 - total_fee_msat - commit_tx_fee_1_holding_cell_htlc;
1681
1685
{
1682
1686
let stat = get_channel_value_stat ! ( nodes[ 0 ] , chan_1. 2 ) ;
1683
- assert_eq ! ( stat. value_to_self_msat - ( stat. pending_outbound_htlcs_amount_msat + recv_value_21 + recv_value_22 + total_fee_msat + total_fee_msat) , stat. channel_reserve_msat) ;
1687
+ assert_eq ! ( stat. value_to_self_msat - ( stat. pending_outbound_htlcs_amount_msat + recv_value_21 + recv_value_22 + total_fee_msat + total_fee_msat + commit_tx_fee_1_holding_cell_htlc + commit_tx_fee_1_acked_htlc + stat01 . commit_tx_fee_outbound ) , stat. channel_reserve_msat) ;
1684
1688
}
1685
1689
1686
1690
// now see if they go through on both sides
@@ -1714,6 +1718,7 @@ fn do_channel_reserve_test(test_recv: bool) {
1714
1718
let ( as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
1715
1719
check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1716
1720
1721
+ // the pending htlc should be promoted to committed
1717
1722
nodes[ 0 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & as_revoke_and_ack) ;
1718
1723
check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1719
1724
let commitment_update_2 = get_htlc_update_msgs ! ( nodes[ 0 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
@@ -1772,13 +1777,27 @@ fn do_channel_reserve_test(test_recv: bool) {
1772
1777
claim_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , our_payment_preimage_21, recv_value_21) ;
1773
1778
claim_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , our_payment_preimage_22, recv_value_22) ;
1774
1779
1775
- let expected_value_to_self = stat01. value_to_self_msat - ( recv_value_1 + total_fee_msat) - ( recv_value_21 + total_fee_msat) - ( recv_value_22 + total_fee_msat) ;
1780
+ let recv_value_3 = commit_tx_fee_1_acked_htlc + commit_tx_fee_1_holding_cell_htlc - total_fee_msat;
1781
+ {
1782
+ let ( route, our_payment_hash, _) = get_route_and_payment_hash ! ( recv_value_3 + 1 ) ;
1783
+ let err = nodes[ 0 ] . node . send_payment ( route. clone ( ) , our_payment_hash) . err ( ) . unwrap ( ) ;
1784
+ match err {
1785
+ APIError :: ChannelUnavailable { err} => assert_eq ! ( err, "Cannot send value that would put us over their reserve value" ) ,
1786
+ _ => panic ! ( "Unknown error variants" ) ,
1787
+ }
1788
+ assert ! ( nodes[ 0 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
1789
+ nodes[ 0 ] . logger . assert_log ( "lightning::ln::channelmanager" . to_string ( ) , "Cannot send value that would put us over their reserve value" . to_string ( ) , 4 ) ;
1790
+ }
1791
+
1792
+ send_payment ( & nodes[ 0 ] , & vec ! [ & nodes[ 1 ] , & nodes[ 2 ] ] [ ..] , recv_value_3, recv_value_3) ;
1793
+
1794
+ let expected_value_to_self = stat01. value_to_self_msat - ( recv_value_1 + total_fee_msat) - ( recv_value_21 + total_fee_msat) - ( recv_value_22 + total_fee_msat) - ( recv_value_3 + total_fee_msat) ;
1776
1795
let stat0 = get_channel_value_stat ! ( nodes[ 0 ] , chan_1. 2 ) ;
1777
1796
assert_eq ! ( stat0. value_to_self_msat, expected_value_to_self) ;
1778
- assert_eq ! ( stat0. value_to_self_msat, stat0. channel_reserve_msat) ;
1797
+ assert_eq ! ( stat0. value_to_self_msat, stat0. channel_reserve_msat + stat0 . commit_tx_fee_outbound ) ;
1779
1798
1780
1799
let stat2 = get_channel_value_stat ! ( nodes[ 2 ] , chan_2. 2 ) ;
1781
- assert_eq ! ( stat2. value_to_self_msat, stat22. value_to_self_msat + recv_value_1 + recv_value_21 + recv_value_22) ;
1800
+ assert_eq ! ( stat2. value_to_self_msat, stat22. value_to_self_msat + recv_value_1 + recv_value_21 + recv_value_22 + recv_value_3 ) ;
1782
1801
}
1783
1802
1784
1803
#[ test]
@@ -5951,15 +5970,18 @@ fn test_update_add_htlc_bolt2_receiver_sender_can_afford_amount_sent() {
5951
5970
let mut nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
5952
5971
let chan = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 100000 , 95000000 , InitFeatures :: supported ( ) , InitFeatures :: supported ( ) ) ;
5953
5972
5954
- let their_channel_reserve = get_channel_value_stat ! ( nodes[ 0 ] , chan. 2 ) . channel_reserve_msat ;
5973
+ let chan_stat = get_channel_value_stat ! ( nodes[ 0 ] , chan. 2 ) ;
5974
+ let their_channel_reserve = chan_stat. channel_reserve_msat ;
5975
+ let commit_tx_fee = chan_stat. commit_tx_fee_outbound ;
5955
5976
5956
- let route = nodes[ 0 ] . router . get_route ( & nodes[ 1 ] . node . get_our_node_id ( ) , None , & [ ] , 5000000 -their_channel_reserve, TEST_FINAL_CLTV ) . unwrap ( ) ;
5977
+ let max_can_send = 5000000 - their_channel_reserve - commit_tx_fee;
5978
+ let route = nodes[ 0 ] . router . get_route ( & nodes[ 1 ] . node . get_our_node_id ( ) , None , & [ ] , max_can_send, TEST_FINAL_CLTV ) . unwrap ( ) ;
5957
5979
let ( _, our_payment_hash) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
5958
5980
nodes[ 0 ] . node . send_payment ( route, our_payment_hash) . unwrap ( ) ;
5959
5981
check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
5960
5982
let mut updates = get_htlc_update_msgs ! ( nodes[ 0 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
5961
5983
5962
- updates. update_add_htlcs [ 0 ] . amount_msat = 5000000 -their_channel_reserve+ 1 ;
5984
+ updates. update_add_htlcs [ 0 ] . amount_msat = max_can_send + 1 ;
5963
5985
nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & updates. update_add_htlcs [ 0 ] ) ;
5964
5986
5965
5987
assert ! ( nodes[ 1 ] . node. list_channels( ) . is_empty( ) ) ;
@@ -6043,7 +6065,7 @@ fn test_update_add_htlc_bolt2_receiver_check_cltv_expiry() {
6043
6065
let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
6044
6066
let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
6045
6067
let mut nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
6046
- create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 100000 , 95000000 , InitFeatures :: supported ( ) , InitFeatures :: supported ( ) ) ;
6068
+ create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 100000 , 94000000 , InitFeatures :: supported ( ) , InitFeatures :: supported ( ) ) ;
6047
6069
let route = nodes[ 0 ] . router . get_route ( & nodes[ 1 ] . node . get_our_node_id ( ) , None , & [ ] , 3999999 , TEST_FINAL_CLTV ) . unwrap ( ) ;
6048
6070
let ( _, our_payment_hash) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
6049
6071
nodes[ 0 ] . node . send_payment ( route, our_payment_hash) . unwrap ( ) ;
0 commit comments