2020-03-04 15:21:27 +03:00
|
|
|
package itest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcutil"
|
2020-03-31 13:44:18 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
|
2020-03-04 15:21:27 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntest"
|
|
|
|
"github.com/lightningnetwork/lnd/lntest/wait"
|
2020-10-26 16:21:09 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-03-04 15:21:27 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// testMultiHopRemoteForceCloseOnChainHtlcTimeout tests that if we extend a
|
|
|
|
// multi-hop HTLC, and the final destination of the HTLC force closes the
|
2020-03-04 15:21:28 +03:00
|
|
|
// channel, then we properly timeout the HTLC directly on *their* commitment
|
|
|
|
// transaction once the timeout has expired. Once we sweep the transaction, we
|
|
|
|
// should also cancel back the initial HTLC.
|
2020-03-04 15:21:27 +03:00
|
|
|
func testMultiHopRemoteForceCloseOnChainHtlcTimeout(net *lntest.NetworkHarness,
|
2020-03-04 15:21:28 +03:00
|
|
|
t *harnessTest, alice, bob *lntest.HarnessNode, c commitType) {
|
2020-03-04 15:21:28 +03:00
|
|
|
|
2020-03-04 15:21:27 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// First, we'll create a three hop network: Alice -> Bob -> Carol, with
|
|
|
|
// Carol refusing to actually settle or directly cancel any HTLC's
|
|
|
|
// self.
|
2020-03-04 15:21:28 +03:00
|
|
|
aliceChanPoint, bobChanPoint, carol := createThreeHopNetwork(
|
2020-03-04 15:21:28 +03:00
|
|
|
t, net, alice, bob, true, c,
|
2020-03-04 15:21:28 +03:00
|
|
|
)
|
2020-03-04 15:21:27 +03:00
|
|
|
|
|
|
|
// Clean up carol's node when the test finishes.
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
|
|
|
// With our channels set up, we'll then send a single HTLC from Alice
|
|
|
|
// to Carol. As Carol is in hodl mode, she won't settle this HTLC which
|
|
|
|
// opens up the base for out tests.
|
|
|
|
const (
|
|
|
|
finalCltvDelta = 40
|
|
|
|
htlcAmt = btcutil.Amount(30000)
|
|
|
|
)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctxb)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// We'll now send a single HTLC across our multi-hop network.
|
|
|
|
carolPubKey := carol.PubKey[:]
|
|
|
|
payHash := makeFakePayHash(t)
|
2020-03-31 13:44:18 +03:00
|
|
|
_, err := alice.RouterClient.SendPaymentV2(
|
2020-10-26 16:21:09 +03:00
|
|
|
ctx, &routerrpc.SendPaymentRequest{
|
2020-03-31 13:44:18 +03:00
|
|
|
Dest: carolPubKey,
|
|
|
|
Amt: int64(htlcAmt),
|
|
|
|
PaymentHash: payHash,
|
|
|
|
FinalCltvDelta: finalCltvDelta,
|
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitMsat: noFeeLimitMsat,
|
|
|
|
},
|
|
|
|
)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2020-03-04 15:21:27 +03:00
|
|
|
|
|
|
|
// Once the HTLC has cleared, all the nodes in our mini network should
|
|
|
|
// show that the HTLC has been locked in.
|
2020-03-04 15:21:28 +03:00
|
|
|
nodes := []*lntest.HarnessNode{alice, bob, carol}
|
2020-10-26 16:21:09 +03:00
|
|
|
err = wait.NoError(func() error {
|
|
|
|
return assertActiveHtlcs(nodes, payHash)
|
|
|
|
}, defaultTimeout)
|
|
|
|
require.NoError(t.t, err)
|
2020-03-04 15:21:27 +03:00
|
|
|
|
2020-09-04 12:19:27 +03:00
|
|
|
// Increase the fee estimate so that the following force close tx will
|
|
|
|
// be cpfp'ed.
|
|
|
|
net.SetFeeEstimate(30000)
|
|
|
|
|
2020-03-04 15:21:27 +03:00
|
|
|
// At this point, we'll now instruct Carol to force close the
|
|
|
|
// transaction. This will let us exercise that Bob is able to sweep the
|
2020-03-04 15:21:29 +03:00
|
|
|
// expired HTLC on Carol's version of the commitment transaction. If
|
|
|
|
// Carol has an anchor, it will be swept too.
|
2020-03-04 15:21:27 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelCloseTimeout)
|
2020-03-04 15:21:29 +03:00
|
|
|
closeChannelAndAssertType(
|
|
|
|
ctxt, t, net, carol, bobChanPoint, c == commitTypeAnchors,
|
|
|
|
true,
|
|
|
|
)
|
2020-03-04 15:21:27 +03:00
|
|
|
|
|
|
|
// At this point, Bob should have a pending force close channel as
|
|
|
|
// Carol has gone directly to chain.
|
2020-10-26 16:21:09 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = waitForNumChannelPendingForceClose(ctxt, bob, 1, nil)
|
|
|
|
require.NoError(t.t, err)
|
2020-03-04 15:21:27 +03:00
|
|
|
|
2020-03-04 15:21:29 +03:00
|
|
|
// Bob can sweep his output immediately. If there is an anchor, Bob will
|
|
|
|
// sweep that as well.
|
|
|
|
expectedTxes := 1
|
|
|
|
if c == commitTypeAnchors {
|
|
|
|
expectedTxes = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = waitForNTxsInMempool(
|
|
|
|
net.Miner.Node, expectedTxes, minerMempoolTimeout,
|
|
|
|
)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2020-03-04 15:21:27 +03:00
|
|
|
|
|
|
|
// Next, we'll mine enough blocks for the HTLC to expire. At this
|
|
|
|
// point, Bob should hand off the output to his internal utxo nursery,
|
|
|
|
// which will broadcast a sweep transaction.
|
|
|
|
numBlocks := padCLTV(finalCltvDelta - 1)
|
2020-10-26 16:21:09 +03:00
|
|
|
_, err = net.Miner.Node.Generate(numBlocks)
|
|
|
|
require.NoError(t.t, err)
|
2020-03-04 15:21:27 +03:00
|
|
|
|
|
|
|
// If we check Bob's pending channel report, it should show that he has
|
|
|
|
// a single HTLC that's now in the second stage, as skip the initial
|
|
|
|
// first stage since this is a direct HTLC.
|
2020-10-26 16:21:09 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = waitForNumChannelPendingForceClose(
|
|
|
|
ctxt, bob, 1, func(c *lnrpcForceCloseChannel) error {
|
|
|
|
if len(c.PendingHtlcs) != 1 {
|
|
|
|
return fmt.Errorf("bob should have pending " +
|
|
|
|
"htlc but doesn't")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.PendingHtlcs[0].Stage != 2 {
|
|
|
|
return fmt.Errorf("bob's htlc should have "+
|
|
|
|
"advanced to the second stage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
|
|
|
|
// We need to generate an additional block to trigger the sweep.
|
|
|
|
_, err = net.Miner.Node.Generate(1)
|
|
|
|
require.NoError(t.t, err)
|
2020-03-04 15:21:27 +03:00
|
|
|
|
|
|
|
// Bob's sweeping transaction should now be found in the mempool at
|
|
|
|
// this point.
|
|
|
|
sweepTx, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
|
|
|
if err != nil {
|
|
|
|
// If Bob's transaction isn't yet in the mempool, then due to
|
|
|
|
// internal message passing and the low period between blocks
|
|
|
|
// being mined, it may have been detected as a late
|
|
|
|
// registration. As a result, we'll mine another block and
|
|
|
|
// repeat the check. If it doesn't go through this time, then
|
|
|
|
// we'll fail.
|
|
|
|
// TODO(halseth): can we use waitForChannelPendingForceClose to
|
|
|
|
// avoid this hack?
|
2020-10-26 16:21:09 +03:00
|
|
|
_, err = net.Miner.Node.Generate(1)
|
|
|
|
require.NoError(t.t, err)
|
2020-03-04 15:21:27 +03:00
|
|
|
sweepTx, err = waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2020-03-04 15:21:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we mine an additional block, then this should confirm Bob's
|
|
|
|
// transaction which sweeps the direct HTLC output.
|
|
|
|
block := mineBlocks(t, net, 1, 1)[0]
|
|
|
|
assertTxInBlock(t, block, sweepTx)
|
|
|
|
|
|
|
|
// Now that the sweeping transaction has been confirmed, Bob should
|
|
|
|
// cancel back that HTLC. As a result, Alice should not know of any
|
|
|
|
// active HTLC's.
|
2020-03-04 15:21:28 +03:00
|
|
|
nodes = []*lntest.HarnessNode{alice}
|
2020-10-26 16:21:09 +03:00
|
|
|
err = wait.NoError(func() error {
|
|
|
|
return assertNumActiveHtlcs(nodes, 0)
|
|
|
|
}, defaultTimeout)
|
|
|
|
require.NoError(t.t, err)
|
2020-03-04 15:21:27 +03:00
|
|
|
|
|
|
|
// Now we'll check Bob's pending channel report. Since this was Carol's
|
|
|
|
// commitment, he doesn't have to wait for any CSV delays. As a result,
|
|
|
|
// he should show no additional pending transactions.
|
2020-10-26 16:21:09 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = waitForNumChannelPendingForceClose(ctxt, bob, 0, nil)
|
|
|
|
require.NoError(t.t, err)
|
2020-03-04 15:21:27 +03:00
|
|
|
|
|
|
|
// We'll close out the test by closing the channel from Alice to Bob,
|
|
|
|
// and then shutting down the new node we created as its no longer
|
2020-03-04 15:21:29 +03:00
|
|
|
// needed. Coop close, no anchors.
|
2020-03-04 15:21:27 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2020-03-04 15:21:29 +03:00
|
|
|
closeChannelAndAssertType(
|
2020-10-26 16:21:09 +03:00
|
|
|
ctxt, t, net, alice, aliceChanPoint, false, false,
|
2020-03-04 15:21:29 +03:00
|
|
|
)
|
2020-03-04 15:21:27 +03:00
|
|
|
}
|