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{
ChannelPoint: cp,
Force: force,
}
closeRespStream, err := lnNode.CloseChannel(ctx, closeReq)
if err != nil {
return nil, nil, fmt.Errorf("unable to close channel: %v", err)
}
var (
closeRespStream lnrpc.Lightning_CloseChannelClient
closeTxid *chainhash.Hash
)
errChan := make(chan error)
fin := make(chan *chainhash.Hash)
go func() {
// 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.
err = wait.NoError(func() error {
closeReq := &lnrpc.CloseChannelRequest{
ChannelPoint: cp, Force: force,
}
closeRespStream, err = lnNode.CloseChannel(ctx, closeReq)
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()
if err != nil {
errChan <- fmt.Errorf("unable to recv() from close "+
return fmt.Errorf("unable to recv() from close "+
"stream: %v", err)
return
}
pendingClose, ok := closeResp.Update.(*lnrpc.CloseStatusUpdate_ClosePending)
if !ok {
errChan <- fmt.Errorf("expected channel close update, "+
return fmt.Errorf("expected channel close update, "+
"instead got %v", pendingClose)
return
}
closeTxid, err := chainhash.NewHash(pendingClose.ClosePending.Txid)
closeTxid, err = chainhash.NewHash(
pendingClose.ClosePending.Txid,
)
if err != nil {
errChan <- fmt.Errorf("unable to decode closeTxid: "+
return fmt.Errorf("unable to decode closeTxid: "+
"%v", err)
return
}
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)
return
}
fin <- closeTxid
}()
// 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
}, ChannelCloseTimeout)
if err != nil {
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

View File

@ -17,6 +17,7 @@ import (
"github.com/lightningnetwork/lnd/lnrpc/wtclientrpc"
"github.com/lightningnetwork/lnd/lntest"
"github.com/lightningnetwork/lnd/lntest/wait"
"github.com/stretchr/testify/require"
)
// 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
// commitment transaction of a prior *revoked* state, so he'll soon
// feel the wrath of Carol's retribution.
var closeUpdates lnrpc.Lightning_CloseChannelClient
force := true
err = wait.Predicate(func() bool {
closeUpdates, _, err = net.CloseChannel(
net.Bob, chanPoint, force,
)
if err != nil {
predErr = err
return false
}
return true
}, defaultTimeout)
if err != nil {
t.Fatalf("unable to close channel: %v", predErr)
}
closeUpdates, _, err := net.CloseChannel(
net.Bob, chanPoint, force,
)
require.NoError(t.t, err, "unable to close channel")
// Wait for Bob's breach transaction to show up in the mempool to ensure
// 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
// commitment transaction of a prior *revoked* state, so she'll soon
// feel the wrath of Dave's retribution.
var (
closeUpdates lnrpc.Lightning_CloseChannelClient
closeTxID *chainhash.Hash
closeErr error
)
force := true
err = wait.Predicate(func() bool {
closeUpdates, closeTxID, closeErr = net.CloseChannel(
carol, chanPoint, force,
)
return closeErr == nil
}, defaultTimeout)
if err != nil {
t.Fatalf("unable to close channel: %v", closeErr)
}
closeUpdates, closeTxID, closeErr := net.CloseChannel(
carol, chanPoint, force,
)
require.NoError(t.t, closeErr, "unable to close channel")
// Query the mempool for the breaching closing transaction, this should
// be broadcast by Carol when she force closes the channel above.