From 2caa7d2b2bb18988e66c842e029f2452816a87b3 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Tue, 31 Oct 2023 16:31:44 +0800 Subject: [PATCH 1/3] invoices: add verbose errors to catch flakes in `TestInvoiceExpiryWithRegistry` --- invoices/invoiceregistry_test.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/invoices/invoiceregistry_test.go b/invoices/invoiceregistry_test.go index f91777f3d..03e58af36 100644 --- a/invoices/invoiceregistry_test.go +++ b/invoices/invoiceregistry_test.go @@ -3,6 +3,7 @@ package invoices_test import ( "context" "crypto/rand" + "fmt" "math" "testing" "testing/quick" @@ -12,6 +13,7 @@ import ( "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/clock" invpkg "github.com/lightningnetwork/lnd/invoices" + "github.com/lightningnetwork/lnd/lntest/wait" "github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/record" @@ -1059,9 +1061,7 @@ func TestInvoiceExpiryWithRegistry(t *testing.T) { require.NoError(t, err) } - if err := registry.Start(); err != nil { - t.Fatalf("cannot start registry: %v", err) - } + require.NoError(t, registry.Start(), "cannot start registry") // Now generate pending and invoices and add them to the registry while // it is up and running. We'll manipulate the clock to let them expire. @@ -1085,10 +1085,8 @@ func TestInvoiceExpiryWithRegistry(t *testing.T) { ctxb, invoicesThatWillCancel[i], ) require.NoError(t, err, "cannot find invoice") - - if invoice.State == invpkg.ContractCanceled { - t.Fatalf("expected pending invoice, got canceled") - } + require.NotEqual(t, invpkg.ContractCanceled, invoice.State, + "expected pending invoice, got canceled") } // Fwd time 1 day. @@ -1101,24 +1099,28 @@ func TestInvoiceExpiryWithRegistry(t *testing.T) { // canceled returns a bool to indicate whether all the invoices are // canceled. - canceled := func() bool { + canceled := func() error { for i := range expectedCancellations { invoice, err := registry.LookupInvoice( ctxb, expectedCancellations[i], ) - require.NoError(t, err) + if err != nil { + return err + } if invoice.State != invpkg.ContractCanceled { - return false + return fmt.Errorf("expected state %v, got %v", + invpkg.ContractCanceled, invoice.State) } } - return true + return nil } // Retrospectively check that all invoices that were expected to be // canceled are indeed canceled. - require.Eventually(t, canceled, testTimeout, 10*time.Millisecond) + err = wait.NoError(canceled, testTimeout) + require.NoError(t, err, "timeout checking invoice state") // Finally stop the registry. require.NoError(t, registry.Stop(), "failed to stop invoice registry") From cc6b20bf2f8dcd2a9c5d9c0964412e8ff85a4e73 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Tue, 31 Oct 2023 16:51:24 +0800 Subject: [PATCH 2/3] lntest: mine empty blocks when cleaning up force close Fix this test flake, ``` harness_miner.go:218: Error Trace: /home/runner/work/lnd/lnd/lntest/harness_miner.go:218 /home/runner/work/lnd/lnd/lntest/harness.go:1641 /home/runner/work/lnd/lnd/lntest/harness.go:1515 /home/runner/work/lnd/lnd/itest/lnd_wipe_fwdpkgs_test.go:121 /home/runner/work/lnd/lnd/lntest/harness.go:286 /home/runner/work/lnd/lnd/itest/lnd_test.go:136 Error: Received unexpected error: want 1, got 0 in mempool: [] Test: TestLightningNetworkDaemon/tranche02/91-of-135/neutrino/wipe_forwarding_packages Messages: assert tx in mempool timeout harness.go:339: finished test: wipe_forwarding_packages, start height=1636, end height=1653, mined blocks=17 harness.go:345: test failed, skipped cleanup ``` --- lntest/harness.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lntest/harness.go b/lntest/harness.go index 5915abed7..ccb5be930 100644 --- a/lntest/harness.go +++ b/lntest/harness.go @@ -1504,11 +1504,13 @@ func (h *HarnessTest) CleanupForceClose(hn *node.HarnessNode) { h.AssertNumPendingForceClose(hn, 1) // Mine enough blocks for the node to sweep its funds from the force - // closed channel. + // closed channel. The commit sweep resolver is able to broadcast the + // sweep tx up to one block before the CSV elapses, so wait until + // defaulCSV-1. // - // The commit sweep resolver is able to broadcast the sweep tx up to - // one block before the CSV elapses, so wait until defaulCSV-1. - h.MineBlocks(node.DefaultCSV - 1) + // NOTE: we might empty blocks here as we don't know the exact number + // of blocks to mine. This may end up mining more blocks than needed. + h.MineEmptyBlocks(node.DefaultCSV - 1) // The node should now sweep the funds, clean up by mining the sweeping // tx. From fc52a010552ed1acb1fa33da576b01408f646129 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Tue, 31 Oct 2023 17:04:01 +0800 Subject: [PATCH 3/3] itest: fix test flake in `testHtlcTimeoutResolverExtractPreimageRemote` From the build, ``` harness_miner.go:218: Error Trace: /home/runner/work/lnd/lnd/lntest/harness_miner.go:218 /home/runner/work/lnd/lnd/lntest/harness.go:1641 /home/runner/work/lnd/lnd/itest/lnd_multi-hop_test.go:2207 /home/runner/work/lnd/lnd/itest/lnd_multi-hop_test.go:152 Error: Received unexpected error: want 2, got 3 in mempool: [a7cbb5724e39579cfc75d0469270f691988cb67fcc51a332256cafb193cf40bd b12a03a0622a3cf7d53867f060a6de8166e564a1a3109bc2be1ec0b720f305da 073d66885ccded1a770dc9aa5f1d44c8e10ac4e916e5ab50ef5149a5649ec8ba] Test: TestLightningNetworkDaemon/tranche03/130-of-135/neutrino/htlc_timeout_resolver_extract_preimage_remote/zeroconf=false/committype=SIMPLE_TAPROOT Messages: assert tx in mempool timeout harness.go:339: finished test: htlc_timeout_resolver_extract_preimage_remote, start height=1312, end height=1347, mined blocks=35 ``` --- itest/lnd_multi-hop_test.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/itest/lnd_multi-hop_test.go b/itest/lnd_multi-hop_test.go index 1841d77ca..e85de046a 100644 --- a/itest/lnd_multi-hop_test.go +++ b/itest/lnd_multi-hop_test.go @@ -2166,7 +2166,6 @@ func runExtraPreimageFromRemoteCommit(ht *lntest.HarnessTest, if ht.IsNeutrinoBackend() { // Mine a block to confirm Carol's 2nd level success tx. ht.MineBlocksAndAssertNumTxes(1, 1) - numTxesMempool-- numBlocks-- } @@ -2195,12 +2194,6 @@ func runExtraPreimageFromRemoteCommit(ht *lntest.HarnessTest, // anchor sweep tx in the mempool. case lnrpc.CommitmentType_SCRIPT_ENFORCED_LEASE: numTxesMempool++ - - // For neutrino backend, because of the additional block mined, - // Bob's output is now mature. - if ht.IsNeutrinoBackend() { - numTxesMempool++ - } } // Mine a block to clean the mempool.