Skip to content

Commit

Permalink
Add test for send_all_to_address behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Aug 29, 2024
1 parent ae8e6ce commit 2266163
Showing 1 changed file with 53 additions and 10 deletions.
63 changes: 53 additions & 10 deletions tests/integration_tests_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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]
Expand Down

0 comments on commit 2266163

Please sign in to comment.