X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-invoice%2Fsrc%2Fpayment.rs;h=a5160ea9b69a4d570e67100fb0389fb0eed16226;hb=61341df39e90de9d650851a624c0644f5c9dd055;hp=028711a630e65587f291d2df783c1dd51fbdf980;hpb=2b8c287be96c19dfc9d77d892eebc38b90db9849;p=rust-lightning diff --git a/lightning-invoice/src/payment.rs b/lightning-invoice/src/payment.rs index 028711a6..a5160ea9 100644 --- a/lightning-invoice/src/payment.rs +++ b/lightning-invoice/src/payment.rs @@ -253,6 +253,10 @@ where ) -> Result { debug_assert!(invoice.amount_milli_satoshis().is_some() ^ amount_msats.is_some()); let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner()); + if invoice.is_expired() { + log_trace!(self.logger, "Invoice expired prior to first send for payment {}", log_bytes!(payment_hash.0)); + return Err(PaymentError::Invoice("Invoice expired prior to send")); + } let retry_data_payment_id = loop { let mut payment_cache = self.payment_cache.lock().unwrap(); match payment_cache.entry(payment_hash) { @@ -728,9 +732,32 @@ mod tests { let payment_preimage = PaymentPreimage([1; 32]); let invoice = expired_invoice(payment_preimage); + if let PaymentError::Invoice(msg) = invoice_payer.pay_invoice(&invoice).unwrap_err() { + assert_eq!(msg, "Invoice expired prior to send"); + } else { panic!("Expected Invoice Error"); } + } + + #[test] + fn fails_retrying_invoice_after_expiration() { + let event_handled = core::cell::RefCell::new(false); + let event_handler = |_: &_| { *event_handled.borrow_mut() = true; }; + + let payer = TestPayer::new(); + let router = TestRouter {}; + let scorer = RefCell::new(TestScorer::new()); + let logger = TestLogger::new(); + let invoice_payer = + InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2)); + + let payment_preimage = PaymentPreimage([1; 32]); + let invoice = invoice(payment_preimage); let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap()); assert_eq!(*payer.attempts.borrow(), 1); + let mut retry_data = TestRouter::retry_for_invoice(&invoice); + retry_data.payee.expiry_time = Some(SystemTime::now() + .checked_sub(Duration::from_secs(2)).unwrap() + .duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()); let event = Event::PaymentPathFailed { payment_id, payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()), @@ -739,7 +766,7 @@ mod tests { all_paths_failed: false, path: vec![], short_channel_id: None, - retry: Some(TestRouter::retry_for_invoice(&invoice)), + retry: Some(retry_data), }; invoice_payer.handle_event(&event); assert_eq!(*event_handled.borrow(), true); @@ -1259,4 +1286,49 @@ mod tests { assert_eq!(htlc_msgs.len(), 2); check_added_monitors!(nodes[0], 2); } + + #[test] + fn immediate_retry_on_failure() { + // Tests that we can/will retry immediately after a failure + let chanmon_cfgs = create_chanmon_cfgs(2); + let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); + let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]); + let nodes = create_network(2, &node_cfgs, &node_chanmgrs); + + create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known()); + create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known()); + let chans = nodes[0].node.list_usable_channels(); + let mut route = Route { + paths: vec![ + vec![RouteHop { + pubkey: nodes[1].node.get_our_node_id(), + node_features: NodeFeatures::known(), + short_channel_id: chans[0].short_channel_id.unwrap(), + channel_features: ChannelFeatures::known(), + fee_msat: 100_000_001, // Our default max-HTLC-value is 10% of the channel value, which this is one more than + cltv_expiry_delta: 100, + }], + ], + payee: Some(Payee::new(nodes[1].node.get_our_node_id())), + }; + let router = ManualRouter(RefCell::new(VecDeque::new())); + router.expect_find_route(Ok(route.clone())); + // On retry, split the payment across both channels. + route.paths.push(route.paths[0].clone()); + route.paths[0][0].short_channel_id = chans[1].short_channel_id.unwrap(); + route.paths[0][0].fee_msat = 50_000_000; + route.paths[1][0].fee_msat = 50_000_001; + router.expect_find_route(Ok(route.clone())); + + let event_handler = |_: &_| { panic!(); }; + let scorer = RefCell::new(TestScorer::new()); + let invoice_payer = InvoicePayer::new(nodes[0].node, router, &scorer, nodes[0].logger, event_handler, RetryAttempts(1)); + + assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager( + &nodes[1].node, nodes[1].keys_manager, Currency::Bitcoin, Some(100_010_000), "Invoice".to_string()).unwrap()) + .is_ok()); + let htlc_msgs = nodes[0].node.get_and_clear_pending_msg_events(); + assert_eq!(htlc_msgs.len(), 2); + check_added_monitors!(nodes[0], 2); + } }