-
Notifications
You must be signed in to change notification settings - Fork 114
Add onchain fund recovery test #396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -315,12 +317,104 @@ 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![42u8; 64]; | ||
|
|
||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't quite follow this. Is the above a self-send? Or is there a second wallet somewhere that isn't immediately obvious to me?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There indeed is a second wallet, namely the one in our |
||
| ); | ||
|
|
||
| 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(); | ||
| let config = random_config(true); | ||
| let chain_source = TestChainSource::Esplora(&electrsd); | ||
| let node = setup_node(&chain_source, config); | ||
| let node = setup_node(&chain_source, config, None); | ||
|
|
||
| // Tests arbitrary message signing and later verification | ||
| let msg = "OK computer".as_bytes(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated to this PR, but why don't we take
[u8; WALLET_KEYS_SEED_LEN]? Bindings?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, bindings unfortunately can't handle raw arrays. We could eventually consider exposing an alternative method based on the
uniffifeature.