Skip to content

Commit 18d7bde

Browse files
committed
Add expect_channel_pending_event and open_channel test utils
1 parent 5f48a55 commit 18d7bde

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

src/test/utils.rs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::builder::NodeBuilder;
22
use crate::io::test_utils::TestSyncStore;
3-
use crate::{Config, Node};
3+
use crate::{Config, Event, Node};
44
use lightning::ln::msgs::SocketAddress;
55
use lightning::util::logger::{Level, Logger, Record};
6+
use lightning::util::persist::KVStore;
67

78
use bitcoin::{Address, Amount, Network, OutPoint, Txid};
89

@@ -26,7 +27,7 @@ macro_rules! expect_event {
2627
($node: expr, $event_type: ident) => {{
2728
match $node.wait_next_event() {
2829
ref e @ Event::$event_type { .. } => {
29-
println!("{} got event {:?}", std::stringify!($node), e);
30+
println!("{} got event {:?}", $node.node_id(), e);
3031
$node.event_handled();
3132
}
3233
ref e => {
@@ -38,6 +39,24 @@ macro_rules! expect_event {
3839

3940
pub(crate) use expect_event;
4041

42+
macro_rules! expect_channel_pending_event {
43+
($node: expr, $counterparty_node_id: expr) => {{
44+
match $node.wait_next_event() {
45+
ref e @ Event::ChannelPending { funding_txo, counterparty_node_id, .. } => {
46+
println!("{} got event {:?}", $node.node_id(), e);
47+
assert_eq!(counterparty_node_id, $counterparty_node_id);
48+
$node.event_handled();
49+
funding_txo
50+
}
51+
ref e => {
52+
panic!("{} got unexpected event!: {:?}", std::stringify!($node), e);
53+
}
54+
}
55+
}};
56+
}
57+
58+
pub(crate) use expect_channel_pending_event;
59+
4160
// Copied over from upstream LDK
4261
#[allow(dead_code)]
4362
pub struct TestLogger {
@@ -162,7 +181,7 @@ pub fn random_config() -> Config {
162181
println!("Setting random LDK listening addresses: {:?}", rand_listening_addresses);
163182
config.listening_addresses = Some(rand_listening_addresses);
164183

165-
config.log_level = Level::Trace;
184+
config.log_level = Level::Gossip;
166185

167186
config
168187
}
@@ -214,6 +233,7 @@ pub(crate) fn setup_node(electrsd: &ElectrsD, config: Config) -> Node<TestSyncSt
214233
}
215234

216235
pub fn generate_blocks_and_wait(bitcoind: &BitcoinD, electrsd: &ElectrsD, num: usize) {
236+
print!("Generating {} blocks...", num);
217237
let cur_height = bitcoind.client.get_block_count().expect("failed to get current block height");
218238
let address = bitcoind
219239
.client
@@ -222,6 +242,8 @@ pub fn generate_blocks_and_wait(bitcoind: &BitcoinD, electrsd: &ElectrsD, num: u
222242
// TODO: expect this Result once the WouldBlock issue is resolved upstream.
223243
let _block_hashes_res = bitcoind.client.generate_to_address(num as u64, &address);
224244
wait_for_block(electrsd, cur_height as usize + num);
245+
print!(" Done!");
246+
println!("\n");
225247
}
226248

227249
pub fn wait_for_block(electrsd: &ElectrsD, min_height: usize) {
@@ -314,3 +336,25 @@ pub fn premine_and_distribute_funds(
314336

315337
generate_blocks_and_wait(bitcoind, electrsd, 1);
316338
}
339+
340+
pub fn open_channel<K: KVStore + Sync + Send>(
341+
node_a: &Node<K>, node_b: &Node<K>, funding_amount_sat: u64, announce: bool,
342+
electrsd: &ElectrsD,
343+
) {
344+
node_a
345+
.connect_open_channel(
346+
node_b.node_id(),
347+
node_b.listening_address().unwrap().into(),
348+
funding_amount_sat,
349+
None,
350+
None,
351+
announce,
352+
)
353+
.unwrap();
354+
assert!(node_a.list_peers().iter().find(|c| { c.node_id == node_b.node_id() }).is_some());
355+
356+
let funding_txo_a = expect_channel_pending_event!(node_a, node_b.node_id());
357+
let funding_txo_b = expect_channel_pending_event!(node_b, node_a.node_id());
358+
assert_eq!(funding_txo_a, funding_txo_b);
359+
wait_for_tx(&electrsd, funding_txo_a.txid);
360+
}

0 commit comments

Comments
 (0)