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
|
|
|
)
|
|
|
|
|
|
|
|
// 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)
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, all 3 nodes should now have an active channel with
|
|
|
|
// the created HTLC pending on all of them.
|
|
|
|
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 {
|
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
|
|
|
}
|
|
|
|
|
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)
|
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
|
|
|
// 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
|
|
|
))
|
2019-05-08 10:31:14 +03:00
|
|
|
if _, err := net.Miner.Node.Generate(numBlocks); err != nil {
|
|
|
|
t.Fatalf("unable to generate blocks")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
txes, err := getNTxsFromMempool(
|
|
|
|
net.Miner.Node, expectedTxes, minerMempoolTimeout,
|
|
|
|
)
|
2019-05-08 10:31:14 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected transaction not found in mempool: %v", err)
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
// The commitment transaction should be spending from the funding
|
|
|
|
// transaction.
|
2020-03-04 15:21:29 +03:00
|
|
|
closingTx := getSpendingTxInMempool(
|
|
|
|
t, net.Miner.Node, minerMempoolTimeout, carolFundingPoint,
|
|
|
|
)
|
|
|
|
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.
|
|
|
|
if err := restartBob(); err != nil {
|
|
|
|
t.Fatalf("unable to restart bob: %v", err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
txes, err = getNTxsFromMempool(net.Miner.Node,
|
|
|
|
expectedTxes, minerMempoolTimeout)
|
2019-05-08 10:31:14 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("transactions not found in mempool: %v", err)
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
if _, err := net.Miner.Node.Generate(1); err != nil {
|
|
|
|
t.Fatalf("unable to generate block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for pending channels: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pendingChanResp.PendingForceClosingChannels) == 0 {
|
|
|
|
t.Fatalf("carol should have pending for close chan but doesn't")
|
|
|
|
}
|
|
|
|
forceCloseChan := pendingChanResp.PendingForceClosingChannels[0]
|
|
|
|
if forceCloseChan.LimboBalance == 0 {
|
|
|
|
t.Fatalf("carol should have nonzero limbo balance instead "+
|
|
|
|
"has: %v", forceCloseChan.LimboBalance)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The pending HTLC carol has should also now be in stage 2.
|
|
|
|
if len(forceCloseChan.PendingHtlcs) != 1 {
|
|
|
|
t.Fatalf("carol should have pending htlc but doesn't")
|
|
|
|
}
|
|
|
|
if forceCloseChan.PendingHtlcs[0].Stage != 2 {
|
|
|
|
t.Fatalf("carol's htlc should have advanced to the second "+
|
|
|
|
"stage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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}
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2019-05-08 10:31:14 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, 0)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we mine 4 additional blocks, then both outputs should now be
|
|
|
|
// mature.
|
|
|
|
if _, err := net.Miner.Node.Generate(defaultCSV); err != nil {
|
|
|
|
t.Fatalf("unable to generate blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should have a new transaction in the mempool.
|
|
|
|
_, err = waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find bob's sweeping transaction: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, if we mine an additional block to confirm these two sweep
|
|
|
|
// transactions, Carol should not show a pending channel in her report
|
|
|
|
// afterwards.
|
|
|
|
if _, err := net.Miner.Node.Generate(1); err != nil {
|
|
|
|
t.Fatalf("unable to mine block: %v", err)
|
|
|
|
}
|
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: %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
|
|
|
// 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
|
|
|
}
|