@@ -1534,7 +1534,8 @@ fn test_basic_channel_reserve() {
1534
1534
1535
1535
// The 2* and +1 are for the fee spike reserve.
1536
1536
let ( _, our_payment_hash) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
1537
- let max_can_send = 5000000 - channel_reserve - 2 * commit_tx_fee_msat ( get_feerate ! ( nodes[ 0 ] , chan. 2 ) , 1 + 1 ) ;
1537
+ let commit_tx_fee = 2 * commit_tx_fee_msat ( get_feerate ! ( nodes[ 0 ] , chan. 2 ) , 1 + 1 ) ;
1538
+ let max_can_send = 5000000 - channel_reserve - commit_tx_fee;
1538
1539
let net_graph_msg_handler = & nodes[ 0 ] . net_graph_msg_handler ;
1539
1540
let route = get_route ( & nodes[ 0 ] . node . get_our_node_id ( ) , net_graph_msg_handler, & nodes. last ( ) . unwrap ( ) . node . get_our_node_id ( ) , None , & Vec :: new ( ) , max_can_send + 1 , TEST_FINAL_CLTV , & logger) . unwrap ( ) ;
1540
1541
let err = nodes[ 0 ] . node . send_payment ( & route, our_payment_hash, & None ) . err ( ) . unwrap ( ) ;
@@ -1560,7 +1561,7 @@ fn test_chan_reserve_violation_inbound_htlc_inbound_chan() {
1560
1561
let node_cfgs = create_node_cfgs ( 3 , & chanmon_cfgs) ;
1561
1562
let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , None , None ] ) ;
1562
1563
let mut nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
1563
- let chan_1 = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 100000 , 95000000 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
1564
+ let chan = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 100000 , 95000000 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
1564
1565
let _ = create_announced_chan_between_nodes_with_value ( & nodes, 1 , 2 , 100000 , 95000000 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
1565
1566
let logger = test_utils:: TestLogger :: new ( ) ;
1566
1567
@@ -1574,13 +1575,14 @@ fn test_chan_reserve_violation_inbound_htlc_inbound_chan() {
1574
1575
} ;
1575
1576
1576
1577
let feemsat = 239 ;
1577
- let total_fee_msat = ( nodes. len ( ) - 2 ) as u64 * feemsat;
1578
- let chan_stat = get_channel_value_stat ! ( nodes[ 0 ] , chan_1 . 2 ) ;
1579
- let feerate = get_feerate ! ( nodes[ 0 ] , chan_1 . 2 ) ;
1578
+ let total_routing_fee_msat = ( nodes. len ( ) - 2 ) as u64 * feemsat;
1579
+ let chan_stat = get_channel_value_stat ! ( nodes[ 0 ] , chan . 2 ) ;
1580
+ let feerate = get_feerate ! ( nodes[ 0 ] , chan . 2 ) ;
1580
1581
1581
1582
// Add a 2* and +1 for the fee spike reserve.
1582
- let recv_value_1 = ( chan_stat. value_to_self_msat - chan_stat. channel_reserve_msat - total_fee_msat - 2 * commit_tx_fee_msat ( feerate, 1 + 1 ) ) /2 ;
1583
- let amt_msat_1 = recv_value_1 + total_fee_msat;
1583
+ let commit_tx_fee_2_htlc = 2 * commit_tx_fee_msat ( feerate, 2 + 1 ) ;
1584
+ let recv_value_1 = ( chan_stat. value_to_self_msat - chan_stat. channel_reserve_msat - total_routing_fee_msat - commit_tx_fee_2_htlc) /2 ;
1585
+ let amt_msat_1 = recv_value_1 + total_routing_fee_msat;
1584
1586
1585
1587
// Add a pending HTLC.
1586
1588
let ( route_1, our_payment_hash_1, _) = get_route_and_payment_hash ! ( amt_msat_1) ;
@@ -1595,8 +1597,10 @@ fn test_chan_reserve_violation_inbound_htlc_inbound_chan() {
1595
1597
nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & payment_event_1. msgs [ 0 ] ) ;
1596
1598
1597
1599
// Attempt to trigger a channel reserve violation --> payment failure.
1598
- let recv_value_2 = chan_stat. value_to_self_msat - amt_msat_1 - chan_stat. channel_reserve_msat - total_fee_msat - commit_tx_fee_msat ( feerate, 2 ) + 1 ;
1599
- let ( route_2, _, _) = get_route_and_payment_hash ! ( recv_value_2) ;
1600
+ let commit_tx_fee_2_htlcs = commit_tx_fee_msat ( feerate, 2 ) ;
1601
+ let recv_value_2 = chan_stat. value_to_self_msat - amt_msat_1 - chan_stat. channel_reserve_msat - total_routing_fee_msat - commit_tx_fee_2_htlcs + 1 ;
1602
+ let amt_msat_2 = recv_value_2 + total_routing_fee_msat;
1603
+ let ( route_2, _, _) = get_route_and_payment_hash ! ( amt_msat_2) ;
1600
1604
1601
1605
// Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
1602
1606
let secp_ctx = Secp256k1 :: new ( ) ;
@@ -1613,9 +1617,9 @@ fn test_chan_reserve_violation_inbound_htlc_inbound_chan() {
1613
1617
let ( onion_payloads, htlc_msat, htlc_cltv) = onion_utils:: build_onion_payloads ( & route_2. paths [ 0 ] , recv_value_2, & None , cur_height) . unwrap ( ) ;
1614
1618
let onion_packet = onion_utils:: construct_onion_packet ( onion_payloads, onion_keys, [ 0 ; 32 ] , & our_payment_hash_1) ;
1615
1619
let msg = msgs:: UpdateAddHTLC {
1616
- channel_id : chan_1 . 2 ,
1620
+ channel_id : chan . 2 ,
1617
1621
htlc_id : 1 ,
1618
- amount_msat : htlc_msat,
1622
+ amount_msat : htlc_msat + 1 ,
1619
1623
payment_hash : our_payment_hash_1,
1620
1624
cltv_expiry : htlc_cltv,
1621
1625
onion_routing_packet : onion_packet,
@@ -1725,7 +1729,8 @@ fn test_channel_reserve_holding_cell_htlcs() {
1725
1729
// the amount of the first of these aforementioned 3 payments. The reason we split into 3 payments
1726
1730
// is to test the behavior of the holding cell with respect to channel reserve and commit tx fee
1727
1731
// policy.
1728
- let recv_value_1 = ( stat01. value_to_self_msat - stat01. channel_reserve_msat - total_fee_msat - 2 * commit_tx_fee_msat ( feerate, 2 + 1 ) ) /2 ;
1732
+ let commit_tx_fee_2_htlcs = 2 * commit_tx_fee_msat ( feerate, 2 + 1 ) ;
1733
+ let recv_value_1 = ( stat01. value_to_self_msat - stat01. channel_reserve_msat - total_fee_msat - commit_tx_fee_2_htlcs) /2 ;
1729
1734
let amt_msat_1 = recv_value_1 + total_fee_msat;
1730
1735
1731
1736
let ( route_1, our_payment_hash_1, our_payment_preimage_1) = get_route_and_payment_hash ! ( recv_value_1) ;
@@ -1740,7 +1745,7 @@ fn test_channel_reserve_holding_cell_htlcs() {
1740
1745
nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & payment_event_1. msgs [ 0 ] ) ;
1741
1746
1742
1747
// channel reserve test with htlc pending output > 0
1743
- let recv_value_2 = stat01. value_to_self_msat - amt_msat_1 - stat01. channel_reserve_msat - total_fee_msat - 2 * commit_tx_fee_msat ( feerate , 2 + 1 ) ;
1748
+ let recv_value_2 = stat01. value_to_self_msat - amt_msat_1 - stat01. channel_reserve_msat - total_fee_msat - commit_tx_fee_2_htlcs ;
1744
1749
{
1745
1750
let ( route, our_payment_hash, _) = get_route_and_payment_hash ! ( recv_value_2 + 1 ) ;
1746
1751
unwrap_send_err ! ( nodes[ 0 ] . node. send_payment( & route, our_payment_hash, & None ) , true , APIError :: ChannelUnavailable { err } ,
@@ -1749,12 +1754,13 @@ fn test_channel_reserve_holding_cell_htlcs() {
1749
1754
}
1750
1755
1751
1756
// split the rest to test holding cell
1752
- let additional_htlc_cost_msat = 2 * commit_tx_fee_msat ( feerate, 3 + 1 ) - 2 * commit_tx_fee_msat ( feerate, 2 + 1 ) ;
1757
+ let commit_tx_fee_3_htlcs = 2 * commit_tx_fee_msat ( feerate, 3 + 1 ) ;
1758
+ let additional_htlc_cost_msat = commit_tx_fee_3_htlcs - commit_tx_fee_2_htlcs;
1753
1759
let recv_value_21 = recv_value_2/2 - additional_htlc_cost_msat/2 ;
1754
1760
let recv_value_22 = recv_value_2 - recv_value_21 - total_fee_msat - additional_htlc_cost_msat;
1755
1761
{
1756
1762
let stat = get_channel_value_stat ! ( nodes[ 0 ] , chan_1. 2 ) ;
1757
- 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 + 2 * commit_tx_fee_msat ( feerate , 3 + 1 ) ) , stat. channel_reserve_msat) ;
1763
+ 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_3_htlcs ) , stat. channel_reserve_msat) ;
1758
1764
}
1759
1765
1760
1766
// now see if they go through on both sides
@@ -1847,7 +1853,8 @@ fn test_channel_reserve_holding_cell_htlcs() {
1847
1853
claim_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , our_payment_preimage_21, recv_value_21) ;
1848
1854
claim_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , our_payment_preimage_22, recv_value_22) ;
1849
1855
1850
- let recv_value_3 = 2 * commit_tx_fee_msat ( feerate, 3 ) - 2 * commit_tx_fee_msat ( feerate, 1 ) - total_fee_msat;
1856
+ let commit_tx_fee_0_htlcs = 2 * commit_tx_fee_msat ( feerate, 1 ) ;
1857
+ let recv_value_3 = commit_tx_fee_2_htlcs - commit_tx_fee_0_htlcs - total_fee_msat;
1851
1858
{
1852
1859
let ( route, our_payment_hash, _) = get_route_and_payment_hash ! ( recv_value_3 + 1 ) ;
1853
1860
let err = nodes[ 0 ] . node . send_payment ( & route, our_payment_hash, & None ) . err ( ) . unwrap ( ) ;
@@ -1867,10 +1874,11 @@ fn test_channel_reserve_holding_cell_htlcs() {
1867
1874
1868
1875
send_payment ( & nodes[ 0 ] , & vec ! [ & nodes[ 1 ] , & nodes[ 2 ] ] [ ..] , recv_value_3, recv_value_3) ;
1869
1876
1877
+ let commit_tx_fee_1_htlc = 2 * commit_tx_fee_msat ( feerate, 1 + 1 ) ;
1870
1878
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) ;
1871
1879
let stat0 = get_channel_value_stat ! ( nodes[ 0 ] , chan_1. 2 ) ;
1872
1880
assert_eq ! ( stat0. value_to_self_msat, expected_value_to_self) ;
1873
- assert_eq ! ( stat0. value_to_self_msat, stat0. channel_reserve_msat + 2 * commit_tx_fee_msat ( feerate , 1 + 1 ) ) ;
1881
+ assert_eq ! ( stat0. value_to_self_msat, stat0. channel_reserve_msat + commit_tx_fee_1_htlc ) ;
1874
1882
1875
1883
let stat2 = get_channel_value_stat ! ( nodes[ 2 ] , chan_2. 2 ) ;
1876
1884
assert_eq ! ( stat2. value_to_self_msat, stat22. value_to_self_msat + recv_value_1 + recv_value_21 + recv_value_22 + recv_value_3) ;
@@ -6167,10 +6175,11 @@ fn test_holding_cell_htlc_with_pending_fee_update() {
6167
6175
let mut chan_stat = get_channel_value_stat ! ( nodes[ 0 ] , chan. 2 ) ;
6168
6176
let channel_reserve = chan_stat. channel_reserve_msat ;
6169
6177
let feerate = get_feerate ! ( nodes[ 0 ] , chan. 2 ) ;
6178
+ let commit_tx_fee = 2 * commit_tx_fee_msat ( feerate, 1 + 1 ) ;
6170
6179
6171
6180
// 2* and +1 HTLCs on the commit tx fee calculation for the fee spike reserve.
6172
6181
let ( _, our_payment_hash) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
6173
- let max_can_send = 5000000 - channel_reserve - 2 * commit_tx_fee_msat ( feerate , 1 + 1 ) ;
6182
+ let max_can_send = 5000000 - channel_reserve - commit_tx_fee ;
6174
6183
let net_graph_msg_handler = & nodes[ 0 ] . net_graph_msg_handler ;
6175
6184
let route = get_route ( & nodes[ 0 ] . node . get_our_node_id ( ) , net_graph_msg_handler, & nodes[ 1 ] . node . get_our_node_id ( ) , None , & [ ] , max_can_send, TEST_FINAL_CLTV , & logger) . unwrap ( ) ;
6176
6185
0 commit comments