Skip to content

Commit

Permalink
Add Unified QR send/receive integration tests
Browse files Browse the repository at this point in the history
  - Added `unified_qr_send_receive` test to verify the `UnifedQrPayment`
    functionality
  - Added logic to handle paying a `BOLT12` offer, `BOLT11` invoice,
    and if those fail `On-chain` tx from a URI.
  - Validated each payments successful event
  - Ensured the off-chain and on-chain balacnes reflected the payment
    attempts
  • Loading branch information
slanesuke committed Jul 1, 2024
1 parent 02cd06e commit 5967e14
Showing 1 changed file with 120 additions and 4 deletions.
124 changes: 120 additions & 4 deletions tests/integration_tests_rust.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
mod common;

use common::{
do_channel_full_cycle, expect_event, expect_payment_received_event,
do_channel_full_cycle, expect_channel_ready_event, expect_event, expect_payment_received_event,
expect_payment_successful_event, generate_blocks_and_wait, open_channel,
premine_and_distribute_funds, random_config, setup_bitcoind_and_electrsd, setup_builder,
setup_node, setup_two_nodes, wait_for_tx, TestSyncStore,
};

use ldk_node::payment::PaymentKind;
use ldk_node::payment::{PaymentKind, PaymentResult};
use ldk_node::{Builder, Event, NodeError};

use lightning::ln::channelmanager::PaymentId;
Expand All @@ -17,8 +17,6 @@ use bitcoin::{Amount, Network};

use std::sync::Arc;

use crate::common::expect_channel_ready_event;

#[test]
fn channel_full_cycle() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
Expand Down Expand Up @@ -552,3 +550,121 @@ fn simple_bolt12_send_receive() {
}
assert_eq!(node_a_payments.first().unwrap().amount_msat, Some(overpaid_amount));
}

#[test]
fn unified_qr_send_receive() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);

let address_a = node_a.onchain_payment().new_address().unwrap();
let premined_sats = 5_000_000;

premine_and_distribute_funds(
&bitcoind.client,
&electrsd.client,
vec![address_a],
Amount::from_sat(premined_sats),
);

node_a.sync_wallets().unwrap();
open_channel(&node_a, &node_b, 4_000_000, true, &electrsd);
generate_blocks_and_wait(&bitcoind.client, &electrsd.client, 6);

node_a.sync_wallets().unwrap();
node_b.sync_wallets().unwrap();

expect_channel_ready_event!(node_a, node_b.node_id());
expect_channel_ready_event!(node_b, node_a.node_id());

// Sleep until we broadcast a node announcement.
while node_b.status().latest_node_announcement_broadcast_timestamp.is_none() {
std::thread::sleep(std::time::Duration::from_millis(10));
}

// Sleep one more sec to make sure the node announcement propagates.
std::thread::sleep(std::time::Duration::from_secs(1));

let expected_amount_msats = 100_000_000;
let offer = node_b.bolt12_payment().receive(expected_amount_msats, "hi");

let offer_str = offer.clone().unwrap().to_string();
let bolt12_offer_param = format!("&lightning={}", offer_str);

let expected_amount_sats = 100_000;
let message = "TestMessage".to_string();
let expiry_sec = 4_000;

let uqr_payment =
node_b.unified_qr_payment().receive(expected_amount_sats, Some(message), expiry_sec);

let uri_str = uqr_payment.clone().unwrap();
let uri_with_offer = format!("{}{}", uri_str, bolt12_offer_param);

let offer_payment_id: PaymentId = match node_a.unified_qr_payment().send(&uri_with_offer) {
Ok(PaymentResult::Bolt12 { payment_id }) => {
println!("\nBolt12 payment sent successfully with PaymentID: {:?}", payment_id);
payment_id
},
Ok(PaymentResult::Bolt11 { payment_id: _ }) => {
panic!("Expected Bolt12 payment but got Bolt11");
},
Ok(PaymentResult::Onchain { txid: _ }) => {
panic!("Expected Bolt12 payment but get On-chain transaction");
},
Err(e) => {
panic!("Expected Bolt12 payment but got error: {:?}", e);
},
};

expect_payment_successful_event!(node_a, Some(offer_payment_id), None);

let uri_with_invalid_offer = format!("{}{}", uri_str, "&lightning=some_invalid_offer");
let invoice_payment_id: PaymentId =
match node_a.unified_qr_payment().send(&uri_with_invalid_offer) {
Ok(PaymentResult::Bolt12 { payment_id: _ }) => {
panic!("Expected Bolt11 payment but got Bolt12");
},
Ok(PaymentResult::Bolt11 { payment_id }) => {
println!("\nBolt11 payment sent successfully with PaymentID: {:?}", payment_id);
payment_id
},
Ok(PaymentResult::Onchain { txid: _ }) => {
panic!("Expected Bolt11 payment but got on-chain transaction");
},
Err(e) => {
panic!("Expected Bolt11 payment but got error: {:?}", e);
},
};
expect_payment_successful_event!(node_a, Some(invoice_payment_id), None);

let expect_onchain_amount_sats = 800_000;
let onchain_uqr_payment = node_b
.unified_qr_payment()
.receive(expect_onchain_amount_sats, Some("Different Message".to_string()), 4_000)
.unwrap();

let txid = match node_a.unified_qr_payment().send(onchain_uqr_payment.as_str()) {
Ok(PaymentResult::Bolt12 { payment_id: _ }) => {
panic!("Expected on-chain payment but got Bolt12")
},
Ok(PaymentResult::Bolt11 { payment_id: _ }) => {
panic!("Expected on-chain payment but got Bolt11");
},
Ok(PaymentResult::Onchain { txid }) => {
println!("\nOn-chain transaction successful with Txid: {}", txid);
txid
},
Err(e) => {
panic!("Expected on-chain payment but got error: {:?}", e);
},
};

generate_blocks_and_wait(&bitcoind.client, &electrsd.client, 6);
wait_for_tx(&electrsd.client, txid);

node_a.sync_wallets().unwrap();
node_b.sync_wallets().unwrap();

assert_eq!(node_b.list_balances().total_onchain_balance_sats, 800_000);
assert_eq!(node_b.list_balances().total_lightning_balance_sats, 200_000);
}

0 comments on commit 5967e14

Please sign in to comment.