@@ -496,6 +496,86 @@ 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
+ assert_eq ! ( node_a. list_balances( ) . spendable_onchain_balance_sats, 0 ) ;
533
+ assert ! ( ( ( premine_amount_sat * 2 - onchain_fee_buffer_sat) ..( premine_amount_sat * 2 ) )
534
+ . contains( & node_b. list_balances( ) . spendable_onchain_balance_sats) ) ;
535
+
536
+ // Refill to make sure we have enough reserve for the channel open.
537
+ let txid = bitcoind
538
+ . client
539
+ . send_to_address ( & addr_a, Amount :: from_sat ( reserve_amount_sat) )
540
+ . unwrap ( )
541
+ . 0
542
+ . parse ( )
543
+ . unwrap ( ) ;
544
+ wait_for_tx ( & electrsd. client , txid) ;
545
+ generate_blocks_and_wait ( & bitcoind. client , & electrsd. client , 6 ) ;
546
+ node_a. sync_wallets ( ) . unwrap ( ) ;
547
+ node_b. sync_wallets ( ) . unwrap ( ) ;
548
+ assert_eq ! ( node_a. list_balances( ) . spendable_onchain_balance_sats, reserve_amount_sat) ;
549
+
550
+ // Open a channel.
551
+ open_channel ( & node_b, & node_a, premine_amount_sat, false , & electrsd) ;
552
+ generate_blocks_and_wait ( & bitcoind. client , & electrsd. client , 6 ) ;
553
+ node_a. sync_wallets ( ) . unwrap ( ) ;
554
+ node_b. sync_wallets ( ) . unwrap ( ) ;
555
+ expect_channel_ready_event ! ( node_a, node_b. node_id( ) ) ;
556
+ expect_channel_ready_event ! ( node_b, node_a. node_id( ) ) ;
557
+
558
+ assert_eq ! ( node_a. list_balances( ) . spendable_onchain_balance_sats, 0 ) ;
559
+ assert ! ( ( ( premine_amount_sat - reserve_amount_sat - onchain_fee_buffer_sat)
560
+ ..premine_amount_sat)
561
+ . contains( & node_b. list_balances( ) . spendable_onchain_balance_sats) ) ;
562
+
563
+ // Send all over again, this time ensuring the reserve is accounted for
564
+ let txid = node_b. onchain_payment ( ) . send_all_to_address ( & addr_a, true , None ) . unwrap ( ) ;
565
+
566
+ wait_for_tx ( & electrsd. client , txid) ;
567
+ generate_blocks_and_wait ( & bitcoind. client , & electrsd. client , 6 ) ;
568
+
569
+ node_a. sync_wallets ( ) . unwrap ( ) ;
570
+ node_b. sync_wallets ( ) . unwrap ( ) ;
571
+
572
+ assert_eq ! ( node_b. list_balances( ) . total_onchain_balance_sats, reserve_amount_sat) ;
573
+ assert_eq ! ( node_b. list_balances( ) . spendable_onchain_balance_sats, 0 ) ;
574
+ assert ! ( ( ( premine_amount_sat - reserve_amount_sat - onchain_fee_buffer_sat)
575
+ ..premine_amount_sat)
576
+ . contains( & node_a. list_balances( ) . spendable_onchain_balance_sats) ) ;
577
+ }
578
+
499
579
#[ test]
500
580
fn onchain_wallet_recovery ( ) {
501
581
let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
0 commit comments