2019-05-15 10:02:53 +03:00
|
|
|
package itest
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2020-05-13 16:47:45 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lncfg"
|
2019-05-08 10:31:14 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2019-04-16 13:11:20 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
|
2020-03-31 13:44:18 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
|
2019-05-08 10:31:14 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntest"
|
2019-09-19 22:46:29 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntest/wait"
|
2019-04-16 13:11:20 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2020-10-26 16:21:09 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-05-08 10:31:14 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// testMultiHopReceiverChainClaim tests that in the multi-hop setting, if the
|
|
|
|
// receiver of an HTLC knows the preimage, but wasn't able to settle the HTLC
|
2020-03-04 15:21:28 +03:00
|
|
|
// off-chain, then it goes on chain to claim the HTLC uing the HTLC success
|
|
|
|
// transaction. In this scenario, the node that sent the outgoing HTLC should
|
|
|
|
// extract the preimage from the sweep transaction, and finish settling the
|
|
|
|
// HTLC backwards into the route.
|
2020-03-04 15:21:28 +03:00
|
|
|
func testMultiHopReceiverChainClaim(net *lntest.NetworkHarness, t *harnessTest,
|
2020-03-04 15:21:28 +03:00
|
|
|
alice, bob *lntest.HarnessNode, c commitType) {
|
2020-03-04 15:21:28 +03:00
|
|
|
|
2019-05-08 10:31:14 +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.
|
2019-05-08 12:22:58 +03:00
|
|
|
aliceChanPoint, bobChanPoint, carol := createThreeHopNetwork(
|
2020-03-04 15:21:28 +03:00
|
|
|
t, net, alice, bob, false, c,
|
2019-05-08 12:22:58 +03:00
|
|
|
)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// Clean up carol's node when the test finishes.
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2019-04-16 13:11:20 +03:00
|
|
|
// With the network active, we'll now add a new hodl invoice at Carol's
|
|
|
|
// end. Make sure the cltv expiry delta is large enough, otherwise Bob
|
|
|
|
// won't send out the outgoing htlc.
|
|
|
|
|
2019-05-08 10:31:14 +03:00
|
|
|
const invoiceAmt = 100000
|
2019-04-16 13:11:20 +03:00
|
|
|
preimage := lntypes.Preimage{1, 2, 4}
|
|
|
|
payHash := preimage.Hash()
|
|
|
|
invoiceReq := &invoicesrpc.AddHoldInvoiceRequest{
|
2019-05-08 10:31:14 +03:00
|
|
|
Value: invoiceAmt,
|
|
|
|
CltvExpiry: 40,
|
2019-04-16 13:11:20 +03:00
|
|
|
Hash: payHash[:],
|
2019-05-08 10:31:14 +03:00
|
|
|
}
|
2019-04-16 13:11:20 +03:00
|
|
|
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
carolInvoice, err := carol.AddHoldInvoice(ctxt, invoiceReq)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// Now that we've created the invoice, we'll send a single payment from
|
|
|
|
// Alice to Carol. We won't wait for the response however, as Carol
|
|
|
|
// will not immediately settle the payment.
|
|
|
|
ctx, cancel := context.WithCancel(ctxb)
|
|
|
|
defer cancel()
|
|
|
|
|
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
|
|
|
PaymentRequest: carolInvoice.PaymentRequest,
|
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitMsat: noFeeLimitMsat,
|
|
|
|
},
|
|
|
|
)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// At this point, all 3 nodes should now have an active channel with
|
|
|
|
// the created HTLC pending on all of them.
|
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)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
2019-04-16 13:11:20 +03:00
|
|
|
// Wait for carol to mark invoice as accepted. There is a small gap to
|
|
|
|
// bridge between adding the htlc to the channel and executing the exit
|
|
|
|
// hop logic.
|
|
|
|
waitForInvoiceAccepted(t, carol, payHash)
|
|
|
|
|
2020-03-04 15:21:28 +03:00
|
|
|
restartBob, err := net.SuspendNode(bob)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-04-16 13:11:20 +03:00
|
|
|
|
|
|
|
// Settle invoice. This will just mark the invoice as settled, as there
|
|
|
|
// is no link anymore to remove the htlc from the commitment tx. For
|
|
|
|
// this test, it is important to actually settle and not leave the
|
|
|
|
// invoice in the accepted state, because without a known preimage, the
|
|
|
|
// channel arbitrator won't go to chain.
|
|
|
|
ctx, cancel = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
_, err = carol.SettleInvoice(ctx, &invoicesrpc.SettleInvoiceMsg{
|
|
|
|
Preimage: preimage[:],
|
|
|
|
})
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-04-16 13:11:20 +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)
|
|
|
|
|
2019-05-08 10:31:14 +03:00
|
|
|
// Now we'll mine enough blocks to prompt carol to actually go to the
|
|
|
|
// chain in order to sweep her HTLC since the value is high enough.
|
|
|
|
// TODO(roasbeef): modify once go to chain policy changes
|
2019-07-24 04:00:30 +03:00
|
|
|
numBlocks := padCLTV(uint32(
|
2020-05-13 16:47:45 +03:00
|
|
|
invoiceReq.CltvExpiry - lncfg.DefaultIncomingBroadcastDelta,
|
2019-07-24 04:00:30 +03:00
|
|
|
))
|
2021-03-10 01:12:26 +03:00
|
|
|
_, err = net.Miner.Client.Generate(numBlocks)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// At this point, Carol should broadcast her active commitment
|
2020-03-04 15:21:29 +03:00
|
|
|
// transaction in order to go to the chain and sweep her HTLC. If there
|
|
|
|
// are anchors, Carol also sweeps hers.
|
|
|
|
expectedTxes := 1
|
|
|
|
if c == commitTypeAnchors {
|
|
|
|
expectedTxes = 2
|
|
|
|
}
|
2020-01-10 17:27:50 +03:00
|
|
|
_, err = getNTxsFromMempool(
|
2021-03-10 01:12:26 +03:00
|
|
|
net.Miner.Client, expectedTxes, minerMempoolTimeout,
|
2020-03-04 15:21:29 +03:00
|
|
|
)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
2021-02-13 11:05:33 +03:00
|
|
|
bobFundingTxid, err := lnrpc.GetChanPointFundingTxid(bobChanPoint)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
carolFundingPoint := wire.OutPoint{
|
|
|
|
Hash: *bobFundingTxid,
|
|
|
|
Index: bobChanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// The commitment transaction should be spending from the funding
|
|
|
|
// transaction.
|
2020-03-04 15:21:29 +03:00
|
|
|
closingTx := getSpendingTxInMempool(
|
2021-03-10 01:12:26 +03:00
|
|
|
t, net.Miner.Client, minerMempoolTimeout, carolFundingPoint,
|
2020-03-04 15:21:29 +03:00
|
|
|
)
|
|
|
|
closingTxid := closingTx.TxHash()
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// Confirm the commitment.
|
2020-03-04 15:21:29 +03:00
|
|
|
mineBlocks(t, net, 1, expectedTxes)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
2019-04-16 13:11:20 +03:00
|
|
|
// Restart bob again.
|
2020-10-26 16:21:09 +03:00
|
|
|
err = restartBob()
|
|
|
|
require.NoError(t.t, err)
|
2019-04-16 13:11:20 +03:00
|
|
|
|
2019-05-08 10:31:14 +03:00
|
|
|
// After the force close transaction is mined, Carol should broadcast
|
|
|
|
// her second level HTLC transaction. Bob will broadcast a sweep tx to
|
|
|
|
// sweep his output in the channel with Carol. When Bob notices Carol's
|
2020-03-04 15:21:29 +03:00
|
|
|
// second level transaction in the mempool, he will extract the preimage
|
|
|
|
// and settle the HTLC back off-chain. Bob will also sweep his anchor,
|
|
|
|
// if present.
|
|
|
|
expectedTxes = 2
|
|
|
|
if c == commitTypeAnchors {
|
|
|
|
expectedTxes = 3
|
|
|
|
}
|
2020-10-26 16:21:09 +03:00
|
|
|
txes, err := getNTxsFromMempool(
|
2021-03-10 01:12:26 +03:00
|
|
|
net.Miner.Client, expectedTxes, minerMempoolTimeout,
|
2020-10-26 16:21:09 +03:00
|
|
|
)
|
|
|
|
require.NoError(t.t, err)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
2020-03-04 15:21:29 +03:00
|
|
|
// All transactions should be spending from the commitment transaction.
|
|
|
|
assertAllTxesSpendFrom(t, txes, closingTxid)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// We'll now mine an additional block which should confirm both the
|
|
|
|
// second layer transactions.
|
2021-03-10 01:12:26 +03:00
|
|
|
_, err = net.Miner.Client.Generate(1)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
time.Sleep(time.Second * 4)
|
|
|
|
|
|
|
|
// TODO(roasbeef): assert bob pending state as well
|
|
|
|
|
|
|
|
// Carol's pending channel report should now show two outputs under
|
|
|
|
// limbo: her commitment output, as well as the second-layer claim
|
|
|
|
// output.
|
|
|
|
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
pendingChanResp, err := carol.PendingChannels(ctxt, pendingChansRequest)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NotZero(t.t, len(pendingChanResp.PendingForceClosingChannels))
|
2019-05-08 10:31:14 +03:00
|
|
|
forceCloseChan := pendingChanResp.PendingForceClosingChannels[0]
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NotZero(t.t, forceCloseChan.LimboBalance)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// The pending HTLC carol has should also now be in stage 2.
|
2020-10-26 16:21:09 +03:00
|
|
|
require.Len(t.t, forceCloseChan.PendingHtlcs, 1)
|
|
|
|
require.Equal(t.t, uint32(2), forceCloseChan.PendingHtlcs[0].Stage)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// Once the second-level transaction confirmed, Bob should have
|
|
|
|
// extracted the preimage from the chain, and sent it back to Alice,
|
|
|
|
// clearing the HTLC off-chain.
|
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)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// If we mine 4 additional blocks, then both outputs should now be
|
|
|
|
// mature.
|
2021-03-10 01:12:26 +03:00
|
|
|
_, err = net.Miner.Client.Generate(defaultCSV)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// We should have a new transaction in the mempool.
|
2021-03-10 01:12:26 +03:00
|
|
|
_, err = waitForTxInMempool(net.Miner.Client, minerMempoolTimeout)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// Finally, if we mine an additional block to confirm these two sweep
|
|
|
|
// transactions, Carol should not show a pending channel in her report
|
|
|
|
// afterwards.
|
2021-03-10 01:12:26 +03:00
|
|
|
_, err = net.Miner.Client.Generate(1)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = waitForNumChannelPendingForceClose(ctxt, carol, 0, nil)
|
|
|
|
require.NoError(t.t, err)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// The invoice should show as settled for Carol, indicating that it was
|
|
|
|
// swept on-chain.
|
|
|
|
invoicesReq := &lnrpc.ListInvoiceRequest{}
|
|
|
|
invoicesResp, err := carol.ListInvoices(ctxb, invoicesReq)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
|
|
|
require.Len(t.t, invoicesResp.Invoices, 1)
|
2019-05-08 10:31:14 +03:00
|
|
|
invoice := invoicesResp.Invoices[0]
|
2020-10-26 16:21:09 +03:00
|
|
|
require.Equal(t.t, lnrpc.Invoice_SETTLED, invoice.State)
|
|
|
|
require.Equal(t.t, int64(invoiceAmt), invoice.AmtPaidSat)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
2019-06-12 11:25:53 +03:00
|
|
|
// Finally, check that the Alice's payment is correctly marked
|
|
|
|
// succeeded.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout)
|
|
|
|
err = checkPaymentStatus(
|
2020-03-04 15:21:28 +03:00
|
|
|
ctxt, alice, preimage, lnrpc.Payment_SUCCEEDED,
|
2019-06-12 11:25:53 +03:00
|
|
|
)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-06-12 11:25:53 +03:00
|
|
|
|
2019-05-08 10:31:14 +03:00
|
|
|
// We'll close out the channel between Alice and Bob, then shutdown
|
|
|
|
// carol to conclude the test.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2020-03-04 15:21:29 +03:00
|
|
|
closeChannelAndAssertType(
|
|
|
|
ctxt, t, net, alice, aliceChanPoint,
|
|
|
|
false, false,
|
|
|
|
)
|
2019-05-08 10:31:14 +03:00
|
|
|
}
|