Skip to content

Commit 296d78f

Browse files
committed
Rename bolt11_payment.rs -> bolt11_payment_tests.rs
1 parent 1d0c656 commit 296d78f

File tree

3 files changed

+160
-161
lines changed

3 files changed

+160
-161
lines changed

lightning/src/ln/bolt11_payment.rs

-160
This file was deleted.
+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
//! Tests for verifying the correct end-to-end handling of BOLT11 payments, including metadata propagation.
11+
12+
use crate::events::Event;
13+
use crate::ln::channelmanager::{PaymentId, Retry};
14+
use crate::ln::functional_test_utils::*;
15+
use crate::ln::msgs::ChannelMessageHandler;
16+
use crate::ln::outbound_payment::Bolt11PaymentError;
17+
use crate::routing::router::RouteParametersConfig;
18+
use bitcoin::hashes::{sha256::Hash as Sha256, Hash};
19+
use bitcoin::secp256k1::Secp256k1;
20+
use lightning_invoice::{Currency, InvoiceBuilder};
21+
use std::time::SystemTime;
22+
23+
#[test]
24+
fn payment_metadata_end_to_end_for_invoice_with_amount() {
25+
// Test that a payment metadata read from an invoice passed to `pay_invoice` makes it all
26+
// the way out through the `PaymentClaimable` event.
27+
let chanmon_cfgs = create_chanmon_cfgs(2);
28+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
29+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
30+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
31+
create_announced_chan_between_nodes(&nodes, 0, 1);
32+
33+
let payment_metadata = vec![42, 43, 44, 45, 46, 47, 48, 49, 42];
34+
35+
let (payment_hash, payment_secret) =
36+
nodes[1].node.create_inbound_payment(None, 7200, None).unwrap();
37+
38+
let secp_ctx = Secp256k1::new();
39+
let node_secret = nodes[1].keys_manager.backing.get_node_secret_key();
40+
let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
41+
let invoice = InvoiceBuilder::new(Currency::Bitcoin)
42+
.description("test".into())
43+
.payment_hash(Sha256::from_slice(&payment_hash.0).unwrap())
44+
.payment_secret(payment_secret)
45+
.duration_since_epoch(timestamp)
46+
.min_final_cltv_expiry_delta(144)
47+
.amount_milli_satoshis(50_000)
48+
.payment_metadata(payment_metadata.clone())
49+
.build_signed(|hash| secp_ctx.sign_ecdsa_recoverable(hash, &node_secret))
50+
.unwrap();
51+
52+
match nodes[0].node.pay_for_bolt11_invoice(
53+
&invoice,
54+
PaymentId(payment_hash.0),
55+
Some(100),
56+
RouteParametersConfig::default(),
57+
Retry::Attempts(0),
58+
) {
59+
Err(Bolt11PaymentError::InvalidAmount) => (),
60+
_ => panic!("Unexpected result"),
61+
};
62+
63+
nodes[0]
64+
.node
65+
.pay_for_bolt11_invoice(
66+
&invoice,
67+
PaymentId(payment_hash.0),
68+
None,
69+
RouteParametersConfig::default(),
70+
Retry::Attempts(0),
71+
)
72+
.unwrap();
73+
74+
check_added_monitors(&nodes[0], 1);
75+
let send_event = SendEvent::from_node(&nodes[0]);
76+
nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &send_event.msgs[0]);
77+
commitment_signed_dance!(nodes[1], nodes[0], &send_event.commitment_msg, false);
78+
79+
expect_pending_htlcs_forwardable!(nodes[1]);
80+
81+
let mut events = nodes[1].node.get_and_clear_pending_events();
82+
assert_eq!(events.len(), 1);
83+
match events.pop().unwrap() {
84+
Event::PaymentClaimable { onion_fields, .. } => {
85+
assert_eq!(Some(payment_metadata), onion_fields.unwrap().payment_metadata);
86+
},
87+
_ => panic!("Unexpected event"),
88+
}
89+
}
90+
91+
#[test]
92+
fn payment_metadata_end_to_end_for_invoice_with_no_amount() {
93+
// Test that a payment metadata read from an invoice passed to `pay_invoice` makes it all
94+
// the way out through the `PaymentClaimable` event.
95+
let chanmon_cfgs = create_chanmon_cfgs(2);
96+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
97+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
98+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
99+
create_announced_chan_between_nodes(&nodes, 0, 1);
100+
101+
let payment_metadata = vec![42, 43, 44, 45, 46, 47, 48, 49, 42];
102+
103+
let (payment_hash, payment_secret) =
104+
nodes[1].node.create_inbound_payment(None, 7200, None).unwrap();
105+
106+
let secp_ctx = Secp256k1::new();
107+
let node_secret = nodes[1].keys_manager.backing.get_node_secret_key();
108+
let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
109+
let invoice = InvoiceBuilder::new(Currency::Bitcoin)
110+
.description("test".into())
111+
.payment_hash(Sha256::from_slice(&payment_hash.0).unwrap())
112+
.payment_secret(payment_secret)
113+
.duration_since_epoch(timestamp)
114+
.min_final_cltv_expiry_delta(144)
115+
.payment_metadata(payment_metadata.clone())
116+
.build_signed(|hash| secp_ctx.sign_ecdsa_recoverable(hash, &node_secret))
117+
.unwrap();
118+
119+
match nodes[0].node.pay_for_bolt11_invoice(
120+
&invoice,
121+
PaymentId(payment_hash.0),
122+
None,
123+
RouteParametersConfig::default(),
124+
Retry::Attempts(0),
125+
) {
126+
Err(Bolt11PaymentError::InvalidAmount) => (),
127+
_ => panic!("Unexpected result"),
128+
};
129+
130+
nodes[0]
131+
.node
132+
.pay_for_bolt11_invoice(
133+
&invoice,
134+
PaymentId(payment_hash.0),
135+
Some(50_000),
136+
RouteParametersConfig::default(),
137+
Retry::Attempts(0),
138+
)
139+
.unwrap();
140+
141+
check_added_monitors(&nodes[0], 1);
142+
let send_event = SendEvent::from_node(&nodes[0]);
143+
nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &send_event.msgs[0]);
144+
commitment_signed_dance!(nodes[1], nodes[0], &send_event.commitment_msg, false);
145+
146+
expect_pending_htlcs_forwardable!(nodes[1]);
147+
148+
let mut events = nodes[1].node.get_and_clear_pending_events();
149+
assert_eq!(events.len(), 1);
150+
match events.pop().unwrap() {
151+
Event::PaymentClaimable { onion_fields, .. } => {
152+
assert_eq!(Some(payment_metadata), onion_fields.unwrap().payment_metadata);
153+
},
154+
_ => panic!("Unexpected event"),
155+
}
156+
}

lightning/src/ln/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ pub mod types;
2828
// TODO: These modules were moved from lightning-invoice and need to be better integrated into this
2929
// crate now:
3030
pub mod invoice_utils;
31-
pub mod bolt11_payment;
3231

3332
#[cfg(fuzzing)]
3433
pub mod peer_channel_encryptor;
@@ -52,6 +51,10 @@ pub use onion_utils::create_payment_onion;
5251
// without the node parameter being mut. This is incorrect, and thus newer rustcs will complain
5352
// about an unnecessary mut. Thus, we silence the unused_mut warning in two test modules below.
5453

54+
#[cfg(test)]
55+
#[allow(unused_mut)]
56+
pub mod bolt11_payment_tests;
57+
5558
#[cfg(test)]
5659
#[allow(unused_mut)]
5760
mod blinded_payment_tests;

0 commit comments

Comments
 (0)