@@ -87,7 +87,12 @@ fn params_from_invoice(
8787#[ cfg( test) ]
8888mod tests {
8989 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 } ;
9196 use crate :: sign:: { NodeSigner , Recipient } ;
9297 use crate :: types:: payment:: PaymentSecret ;
9398 use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
@@ -160,12 +165,7 @@ mod tests {
160165 }
161166
162167 #[ test]
163- fn payment_metadata_end_to_end ( ) {
164- use crate :: events:: Event ;
165- use crate :: ln:: channelmanager:: { PaymentId , Retry } ;
166- use crate :: ln:: functional_test_utils:: * ;
167- use crate :: ln:: msgs:: ChannelMessageHandler ;
168-
168+ fn payment_metadata_end_to_end_for_invoice_with_amount ( ) {
169169 // Test that a payment metadata read from an invoice passed to `pay_invoice` makes it all
170170 // the way out through the `PaymentClaimable` event.
171171 let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
@@ -194,11 +194,96 @@ mod tests {
194194 let invoice = invoice. sign :: < _ , ( ) > ( |_| Ok ( sig) ) . unwrap ( ) ;
195195 let invoice = Bolt11Invoice :: from_signed ( invoice) . unwrap ( ) ;
196196
197- let ( hash, onion, params) = payment_parameters_from_invoice ( & invoice) . unwrap ( ) ;
197+ match nodes[ 0 ] . node . pay_for_bolt11_invoice (
198+ & invoice,
199+ PaymentId ( payment_hash. 0 ) ,
200+ Some ( 100 ) ,
201+ RouteParametersConfig :: default ( ) ,
202+ Retry :: Attempts ( 0 ) ,
203+ ) {
204+ Err ( Bolt11PaymentError :: InvalidAmount ) => ( ) ,
205+ _ => panic ! ( "Unexpected result" ) ,
206+ } ;
207+
208+ nodes[ 0 ]
209+ . node
210+ . pay_for_bolt11_invoice (
211+ & invoice,
212+ PaymentId ( payment_hash. 0 ) ,
213+ None ,
214+ RouteParametersConfig :: default ( ) ,
215+ Retry :: Attempts ( 0 ) ,
216+ )
217+ . unwrap ( ) ;
218+
219+ check_added_monitors ( & nodes[ 0 ] , 1 ) ;
220+ let send_event = SendEvent :: from_node ( & nodes[ 0 ] ) ;
221+ nodes[ 1 ] . node . handle_update_add_htlc ( nodes[ 0 ] . node . get_our_node_id ( ) , & send_event. msgs [ 0 ] ) ;
222+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , & send_event. commitment_msg, false ) ;
223+
224+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
225+
226+ let mut events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
227+ assert_eq ! ( events. len( ) , 1 ) ;
228+ match events. pop ( ) . unwrap ( ) {
229+ Event :: PaymentClaimable { onion_fields, .. } => {
230+ assert_eq ! ( Some ( payment_metadata) , onion_fields. unwrap( ) . payment_metadata) ;
231+ } ,
232+ _ => panic ! ( "Unexpected event" ) ,
233+ }
234+ }
235+
236+ #[ test]
237+ fn payment_metadata_end_to_end_for_invoice_with_no_amount ( ) {
238+ // Test that a payment metadata read from an invoice passed to `pay_invoice` makes it all
239+ // the way out through the `PaymentClaimable` event.
240+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
241+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
242+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
243+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
244+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
245+
246+ let payment_metadata = vec ! [ 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 42 ] ;
247+
248+ let ( payment_hash, payment_secret) =
249+ nodes[ 1 ] . node . create_inbound_payment ( None , 7200 , None ) . unwrap ( ) ;
250+
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_raw ( )
260+ . unwrap ( ) ;
261+ let sig = nodes[ 1 ] . keys_manager . backing . sign_invoice ( & invoice, Recipient :: Node ) . unwrap ( ) ;
262+ let invoice = invoice. sign :: < _ , ( ) > ( |_| Ok ( sig) ) . unwrap ( ) ;
263+ let invoice = Bolt11Invoice :: from_signed ( invoice) . unwrap ( ) ;
264+
265+ match nodes[ 0 ] . node . pay_for_bolt11_invoice (
266+ & invoice,
267+ PaymentId ( payment_hash. 0 ) ,
268+ None ,
269+ RouteParametersConfig :: default ( ) ,
270+ Retry :: Attempts ( 0 ) ,
271+ ) {
272+ Err ( Bolt11PaymentError :: InvalidAmount ) => ( ) ,
273+ _ => panic ! ( "Unexpected result" ) ,
274+ } ;
275+
198276 nodes[ 0 ]
199277 . node
200- . send_payment ( hash, onion, PaymentId ( hash. 0 ) , params, Retry :: Attempts ( 0 ) )
278+ . pay_for_bolt11_invoice (
279+ & invoice,
280+ PaymentId ( payment_hash. 0 ) ,
281+ Some ( 50_000 ) ,
282+ RouteParametersConfig :: default ( ) ,
283+ Retry :: Attempts ( 0 ) ,
284+ )
201285 . unwrap ( ) ;
286+
202287 check_added_monitors ( & nodes[ 0 ] , 1 ) ;
203288 let send_event = SendEvent :: from_node ( & nodes[ 0 ] ) ;
204289 nodes[ 1 ] . node . handle_update_add_htlc ( nodes[ 0 ] . node . get_our_node_id ( ) , & send_event. msgs [ 0 ] ) ;
0 commit comments