Skip to content

Commit

Permalink
Merge pull request #198 from tnull/2023-11-test-utils
Browse files Browse the repository at this point in the history
Allow GOSSIP logging and add more test utils
  • Loading branch information
tnull committed Nov 16, 2023
2 parents beb2345 + 554a520 commit 878892e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ panic = 'abort' # Abort on panic
default = []

[dependencies]
lightning = { version = "0.0.118", features = ["max_level_trace", "std"] }
lightning = { version = "0.0.118", features = ["std"] }
lightning-invoice = { version = "0.26.0" }
lightning-net-tokio = { version = "0.0.118" }
lightning-persister = { version = "0.0.118" }
lightning-background-processor = { version = "0.0.118", features = ["futures"] }
lightning-rapid-gossip-sync = { version = "0.0.118" }
lightning-transaction-sync = { version = "0.0.118", features = ["esplora-async-https"] }

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

#lightning = { path = "../rust-lightning/lightning", features = ["max_level_trace", "std"] }
#lightning = { path = "../rust-lightning/lightning", features = ["std"] }
#lightning-invoice = { path = "../rust-lightning/lightning-invoice" }
#lightning-net-tokio = { path = "../rust-lightning/lightning-net-tokio" }
#lightning-persister = { path = "../rust-lightning/lightning-persister" }
Expand Down Expand Up @@ -72,8 +72,8 @@ vss-client = "0.1"
winapi = { version = "0.3", features = ["winbase"] }

[dev-dependencies]
lightning = { version = "0.0.118", features = ["max_level_trace", "std", "_test_utils"] }
#lightning = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main", features = ["max_level_trace", "std", "_test_utils"] }
lightning = { version = "0.0.118", features = ["std", "_test_utils"] }
#lightning = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main", features = ["std", "_test_utils"] }
electrsd = { version = "0.22.0", features = ["legacy", "esplora_a33e97e1", "bitcoind_23_0"] }
electrum-client = "0.12.0"
proptest = "1.0.0"
Expand Down
50 changes: 47 additions & 3 deletions src/test/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::builder::NodeBuilder;
use crate::io::test_utils::TestSyncStore;
use crate::{Config, Node};
use crate::{Config, Event, Node};
use lightning::ln::msgs::SocketAddress;
use lightning::util::logger::{Level, Logger, Record};
use lightning::util::persist::KVStore;

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

Expand All @@ -26,7 +27,7 @@ macro_rules! expect_event {
($node: expr, $event_type: ident) => {{
match $node.wait_next_event() {
ref e @ Event::$event_type { .. } => {
println!("{} got event {:?}", std::stringify!($node), e);
println!("{} got event {:?}", $node.node_id(), e);
$node.event_handled();
}
ref e => {
Expand All @@ -38,6 +39,24 @@ macro_rules! expect_event {

pub(crate) use expect_event;

macro_rules! expect_channel_pending_event {
($node: expr, $counterparty_node_id: expr) => {{
match $node.wait_next_event() {
ref e @ Event::ChannelPending { funding_txo, counterparty_node_id, .. } => {
println!("{} got event {:?}", $node.node_id(), e);
assert_eq!(counterparty_node_id, $counterparty_node_id);
$node.event_handled();
funding_txo
}
ref e => {
panic!("{} got unexpected event!: {:?}", std::stringify!($node), e);
}
}
}};
}

pub(crate) use expect_channel_pending_event;

// Copied over from upstream LDK
#[allow(dead_code)]
pub struct TestLogger {
Expand Down Expand Up @@ -162,7 +181,7 @@ pub fn random_config() -> Config {
println!("Setting random LDK listening addresses: {:?}", rand_listening_addresses);
config.listening_addresses = Some(rand_listening_addresses);

config.log_level = Level::Trace;
config.log_level = Level::Gossip;

config
}
Expand Down Expand Up @@ -214,6 +233,7 @@ pub(crate) fn setup_node(electrsd: &ElectrsD, config: Config) -> Node<TestSyncSt
}

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

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

generate_blocks_and_wait(bitcoind, electrsd, 1);
}

pub fn open_channel<K: KVStore + Sync + Send>(
node_a: &Node<K>, node_b: &Node<K>, funding_amount_sat: u64, announce: bool,
electrsd: &ElectrsD,
) {
node_a
.connect_open_channel(
node_b.node_id(),
node_b.listening_addresses().unwrap().first().unwrap().clone(),
funding_amount_sat,
None,
None,
announce,
)
.unwrap();
assert!(node_a.list_peers().iter().find(|c| { c.node_id == node_b.node_id() }).is_some());

let funding_txo_a = expect_channel_pending_event!(node_a, node_b.node_id());
let funding_txo_b = expect_channel_pending_event!(node_b, node_a.node_id());
assert_eq!(funding_txo_a, funding_txo_b);
wait_for_tx(&electrsd, funding_txo_a.txid);
}

0 comments on commit 878892e

Please sign in to comment.