Skip to content

Commit 949998b

Browse files
committed
WIP: Functional tests for BOLT 12 Offers payment flow
ChannelManager provides utilities to create offers and refunds along with utilities to initiate and request payment for them, respectively. It also manages the payment flow via implementing OffersMessageHandler. Test that functionality, including the resulting event generation.
1 parent 76486bb commit 949998b

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

lightning/src/ln/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ mod monitor_tests;
7373
#[cfg(test)]
7474
#[allow(unused_mut)]
7575
mod shutdown_tests;
76+
#[cfg(test)]
77+
#[allow(unused_mut)]
78+
mod offers_tests;
7679

7780
pub use self::peer_channel_encryptor::LN_MAX_MSG_LEN;
7881

lightning/src/ln/offers_tests.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
//! Functional tests for the BOLT 12 Offers payment flow.
11+
//!
12+
//! [`ChannelManager`] provides utilities to create [`Offer`]s and [`Refund`]s along with utilities
13+
//! to initiate and request payment for them, respectively. It also manages the payment flow via
14+
//! implementing [`OffersMessageHandler`]. This module tests that functionality, including the
15+
//! resulting [`Event`] generation.
16+
17+
use crate::events::MessageSendEventsProvider;
18+
use crate::ln::PaymentHash;
19+
use crate::ln::channelmanager::{PaymentId, Retry};
20+
use crate::ln::functional_test_utils::*;
21+
use crate::ln::msgs::OnionMessageHandler;
22+
23+
use crate::prelude::*;
24+
25+
#[test]
26+
fn pays_for_offer() {
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+
32+
create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
33+
34+
let alice = &nodes[0];
35+
let alice_id = alice.node.get_our_node_id();
36+
let bob = &nodes[1];
37+
let bob_id = bob.node.get_our_node_id();
38+
39+
let offer = alice.node.create_offer_builder("coffee".to_string()).build().unwrap();
40+
41+
let payment_id = PaymentId([1; 32]);
42+
bob.node.pay_for_offer(
43+
&offer, None, Some(10_000_000), None, payment_id, Retry::Attempts(0), None
44+
).unwrap();
45+
46+
let invoice_request = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
47+
alice.onion_messenger.handle_onion_message(&bob_id, &invoice_request);
48+
49+
let invoice = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
50+
bob.onion_messenger.handle_onion_message(&alice_id, &invoice);
51+
52+
let mut events = bob.node.get_and_clear_pending_msg_events();
53+
assert_eq!(events.len(), 1);
54+
let ev = remove_first_msg_event_to_node(&alice_id, &mut events);
55+
56+
// Use a fake payment_hash and bypass checking for the PaymentClaimable event since the invoice
57+
// contains the payment_hash but it is encrypted inside an onion message.
58+
let fake_payment_hash = PaymentHash([0; 32]);
59+
do_pass_along_path(bob, &[alice], 10_000_000, fake_payment_hash, None, ev, false, false, None);
60+
61+
// TODO: Check for PaymentClaimable event to obtain the preimage
62+
63+
// TODO: Claim payment and check for PaymentSent event
64+
}

0 commit comments

Comments
 (0)