Skip to content

Commit 658eb7c

Browse files
committed
Introduce pay_for_bolt11_invoice tests
1 parent 25b94f9 commit 658eb7c

File tree

2 files changed

+99
-21
lines changed

2 files changed

+99
-21
lines changed

lightning/src/ln/bolt11_payment.rs

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,12 @@ fn params_from_invoice(
8787
#[cfg(test)]
8888
mod 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::types::payment::PaymentSecret;
9297
use bitcoin::hashes::sha256::Hash as Sha256;
9398
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
@@ -159,12 +164,7 @@ mod tests {
159164
}
160165

161166
#[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() {
168168
// Test that a payment metadata read from an invoice passed to `pay_invoice` makes it all
169169
// the way out through the `PaymentClaimable` event.
170170
let chanmon_cfgs = create_chanmon_cfgs(2);
@@ -192,11 +192,95 @@ mod tests {
192192
.build_signed(|hash| secp_ctx.sign_ecdsa_recoverable(hash, &node_secret))
193193
.unwrap();
194194

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+
196206
nodes[0]
197207
.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+
)
199215
.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+
200284
check_added_monitors(&nodes[0], 1);
201285
let send_event = SendEvent::from_node(&nodes[0]);
202286
nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &send_event.msgs[0]);

lightning/src/ln/invoice_utils.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ mod test {
716716
use crate::ln::channelmanager::{Bolt11InvoiceParameters, PhantomRouteHints, MIN_FINAL_CLTV_EXPIRY_DELTA, PaymentId, RecipientOnionFields, Retry};
717717
use crate::ln::functional_test_utils::*;
718718
use crate::ln::msgs::ChannelMessageHandler;
719-
use crate::routing::router::{PaymentParameters, RouteParameters};
719+
use crate::routing::router::{PaymentParameters, RouteParameters, RouteParametersConfig};
720720
use crate::util::test_utils;
721721
use crate::util::config::UserConfig;
722722
use std::collections::HashSet;
@@ -750,7 +750,7 @@ mod test {
750750

751751

752752
#[test]
753-
fn test_from_channelmanager() {
753+
fn create_and_pay_for_bolt11_invoice() {
754754
let chanmon_cfgs = create_chanmon_cfgs(2);
755755
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
756756
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
@@ -784,17 +784,11 @@ mod test {
784784
assert_eq!(invoice.route_hints()[0].0[0].htlc_minimum_msat, chan.inbound_htlc_minimum_msat);
785785
assert_eq!(invoice.route_hints()[0].0[0].htlc_maximum_msat, chan.inbound_htlc_maximum_msat);
786786

787-
let payment_params = PaymentParameters::from_node_id(invoice.recover_payee_pub_key(),
788-
invoice.min_final_cltv_expiry_delta() as u32)
789-
.with_bolt11_features(invoice.features().unwrap().clone()).unwrap()
790-
.with_route_hints(invoice.route_hints()).unwrap();
791-
let route_params = RouteParameters::from_payment_params_and_value(
792-
payment_params, invoice.amount_milli_satoshis().unwrap());
793787
let payment_event = {
794-
let payment_hash = PaymentHash(invoice.payment_hash().to_byte_array());
795-
nodes[0].node.send_payment(payment_hash,
796-
RecipientOnionFields::secret_only(*invoice.payment_secret()),
797-
PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
788+
nodes[0].node.pay_for_bolt11_invoice(
789+
&invoice, PaymentId([42; 32]), None, RouteParametersConfig::default(),
790+
Retry::Attempts(0)
791+
).unwrap();
798792
check_added_monitors(&nodes[0], 1);
799793

800794
let mut events = nodes[0].node.get_and_clear_pending_msg_events();

0 commit comments

Comments
 (0)