@@ -87,7 +87,12 @@ fn params_from_invoice(
87
87
#[ cfg( test) ]
88
88
mod tests {
89
89
use super :: * ;
90
- use crate :: routing:: router:: Payee ;
90
+ use crate :: events:: Event ;
91
+ use crate :: ln:: channelmanager:: { PaymentId , Retry } ;
92
+ use crate :: ln:: functional_test_utils:: * ;
93
+ use crate :: ln:: msgs:: ChannelMessageHandler ;
94
+ use crate :: ln:: outbound_payment:: Bolt11PaymentError ;
95
+ use crate :: routing:: router:: { Payee , RouteParametersConfig } ;
91
96
use crate :: types:: payment:: PaymentSecret ;
92
97
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
93
98
use bitcoin:: secp256k1:: { PublicKey , Secp256k1 , SecretKey } ;
@@ -159,12 +164,7 @@ mod tests {
159
164
}
160
165
161
166
#[ test]
162
- fn payment_metadata_end_to_end ( ) {
163
- use crate :: events:: Event ;
164
- use crate :: ln:: channelmanager:: { PaymentId , Retry } ;
165
- use crate :: ln:: functional_test_utils:: * ;
166
- use crate :: ln:: msgs:: ChannelMessageHandler ;
167
-
167
+ fn payment_metadata_end_to_end_for_invoice_with_amount ( ) {
168
168
// Test that a payment metadata read from an invoice passed to `pay_invoice` makes it all
169
169
// the way out through the `PaymentClaimable` event.
170
170
let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
@@ -192,11 +192,95 @@ mod tests {
192
192
. build_signed ( |hash| secp_ctx. sign_ecdsa_recoverable ( hash, & node_secret) )
193
193
. unwrap ( ) ;
194
194
195
- let ( hash, onion, params) = payment_parameters_from_invoice ( & invoice) . unwrap ( ) ;
195
+ match nodes[ 0 ] . node . pay_for_bolt11_invoice (
196
+ & invoice,
197
+ PaymentId ( payment_hash. 0 ) ,
198
+ Some ( 100 ) ,
199
+ RouteParametersConfig :: default ( ) ,
200
+ Retry :: Attempts ( 0 ) ,
201
+ ) {
202
+ Err ( Bolt11PaymentError :: InvalidAmount ) => ( ) ,
203
+ _ => panic ! ( "Unexpected result" ) ,
204
+ } ;
205
+
196
206
nodes[ 0 ]
197
207
. node
198
- . send_payment ( hash, onion, PaymentId ( hash. 0 ) , params, Retry :: Attempts ( 0 ) )
208
+ . pay_for_bolt11_invoice (
209
+ & invoice,
210
+ PaymentId ( payment_hash. 0 ) ,
211
+ None ,
212
+ RouteParametersConfig :: default ( ) ,
213
+ Retry :: Attempts ( 0 ) ,
214
+ )
199
215
. unwrap ( ) ;
216
+
217
+ check_added_monitors ( & nodes[ 0 ] , 1 ) ;
218
+ let send_event = SendEvent :: from_node ( & nodes[ 0 ] ) ;
219
+ nodes[ 1 ] . node . handle_update_add_htlc ( nodes[ 0 ] . node . get_our_node_id ( ) , & send_event. msgs [ 0 ] ) ;
220
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , & send_event. commitment_msg, false ) ;
221
+
222
+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
223
+
224
+ let mut events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
225
+ assert_eq ! ( events. len( ) , 1 ) ;
226
+ match events. pop ( ) . unwrap ( ) {
227
+ Event :: PaymentClaimable { onion_fields, .. } => {
228
+ assert_eq ! ( Some ( payment_metadata) , onion_fields. unwrap( ) . payment_metadata) ;
229
+ } ,
230
+ _ => panic ! ( "Unexpected event" ) ,
231
+ }
232
+ }
233
+
234
+ #[ test]
235
+ fn payment_metadata_end_to_end_for_invoice_with_no_amount ( ) {
236
+ // Test that a payment metadata read from an invoice passed to `pay_invoice` makes it all
237
+ // the way out through the `PaymentClaimable` event.
238
+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
239
+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
240
+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
241
+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
242
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
243
+
244
+ let payment_metadata = vec ! [ 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 42 ] ;
245
+
246
+ let ( payment_hash, payment_secret) =
247
+ nodes[ 1 ] . node . create_inbound_payment ( None , 7200 , None ) . unwrap ( ) ;
248
+
249
+ let secp_ctx = Secp256k1 :: new ( ) ;
250
+ let node_secret = nodes[ 1 ] . keys_manager . backing . get_node_secret_key ( ) ;
251
+ let timestamp = SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( ) ;
252
+ let invoice = InvoiceBuilder :: new ( Currency :: Bitcoin )
253
+ . description ( "test" . into ( ) )
254
+ . payment_hash ( Sha256 :: from_slice ( & payment_hash. 0 ) . unwrap ( ) )
255
+ . payment_secret ( payment_secret)
256
+ . duration_since_epoch ( timestamp)
257
+ . min_final_cltv_expiry_delta ( 144 )
258
+ . payment_metadata ( payment_metadata. clone ( ) )
259
+ . build_signed ( |hash| secp_ctx. sign_ecdsa_recoverable ( hash, & node_secret) )
260
+ . unwrap ( ) ;
261
+
262
+ match nodes[ 0 ] . node . pay_for_bolt11_invoice (
263
+ & invoice,
264
+ PaymentId ( payment_hash. 0 ) ,
265
+ None ,
266
+ RouteParametersConfig :: default ( ) ,
267
+ Retry :: Attempts ( 0 ) ,
268
+ ) {
269
+ Err ( Bolt11PaymentError :: InvalidAmount ) => ( ) ,
270
+ _ => panic ! ( "Unexpected result" ) ,
271
+ } ;
272
+
273
+ nodes[ 0 ]
274
+ . node
275
+ . pay_for_bolt11_invoice (
276
+ & invoice,
277
+ PaymentId ( payment_hash. 0 ) ,
278
+ Some ( 50_000 ) ,
279
+ RouteParametersConfig :: default ( ) ,
280
+ Retry :: Attempts ( 0 ) ,
281
+ )
282
+ . unwrap ( ) ;
283
+
200
284
check_added_monitors ( & nodes[ 0 ] , 1 ) ;
201
285
let send_event = SendEvent :: from_node ( & nodes[ 0 ] ) ;
202
286
nodes[ 1 ] . node . handle_update_add_htlc ( nodes[ 0 ] . node . get_our_node_id ( ) , & send_event. msgs [ 0 ] ) ;
0 commit comments