2019-05-08 10:31:14 +03:00
|
|
|
// +build rpctest
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
package itest
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
2019-05-15 10:02:53 +03:00
|
|
|
"github.com/lightningnetwork/lnd"
|
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"
|
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"
|
2019-05-08 10:31:14 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// testMultiHopHtlcRemoteChainClaim tests that in the multi-hop HTLC scenario,
|
|
|
|
// if the remote party goes to chain while we have an incoming HTLC, then when
|
|
|
|
// we found out the preimage via the witness beacon, we properly settle the
|
2020-03-04 15:21:28 +03:00
|
|
|
// HTLC directly on-chain using the preimage in order to ensure that we don't
|
|
|
|
// lose any funds.
|
2020-03-04 15:21:28 +03:00
|
|
|
func testMultiHopHtlcRemoteChainClaim(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, 5}
|
|
|
|
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)
|
2019-05-08 10:31:14 +03:00
|
|
|
if err != nil {
|
2019-04-16 13:11:20 +03:00
|
|
|
t.Fatalf("unable to add invoice: %v", 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-04 15:21:28 +03:00
|
|
|
alicePayStream, err := alice.SendPayment(ctx)
|
2019-05-08 10:31:14 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream for alice: %v", err)
|
|
|
|
}
|
|
|
|
err = alicePayStream.Send(&lnrpc.SendRequest{
|
|
|
|
PaymentRequest: carolInvoice.PaymentRequest,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-04-16 13:11:20 +03:00
|
|
|
// At this point, all 3 nodes should now have an active channel with
|
|
|
|
// the created HTLC pending on all of them.
|
2019-05-08 10:31:14 +03:00
|
|
|
var predErr error
|
2020-03-04 15:21:28 +03:00
|
|
|
nodes := []*lntest.HarnessNode{alice, bob, carol}
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2019-04-16 13:11:20 +03:00
|
|
|
predErr = assertActiveHtlcs(nodes, payHash[:])
|
2019-05-08 10:31:14 +03:00
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
2019-04-16 13:11:20 +03:00
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
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)
|
|
|
|
|
2019-05-08 10:31:14 +03:00
|
|
|
// Next, Alice decides that she wants to exit the channel, so she'll
|
|
|
|
// immediately force close the channel by broadcast her commitment
|
|
|
|
// transaction.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2020-03-04 15:21:29 +03:00
|
|
|
aliceForceClose := closeChannelAndAssertType(ctxt, t, net, alice,
|
|
|
|
aliceChanPoint, c == commitTypeAnchors, true)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// Wait for the channel to be marked pending force close.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2020-03-04 15:21:28 +03:00
|
|
|
err = waitForChannelPendingForceClose(ctxt, alice, aliceChanPoint)
|
2019-05-08 10:31:14 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("channel not pending force close: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mine enough blocks for Alice to sweep her funds from the force
|
|
|
|
// closed channel.
|
|
|
|
_, err = net.Miner.Node.Generate(defaultCSV)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-04 15:21:29 +03:00
|
|
|
// Alice should now sweep her funds. If there are anchors, Alice should
|
|
|
|
// also sweep hers.
|
|
|
|
expectedTxes := 1
|
|
|
|
if c == commitTypeAnchors {
|
|
|
|
expectedTxes = 2
|
|
|
|
}
|
|
|
|
_, err = waitForNTxsInMempool(net.Miner.Node, expectedTxes, minerMempoolTimeout)
|
2019-05-08 10:31:14 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find sweeping tx in mempool: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-04-16 13:11:20 +03:00
|
|
|
// Suspend bob, so Carol is forced to go on chain.
|
2020-03-04 15:21:28 +03:00
|
|
|
restartBob, err := net.SuspendNode(bob)
|
2019-04-16 13:11:20 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to suspend bob: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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[:],
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("settle invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-08 10:31:14 +03:00
|
|
|
// We'll now mine enough blocks so Carol decides that she needs to go
|
|
|
|
// on-chain to claim the HTLC as Bob has been inactive.
|
2019-07-24 04:00:30 +03:00
|
|
|
numBlocks := padCLTV(uint32(invoiceReq.CltvExpiry-
|
2020-05-13 16:47:45 +03:00
|
|
|
lncfg.DefaultIncomingBroadcastDelta) - defaultCSV)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
if _, err := net.Miner.Node.Generate(numBlocks); err != nil {
|
|
|
|
t.Fatalf("unable to generate blocks")
|
|
|
|
}
|
|
|
|
|
2020-03-04 15:21:29 +03:00
|
|
|
// Carol's commitment transaction should now be in the mempool. If there
|
|
|
|
// are anchors, Carol also sweeps her anchor.
|
|
|
|
_, err = waitForNTxsInMempool(
|
|
|
|
net.Miner.Node, expectedTxes, minerMempoolTimeout,
|
|
|
|
)
|
2019-05-08 10:31:14 +03:00
|
|
|
if err != nil {
|
2020-03-04 15:21:29 +03:00
|
|
|
t.Fatalf("unable to find carol's txes: %v", err)
|
2019-05-08 10:31:14 +03:00
|
|
|
}
|
2019-05-15 10:02:53 +03:00
|
|
|
bobFundingTxid, err := lnd.GetChanPointFundingTxid(bobChanPoint)
|
2019-05-08 10:31:14 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
carolFundingPoint := wire.OutPoint{
|
|
|
|
Hash: *bobFundingTxid,
|
|
|
|
Index: bobChanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
|
2020-03-04 15:21:29 +03:00
|
|
|
// The closing transaction should be spending from the funding
|
|
|
|
// transaction.
|
|
|
|
closingTx := getSpendingTxInMempool(
|
|
|
|
t, net.Miner.Node, minerMempoolTimeout, carolFundingPoint,
|
|
|
|
)
|
|
|
|
closingTxid := closingTx.TxHash()
|
2019-05-08 10:31:14 +03:00
|
|
|
|
2020-03-04 15:21:29 +03:00
|
|
|
// Mine a block, which should contain: the commitment, possibly an
|
|
|
|
// anchor sweep and the coinbase tx.
|
|
|
|
block := mineBlocks(t, net, 1, expectedTxes)[0]
|
|
|
|
if len(block.Transactions) != expectedTxes+1 {
|
|
|
|
t.Fatalf("expected %v transactions in block, got %v",
|
|
|
|
expectedTxes, len(block.Transactions))
|
2019-05-08 10:31:14 +03:00
|
|
|
}
|
2020-03-04 15:21:29 +03:00
|
|
|
assertTxInBlock(t, block, &closingTxid)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
2019-04-16 13:11:20 +03:00
|
|
|
// Restart bob again.
|
|
|
|
if err := restartBob(); err != nil {
|
|
|
|
t.Fatalf("unable to restart bob: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-04 15:21:29 +03:00
|
|
|
// After the force close transacion is mined, Carol should broadcast her
|
|
|
|
// second level HTLC transacion. Bob will broadcast a sweep tx to sweep
|
|
|
|
// his output in the channel with Carol. He can do this immediately, as
|
|
|
|
// the output is not timelocked since Carol was the one force closing.
|
|
|
|
// If there are anchors, Bob should also sweep his.
|
|
|
|
expectedTxes = 2
|
|
|
|
if c == commitTypeAnchors {
|
|
|
|
expectedTxes = 3
|
|
|
|
}
|
|
|
|
txes, err := getNTxsFromMempool(net.Miner.Node, expectedTxes,
|
2019-05-08 10:31:14 +03:00
|
|
|
minerMempoolTimeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("transactions not found in mempool: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-04 15:21:29 +03:00
|
|
|
// All transactions should be pending from the commitment transaction.
|
|
|
|
assertAllTxesSpendFrom(t, txes, closingTxid)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// Mine a block to confirm the two transactions (+ coinbase).
|
2020-03-04 15:21:29 +03:00
|
|
|
block = mineBlocks(t, net, 1, expectedTxes)[0]
|
|
|
|
if len(block.Transactions) != expectedTxes+1 {
|
2019-05-08 10:31:14 +03:00
|
|
|
t.Fatalf("expected 3 transactions in block, got %v",
|
|
|
|
len(block.Transactions))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track of the second level tx maturity.
|
|
|
|
carolSecondLevelCSV := uint32(defaultCSV)
|
|
|
|
|
|
|
|
// When Bob notices Carol's second level transaction in the block, he
|
|
|
|
// will extract the preimage and broadcast a sweep tx to directly claim
|
|
|
|
// the HTLC in his (already closed) channel with Alice.
|
|
|
|
bobHtlcSweep, err := waitForTxInMempool(net.Miner.Node,
|
|
|
|
minerMempoolTimeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("transactions not found in mempool: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It should spend from the commitment in the channel with Alice.
|
|
|
|
tx, err := net.Miner.Node.GetRawTransaction(bobHtlcSweep)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txn: %v", err)
|
|
|
|
}
|
|
|
|
if tx.MsgTx().TxIn[0].PreviousOutPoint.Hash != *aliceForceClose {
|
|
|
|
t.Fatalf("tx did not spend from alice's force close tx")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now mine a block which should confirm Bob's HTLC sweep
|
|
|
|
// transaction.
|
|
|
|
block = mineBlocks(t, net, 1, 1)[0]
|
|
|
|
if len(block.Transactions) != 2 {
|
|
|
|
t.Fatalf("expected 2 transactions in block, got %v",
|
|
|
|
len(block.Transactions))
|
|
|
|
}
|
|
|
|
assertTxInBlock(t, block, bobHtlcSweep)
|
|
|
|
carolSecondLevelCSV--
|
|
|
|
|
|
|
|
// Now that the sweeping transaction has been confirmed, Bob should now
|
|
|
|
// recognize that all contracts have been fully resolved, and show no
|
|
|
|
// pending close channels.
|
|
|
|
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2019-05-08 10:31:14 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2020-03-04 15:21:28 +03:00
|
|
|
pendingChanResp, err := bob.PendingChannels(
|
2019-05-08 10:31:14 +03:00
|
|
|
ctxt, pendingChansRequest,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(pendingChanResp.PendingForceClosingChannels) != 0 {
|
|
|
|
predErr = fmt.Errorf("bob still has pending channels "+
|
|
|
|
"but shouldn't: %v", spew.Sdump(pendingChanResp))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(predErr.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we then mine 3 additional blocks, Carol's second level tx will
|
|
|
|
// mature, and she should pull the funds.
|
|
|
|
if _, err := net.Miner.Node.Generate(carolSecondLevelCSV); err != nil {
|
|
|
|
t.Fatalf("unable to generate block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
carolSweep, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find Carol's sweeping transaction: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// When Carol's sweep gets confirmed, she should have no more pending
|
|
|
|
// channels.
|
|
|
|
block = mineBlocks(t, net, 1, 1)[0]
|
|
|
|
assertTxInBlock(t, block, carolSweep)
|
|
|
|
|
|
|
|
pendingChansRequest = &lnrpc.PendingChannelsRequest{}
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2019-05-08 10:31:14 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
pendingChanResp, err := carol.PendingChannels(
|
|
|
|
ctxt, pendingChansRequest,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(pendingChanResp.PendingForceClosingChannels) != 0 {
|
|
|
|
predErr = fmt.Errorf("carol still has pending channels "+
|
|
|
|
"but shouldn't: %v", spew.Sdump(pendingChanResp))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(predErr.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// The invoice should show as settled for Carol, indicating that it was
|
|
|
|
// swept on-chain.
|
|
|
|
invoicesReq := &lnrpc.ListInvoiceRequest{}
|
|
|
|
invoicesResp, err := carol.ListInvoices(ctxb, invoicesReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to retrieve invoices: %v", err)
|
|
|
|
}
|
|
|
|
if len(invoicesResp.Invoices) != 1 {
|
|
|
|
t.Fatalf("expected 1 invoice, got %d", len(invoicesResp.Invoices))
|
|
|
|
}
|
|
|
|
invoice := invoicesResp.Invoices[0]
|
|
|
|
if invoice.State != lnrpc.Invoice_SETTLED {
|
|
|
|
t.Fatalf("expected invoice to be settled on chain")
|
|
|
|
}
|
|
|
|
if invoice.AmtPaidSat != invoiceAmt {
|
|
|
|
t.Fatalf("expected invoice to be settled with %d sat, got "+
|
|
|
|
"%d sat", invoiceAmt, invoice.AmtPaidSat)
|
|
|
|
}
|
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
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
2019-05-08 10:31:14 +03:00
|
|
|
}
|