itest: put node.CloseChannel inside wait

This commit is contained in:
yyforyongyu 2021-08-25 15:22:49 +08:00
parent e0e1bfb935
commit 66dae6ecf7
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
2 changed files with 37 additions and 62 deletions

View File

@ -1263,56 +1263,52 @@ func (n *NetworkHarness) CloseChannel(lnNode *HarnessNode,
} }
} }
closeReq := &lnrpc.CloseChannelRequest{ var (
ChannelPoint: cp, closeRespStream lnrpc.Lightning_CloseChannelClient
Force: force, closeTxid *chainhash.Hash
} )
closeRespStream, err := lnNode.CloseChannel(ctx, closeReq)
if err != nil {
return nil, nil, fmt.Errorf("unable to close channel: %v", err)
}
errChan := make(chan error) err = wait.NoError(func() error {
fin := make(chan *chainhash.Hash) closeReq := &lnrpc.CloseChannelRequest{
go func() { ChannelPoint: cp, Force: force,
// Consume the "channel close" update in order to wait for the closing }
// transaction to be broadcast, then wait for the closing tx to be seen closeRespStream, err = lnNode.CloseChannel(ctx, closeReq)
// within the network. if err != nil {
return fmt.Errorf("unable to close channel: %v", err)
}
// Consume the "channel close" update in order to wait for the
// closing transaction to be broadcast, then wait for the
// closing tx to be seen within the network.
closeResp, err := closeRespStream.Recv() closeResp, err := closeRespStream.Recv()
if err != nil { if err != nil {
errChan <- fmt.Errorf("unable to recv() from close "+ return fmt.Errorf("unable to recv() from close "+
"stream: %v", err) "stream: %v", err)
return
} }
pendingClose, ok := closeResp.Update.(*lnrpc.CloseStatusUpdate_ClosePending) pendingClose, ok := closeResp.Update.(*lnrpc.CloseStatusUpdate_ClosePending)
if !ok { if !ok {
errChan <- fmt.Errorf("expected channel close update, "+ return fmt.Errorf("expected channel close update, "+
"instead got %v", pendingClose) "instead got %v", pendingClose)
return
} }
closeTxid, err := chainhash.NewHash(pendingClose.ClosePending.Txid) closeTxid, err = chainhash.NewHash(
pendingClose.ClosePending.Txid,
)
if err != nil { if err != nil {
errChan <- fmt.Errorf("unable to decode closeTxid: "+ return fmt.Errorf("unable to decode closeTxid: "+
"%v", err) "%v", err)
return
} }
if err := n.waitForTxInMempool(ctx, *closeTxid); err != nil { if err := n.waitForTxInMempool(ctx, *closeTxid); err != nil {
errChan <- fmt.Errorf("error while waiting for "+ return fmt.Errorf("error while waiting for "+
"broadcast tx: %v", err) "broadcast tx: %v", err)
return
} }
fin <- closeTxid return nil
}() }, ChannelCloseTimeout)
if err != nil {
// Wait until either the deadline for the context expires, an error
// occurs, or the channel close update is received.
select {
case err := <-errChan:
return nil, nil, err return nil, nil, err
case closeTxid := <-fin:
return closeRespStream, closeTxid, nil
} }
return closeRespStream, closeTxid, nil
} }
// WaitForChannelClose waits for a notification from the passed channel close // WaitForChannelClose waits for a notification from the passed channel close

View File

@ -17,6 +17,7 @@ import (
"github.com/lightningnetwork/lnd/lnrpc/wtclientrpc" "github.com/lightningnetwork/lnd/lnrpc/wtclientrpc"
"github.com/lightningnetwork/lnd/lntest" "github.com/lightningnetwork/lnd/lntest"
"github.com/lightningnetwork/lnd/lntest/wait" "github.com/lightningnetwork/lnd/lntest/wait"
"github.com/stretchr/testify/require"
) )
// testRevokedCloseRetribution tests that Carol is able carry out // testRevokedCloseRetribution tests that Carol is able carry out
@ -159,22 +160,11 @@ func testRevokedCloseRetribution(net *lntest.NetworkHarness, t *harnessTest) {
// broadcasting his current channel state. This is actually the // broadcasting his current channel state. This is actually the
// commitment transaction of a prior *revoked* state, so he'll soon // commitment transaction of a prior *revoked* state, so he'll soon
// feel the wrath of Carol's retribution. // feel the wrath of Carol's retribution.
var closeUpdates lnrpc.Lightning_CloseChannelClient
force := true force := true
err = wait.Predicate(func() bool { closeUpdates, _, err := net.CloseChannel(
closeUpdates, _, err = net.CloseChannel( net.Bob, chanPoint, force,
net.Bob, chanPoint, force, )
) require.NoError(t.t, err, "unable to close channel")
if err != nil {
predErr = err
return false
}
return true
}, defaultTimeout)
if err != nil {
t.Fatalf("unable to close channel: %v", predErr)
}
// Wait for Bob's breach transaction to show up in the mempool to ensure // Wait for Bob's breach transaction to show up in the mempool to ensure
// that Carol's node has started waiting for confirmations. // that Carol's node has started waiting for confirmations.
@ -386,22 +376,11 @@ func testRevokedCloseRetributionZeroValueRemoteOutput(net *lntest.NetworkHarness
// broadcasting her current channel state. This is actually the // broadcasting her current channel state. This is actually the
// commitment transaction of a prior *revoked* state, so she'll soon // commitment transaction of a prior *revoked* state, so she'll soon
// feel the wrath of Dave's retribution. // feel the wrath of Dave's retribution.
var (
closeUpdates lnrpc.Lightning_CloseChannelClient
closeTxID *chainhash.Hash
closeErr error
)
force := true force := true
err = wait.Predicate(func() bool { closeUpdates, closeTxID, closeErr := net.CloseChannel(
closeUpdates, closeTxID, closeErr = net.CloseChannel( carol, chanPoint, force,
carol, chanPoint, force, )
) require.NoError(t.t, closeErr, "unable to close channel")
return closeErr == nil
}, defaultTimeout)
if err != nil {
t.Fatalf("unable to close channel: %v", closeErr)
}
// Query the mempool for the breaching closing transaction, this should // Query the mempool for the breaching closing transaction, this should
// be broadcast by Carol when she force closes the channel above. // be broadcast by Carol when she force closes the channel above.