From f7943448b9d65707f20b285d2567a7af3660a9dd Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Mon, 9 Aug 2021 13:55:38 +0200 Subject: [PATCH] itest: fix channel open and close ntfn test Because we now get one more update per channel when closing it, we need to update our test that looks at the close notifications sent by the SubscribeChannelEvent RPC. --- lntest/itest/assertions.go | 8 +++ lntest/itest/lnd_open_channel_test.go | 72 +++++++++++++++------------ 2 files changed, 48 insertions(+), 32 deletions(-) diff --git a/lntest/itest/assertions.go b/lntest/itest/assertions.go index f465ee9a..4389f8fe 100644 --- a/lntest/itest/assertions.go +++ b/lntest/itest/assertions.go @@ -1486,6 +1486,7 @@ func verifyCloseUpdate(chanUpdate *lnrpc.ChannelEventUpdate, lnrpc.ChannelEventUpdate_INACTIVE_CHANNEL, chanUpdate.Type) } + case *lnrpc.ChannelEventUpdate_ClosedChannel: if chanUpdate.Type != lnrpc.ChannelEventUpdate_CLOSED_CHANNEL { @@ -1507,6 +1508,13 @@ func verifyCloseUpdate(chanUpdate *lnrpc.ChannelEventUpdate, update.ClosedChannel.CloseInitiator) } + case *lnrpc.ChannelEventUpdate_FullyResolvedChannel: + if chanUpdate.Type != lnrpc.ChannelEventUpdate_FULLY_RESOLVED_CHANNEL { + return fmt.Errorf("update type mismatch: expected %v, got %v", + lnrpc.ChannelEventUpdate_FULLY_RESOLVED_CHANNEL, + chanUpdate.Type) + } + default: return fmt.Errorf("channel update channel of wrong type, "+ "expected closed channel, got %T", diff --git a/lntest/itest/lnd_open_channel_test.go b/lntest/itest/lnd_open_channel_test.go index 98b8a1ec..90bfaa4b 100644 --- a/lntest/itest/lnd_open_channel_test.go +++ b/lntest/itest/lnd_open_channel_test.go @@ -276,8 +276,8 @@ func testBasicChannelCreationAndUpdates(net *lntest.NetworkHarness, t *harnessTe aliceChanSub := subscribeChannelNotifications(ctxb, t, net.Alice) defer close(aliceChanSub.quit) - // Open the channel between Alice and Bob, asserting that the - // channel has been properly open on-chain. + // Open the channels between Alice and Bob, asserting that the channels + // have been properly opened on-chain. chanPoints := make([]*lnrpc.ChannelPoint, numChannels) for i := 0; i < numChannels; i++ { ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout) @@ -291,11 +291,10 @@ func testBasicChannelCreationAndUpdates(net *lntest.NetworkHarness, t *harnessTe // Since each of the channels just became open, Bob and Alice should // each receive an open and an active notification for each channel. - var numChannelUpds int - const totalNtfns = 3 * numChannels + const numExpectedOpenUpdates = 3 * numChannels verifyOpenUpdatesReceived := func(sub channelSubscription) error { - numChannelUpds = 0 - for numChannelUpds < totalNtfns { + numChannelUpds := 0 + for numChannelUpds < numExpectedOpenUpdates { select { case update := <-sub.updateChan: switch update.Type { @@ -327,30 +326,32 @@ func testBasicChannelCreationAndUpdates(net *lntest.NetworkHarness, t *harnessTe update.Type) } numChannelUpds++ + case <-time.After(time.Second * 10): return fmt.Errorf("timeout waiting for channel "+ "notifications, only received %d/%d "+ "chanupds", numChannelUpds, - totalNtfns) + numExpectedOpenUpdates) } } return nil } - if err := verifyOpenUpdatesReceived(bobChanSub); err != nil { - t.Fatalf("error verifying open updates: %v", err) - } - if err := verifyOpenUpdatesReceived(aliceChanSub); err != nil { - t.Fatalf("error verifying open updates: %v", err) - } + require.NoError( + t.t, verifyOpenUpdatesReceived(bobChanSub), "bob open channels", + ) + require.NoError( + t.t, verifyOpenUpdatesReceived(aliceChanSub), "alice open "+ + "channels", + ) - // Close the channel between Alice and Bob, asserting that the channel - // has been properly closed on-chain. + // Close the channels between Alice and Bob, asserting that the channels + // have been properly closed on-chain. for i, chanPoint := range chanPoints { ctx, _ := context.WithTimeout(context.Background(), defaultTimeout) - // Force close half of the channels. + // Force close the first of the two channels. force := i%2 == 0 closeChannelAndAssert(ctx, t, net, net.Alice, chanPoint, force) if force { @@ -360,20 +361,21 @@ func testBasicChannelCreationAndUpdates(net *lntest.NetworkHarness, t *harnessTe // verifyCloseUpdatesReceived is used to verify that Alice and Bob // receive the correct channel updates in order. + const numExpectedCloseUpdates = 3 * numChannels verifyCloseUpdatesReceived := func(sub channelSubscription, forceType lnrpc.ChannelCloseSummary_ClosureType, closeInitiator lnrpc.Initiator) error { - // Ensure one inactive and one closed notification is received for each - // closed channel. + // Ensure one inactive and one closed notification is received + // for each closed channel. numChannelUpds := 0 - for numChannelUpds < 2*numChannels { + for numChannelUpds < numExpectedCloseUpdates { expectedCloseType := lnrpc.ChannelCloseSummary_COOPERATIVE_CLOSE // Every other channel should be force closed. If this // channel was force closed, set the expected close type - // the the type passed in. - force := (numChannelUpds/2)%2 == 0 + // to the type passed in. + force := (numChannelUpds/3)%2 == 0 if force { expectedCloseType = forceType } @@ -389,13 +391,15 @@ func testBasicChannelCreationAndUpdates(net *lntest.NetworkHarness, t *harnessTe } numChannelUpds++ + case err := <-sub.errChan: return err + case <-time.After(time.Second * 10): return fmt.Errorf("timeout waiting "+ "for channel notifications, only "+ "received %d/%d chanupds", - numChannelUpds, 2*numChannels) + numChannelUpds, numChannelUpds) } } @@ -406,19 +410,23 @@ func testBasicChannelCreationAndUpdates(net *lntest.NetworkHarness, t *harnessTe // receive a remote force close notification for force closed channels. // All channels (cooperatively and force closed) should have a remote // close initiator because Alice closed the channels. - if err := verifyCloseUpdatesReceived(bobChanSub, - lnrpc.ChannelCloseSummary_REMOTE_FORCE_CLOSE, - lnrpc.Initiator_INITIATOR_REMOTE); err != nil { - t.Fatalf("errored verifying close updates: %v", err) - } + require.NoError( + t.t, verifyCloseUpdatesReceived( + bobChanSub, + lnrpc.ChannelCloseSummary_REMOTE_FORCE_CLOSE, + lnrpc.Initiator_INITIATOR_REMOTE, + ), "verifying bob close updates", + ) // Verify Alice receives all closed channel notifications. She should // receive a remote force close notification for force closed channels. // All channels (cooperatively and force closed) should have a local // close initiator because Alice closed the channels. - if err := verifyCloseUpdatesReceived(aliceChanSub, - lnrpc.ChannelCloseSummary_LOCAL_FORCE_CLOSE, - lnrpc.Initiator_INITIATOR_LOCAL); err != nil { - t.Fatalf("errored verifying close updates: %v", err) - } + require.NoError( + t.t, verifyCloseUpdatesReceived( + aliceChanSub, + lnrpc.ChannelCloseSummary_LOCAL_FORCE_CLOSE, + lnrpc.Initiator_INITIATOR_LOCAL, + ), "verifying alice close updates", + ) }