@@ -1009,3 +1009,134 @@ pub fn accept_busted_but_better_fee() {
1009
1009
_ => panic ! ( "Unexpected event" ) ,
1010
1010
} ;
1011
1011
}
1012
+
1013
+ #[ xtest( feature = "_externalize_tests" ) ]
1014
+ pub fn cant_afford_on_holding_cell_release ( ) {
1015
+ do_cant_afford_on_holding_cell_release ( true ) ;
1016
+ do_cant_afford_on_holding_cell_release ( false ) ;
1017
+ }
1018
+
1019
+ pub fn do_cant_afford_on_holding_cell_release ( can_afford : bool ) {
1020
+ // Test that if we can't afford a feerate update when releasing an
1021
+ // update_fee from its holding cell, we do not generate any msg events
1022
+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
1023
+
1024
+ let mut default_config = test_default_channel_config ( ) ;
1025
+ default_config. channel_handshake_config . negotiate_anchors_zero_fee_htlc_tx = true ;
1026
+ default_config. manually_accept_inbound_channels = true ;
1027
+ default_config. channel_handshake_config . max_inbound_htlc_value_in_flight_percent_of_channel = 100 ;
1028
+
1029
+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
1030
+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ Some ( default_config. clone ( ) ) , Some ( default_config) ] ) ;
1031
+
1032
+ let mut nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
1033
+
1034
+ let node_a_id = nodes[ 0 ] . node . get_our_node_id ( ) ;
1035
+ let node_b_id = nodes[ 1 ] . node . get_our_node_id ( ) ;
1036
+
1037
+ let target_feerate = 257 ;
1038
+ let channel_type = ChannelTypeFeatures :: anchors_zero_htlc_fee_and_dependencies ( ) ;
1039
+ // This is the number of htlcs that `send_update_fee` will account for when checking whether
1040
+ // it can afford the new feerate upon releasing an update_fee from its holding cell,
1041
+ // ie the buffer + the inbound HTLC we will add while the update_fee is in the holding cell
1042
+ let num_htlcs = crate :: ln:: channel:: CONCURRENT_INBOUND_HTLC_FEE_BUFFER + 1 ;
1043
+ let commit_tx_fee_sat = chan_utils:: commit_tx_fee_sat ( target_feerate, num_htlcs as usize , & channel_type) ;
1044
+ let anchor_value_satoshis = 2 * crate :: ln:: channel:: ANCHOR_OUTPUT_VALUE_SATOSHI ;
1045
+ let channel_reserve_satoshis = 1000 ;
1046
+
1047
+ let channel_value_sat = 100_000 ;
1048
+ let node_0_balance_sat = commit_tx_fee_sat + anchor_value_satoshis + channel_reserve_satoshis - if can_afford { 0 } else { 1 } ;
1049
+ let node_1_balance_sat = channel_value_sat - node_0_balance_sat;
1050
+
1051
+ let chan_id = create_chan_between_nodes_with_value ( & nodes[ 0 ] , & nodes[ 1 ] , channel_value_sat, 0 ) . 3 ;
1052
+
1053
+ // Set node 0's balance to the can/can't afford threshold
1054
+ send_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , node_1_balance_sat * 1000 ) ;
1055
+
1056
+ {
1057
+ // Sanity check the reserve
1058
+ let per_peer_state_lock;
1059
+ let mut peer_state_lock;
1060
+ let chan =
1061
+ get_channel_ref ! ( nodes[ 1 ] , nodes[ 0 ] , per_peer_state_lock, peer_state_lock, chan_id) ;
1062
+ assert_eq ! ( chan. funding( ) . holder_selected_channel_reserve_satoshis, channel_reserve_satoshis) ;
1063
+ }
1064
+
1065
+ {
1066
+ // Bump the feerate
1067
+ let mut feerate_lock = chanmon_cfgs[ 0 ] . fee_estimator . sat_per_kw . lock ( ) . unwrap ( ) ;
1068
+ * feerate_lock += 4 ;
1069
+ assert_eq ! ( * feerate_lock, target_feerate) ;
1070
+ }
1071
+
1072
+ // Put the update fee into the holding cell of node 0
1073
+
1074
+ nodes[ 0 ] . node . maybe_update_chan_fees ( ) ;
1075
+
1076
+ // While the update_fee is in the holding cell, add an inbound HTLC
1077
+
1078
+ let ( route, payment_hash, _, payment_secret) =
1079
+ get_route_and_payment_hash ! ( nodes[ 1 ] , nodes[ 0 ] , 1000 * 1000 ) ;
1080
+ let onion = RecipientOnionFields :: secret_only ( payment_secret) ;
1081
+ let id = PaymentId ( payment_hash. 0 ) ;
1082
+ nodes[ 1 ] . node . send_payment_with_route ( route, payment_hash, onion, id) . unwrap ( ) ;
1083
+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
1084
+
1085
+ let payment_event = {
1086
+ let mut events_1 = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
1087
+ assert_eq ! ( events_1. len( ) , 1 ) ;
1088
+ SendEvent :: from_event ( events_1. pop ( ) . unwrap ( ) )
1089
+ } ;
1090
+ assert_eq ! ( payment_event. node_id, node_a_id) ;
1091
+ assert_eq ! ( payment_event. msgs. len( ) , 1 ) ;
1092
+
1093
+ nodes[ 0 ] . node . handle_update_add_htlc ( node_b_id, & payment_event. msgs [ 0 ] ) ;
1094
+ nodes[ 0 ] . node . handle_commitment_signed ( node_b_id, & payment_event. commitment_msg [ 0 ] ) ;
1095
+ check_added_monitors ( & nodes[ 0 ] , 1 ) ;
1096
+
1097
+ let ( revoke_ack, commitment_signed) = get_revoke_commit_msgs ! ( nodes[ 0 ] , node_b_id) ;
1098
+
1099
+ nodes[ 1 ] . node . handle_revoke_and_ack ( node_a_id, & revoke_ack) ;
1100
+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
1101
+ nodes[ 1 ] . node . handle_commitment_signed ( node_a_id, & commitment_signed[ 0 ] ) ;
1102
+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
1103
+
1104
+ let mut events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
1105
+ assert_eq ! ( events. len( ) , 1 ) ;
1106
+
1107
+ if let MessageSendEvent :: SendRevokeAndACK { node_id, msg } = events. pop ( ) . unwrap ( ) {
1108
+ assert_eq ! ( node_id, node_a_id) ;
1109
+ nodes[ 0 ] . node . handle_revoke_and_ack ( node_b_id, & msg) ;
1110
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1111
+ } else {
1112
+ panic ! ( ) ;
1113
+ }
1114
+
1115
+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
1116
+ assert_eq ! ( events. len( ) , 1 ) ;
1117
+ assert ! ( matches!( events[ 0 ] , Event :: PendingHTLCsForwardable { .. } ) ) ;
1118
+
1119
+ // Release the update_fee from its holding cell
1120
+
1121
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
1122
+ if can_afford {
1123
+ // We could afford the update_fee, sanity check everything
1124
+ if let MessageSendEvent :: UpdateHTLCs { node_id, channel_id, updates } = events. pop ( ) . unwrap ( ) {
1125
+ assert_eq ! ( node_id, node_b_id) ;
1126
+ assert_eq ! ( updates. commitment_signed. len( ) , 1 ) ;
1127
+ assert_eq ! ( updates. commitment_signed[ 0 ] . htlc_signatures. len( ) , 1 ) ;
1128
+ assert_eq ! ( updates. update_add_htlcs. len( ) , 0 ) ;
1129
+ assert_eq ! ( updates. update_fulfill_htlcs. len( ) , 0 ) ;
1130
+ assert_eq ! ( updates. update_fail_htlcs. len( ) , 0 ) ;
1131
+ assert_eq ! ( updates. update_fail_malformed_htlcs. len( ) , 0 ) ;
1132
+ let update_fee = updates. update_fee . unwrap ( ) ;
1133
+ assert_eq ! ( update_fee. channel_id, chan_id) ;
1134
+ assert_eq ! ( update_fee. feerate_per_kw, target_feerate) ;
1135
+ } else {
1136
+ panic ! ( ) ;
1137
+ }
1138
+ } else {
1139
+ // We could not afford the update_fee, no events should be generated
1140
+ assert_eq ! ( events. len( ) , 0 ) ;
1141
+ }
1142
+ }
0 commit comments