@@ -496,6 +496,89 @@ fn onchain_send_receive() {
496
496
assert_eq ! ( node_b_payments. len( ) , 5 ) ;
497
497
}
498
498
499
+ #[ test]
500
+ fn onchain_send_all_retains_reserve ( ) {
501
+ let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
502
+ let chain_source = TestChainSource :: Esplora ( & electrsd) ;
503
+ let ( node_a, node_b) = setup_two_nodes ( & chain_source, false , true , false ) ;
504
+
505
+ // Setup nodes
506
+ let addr_a = node_a. onchain_payment ( ) . new_address ( ) . unwrap ( ) ;
507
+ let addr_b = node_b. onchain_payment ( ) . new_address ( ) . unwrap ( ) ;
508
+
509
+ let premine_amount_sat = 1_000_000 ;
510
+ let reserve_amount_sat = 25_000 ;
511
+ let onchain_fee_buffer_sat = 1000 ;
512
+ premine_and_distribute_funds (
513
+ & bitcoind. client ,
514
+ & electrsd. client ,
515
+ vec ! [ addr_a. clone( ) , addr_b. clone( ) ] ,
516
+ Amount :: from_sat ( premine_amount_sat) ,
517
+ ) ;
518
+
519
+ node_a. sync_wallets ( ) . unwrap ( ) ;
520
+ node_b. sync_wallets ( ) . unwrap ( ) ;
521
+ assert_eq ! ( node_a. list_balances( ) . spendable_onchain_balance_sats, premine_amount_sat) ;
522
+ assert_eq ! ( node_b. list_balances( ) . spendable_onchain_balance_sats, premine_amount_sat) ;
523
+
524
+ // Send all over, with 0 reserve as we don't have any channels open.
525
+ let txid = node_a. onchain_payment ( ) . send_all_to_address ( & addr_b, true , None ) . unwrap ( ) ;
526
+
527
+ wait_for_tx ( & electrsd. client , txid) ;
528
+ generate_blocks_and_wait ( & bitcoind. client , & electrsd. client , 6 ) ;
529
+
530
+ node_a. sync_wallets ( ) . unwrap ( ) ;
531
+ node_b. sync_wallets ( ) . unwrap ( ) ;
532
+ // Check node a sent all and node b received it
533
+ assert_eq ! ( node_a. list_balances( ) . spendable_onchain_balance_sats, 0 ) ;
534
+ assert ! ( ( ( premine_amount_sat * 2 - onchain_fee_buffer_sat) ..=( premine_amount_sat * 2 ) )
535
+ . contains( & node_b. list_balances( ) . spendable_onchain_balance_sats) ) ;
536
+
537
+ // Refill to make sure we have enough reserve for the channel open.
538
+ let txid = bitcoind
539
+ . client
540
+ . send_to_address ( & addr_a, Amount :: from_sat ( reserve_amount_sat) )
541
+ . unwrap ( )
542
+ . 0
543
+ . parse ( )
544
+ . unwrap ( ) ;
545
+ wait_for_tx ( & electrsd. client , txid) ;
546
+ generate_blocks_and_wait ( & bitcoind. client , & electrsd. client , 6 ) ;
547
+ node_a. sync_wallets ( ) . unwrap ( ) ;
548
+ node_b. sync_wallets ( ) . unwrap ( ) ;
549
+ assert_eq ! ( node_a. list_balances( ) . spendable_onchain_balance_sats, reserve_amount_sat) ;
550
+
551
+ // Open a channel.
552
+ open_channel ( & node_b, & node_a, premine_amount_sat, false , & electrsd) ;
553
+ generate_blocks_and_wait ( & bitcoind. client , & electrsd. client , 6 ) ;
554
+ node_a. sync_wallets ( ) . unwrap ( ) ;
555
+ node_b. sync_wallets ( ) . unwrap ( ) ;
556
+ expect_channel_ready_event ! ( node_a, node_b. node_id( ) ) ;
557
+ expect_channel_ready_event ! ( node_b, node_a. node_id( ) ) ;
558
+
559
+ // Check node a sent all and node b received it
560
+ assert_eq ! ( node_a. list_balances( ) . spendable_onchain_balance_sats, 0 ) ;
561
+ assert ! ( ( ( premine_amount_sat - reserve_amount_sat - onchain_fee_buffer_sat)
562
+ ..=premine_amount_sat)
563
+ . contains( & node_b. list_balances( ) . spendable_onchain_balance_sats) ) ;
564
+
565
+ // Send all over again, this time ensuring the reserve is accounted for
566
+ let txid = node_b. onchain_payment ( ) . send_all_to_address ( & addr_a, true , None ) . unwrap ( ) ;
567
+
568
+ wait_for_tx ( & electrsd. client , txid) ;
569
+ generate_blocks_and_wait ( & bitcoind. client , & electrsd. client , 6 ) ;
570
+
571
+ node_a. sync_wallets ( ) . unwrap ( ) ;
572
+ node_b. sync_wallets ( ) . unwrap ( ) ;
573
+
574
+ // Check node b sent all and node a received it
575
+ assert_eq ! ( node_b. list_balances( ) . total_onchain_balance_sats, reserve_amount_sat) ;
576
+ assert_eq ! ( node_b. list_balances( ) . spendable_onchain_balance_sats, 0 ) ;
577
+ assert ! ( ( ( premine_amount_sat - reserve_amount_sat - onchain_fee_buffer_sat)
578
+ ..=premine_amount_sat)
579
+ . contains( & node_a. list_balances( ) . spendable_onchain_balance_sats) ) ;
580
+ }
581
+
499
582
#[ test]
500
583
fn onchain_wallet_recovery ( ) {
501
584
let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
0 commit comments