Skip to content

Commit

Permalink
Add ChainSource support to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Oct 10, 2024
1 parent 9dc7a7c commit 107edc6
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 24 deletions.
72 changes: 63 additions & 9 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use rand::{thread_rng, Rng};

use std::env;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::{Arc, RwLock};
use std::time::Duration;

Expand Down Expand Up @@ -241,6 +242,12 @@ type TestNode = Arc<Node>;
#[cfg(not(feature = "uniffi"))]
type TestNode = Node;

#[derive(Clone)]
pub(crate) enum TestChainSource<'a> {
Esplora(&'a ElectrsD),
BitcoinRpc(&'a BitcoinD),
}

macro_rules! setup_builder {
($builder: ident, $config: expr) => {
#[cfg(feature = "uniffi")]
Expand All @@ -253,11 +260,12 @@ macro_rules! setup_builder {
pub(crate) use setup_builder;

pub(crate) fn setup_two_nodes(
electrsd: &ElectrsD, allow_0conf: bool, anchor_channels: bool, anchors_trusted_no_reserve: bool,
chain_source: &TestChainSource, allow_0conf: bool, anchor_channels: bool,
anchors_trusted_no_reserve: bool,
) -> (TestNode, TestNode) {
println!("== Node A ==");
let config_a = random_config(anchor_channels);
let node_a = setup_node(electrsd, config_a);
let node_a = setup_node(chain_source, config_a);

println!("\n== Node B ==");
let mut config_b = random_config(anchor_channels);
Expand All @@ -272,17 +280,29 @@ pub(crate) fn setup_two_nodes(
.trusted_peers_no_reserve
.push(node_a.node_id());
}
let node_b = setup_node(electrsd, config_b);
let node_b = setup_node(chain_source, config_b);
(node_a, node_b)
}

pub(crate) fn setup_node(electrsd: &ElectrsD, config: Config) -> TestNode {
let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());
let mut sync_config = EsploraSyncConfig::default();
sync_config.onchain_wallet_sync_interval_secs = 100000;
sync_config.lightning_wallet_sync_interval_secs = 100000;
pub(crate) fn setup_node(chain_source: &TestChainSource, config: Config) -> TestNode {
setup_builder!(builder, config);
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));
match chain_source {
TestChainSource::Esplora(electrsd) => {
let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());
let mut sync_config = EsploraSyncConfig::default();
sync_config.onchain_wallet_sync_interval_secs = 100000;
sync_config.lightning_wallet_sync_interval_secs = 100000;
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));
},
TestChainSource::BitcoinRpc(bitcoind) => {
let rpc_host = bitcoind.params.rpc_socket.ip().to_string();
let rpc_port = bitcoind.params.rpc_socket.port();
let values = bitcoind.params.get_cookie_values().unwrap().unwrap();
let rpc_user = values.user;
let rpc_password = values.password;
builder.set_chain_source_bitcoind_rpc(rpc_host, rpc_port, rpc_user, rpc_password);
},
}
let test_sync_store = Arc::new(TestSyncStore::new(config.storage_dir_path.into()));
let node = builder.build_with_store(test_sync_store).unwrap();
node.start().unwrap();
Expand Down Expand Up @@ -382,6 +402,40 @@ where
}
}

// Premine dummy blocks to populate estimatesmartfee
pub(crate) fn premine_dummy_blocks<E: ElectrumApi>(bitcoind: &BitcoindClient, electrs: &E) {
let _ = bitcoind.create_wallet("ldk_node_test", None, None, None, None);
let _ = bitcoind.load_wallet("ldk_node_test");
let mut rng = thread_rng();

generate_blocks_and_wait(bitcoind, electrs, 101);

let amount = Amount::from_sat(100000);
for i in 0..5 {
println!("Pre-mining block {}", i);
let num_txs = rng.gen_range(10..42);
let mut last_txid = None;
for _ in 10..num_txs {
let dummy_address: Address =
Address::from_str("bcrt1qh3mvjaldwxynmmwsmx4az4vdg5yj7sjzjpdga5")
.unwrap()
.require_network(Network::Regtest)
.unwrap();
let txid = bitcoind
.send_to_address(&dummy_address, amount, None, None, None, None, Some(1), None)
.unwrap();
println!("Created dummy transaction {}", txid);
last_txid = Some(txid);
}
if let Some(last_txid) = last_txid {
wait_for_tx(electrs, last_txid);
}
generate_blocks_and_wait(bitcoind, electrs, 1);
}

generate_blocks_and_wait(bitcoind, electrs, 1);
}

pub(crate) fn premine_and_distribute_funds<E: ElectrumApi>(
bitcoind: &BitcoindClient, electrs: &E, addrs: Vec<Address>, amount: Amount,
) {
Expand Down
54 changes: 39 additions & 15 deletions tests/integration_tests_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ mod common;
use common::{
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,
premine_and_distribute_funds, premine_dummy_blocks, random_config, setup_bitcoind_and_electrsd,
setup_builder, setup_node, setup_two_nodes, wait_for_tx, TestChainSource, TestSyncStore,
};

use ldk_node::config::EsploraSyncConfig;
Expand All @@ -22,48 +22,65 @@ use lightning::ln::channelmanager::PaymentId;
use lightning::util::persist::KVStore;

use bitcoin::{Amount, Network};
use bitcoincore_rpc::RpcApi;

use std::sync::Arc;

#[test]
fn channel_full_cycle() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);
let chain_source = TestChainSource::Esplora(&electrsd);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, false);
}

#[test]
fn channel_full_cycle_bitcoind() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
premine_dummy_blocks(&bitcoind.client, &electrsd.client);
println!("RESULT: {:?}", bitcoind.client.estimate_smart_fee(1, None));
let chain_source = TestChainSource::BitcoinRpc(&bitcoind);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, false);
}

#[test]
fn channel_full_cycle_force_close() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);
let chain_source = TestChainSource::Esplora(&electrsd);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, true);
}

#[test]
fn channel_full_cycle_force_close_trusted_no_reserve() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, true);
let chain_source = TestChainSource::Esplora(&electrsd);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, true);
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, true);
}

#[test]
fn channel_full_cycle_0conf() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let (node_a, node_b) = setup_two_nodes(&electrsd, true, true, false);
let chain_source = TestChainSource::Esplora(&electrsd);
let (node_a, node_b) = setup_two_nodes(&chain_source, true, true, false);
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, true, true, false)
}

#[test]
fn channel_full_cycle_legacy_staticremotekey() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let (node_a, node_b) = setup_two_nodes(&electrsd, false, false, false);
let chain_source = TestChainSource::Esplora(&electrsd);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, false, false);
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, false, false);
}

#[test]
fn channel_open_fails_when_funds_insufficient() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);
let chain_source = TestChainSource::Esplora(&electrsd);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);

let addr_a = node_a.onchain_payment().new_address().unwrap();
let addr_b = node_b.onchain_payment().new_address().unwrap();
Expand Down Expand Up @@ -266,7 +283,8 @@ fn start_stop_reinit() {
#[test]
fn onchain_spend_receive() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);
let chain_source = TestChainSource::Esplora(&electrsd);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);

let addr_a = node_a.onchain_payment().new_address().unwrap();
let addr_b = node_b.onchain_payment().new_address().unwrap();
Expand Down Expand Up @@ -315,7 +333,8 @@ fn onchain_spend_receive() {
fn sign_verify_msg() {
let (_bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let config = random_config(true);
let node = setup_node(&electrsd, config);
let chain_source = TestChainSource::Esplora(&electrsd);
let node = setup_node(&chain_source, config);

// Tests arbitrary message signing and later verification
let msg = "OK computer".as_bytes();
Expand All @@ -332,7 +351,8 @@ fn connection_restart_behavior() {

fn do_connection_restart_behavior(persist: bool) {
let (_bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let (node_a, node_b) = setup_two_nodes(&electrsd, false, false, false);
let chain_source = TestChainSource::Esplora(&electrsd);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, false, false);

let node_id_a = node_a.node_id();
let node_id_b = node_b.node_id();
Expand Down Expand Up @@ -383,7 +403,8 @@ fn do_connection_restart_behavior(persist: bool) {
#[test]
fn concurrent_connections_succeed() {
let (_bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);
let chain_source = TestChainSource::Esplora(&electrsd);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);

let node_a = Arc::new(node_a);
let node_b = Arc::new(node_b);
Expand Down Expand Up @@ -413,7 +434,8 @@ fn concurrent_connections_succeed() {
#[test]
fn simple_bolt12_send_receive() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);
let chain_source = TestChainSource::Esplora(&electrsd);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);

let address_a = node_a.onchain_payment().new_address().unwrap();
let premine_amount_sat = 5_000_000;
Expand Down Expand Up @@ -620,7 +642,8 @@ fn simple_bolt12_send_receive() {
#[test]
fn generate_bip21_uri() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);
let chain_source = TestChainSource::Esplora(&electrsd);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);

let address_a = node_a.onchain_payment().new_address().unwrap();
let premined_sats = 5_000_000;
Expand Down Expand Up @@ -661,7 +684,8 @@ fn generate_bip21_uri() {
#[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 chain_source = TestChainSource::Esplora(&electrsd);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);

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

0 comments on commit 107edc6

Please sign in to comment.