Skip to content

Commit 878892e

Browse files
authored
Merge pull request #198 from tnull/2023-11-test-utils
Allow GOSSIP logging and add more test utils
2 parents beb2345 + 554a520 commit 878892e

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@ panic = 'abort' # Abort on panic
2828
default = []
2929

3030
[dependencies]
31-
lightning = { version = "0.0.118", features = ["max_level_trace", "std"] }
31+
lightning = { version = "0.0.118", features = ["std"] }
3232
lightning-invoice = { version = "0.26.0" }
3333
lightning-net-tokio = { version = "0.0.118" }
3434
lightning-persister = { version = "0.0.118" }
3535
lightning-background-processor = { version = "0.0.118", features = ["futures"] }
3636
lightning-rapid-gossip-sync = { version = "0.0.118" }
3737
lightning-transaction-sync = { version = "0.0.118", features = ["esplora-async-https"] }
3838

39-
# lightning = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main", features = ["max_level_trace", "std"] }
39+
# lightning = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main", features = ["std"] }
4040
# lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main" }
4141
# lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main" }
4242
# lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main" }
4343
# lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main", features = ["futures"] }
4444
# lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main" }
4545
# lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main", features = ["esplora-async"] }
4646

47-
#lightning = { path = "../rust-lightning/lightning", features = ["max_level_trace", "std"] }
47+
#lightning = { path = "../rust-lightning/lightning", features = ["std"] }
4848
#lightning-invoice = { path = "../rust-lightning/lightning-invoice" }
4949
#lightning-net-tokio = { path = "../rust-lightning/lightning-net-tokio" }
5050
#lightning-persister = { path = "../rust-lightning/lightning-persister" }
@@ -72,8 +72,8 @@ vss-client = "0.1"
7272
winapi = { version = "0.3", features = ["winbase"] }
7373

7474
[dev-dependencies]
75-
lightning = { version = "0.0.118", features = ["max_level_trace", "std", "_test_utils"] }
76-
#lightning = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main", features = ["max_level_trace", "std", "_test_utils"] }
75+
lightning = { version = "0.0.118", features = ["std", "_test_utils"] }
76+
#lightning = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main", features = ["std", "_test_utils"] }
7777
electrsd = { version = "0.22.0", features = ["legacy", "esplora_a33e97e1", "bitcoind_23_0"] }
7878
electrum-client = "0.12.0"
7979
proptest = "1.0.0"

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_addresses().unwrap().first().unwrap().clone(),
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)