From 28fed4ce168d6d41dbaebfedf0e8e6e96eb0a313 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Wed, 28 Aug 2024 12:04:17 +0200 Subject: [PATCH] Add test for `send_all_to_address` behavior --- tests/integration_tests_rust.rs | 63 +++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/tests/integration_tests_rust.rs b/tests/integration_tests_rust.rs index 27f8c03a5..5197ea6bb 100644 --- a/tests/integration_tests_rust.rs +++ b/tests/integration_tests_rust.rs @@ -258,20 +258,39 @@ fn onchain_spend_receive() { let addr_a = node_a.onchain_payment().new_address().unwrap(); let addr_b = node_b.onchain_payment().new_address().unwrap(); + let premine_amount_sat = 1_100_000; premine_and_distribute_funds( &bitcoind.client, &electrsd.client, - vec![addr_b.clone()], - Amount::from_sat(100000), + vec![addr_a.clone(), addr_b.clone()], + Amount::from_sat(premine_amount_sat), ); node_a.sync_wallets().unwrap(); node_b.sync_wallets().unwrap(); - assert_eq!(node_b.list_balances().spendable_onchain_balance_sats, 100000); + assert_eq!(node_a.list_balances().spendable_onchain_balance_sats, premine_amount_sat); + assert_eq!(node_b.list_balances().spendable_onchain_balance_sats, premine_amount_sat); + + let channel_amount_sat = 1_000_000; + let reserve_amount_sat = 25_000; + open_channel(&node_b, &node_a, channel_amount_sat, true, &electrsd); + generate_blocks_and_wait(&bitcoind.client, &electrsd.client, 6); + + node_a.sync_wallets().unwrap(); + node_b.sync_wallets().unwrap(); + expect_channel_ready_event!(node_a, node_b.node_id()); + expect_channel_ready_event!(node_b, node_a.node_id()); + + let node_a_balance = premine_amount_sat - reserve_amount_sat; + let node_b_balance_lower = premine_amount_sat - channel_amount_sat - reserve_amount_sat - 1000; + let node_b_balance_upper = premine_amount_sat - channel_amount_sat - reserve_amount_sat; + assert_eq!(node_a.list_balances().spendable_onchain_balance_sats, node_a_balance); + assert!(node_b.list_balances().spendable_onchain_balance_sats > node_b_balance_lower); + assert!(node_b.list_balances().spendable_onchain_balance_sats < node_b_balance_upper); assert_eq!( Err(NodeError::InsufficientFunds), - node_a.onchain_payment().send_to_address(&addr_b, 1000) + node_a.onchain_payment().send_to_address(&addr_b, node_a_balance + 1) ); let txid = node_b.onchain_payment().send_to_address(&addr_a, 1000).unwrap(); @@ -281,9 +300,28 @@ fn onchain_spend_receive() { node_a.sync_wallets().unwrap(); node_b.sync_wallets().unwrap(); - assert_eq!(node_a.list_balances().spendable_onchain_balance_sats, 1000); - assert!(node_b.list_balances().spendable_onchain_balance_sats > 98000); - assert!(node_b.list_balances().spendable_onchain_balance_sats < 100000); + let node_a_balance = node_a_balance + 1000; + let node_b_balance_lower = node_b_balance_lower - 1000; + let node_b_balance_upper = node_b_balance_upper - 1000; + assert_eq!(node_a.list_balances().spendable_onchain_balance_sats, node_a_balance); + assert!(node_b.list_balances().spendable_onchain_balance_sats > node_b_balance_lower); + assert!(node_b.list_balances().spendable_onchain_balance_sats < node_b_balance_upper); + + let addr_b = node_b.onchain_payment().new_address().unwrap(); + let txid = node_a.onchain_payment().send_all_to_address(&addr_b, true).unwrap(); + generate_blocks_and_wait(&bitcoind.client, &electrsd.client, 6); + wait_for_tx(&electrsd.client, txid); + + node_a.sync_wallets().unwrap(); + node_b.sync_wallets().unwrap(); + + let node_b_balance_lower = node_b_balance_lower + node_a_balance; + let node_b_balance_upper = node_b_balance_upper + node_a_balance; + let node_a_balance = 0; + assert_eq!(node_a.list_balances().spendable_onchain_balance_sats, node_a_balance); + assert_eq!(node_a.list_balances().total_onchain_balance_sats, reserve_amount_sat); + assert!(node_b.list_balances().spendable_onchain_balance_sats > node_b_balance_lower); + assert!(node_b.list_balances().spendable_onchain_balance_sats < node_b_balance_upper); let addr_b = node_b.onchain_payment().new_address().unwrap(); let txid = node_a.onchain_payment().send_all_to_address(&addr_b, false).unwrap(); @@ -293,9 +331,14 @@ fn onchain_spend_receive() { node_a.sync_wallets().unwrap(); node_b.sync_wallets().unwrap(); - assert_eq!(node_a.list_balances().total_onchain_balance_sats, 0); - assert!(node_b.list_balances().spendable_onchain_balance_sats > 99000); - assert!(node_b.list_balances().spendable_onchain_balance_sats < 100000); + let node_b_balance_lower = node_b_balance_lower + reserve_amount_sat; + let node_b_balance_upper = node_b_balance_upper + reserve_amount_sat; + let node_a_balance = 0; + + assert_eq!(node_a.list_balances().spendable_onchain_balance_sats, node_a_balance); + assert_eq!(node_a.list_balances().total_onchain_balance_sats, node_a_balance); + assert!(node_b.list_balances().spendable_onchain_balance_sats > node_b_balance_lower); + assert!(node_b.list_balances().spendable_onchain_balance_sats < node_b_balance_upper); } #[test]