From 07ba9d6015ea268a9a4742a4a108d939e28fef04 Mon Sep 17 00:00:00 2001 From: Alex Akselrod Date: Wed, 21 Feb 2024 09:46:33 -0800 Subject: [PATCH] itest: add open channel locked balance test --- itest/list_on_test.go | 4 +++ itest/lnd_open_channel_test.go | 57 ++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/itest/list_on_test.go b/itest/list_on_test.go index 13d7814a3..47cffcf6d 100644 --- a/itest/list_on_test.go +++ b/itest/list_on_test.go @@ -570,4 +570,8 @@ var allTestCases = []*lntest.TestCase{ Name: "coop close with htlcs", TestFunc: testCoopCloseWithHtlcs, }, + { + Name: "open channel locked balance", + TestFunc: testOpenChannelLockedBalance, + }, } diff --git a/itest/lnd_open_channel_test.go b/itest/lnd_open_channel_test.go index d8cf66485..919e6ae97 100644 --- a/itest/lnd_open_channel_test.go +++ b/itest/lnd_open_channel_test.go @@ -14,6 +14,7 @@ import ( "github.com/lightningnetwork/lnd/lntest" "github.com/lightningnetwork/lnd/lntest/node" "github.com/lightningnetwork/lnd/lntest/rpc" + "github.com/lightningnetwork/lnd/lntest/wait" "github.com/stretchr/testify/require" ) @@ -822,3 +823,59 @@ func testSimpleTaprootChannelActivation(ht *lntest.HarnessTest) { // Our test is done and Alice closes her channel to Bob. ht.CloseChannel(alice, chanPoint) } + +// testOpenChannelLockedBalance tests that when a funding reservation is +// made for opening a channel, the balance of the required outputs shows +// up as locked balance in the WalletBalance response. +func testOpenChannelLockedBalance(ht *lntest.HarnessTest) { + var ( + alice = ht.Alice + bob = ht.Bob + req *lnrpc.ChannelAcceptRequest + err error + ) + + // We first make sure Alice has no locked wallet balance. + balance := alice.RPC.WalletBalance() + require.EqualValues(ht, 0, balance.LockedBalance) + + // Next, we register a ChannelAcceptor on Bob. This way, we can get + // Alice's wallet balance after coin selection is done and outpoints + // are locked. + stream, cancel := bob.RPC.ChannelAcceptor() + defer cancel() + + // Then, we request creation of a channel from Alice to Bob. We don't + // use OpenChannelSync since we want to receive Bob's message in the + // same goroutine. + openChannelReq := &lnrpc.OpenChannelRequest{ + NodePubkey: bob.PubKey[:], + LocalFundingAmount: int64(funding.MaxBtcFundingAmount), + } + _ = alice.RPC.OpenChannel(openChannelReq) + + // After that, we receive the request on Bob's side, get the wallet + // balance from Alice, and ensure the locked balance is non-zero. + err = wait.NoError(func() error { + req, err = stream.Recv() + return err + }, defaultTimeout) + require.NoError(ht, err) + + balance = alice.RPC.WalletBalance() + require.NotEqualValues(ht, 0, balance.LockedBalance) + + // Next, we let Bob deny the request. + resp := &lnrpc.ChannelAcceptResponse{ + Accept: false, + PendingChanId: req.PendingChanId, + } + err = wait.NoError(func() error { + return stream.Send(resp) + }, defaultTimeout) + require.NoError(ht, err) + + // Finally, we check to make sure the balance is unlocked again. + balance = alice.RPC.WalletBalance() + require.EqualValues(ht, 0, balance.LockedBalance) +}