|
8 | 8 | #![cfg(cln_test)] |
9 | 9 |
|
10 | 10 | mod common; |
11 | | - |
12 | | -use std::default::Default; |
13 | | -use std::str::FromStr; |
14 | | - |
15 | | -use clightningrpc::lightningrpc::LightningRPC; |
16 | | -use clightningrpc::responses::NetworkAddress; |
17 | | -use electrsd::corepc_client::client_sync::Auth; |
18 | | -use electrsd::corepc_node::Client as BitcoindClient; |
19 | | -use electrum_client::Client as ElectrumClient; |
20 | | -use ldk_node::bitcoin::secp256k1::PublicKey; |
21 | | -use ldk_node::bitcoin::Amount; |
22 | | -use ldk_node::lightning::ln::msgs::SocketAddress; |
23 | | -use ldk_node::{Builder, Event}; |
24 | | -use lightning_invoice::{Bolt11Invoice, Bolt11InvoiceDescription, Description}; |
25 | | -use rand::distr::Alphanumeric; |
26 | | -use rand::{rng, Rng}; |
27 | | - |
28 | | -#[tokio::test(flavor = "multi_thread", worker_threads = 1)] |
29 | | -async fn test_cln() { |
30 | | - // Setup bitcoind / electrs clients |
31 | | - let bitcoind_client = BitcoindClient::new_with_auth( |
32 | | - "http://127.0.0.1:18443", |
33 | | - Auth::UserPass("user".to_string(), "pass".to_string()), |
34 | | - ) |
35 | | - .unwrap(); |
36 | | - let electrs_client = ElectrumClient::new("tcp://127.0.0.1:50001").unwrap(); |
37 | | - |
38 | | - // Give electrs a kick. |
39 | | - common::generate_blocks_and_wait(&bitcoind_client, &electrs_client, 1).await; |
40 | | - |
41 | | - // Setup LDK Node |
42 | | - let config = common::random_config(true); |
43 | | - let mut builder = Builder::from_config(config.node_config); |
44 | | - builder.set_chain_source_electrum("tcp://127.0.0.1:50001".to_string(), None); |
45 | | - let node = builder.build(config.node_entropy).unwrap(); |
46 | | - node.start().unwrap(); |
47 | | - |
48 | | - // Premine some funds and distribute |
49 | | - let address = node.onchain_payment().new_address().unwrap(); |
50 | | - let premine_amount = Amount::from_sat(5_000_000); |
51 | | - common::premine_and_distribute_funds( |
52 | | - &bitcoind_client, |
53 | | - &electrs_client, |
54 | | - vec![address], |
55 | | - premine_amount, |
56 | | - ) |
57 | | - .await; |
58 | | - |
59 | | - // Setup CLN |
60 | | - let sock = "/tmp/lightning-rpc"; |
61 | | - let cln_client = LightningRPC::new(&sock); |
62 | | - let cln_info = { |
63 | | - loop { |
64 | | - let info = cln_client.getinfo().unwrap(); |
65 | | - // Wait for CLN to sync block height before channel open. |
66 | | - // Prevents crash due to unset blockheight (see LDK Node issue #527). |
67 | | - if info.blockheight > 0 { |
68 | | - break info; |
69 | | - } |
70 | | - tokio::time::sleep(std::time::Duration::from_millis(250)).await; |
71 | | - } |
72 | | - }; |
73 | | - let cln_node_id = PublicKey::from_str(&cln_info.id).unwrap(); |
74 | | - let cln_address: SocketAddress = match cln_info.binding.first().unwrap() { |
75 | | - NetworkAddress::Ipv4 { address, port } => { |
76 | | - std::net::SocketAddrV4::new(*address, *port).into() |
77 | | - }, |
78 | | - NetworkAddress::Ipv6 { address, port } => { |
79 | | - std::net::SocketAddrV6::new(*address, *port, 0, 0).into() |
80 | | - }, |
81 | | - _ => { |
82 | | - panic!() |
83 | | - }, |
84 | | - }; |
85 | | - |
86 | | - node.sync_wallets().unwrap(); |
87 | | - |
88 | | - // Open the channel |
89 | | - let funding_amount_sat = 1_000_000; |
90 | | - |
91 | | - node.open_channel(cln_node_id, cln_address, funding_amount_sat, Some(500_000_000), None) |
92 | | - .unwrap(); |
93 | | - |
94 | | - let funding_txo = common::expect_channel_pending_event!(node, cln_node_id); |
95 | | - common::wait_for_tx(&electrs_client, funding_txo.txid).await; |
96 | | - common::generate_blocks_and_wait(&bitcoind_client, &electrs_client, 6).await; |
97 | | - node.sync_wallets().unwrap(); |
98 | | - let user_channel_id = common::expect_channel_ready_event!(node, cln_node_id); |
99 | | - |
100 | | - // Send a payment to CLN |
101 | | - let mut rng = rng(); |
102 | | - let rand_label: String = (0..7).map(|_| rng.sample(Alphanumeric) as char).collect(); |
103 | | - let cln_invoice = |
104 | | - cln_client.invoice(Some(10_000_000), &rand_label, &rand_label, None, None, None).unwrap(); |
105 | | - let parsed_invoice = Bolt11Invoice::from_str(&cln_invoice.bolt11).unwrap(); |
106 | | - |
107 | | - node.bolt11_payment().send(&parsed_invoice, None).unwrap(); |
108 | | - common::expect_event!(node, PaymentSuccessful); |
109 | | - let cln_listed_invoices = |
110 | | - cln_client.listinvoices(Some(&rand_label), None, None, None).unwrap().invoices; |
111 | | - assert_eq!(cln_listed_invoices.len(), 1); |
112 | | - assert_eq!(cln_listed_invoices.first().unwrap().status, "paid"); |
113 | | - |
114 | | - // Send a payment to LDK |
115 | | - let rand_label: String = (0..7).map(|_| rng.sample(Alphanumeric) as char).collect(); |
116 | | - let invoice_description = |
117 | | - Bolt11InvoiceDescription::Direct(Description::new(rand_label).unwrap()); |
118 | | - let ldk_invoice = |
119 | | - node.bolt11_payment().receive(10_000_000, &invoice_description, 3600).unwrap(); |
120 | | - cln_client.pay(&ldk_invoice.to_string(), Default::default()).unwrap(); |
121 | | - common::expect_event!(node, PaymentReceived); |
122 | | - |
123 | | - node.close_channel(&user_channel_id, cln_node_id).unwrap(); |
124 | | - common::expect_event!(node, ChannelClosed); |
125 | | - node.stop().unwrap(); |
126 | | -} |
0 commit comments