Skip to content

Commit

Permalink
DROPME: Separate premining from distributing funds
Browse files Browse the repository at this point in the history
BDK currently panics if we start syncing the node while the
tip height is still 0, which happens in our usual regtest setup.

Here, we make sure to premine *before* starting up the nodes to avoid
hitting this panic. While splitting premining and distributing funds in
this way is fine (and even makes sense) in most cases, in some cases
it's annoying as we wouldn't previously premine at all, but are now
required to. So we should eventually drop/revert (at least this part of)
this workaround as soon as BDK ships the fix on their end
(bitcoindevkit/bdk#1601).
  • Loading branch information
tnull committed Sep 13, 2024
1 parent 9e98c5f commit 9db3046
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
13 changes: 12 additions & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ pub(crate) fn setup_bitcoind_and_electrsd() -> (BitcoinD, ElectrsD) {
electrsd_conf.http_enabled = true;
electrsd_conf.network = "regtest";
let electrsd = ElectrsD::with_conf(electrs_exe, &bitcoind, &electrsd_conf).unwrap();

generate_blocks_and_wait(&bitcoind.client, &electrsd.client, 101);
(bitcoind, electrsd)
}

Expand Down Expand Up @@ -371,10 +373,19 @@ where
pub(crate) fn premine_and_distribute_funds<E: ElectrumApi>(
bitcoind: &BitcoindClient, electrs: &E, addrs: Vec<Address>, amount: Amount,
) {
premine(bitcoind, electrs);
distribute_funds(bitcoind, electrs, addrs, amount);
}

pub(crate) fn premine<E: ElectrumApi>(bitcoind: &BitcoindClient, electrs: &E) {
let _ = bitcoind.create_wallet("ldk_node_test", None, None, None, None);
let _ = bitcoind.load_wallet("ldk_node_test");
generate_blocks_and_wait(bitcoind, electrs, 101);
}

pub(crate) fn distribute_funds<E: ElectrumApi>(
bitcoind: &BitcoindClient, electrs: &E, addrs: Vec<Address>, amount: Amount,
) {
for addr in addrs {
let txid =
bitcoind.send_to_address(&addr, amount, None, None, None, None, None, None).unwrap();
Expand Down Expand Up @@ -415,7 +426,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(

let premine_amount_sat = if expect_anchor_channel { 2_125_000 } else { 2_100_000 };

premine_and_distribute_funds(
distribute_funds(
&bitcoind,
electrsd,
vec![addr_a, addr_b],
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests_cln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn test_cln() {
let electrs_client = ElectrumClient::new("tcp://127.0.0.1:50001").unwrap();

// Give electrs a kick.
common::generate_blocks_and_wait(&bitcoind_client, &electrs_client, 1);
common::generate_blocks_and_wait(&bitcoind_client, &electrs_client, 101);

// Setup LDK Node
let config = common::random_config(true);
Expand Down
63 changes: 44 additions & 19 deletions tests/integration_tests_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
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,
distribute_funds, 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, random_config, setup_bitcoind_and_electrsd, setup_builder, setup_node,
setup_two_nodes, wait_for_tx, TestSyncStore,
};

use ldk_node::payment::{PaymentKind, QrPaymentResult, SendingParameters};
Expand All @@ -27,49 +27,61 @@ use std::sync::Arc;
#[test]
fn channel_full_cycle() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
premine(&bitcoind.client, &electrsd.client);

let (node_a, node_b) = setup_two_nodes(&electrsd, 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();
premine(&bitcoind.client, &electrsd.client);

let (node_a, node_b) = setup_two_nodes(&electrsd, 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();
premine(&bitcoind.client, &electrsd.client);

let (node_a, node_b) = setup_two_nodes(&electrsd, 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();
premine(&bitcoind.client, &electrsd.client);

let (node_a, node_b) = setup_two_nodes(&electrsd, 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();
premine(&bitcoind.client, &electrsd.client);

let (node_a, node_b) = setup_two_nodes(&electrsd, 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();
premine(&bitcoind.client, &electrsd.client);

let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);

let addr_a = node_a.onchain_payment().new_address().unwrap();
let addr_b = node_b.onchain_payment().new_address().unwrap();

let premine_amount_sat = 100_000;

premine_and_distribute_funds(
distribute_funds(
&bitcoind.client,
&electrsd.client,
vec![addr_a, addr_b],
Expand Down Expand Up @@ -97,6 +109,8 @@ fn channel_open_fails_when_funds_insufficient() {
#[test]
fn multi_hop_sending() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
premine(&bitcoind.client, &electrsd.client);

let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());

// Setup and fund 5 nodes
Expand All @@ -112,7 +126,7 @@ fn multi_hop_sending() {

let addresses = nodes.iter().map(|n| n.onchain_payment().new_address().unwrap()).collect();
let premine_amount_sat = 5_000_000;
premine_and_distribute_funds(
distribute_funds(
&bitcoind.client,
&electrsd.client,
addresses,
Expand Down Expand Up @@ -192,6 +206,8 @@ fn connect_to_public_testnet_esplora() {
#[test]
fn start_stop_reinit() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
premine(&bitcoind.client, &electrsd.client);

let config = random_config(true);

let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());
Expand All @@ -213,12 +229,7 @@ fn start_stop_reinit() {
assert_eq!(node.list_balances().total_onchain_balance_sats, 0);

let expected_amount = Amount::from_sat(100000);
premine_and_distribute_funds(
&bitcoind.client,
&electrsd.client,
vec![funding_address],
expected_amount,
);
distribute_funds(&bitcoind.client, &electrsd.client, vec![funding_address], expected_amount);

node.sync_wallets().unwrap();
assert_eq!(node.list_balances().spendable_onchain_balance_sats, expected_amount.to_sat());
Expand Down Expand Up @@ -260,12 +271,14 @@ fn start_stop_reinit() {
#[test]
fn onchain_spend_receive() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
premine(&bitcoind.client, &electrsd.client);

let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);

let addr_a = node_a.onchain_payment().new_address().unwrap();
let addr_b = node_b.onchain_payment().new_address().unwrap();

premine_and_distribute_funds(
distribute_funds(
&bitcoind.client,
&electrsd.client,
vec![addr_b.clone()],
Expand Down Expand Up @@ -307,7 +320,9 @@ fn onchain_spend_receive() {

#[test]
fn sign_verify_msg() {
let (_bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
premine(&bitcoind.client, &electrsd.client);

let config = random_config(true);
let node = setup_node(&electrsd, config);

Expand All @@ -325,7 +340,9 @@ fn connection_restart_behavior() {
}

fn do_connection_restart_behavior(persist: bool) {
let (_bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
premine(&bitcoind.client, &electrsd.client);

let (node_a, node_b) = setup_two_nodes(&electrsd, false, false, false);

let node_id_a = node_a.node_id();
Expand Down Expand Up @@ -376,7 +393,9 @@ fn do_connection_restart_behavior(persist: bool) {

#[test]
fn concurrent_connections_succeed() {
let (_bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
premine(&bitcoind.client, &electrsd.client);

let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);

let node_a = Arc::new(node_a);
Expand Down Expand Up @@ -407,11 +426,13 @@ fn concurrent_connections_succeed() {
#[test]
fn simple_bolt12_send_receive() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
premine(&bitcoind.client, &electrsd.client);

let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);

let address_a = node_a.onchain_payment().new_address().unwrap();
let premine_amount_sat = 5_000_000;
premine_and_distribute_funds(
distribute_funds(
&bitcoind.client,
&electrsd.client,
vec![address_a],
Expand Down Expand Up @@ -614,12 +635,14 @@ fn simple_bolt12_send_receive() {
#[test]
fn generate_bip21_uri() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
premine(&bitcoind.client, &electrsd.client);

let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);

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

premine_and_distribute_funds(
distribute_funds(
&bitcoind.client,
&electrsd.client,
vec![address_a],
Expand Down Expand Up @@ -655,12 +678,14 @@ fn generate_bip21_uri() {
#[test]
fn unified_qr_send_receive() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
premine(&bitcoind.client, &electrsd.client);

let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);

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

premine_and_distribute_funds(
distribute_funds(
&bitcoind.client,
&electrsd.client,
vec![address_a],
Expand Down
5 changes: 4 additions & 1 deletion tests/integration_tests_vss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ fn channel_full_cycle_with_vss_store() {
let vss_base_url = std::env::var("TEST_VSS_BASE_URL").unwrap();
let node_a =
builder_a.build_with_vss_store(vss_base_url.clone(), "node_1_store".to_string()).unwrap();
node_a.start().unwrap();

println!("\n== Node B ==");
let config_b = common::random_config(true);
let mut builder_b = Builder::from_config(config_b);
builder_b.set_esplora_server(esplora_url);
let node_b = builder_b.build_with_vss_store(vss_base_url, "node_2_store".to_string()).unwrap();

common::premine(&bitcoind.client, electrsd.client);

node_a.start().unwrap();
node_b.start().unwrap();

common::do_channel_full_cycle(
Expand Down

0 comments on commit 9db3046

Please sign in to comment.