From 3f3656ae4af8b20ae301253c94e2ea8009116dbd Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Wed, 6 Feb 2019 21:47:40 +0100 Subject: [PATCH 1/4] lnd_test: assert htlc sweep is mined To ensure the sweep has properly propagated to the miner and included in the mined block. --- lnd_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lnd_test.go b/lnd_test.go index 194f692cb..ee0becfc1 100644 --- a/lnd_test.go +++ b/lnd_test.go @@ -3056,7 +3056,9 @@ func testChannelForceClosure(net *lntest.NetworkHarness, t *harnessTest) { } // Wait for the single sweep txn to appear in the mempool. - htlcSweepTxID, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout) + htlcSweepTxID, err := waitForTxInMempool( + net.Miner.Node, minerMempoolTimeout, + ) if err != nil { t.Fatalf("failed to get sweep tx from mempool: %v", err) } @@ -3152,11 +3154,9 @@ func testChannelForceClosure(net *lntest.NetworkHarness, t *harnessTest) { } // Generate the final block that sweeps all htlc funds into the user's - // wallet. - blockHash, err = net.Miner.Node.Generate(1) - if err != nil { - t.Fatalf("unable to generate block: %v", err) - } + // wallet, and make sure the sweep is in this block. + block = mineBlocks(t, net, 1, 1)[0] + assertTxInBlock(t, block, htlcSweepTxID) // Now that the channel has been fully swept, it should no longer show // up within the pending channels RPC. From 81b2e3502e50e97989028d902374ca32e21a0925 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Wed, 6 Feb 2019 21:47:40 +0100 Subject: [PATCH 2/4] lnd_test: convert assertNumOpenChannelsPending to use waitNoError --- lnd_test.go | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/lnd_test.go b/lnd_test.go index ee0becfc1..e018c0b66 100644 --- a/lnd_test.go +++ b/lnd_test.go @@ -394,43 +394,36 @@ func numOpenChannelsPending(ctxt context.Context, node *lntest.HarnessNode) (int func assertNumOpenChannelsPending(ctxt context.Context, t *harnessTest, alice, bob *lntest.HarnessNode, expected int) { - const nPolls = 10 - - ticker := time.NewTicker(200 * time.Millisecond) - defer ticker.Stop() - - for i := 0; i < nPolls; i++ { + err := lntest.WaitNoError(func() error { aliceNumChans, err := numOpenChannelsPending(ctxt, alice) if err != nil { - t.Fatalf("error fetching alice's node (%v) pending channels %v", - alice.NodeID, err) + return fmt.Errorf("error fetching alice's node (%v) "+ + "pending channels %v", alice.NodeID, err) } bobNumChans, err := numOpenChannelsPending(ctxt, bob) if err != nil { - t.Fatalf("error fetching bob's node (%v) pending channels %v", - bob.NodeID, err) + return fmt.Errorf("error fetching bob's node (%v) "+ + "pending channels %v", bob.NodeID, err) } - isLastIteration := i == nPolls-1 - aliceStateCorrect := aliceNumChans == expected - if !aliceStateCorrect && isLastIteration { - t.Fatalf("number of pending channels for alice incorrect. "+ - "expected %v, got %v", expected, aliceNumChans) + if !aliceStateCorrect { + return fmt.Errorf("number of pending channels for "+ + "alice incorrect. expected %v, got %v", + expected, aliceNumChans) } bobStateCorrect := bobNumChans == expected - if !bobStateCorrect && isLastIteration { - t.Fatalf("number of pending channels for bob incorrect. "+ - "expected %v, got %v", - expected, bobNumChans) + if !bobStateCorrect { + return fmt.Errorf("number of pending channels for bob "+ + "incorrect. expected %v, got %v", expected, + bobNumChans) } - if aliceStateCorrect && bobStateCorrect { - return - } - - <-ticker.C + return nil + }, 15*time.Second) + if err != nil { + t.Fatalf(err.Error()) } } From b19b26003b8a284cd6278367bfdbbb278a738c3d Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Wed, 6 Feb 2019 21:47:41 +0100 Subject: [PATCH 3/4] lnd_test: disable mempool check on justice tx rebroadcast By mistake we were checking that only one transaction was in the mempool when looking for the justice tx, which wasn't always the case, and would make the test fail. --- lnd_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lnd_test.go b/lnd_test.go index e018c0b66..fb3bf82c5 100644 --- a/lnd_test.go +++ b/lnd_test.go @@ -7375,8 +7375,11 @@ func testRevokedCloseRetributionRemoteHodl(net *lntest.NetworkHarness, // attempt because of the second layer transactions, he will // wait until the next block epoch before trying again. Because // of this, we'll mine a block if we cannot find the justice tx - // immediately. - mineBlocks(t, net, 1, 1) + // immediately. Since we cannot tell for sure how many + // transactions will be in the mempool at this point, we pass 0 + // as the last argument, indicating we don't care what's in the + // mempool. + mineBlocks(t, net, 1, 0) err = lntest.WaitPredicate(func() bool { txid, err := findJusticeTx() if err != nil { From aefc23938cea1aa6e6441517a2615940a1684f32 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Wed, 6 Feb 2019 21:47:41 +0100 Subject: [PATCH 4/4] lnd_test: assert num channels before state check Since the getChanInfo call would require the node to have only one channel, it made no sense to check the number of channels after calling it. Instead we check it first, giving the node time to restart. --- lnd_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lnd_test.go b/lnd_test.go index fb3bf82c5..bb0bffef1 100644 --- a/lnd_test.go +++ b/lnd_test.go @@ -7665,6 +7665,10 @@ func testDataLossProtection(net *lntest.NetworkHarness, t *harnessTest) { t.Fatalf("unable to restart node: %v", err) } + // Make sure the channel is still there from the PoV of the + // node. + assertNodeNumChannels(t, node, 1) + // Now query for the channel state, it should show that it's at // a state number in the past, not the *latest* state. ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) @@ -7675,7 +7679,6 @@ func testDataLossProtection(net *lntest.NetworkHarness, t *harnessTest) { if nodeChan.NumUpdates != stateNumPreCopy { t.Fatalf("db copy failed: %v", nodeChan.NumUpdates) } - assertNodeNumChannels(t, node, 1) balReq := &lnrpc.WalletBalanceRequest{} ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)