From e324fe58181ab1829d4a922f3409e0bf356aefa8 Mon Sep 17 00:00:00 2001 From: BitfuryLightning Date: Mon, 19 Jun 2017 16:53:52 +0300 Subject: [PATCH] test: ensure blocks are synchronized before opening channels, allow more time for sync payments Fix bug with synchronizing blockchain by adding several retries. Allow to launch individual tests. Increase timeout for async payments. Fixes #213. --- fundingmanager.go | 1 + lnd_test.go | 18 ++++++++++++----- networktest.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/fundingmanager.go b/fundingmanager.go index 2ee24d07..707a7443 100644 --- a/fundingmanager.go +++ b/fundingmanager.go @@ -504,6 +504,7 @@ func (f *fundingManager) handleFundingRequest(fmsg *fundingRequestMsg) { fndgLog.Errorf("unable to query wallet: %v", err) return } + if !isSynced { errMsg := &lnwire.Error{ ChanID: fmsg.msg.PendingChannelID, diff --git a/lnd_test.go b/lnd_test.go index afc5a793..c749ca14 100644 --- a/lnd_test.go +++ b/lnd_test.go @@ -82,7 +82,7 @@ func (h *harnessTest) RunTestCase(testCase *testCase, net *networkHarness) { }() testCase.test(net, h) - h.t.Logf("Passed: (%v)", h.testCase.name) + return } @@ -2823,15 +2823,16 @@ func testBidirectionalAsyncPayments(net *networkHarness, t *harnessTest) { // Wait for Alice and Bob receive their payments, and throw and error // if something goes wrong. + maxTime := 20 * time.Second for i := 0; i < 2; i++ { select { case err := <-errChan: if err != nil { t.Fatalf(err.Error()) } - case <-time.After(time.Second * 7): - t.Fatalf("waiting for payments to finish too long " + - "(7s)") + case <-time.After(time.Second * maxTime): + t.Fatalf("waiting for payments to finish too long "+ + "(%v)", maxTime) } } @@ -3038,7 +3039,14 @@ func TestLightningNetworkDaemon(t *testing.T) { t.Logf("Running %v integration tests", len(testsCases)) for _, testCase := range testsCases { - ht.RunTestCase(testCase, lndHarness) + success := t.Run(testCase.name, func(t1 *testing.T) { + ht := newHarnessTest(t1) + ht.RunTestCase(testCase, lndHarness) + }) + // Stop at the first failure. Mimic behavior of original test + if !success { + break + } } close(testsFin) diff --git a/networktest.go b/networktest.go index 56a3f295..72cb823b 100644 --- a/networktest.go +++ b/networktest.go @@ -574,6 +574,41 @@ func (l *lightningNode) WaitForNetworkChannelClose(ctx context.Context, } } +// WaitForBlockchainSync will block until node synchronizes its blockchain +func (l *lightningNode) WaitForBlockchainSync(ctx context.Context) error { + errChan := make(chan error, 1) + retryDelay := time.Millisecond * 100 + go func() { + for { + select { + case <-ctx.Done(): + break + default: + } + getInfoReq := &lnrpc.GetInfoRequest{} + getInfoResp, err := l.GetInfo(ctx, getInfoReq) + if err != nil { + errChan <- err + break + } + if getInfoResp.SyncedToChain { + errChan <- nil + } + select { + case <-ctx.Done(): + break + case <-time.After(retryDelay): + } + } + }() + select { + case err := <-errChan: + return err + case <-ctx.Done(): + return fmt.Errorf("Timeout while waiting for blockchain sync") + } +} + // networkHarness is an integration testing harness for the lightning network. // The harness by default is created with two active nodes on the network: // Alice and Bob. @@ -972,6 +1007,14 @@ func (n *networkHarness) OpenChannel(ctx context.Context, srcNode, destNode *lightningNode, amt btcutil.Amount, pushAmt btcutil.Amount, numConfs uint32) (lnrpc.Lightning_OpenChannelClient, error) { + // Wait until srcNode and destNode have blockchain synced + if err := srcNode.WaitForBlockchainSync(ctx); err != nil { + return nil, fmt.Errorf("Unable to sync srcNode chain: %v", err) + } + if err := destNode.WaitForBlockchainSync(ctx); err != nil { + return nil, fmt.Errorf("Unable to sync destNode chain: %v", err) + } + openReq := &lnrpc.OpenChannelRequest{ NodePubkey: destNode.PubKey[:], LocalFundingAmount: int64(amt), @@ -1024,6 +1067,14 @@ func (n *networkHarness) OpenPendingChannel(ctx context.Context, srcNode, destNode *lightningNode, amt btcutil.Amount, pushAmt btcutil.Amount, numConfs uint32) (*lnrpc.PendingUpdate, error) { + // Wait until srcNode and destNode have blockchain synced + if err := srcNode.WaitForBlockchainSync(ctx); err != nil { + return nil, fmt.Errorf("Unable to sync srcNode chain: %v", err) + } + if err := destNode.WaitForBlockchainSync(ctx); err != nil { + return nil, fmt.Errorf("Unable to sync destNode chain: %v", err) + } + openReq := &lnrpc.OpenChannelRequest{ NodePubkey: destNode.PubKey[:], LocalFundingAmount: int64(amt),