Skip to content

Commit f225b09

Browse files
itest: add test for ConfirmationsUntilActive and ConfirmationHeight
Signed-off-by: Nishant Bansal <[email protected]>
1 parent 0b56d21 commit f225b09

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

itest/list_on_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ var allTestCases = []*lntest.TestCase{
6262
Name: "funding expiry blocks on pending",
6363
TestFunc: testFundingExpiryBlocksOnPending,
6464
},
65+
{
66+
Name: "pending channel confirmation until active",
67+
TestFunc: testPendingChannelConfirmationUntilActive,
68+
},
6569
{
6670
Name: "list channels",
6771
TestFunc: testListChannels,

itest/lnd_open_channel_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,124 @@ func testFundingExpiryBlocksOnPending(ht *lntest.HarnessTest) {
877877
ht.MineBlocksAndAssertNumTxes(1, 1)
878878
}
879879

880+
// testPendingChannelConfirmationUntilActive verifies the value for the rpc
881+
// field ConfirmationUntilActive updates correctly as soon as blocks are
882+
// confirmed.
883+
func testPendingChannelConfirmationUntilActive(ht *lntest.HarnessTest) {
884+
var (
885+
numConfs uint32 = 5
886+
chanAmt btcutil.Amount = 100000
887+
)
888+
889+
// Since we want Bob's channels to require more than 1 on-chain
890+
// confirmation before becoming active, we will launch Bob with the
891+
// custom defaultchanconfs flag.
892+
alice := ht.NewNodeWithCoins("Alice", nil)
893+
bob := ht.NewNode("Bob", []string{
894+
fmt.Sprintf("--bitcoin.defaultchanconfs=%v", numConfs),
895+
})
896+
897+
// Ensure Alice and Bob are connected.
898+
ht.EnsureConnected(alice, bob)
899+
900+
// Alice initiates a channel opening to Bob.
901+
param := lntest.OpenChannelParams{Amt: chanAmt}
902+
ht.OpenChannelAssertPending(alice, bob, param)
903+
904+
// Both Alice and Bob have one pending open channel.
905+
ht.AssertNumPendingOpenChannels(alice, 1)
906+
ht.AssertNumPendingOpenChannels(bob, 1)
907+
908+
// assertConfirmation is a helper closure to assert the
909+
// ConfirmationsUntilActive and ConfirmationHeight has been updated to
910+
// the expected value.
911+
assertConfirmation := func(hn *node.HarnessNode, expConfLeft uint32,
912+
expConfHeight uint32) {
913+
914+
err := wait.NoError(func() error {
915+
// Node should have one pending open channel.
916+
pendingChan := ht.AssertNumPendingOpenChannels(hn, 1)[0]
917+
918+
// Check if the ConfirmationsUntilActive is updated to
919+
// the expected value.
920+
if expConfLeft != pendingChan.ConfirmationsUntilActive {
921+
return fmt.Errorf("remaining confirmations "+
922+
"mismatch, want %v, got %v",
923+
expConfLeft,
924+
pendingChan.ConfirmationsUntilActive)
925+
}
926+
927+
// Check if the ConfirmationHeight is updated to the
928+
// expected value.
929+
if expConfHeight != pendingChan.ConfirmationHeight {
930+
return fmt.Errorf("confirmation height "+
931+
"mismatch, want %v, got %v",
932+
expConfLeft,
933+
pendingChan.ConfirmationsUntilActive)
934+
}
935+
936+
return nil
937+
}, defaultTimeout)
938+
require.NoError(ht, err)
939+
}
940+
941+
// Since the funding transaction is not confirmed yet,
942+
// ConfirmationsUntilActive will always be numConfs, and confirmation
943+
// height will be 0.
944+
assertConfirmation(alice, numConfs, 0)
945+
assertConfirmation(bob, numConfs, 0)
946+
947+
// Mine the first block containing the funding transaction, This
948+
// confirms the funding transaction but the channel should still remain
949+
// pending.
950+
ht.MineBlocksAndAssertNumTxes(1, 1)
951+
952+
// Decrement numConfs to reflect that one confirmation has been
953+
// received.
954+
numConfs--
955+
956+
// Since the funding transaction has been mined, the best block height
957+
// corresponds to the confirmation height of the channel's opening tx.
958+
_, expConfHeight := ht.GetBestBlock()
959+
960+
// Channel remains pending after the first confirmation.
961+
ht.AssertNumPendingOpenChannels(alice, 1)
962+
ht.AssertNumPendingOpenChannels(bob, 1)
963+
964+
// Make sure the ConfirmationsUntilActive and ConfirmationHeight
965+
// fields have been updated to the expected values before restarting the
966+
// nodes.
967+
assertConfirmation(alice, numConfs, uint32(expConfHeight))
968+
assertConfirmation(bob, numConfs, uint32(expConfHeight))
969+
970+
// Restart both nodes to test that the appropriate state has been
971+
// persisted and that both nodes recover gracefully.
972+
ht.RestartNode(alice)
973+
ht.RestartNode(bob)
974+
ht.EnsureConnected(alice, bob)
975+
976+
// ConfirmationsUntilActive field should decrease as each block is
977+
// mined until the required number of confirmations is reached. Let's
978+
// mine a few blocks and verify the value of ConfirmationsUntilActive at
979+
// each step.
980+
for i := numConfs; i > 0; i-- {
981+
expConfLeft := i
982+
983+
// Retrieve pending channels for both Alice and Bob and verify
984+
// the remaining confirmations and confirmation height.
985+
assertConfirmation(alice, expConfLeft, uint32(expConfHeight))
986+
assertConfirmation(bob, expConfLeft, uint32(expConfHeight))
987+
988+
// Mine the next block.
989+
ht.MineBlocks(1)
990+
}
991+
992+
// After the required number of confirmations, the channel should be
993+
// marked as active.
994+
ht.AssertNumPendingOpenChannels(alice, 0)
995+
ht.AssertNumPendingOpenChannels(bob, 0)
996+
}
997+
880998
// testSimpleTaprootChannelActivation ensures that a simple taproot channel is
881999
// active if the initiator disconnects and reconnects in between channel opening
8821000
// and channel confirmation.

0 commit comments

Comments
 (0)