@@ -3087,6 +3087,111 @@ fn do_test_custom_tlvs(spontaneous: bool) {
3087
3087
claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , our_payment_preimage) ;
3088
3088
}
3089
3089
3090
+ #[ test]
3091
+ fn do_test_custom_tlvs_consistency ( ) {
3092
+ // Test that if we recieve two HTLCs with different custom TLVs we drop the non-matching TLVs
3093
+ let chanmon_cfgs = create_chanmon_cfgs ( 4 ) ;
3094
+ let node_cfgs = create_node_cfgs ( 4 , & chanmon_cfgs) ;
3095
+ let node_chanmgrs = create_node_chanmgrs ( 4 , & node_cfgs, & [ None , None , None , None ] ) ;
3096
+ let nodes = create_network ( 4 , & node_cfgs, & node_chanmgrs) ;
3097
+
3098
+ create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 100_000 , 0 ) ;
3099
+ create_announced_chan_between_nodes_with_value ( & nodes, 0 , 2 , 100_000 , 0 ) ;
3100
+ create_announced_chan_between_nodes_with_value ( & nodes, 1 , 3 , 100_000 , 0 ) ;
3101
+ create_announced_chan_between_nodes_with_value ( & nodes, 2 , 3 , 100_000 , 0 ) ;
3102
+
3103
+ let payment_params = PaymentParameters :: from_node_id ( nodes[ 3 ] . node . get_our_node_id ( ) , TEST_FINAL_CLTV )
3104
+ . with_bolt11_features ( nodes[ 3 ] . node . invoice_features ( ) ) . unwrap ( ) ;
3105
+ let mut route = get_route ! ( nodes[ 0 ] , payment_params, 15_000_000 ) . unwrap ( ) ;
3106
+ assert_eq ! ( route. paths. len( ) , 2 ) ;
3107
+ route. paths . sort_by ( |path_a, _| {
3108
+ // Sort the path so that the path through nodes[1] comes first
3109
+ if path_a. hops [ 0 ] . pubkey == nodes[ 1 ] . node . get_our_node_id ( ) {
3110
+ core:: cmp:: Ordering :: Less } else { core:: cmp:: Ordering :: Greater }
3111
+ } ) ;
3112
+
3113
+ let ( our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash ! ( & nodes[ 3 ] ) ;
3114
+ let payment_id = PaymentId ( [ 42 ; 32 ] ) ;
3115
+ let amt_msat = 15_000_000 ;
3116
+ let field1 = 0x01020304 ;
3117
+ let field2 = Some ( vec ! [ 0x42u8 ; 16 ] ) ;
3118
+ let custom_tlvs = create_custom_tlvs ( field1, field2) ;
3119
+ let onion_fields = RecipientOnionFields {
3120
+ payment_secret : Some ( our_payment_secret) ,
3121
+ payment_metadata : None ,
3122
+ custom_tlvs : Some ( custom_tlvs. clone ( ) )
3123
+ } ;
3124
+ let session_privs = nodes[ 0 ] . node . test_add_new_pending_payment ( our_payment_hash,
3125
+ onion_fields. clone ( ) , payment_id, & route) . unwrap ( ) ;
3126
+ let cur_height = nodes[ 0 ] . best_block_info ( ) . 1 ;
3127
+ nodes[ 0 ] . node . test_send_payment_along_path ( & route. paths [ 0 ] , & our_payment_hash,
3128
+ onion_fields. clone ( ) , amt_msat, cur_height, payment_id,
3129
+ & None , session_privs[ 0 ] ) . unwrap ( ) ;
3130
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
3131
+
3132
+ {
3133
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
3134
+ assert_eq ! ( events. len( ) , 1 ) ;
3135
+ pass_along_path ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 3 ] ] , amt_msat, our_payment_hash, Some ( our_payment_secret) , events. pop ( ) . unwrap ( ) , false , None ) ;
3136
+ }
3137
+ assert ! ( nodes[ 3 ] . node. get_and_clear_pending_events( ) . is_empty( ) ) ;
3138
+
3139
+ let field1 = 0x01020304 ;
3140
+ let field2 = None ;
3141
+ let custom_tlvs = create_custom_tlvs ( field1, field2) ;
3142
+ let onion_fields = RecipientOnionFields {
3143
+ payment_secret : Some ( our_payment_secret) ,
3144
+ payment_metadata : None ,
3145
+ custom_tlvs : Some ( custom_tlvs. clone ( ) )
3146
+ } ;
3147
+ nodes[ 0 ] . node . test_send_payment_along_path ( & route. paths [ 1 ] , & our_payment_hash,
3148
+ onion_fields. clone ( ) , amt_msat, cur_height, payment_id, & None , session_privs[ 1 ] ) . unwrap ( ) ;
3149
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
3150
+
3151
+ {
3152
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
3153
+ assert_eq ! ( events. len( ) , 1 ) ;
3154
+ let payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
3155
+
3156
+ nodes[ 2 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) ;
3157
+ commitment_signed_dance ! ( nodes[ 2 ] , nodes[ 0 ] , payment_event. commitment_msg, false ) ;
3158
+
3159
+ expect_pending_htlcs_forwardable ! ( nodes[ 2 ] ) ;
3160
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
3161
+
3162
+ let mut events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
3163
+ assert_eq ! ( events. len( ) , 1 ) ;
3164
+ let payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
3165
+
3166
+ nodes[ 3 ] . node . handle_update_add_htlc ( & nodes[ 2 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) ;
3167
+ check_added_monitors ! ( nodes[ 3 ] , 0 ) ;
3168
+ commitment_signed_dance ! ( nodes[ 3 ] , nodes[ 2 ] , payment_event. commitment_msg, true , true ) ;
3169
+ }
3170
+ expect_pending_htlcs_forwardable_ignore ! ( nodes[ 3 ] ) ;
3171
+ nodes[ 3 ] . node . process_pending_htlc_forwards ( ) ;
3172
+
3173
+ let events = nodes[ 3 ] . node . get_and_clear_pending_events ( ) ;
3174
+ assert_eq ! ( events. len( ) , 1 ) ;
3175
+ match events[ 0 ] {
3176
+ Event :: PaymentClaimable { ref purpose, amount_msat, ref onion_fields, .. } => {
3177
+ match & purpose {
3178
+ PaymentPurpose :: InvoicePayment { payment_secret, .. } => {
3179
+ assert_eq ! ( our_payment_secret, * payment_secret) ;
3180
+ assert_eq ! ( Some ( * payment_secret) , onion_fields. as_ref( ) . unwrap( ) . payment_secret) ;
3181
+ } ,
3182
+ PaymentPurpose :: SpontaneousPayment ( payment_preimage) => {
3183
+ assert_eq ! ( our_payment_preimage, * payment_preimage) ;
3184
+ } ,
3185
+ }
3186
+ assert_eq ! ( amount_msat, amt_msat) ;
3187
+ assert_eq ! ( onion_fields. clone( ) . unwrap( ) . custom_tlvs. unwrap( ) , custom_tlvs) ;
3188
+ } ,
3189
+ _ => panic ! ( "Unexpected event" ) ,
3190
+ }
3191
+
3192
+ do_claim_payment_along_route ( & nodes[ 0 ] , & [ & [ & nodes[ 1 ] , & nodes[ 3 ] ] , & [ & nodes[ 2 ] , & nodes[ 3 ] ] ] , false , our_payment_preimage) ;
3193
+ expect_payment_sent ( & nodes[ 0 ] , our_payment_preimage, Some ( Some ( 2000 ) ) , true ) ;
3194
+ }
3090
3195
3091
3196
fn do_test_payment_metadata_consistency ( do_reload : bool , do_modify : bool ) {
3092
3197
// Check that a payment metadata received on one HTLC that doesn't match the one received on
0 commit comments