Skip to content

Commit

Permalink
Test we can recover on-chain funds from seed-only
Browse files Browse the repository at this point in the history
We add a test that makes sure a completely new node is able to recover
any previously-confirmed on-chain funds from the seed only.
  • Loading branch information
tnull committed Nov 4, 2024
1 parent d97085a commit 029e86a
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions tests/integration_tests_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use ldk_node::{Builder, Event, NodeError};
use lightning::ln::channelmanager::PaymentId;
use lightning::util::persist::KVStore;

use bitcoincore_rpc::RpcApi;

use bitcoin::Amount;

use std::sync::Arc;
Expand Down Expand Up @@ -315,6 +317,98 @@ fn onchain_spend_receive() {
assert!(node_b.list_balances().spendable_onchain_balance_sats < 100000);
}

#[test]
fn onchain_wallet_recovery() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();

let chain_source = TestChainSource::Esplora(&electrsd);

let seed_bytes: Vec<u8> = std::iter::repeat(42u8).take(64).collect();

let original_config = random_config(true);
let original_node = setup_node(&chain_source, original_config, Some(seed_bytes.clone()));

let premine_amount_sat = 100_000;

let addr_1 = original_node.onchain_payment().new_address().unwrap();

premine_and_distribute_funds(
&bitcoind.client,
&electrsd.client,
vec![addr_1],
Amount::from_sat(premine_amount_sat),
);
original_node.sync_wallets().unwrap();
assert_eq!(original_node.list_balances().spendable_onchain_balance_sats, premine_amount_sat);

let addr_2 = original_node.onchain_payment().new_address().unwrap();

let txid = bitcoind
.client
.send_to_address(
&addr_2,
Amount::from_sat(premine_amount_sat),
None,
None,
None,
None,
None,
None,
)
.unwrap();
wait_for_tx(&electrsd.client, txid);

generate_blocks_and_wait(&bitcoind.client, &electrsd.client, 1);

original_node.sync_wallets().unwrap();
assert_eq!(
original_node.list_balances().spendable_onchain_balance_sats,
premine_amount_sat * 2
);

original_node.stop().unwrap();
drop(original_node);

// Now we start from scratch, only the seed remains the same.
let recovered_config = random_config(true);
let recovered_node = setup_node(&chain_source, recovered_config, Some(seed_bytes));

recovered_node.sync_wallets().unwrap();
assert_eq!(
recovered_node.list_balances().spendable_onchain_balance_sats,
premine_amount_sat * 2
);

// Check we sync even when skipping some addresses.
let _addr_3 = recovered_node.onchain_payment().new_address().unwrap();
let _addr_4 = recovered_node.onchain_payment().new_address().unwrap();
let _addr_5 = recovered_node.onchain_payment().new_address().unwrap();
let addr_6 = recovered_node.onchain_payment().new_address().unwrap();

let txid = bitcoind
.client
.send_to_address(
&addr_6,
Amount::from_sat(premine_amount_sat),
None,
None,
None,
None,
None,
None,
)
.unwrap();
wait_for_tx(&electrsd.client, txid);

generate_blocks_and_wait(&bitcoind.client, &electrsd.client, 1);

recovered_node.sync_wallets().unwrap();
assert_eq!(
recovered_node.list_balances().spendable_onchain_balance_sats,
premine_amount_sat * 3
);
}

#[test]
fn sign_verify_msg() {
let (_bitcoind, electrsd) = setup_bitcoind_and_electrsd();
Expand Down

0 comments on commit 029e86a

Please sign in to comment.