Skip to content

Commit

Permalink
WIP: Functional tests for BOLT 12 Offers payment flow
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jkczyz committed Oct 31, 2023
1 parent 76486bb commit ffca644
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lightning/src/ln/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ mod monitor_tests;
#[cfg(test)]
#[allow(unused_mut)]
mod shutdown_tests;
#[cfg(test)]
#[allow(unused_mut)]
mod offers_tests;

pub use self::peer_channel_encryptor::LN_MAX_MSG_LEN;

Expand Down
107 changes: 107 additions & 0 deletions lightning/src/ln/offers_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// This file is Copyright its original authors, visible in version control
// history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.

//! Functional tests for the BOLT 12 Offers payment flow.
//!
//! [`ChannelManager`] provides utilities to create [`Offer`]s and [`Refund`]s along with utilities
//! to initiate and request payment for them, respectively. It also manages the payment flow via
//! implementing [`OffersMessageHandler`]. This module tests that functionality, including the
//! resulting [`Event`] generation.

use crate::events::{Event, MessageSendEventsProvider, PaymentPurpose};
use crate::ln::PaymentHash;
use crate::ln::channelmanager::{PaymentId, RecentPaymentDetails, Retry};
use crate::ln::functional_test_utils::*;
use crate::ln::msgs::OnionMessageHandler;

use crate::prelude::*;

macro_rules! expect_recent_payment {
($node: expr, $payment_state: path, $payment_id: expr) => {
match $node.node.list_recent_payments().first() {
Some(&$payment_state { payment_id: actual_payment_id, .. }) => {
assert_eq!($payment_id, actual_payment_id);
},
Some(_) => panic!("Unexpected recent payment state"),
None => panic!("No recent payments"),
}
}
}

macro_rules! route_payment {
($node: expr, $path: expr, $amount_msats: expr) => {
{
// Monitor added when handling the invoice onion message.
check_added_monitors($node, 1);

// Use a fake payment_hash and bypass checking for the PaymentClaimable event since the
// invoice contains the payment_hash but it was encrypted inside an onion message.
let fake_payment_hash = PaymentHash([0; 32]);

let mut events = $node.node.get_and_clear_pending_msg_events();
assert_eq!(events.len(), 1);
let ev = remove_first_msg_event_to_node(&$path[0].node.get_our_node_id(), &mut events);

do_pass_along_path(
$node, $path, $amount_msats, fake_payment_hash, None, ev, false, false, None
);
}
}
}

macro_rules! claim_payment {
($node: expr, $path: expr) => {
{
let recipient = &$path[$path.len() - 1];
match get_event!(recipient, Event::PaymentClaimable) {
Event::PaymentClaimable {
purpose: PaymentPurpose::InvoicePayment {
payment_preimage: Some(payment_preimage), ..
}, ..
} => claim_payment($node, $path, payment_preimage),
_ => panic!(),
};
}
}
}

#[test]
fn pays_for_offer() {
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]);
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);

create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);

let alice = &nodes[0];
let alice_id = alice.node.get_our_node_id();
let bob = &nodes[1];
let bob_id = bob.node.get_our_node_id();

let offer = alice.node.create_offer_builder("coffee".to_string()).build().unwrap();

let payment_id = PaymentId([1; 32]);
bob.node.pay_for_offer(
&offer, None, Some(10_000_000), None, payment_id, Retry::Attempts(0), None
).unwrap();
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);

let invoice_request = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
alice.onion_messenger.handle_onion_message(&bob_id, &invoice_request);

let invoice = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
bob.onion_messenger.handle_onion_message(&alice_id, &invoice);

route_payment!(bob, &[alice], 10_000_000);
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);

claim_payment!(bob, &[alice]);
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
}

0 comments on commit ffca644

Please sign in to comment.