lnd_test: add wait predicates to pending channel checks

This commit is contained in:
Conner Fromknecht 2018-08-29 18:00:54 -07:00
parent 2f1b024679
commit 95a98d86f6
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7

View File

@ -1190,7 +1190,7 @@ func testUpdateChannelPolicy(net *lntest.NetworkHarness, t *harnessTest) {
for _, s := range substrs { for _, s := range substrs {
if !strings.Contains(sendResp.PaymentError, s) { if !strings.Contains(sendResp.PaymentError, s) {
t.Fatalf("expected error to contain \"%v\", instead "+ t.Fatalf("expected error to contain \"%v\", instead "+
"got %v", sendResp.PaymentError) "got %v", s, sendResp.PaymentError)
} }
} }
@ -2225,8 +2225,9 @@ func testChannelForceClosure(net *lntest.NetworkHarness, t *harnessTest) {
// Now that the commitment has been confirmed, the channel should be // Now that the commitment has been confirmed, the channel should be
// marked as force closed. // marked as force closed.
err = lntest.WaitPredicate(func() bool { err = lntest.WaitPredicate(func() bool {
ctxt, _ := context.WithTimeout(ctxb, timeout)
pendingChanResp, err := net.Alice.PendingChannels( pendingChanResp, err := net.Alice.PendingChannels(
ctxb, pendingChansRequest, ctxt, pendingChansRequest,
) )
if err != nil { if err != nil {
predErr = fmt.Errorf("unable to query for pending "+ predErr = fmt.Errorf("unable to query for pending "+
@ -2300,8 +2301,9 @@ func testChannelForceClosure(net *lntest.NetworkHarness, t *harnessTest) {
// Alice should see the channel in her set of pending force closed // Alice should see the channel in her set of pending force closed
// channels with her funds still in limbo. // channels with her funds still in limbo.
err = lntest.WaitPredicate(func() bool { err = lntest.WaitPredicate(func() bool {
ctxt, _ := context.WithTimeout(ctxb, timeout)
pendingChanResp, err := net.Alice.PendingChannels( pendingChanResp, err := net.Alice.PendingChannels(
ctxb, pendingChansRequest, ctxt, pendingChansRequest,
) )
if err != nil { if err != nil {
predErr = fmt.Errorf("unable to query for pending "+ predErr = fmt.Errorf("unable to query for pending "+
@ -2399,34 +2401,49 @@ func testChannelForceClosure(net *lntest.NetworkHarness, t *harnessTest) {
assertTxInBlock(t, block, sweepTx.Hash()) assertTxInBlock(t, block, sweepTx.Hash())
// Now that the commit output has been fully swept, check to see that err = lntest.WaitPredicate(func() bool {
// the channel remains open for the pending htlc outputs. // Now that the commit output has been fully swept, check to see
pendingChanResp, err = net.Alice.PendingChannels(ctxb, pendingChansRequest) // that the channel remains open for the pending htlc outputs.
if err != nil { ctxt, _ := context.WithTimeout(ctxb, timeout)
t.Fatalf("unable to query for pending channels: %v", err) pendingChanResp, err := net.Alice.PendingChannels(
} ctxt, pendingChansRequest,
)
if err != nil {
predErr = fmt.Errorf("unable to query for pending "+
"channels: %v", err)
return false
}
err = checkNumForceClosedChannels(pendingChanResp, 1) err = checkNumForceClosedChannels(pendingChanResp, 1)
if err != nil { if err != nil {
t.Fatalf(err.Error()) predErr = err
} return false
}
// The commitment funds will have been recovered after the commit txn // The commitment funds will have been recovered after the
// was included in the last block. The htlc funds will not be shown in // commit txn was included in the last block. The htlc funds
// limbo, since they are still in their first stage and the nursery // will not be shown in limbo, since they are still in their
// hasn't received them from the contract court. // first stage and the nursery hasn't received them from the
forceClose, err := findForceClosedChannel(pendingChanResp, &op) // contract court.
forceClose, err := findForceClosedChannel(pendingChanResp, &op)
if err != nil {
predErr = err
return false
}
predErr = checkPendingChannelNumHtlcs(forceClose, 0)
if predErr != nil {
return false
}
if forceClose.LimboBalance != 0 {
predErr = fmt.Errorf("expected 0 funds in limbo, "+
"found %d", forceClose.LimboBalance)
return false
}
return true
}, 15*time.Second)
if err != nil { if err != nil {
t.Fatalf(err.Error()) t.Fatalf(predErr.Error())
}
err = checkPendingChannelNumHtlcs(forceClose, 0)
if err != nil {
t.Fatalf("expected 0 pending htlcs, found %d",
len(forceClose.PendingHtlcs))
}
if forceClose.LimboBalance != 0 {
t.Fatalf("expected 0 funds in limbo, found %d",
forceClose.LimboBalance)
} }
// Compute the height preceding that which will cause the htlc CLTV // Compute the height preceding that which will cause the htlc CLTV
@ -2454,8 +2471,9 @@ func testChannelForceClosure(net *lntest.NetworkHarness, t *harnessTest) {
// Alice should now see the channel in her set of pending force closed // Alice should now see the channel in her set of pending force closed
// channels with one pending HTLC. // channels with one pending HTLC.
err = lntest.WaitPredicate(func() bool { err = lntest.WaitPredicate(func() bool {
pendingChanResp, err = net.Alice.PendingChannels( ctxt, _ := context.WithTimeout(ctxb, timeout)
ctxb, pendingChansRequest, pendingChanResp, err := net.Alice.PendingChannels(
ctxt, pendingChansRequest,
) )
if err != nil { if err != nil {
predErr = fmt.Errorf("unable to query for pending "+ predErr = fmt.Errorf("unable to query for pending "+
@ -2468,7 +2486,7 @@ func testChannelForceClosure(net *lntest.NetworkHarness, t *harnessTest) {
return false return false
} }
forceClose, predErr = findForceClosedChannel( forceClose, predErr := findForceClosedChannel(
pendingChanResp, &op, pendingChanResp, &op,
) )
if predErr != nil { if predErr != nil {
@ -2582,27 +2600,42 @@ func testChannelForceClosure(net *lntest.NetworkHarness, t *harnessTest) {
// Now that the channel has been fully swept, it should no longer show // Now that the channel has been fully swept, it should no longer show
// incubated, check to see that Alice's node still reports the channel // incubated, check to see that Alice's node still reports the channel
// as pending force closed. // as pending force closed.
pendingChanResp, err = net.Alice.PendingChannels(ctxb, pendingChansRequest) err = lntest.WaitPredicate(func() bool {
if err != nil { ctxt, _ := context.WithTimeout(ctxb, timeout)
t.Fatalf("unable to query for pending channels: %v", err) pendingChanResp, err = net.Alice.PendingChannels(
} ctxt, pendingChansRequest,
err = checkNumForceClosedChannels(pendingChanResp, 1) )
if err != nil { if err != nil {
t.Fatalf(err.Error()) predErr = fmt.Errorf("unable to query for pending "+
} "channels: %v", err)
return false
}
err = checkNumForceClosedChannels(pendingChanResp, 1)
if err != nil {
predErr = err
return false
}
forceClose, err = findForceClosedChannel(pendingChanResp, &op) forceClose, err := findForceClosedChannel(pendingChanResp, &op)
if err != nil { if err != nil {
t.Fatalf(err.Error()) predErr = err
} return false
}
if forceClose.LimboBalance == 0 { if forceClose.LimboBalance == 0 {
t.Fatalf("htlc funds should still be in limbo") predErr = fmt.Errorf("htlc funds should still be in limbo")
} return false
}
err = checkPendingChannelNumHtlcs(forceClose, numInvoices) predErr = checkPendingChannelNumHtlcs(forceClose, numInvoices)
if predErr != nil {
return false
}
return true
}, 15*time.Second)
if err != nil { if err != nil {
t.Fatalf(err.Error()) t.Fatalf(predErr.Error())
} }
// Generate a block that causes Alice to sweep the htlc outputs in the // Generate a block that causes Alice to sweep the htlc outputs in the
@ -2666,30 +2699,46 @@ func testChannelForceClosure(net *lntest.NetworkHarness, t *harnessTest) {
// Now that the channel has been fully swept, it should no longer show // Now that the channel has been fully swept, it should no longer show
// incubated, check to see that Alice's node still reports the channel // incubated, check to see that Alice's node still reports the channel
// as pending force closed. // as pending force closed.
pendingChanResp, err = net.Alice.PendingChannels(ctxb, pendingChansRequest) err = lntest.WaitPredicate(func() bool {
if err != nil { ctxt, _ := context.WithTimeout(ctxb, timeout)
t.Fatalf("unable to query for pending channels: %v", err) pendingChanResp, err := net.Alice.PendingChannels(
} ctxt, pendingChansRequest,
err = checkNumForceClosedChannels(pendingChanResp, 1) )
if err != nil { if err != nil {
t.Fatalf(err.Error()) predErr = fmt.Errorf("unable to query for pending "+
} "channels: %v", err)
return false
}
err = checkNumForceClosedChannels(pendingChanResp, 1)
if err != nil {
predErr = err
return false
}
// All htlcs should show zero blocks until maturity, as evidenced by // All htlcs should show zero blocks until maturity, as
// having checked the sweep transaction in the mempool. // evidenced by having checked the sweep transaction in the
forceClose, err = findForceClosedChannel(pendingChanResp, &op) // mempool.
forceClose, err := findForceClosedChannel(pendingChanResp, &op)
if err != nil {
predErr = err
return false
}
predErr = checkPendingChannelNumHtlcs(forceClose, numInvoices)
if predErr != nil {
return false
}
err = checkPendingHtlcStageAndMaturity(
forceClose, 2, htlcCsvMaturityHeight, 0,
)
if err != nil {
predErr = err
return false
}
return true
}, 15*time.Second)
if err != nil { if err != nil {
t.Fatalf(err.Error()) t.Fatalf(predErr.Error())
}
err = checkPendingChannelNumHtlcs(forceClose, numInvoices)
if err != nil {
t.Fatalf(err.Error())
}
err = checkPendingHtlcStageAndMaturity(
forceClose, 2, htlcCsvMaturityHeight, 0,
)
if err != nil {
t.Fatalf(err.Error())
} }
// Generate the final block that sweeps all htlc funds into the user's // Generate the final block that sweeps all htlc funds into the user's
@ -2702,8 +2751,9 @@ func testChannelForceClosure(net *lntest.NetworkHarness, t *harnessTest) {
// Now that the channel has been fully swept, it should no longer show // Now that the channel has been fully swept, it should no longer show
// up within the pending channels RPC. // up within the pending channels RPC.
err = lntest.WaitPredicate(func() bool { err = lntest.WaitPredicate(func() bool {
ctxt, _ := context.WithTimeout(ctxb, timeout)
pendingChanResp, err := net.Alice.PendingChannels( pendingChanResp, err := net.Alice.PendingChannels(
ctxb, pendingChansRequest, ctxt, pendingChansRequest,
) )
if err != nil { if err != nil {
predErr = fmt.Errorf("unable to query for pending "+ predErr = fmt.Errorf("unable to query for pending "+