2019-05-15 10:02:53 +03:00
|
|
|
package itest
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
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"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// testMultiHopHtlcLocalChainClaim tests that in a multi-hop HTLC scenario, if
|
2020-03-04 15:21:28 +03:00
|
|
|
// we force close a channel with an incoming HTLC, and later find out the
|
|
|
|
// preimage via the witness beacon, we properly settle the HTLC on-chain using
|
|
|
|
// the HTLC success transaction in order to ensure we don't lose any funds.
|
2020-03-04 15:21:28 +03:00
|
|
|
func testMultiHopHtlcLocalChainClaim(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.
|
|
|
|
|
|
|
|
const invoiceAmt = 100000
|
|
|
|
preimage := lntypes.Preimage{1, 2, 3}
|
|
|
|
payHash := preimage.Hash()
|
|
|
|
invoiceReq := &invoicesrpc.AddHoldInvoiceRequest{
|
|
|
|
Value: invoiceAmt,
|
2019-05-08 10:31:14 +03:00
|
|
|
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
|
|
|
|
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.
|
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-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
|
|
|
// At this point, Bob decides that he wants to exit the channel
|
|
|
|
// immediately, so he force closes his commitment transaction.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2020-10-26 16:21:09 +03:00
|
|
|
bobForceClose := closeChannelAndAssertType(
|
|
|
|
ctxt, t, net, bob, aliceChanPoint, c == commitTypeAnchors, true,
|
|
|
|
)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
2020-03-04 15:21:29 +03:00
|
|
|
// Alice will sweep her commitment output immediately. If there are
|
|
|
|
// anchors, Alice will also sweep hers.
|
|
|
|
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)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
2019-04-16 13:11:20 +03:00
|
|
|
// Suspend Bob to force Carol to go to chain.
|
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
|
|
|
|
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))
|
2019-05-08 10:31:14 +03:00
|
|
|
|
2020-10-26 16:21:09 +03:00
|
|
|
_, err = net.Miner.Node.Generate(numBlocks)
|
|
|
|
require.NoError(t.t, err)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
2020-03-04 15:21:29 +03:00
|
|
|
// Carol's commitment transaction should now be in the mempool. If there
|
|
|
|
// is an anchor, Carol will sweep that too.
|
|
|
|
_, err = waitForNTxsInMempool(
|
|
|
|
net.Miner.Node, expectedTxes, minerMempoolTimeout,
|
|
|
|
)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-05-15 10:02:53 +03:00
|
|
|
bobFundingTxid, err := lnd.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,
|
|
|
|
}
|
|
|
|
|
2020-03-04 15:21:29 +03:00
|
|
|
// Look up the closing transaction. It 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 that should confirm the commit tx, the anchor if present
|
|
|
|
// and the coinbase.
|
|
|
|
block := mineBlocks(t, net, 1, expectedTxes)[0]
|
2020-10-26 16:21:09 +03:00
|
|
|
require.Len(t.t, block.Transactions, expectedTxes+1)
|
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.
|
2020-10-26 16:21:09 +03:00
|
|
|
err = restartBob()
|
|
|
|
require.NoError(t.t, err)
|
2019-04-16 13:11:20 +03:00
|
|
|
|
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 on the commitment, Bob will also sweep his
|
|
|
|
// anchor.
|
|
|
|
expectedTxes = 2
|
|
|
|
if c == commitTypeAnchors {
|
|
|
|
expectedTxes = 3
|
|
|
|
}
|
|
|
|
txes, err := getNTxsFromMempool(
|
|
|
|
net.Miner.Node, expectedTxes, minerMempoolTimeout,
|
|
|
|
)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// Both Carol's second level transaction and Bob's sweep should be
|
|
|
|
// spending from the commitment transaction.
|
2020-03-04 15:21:29 +03:00
|
|
|
assertAllTxesSpendFrom(t, txes, closingTxid)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
2019-06-12 11:44:22 +03:00
|
|
|
// At this point we suspend Alice to make sure she'll handle the
|
|
|
|
// on-chain settle after a restart.
|
2020-03-04 15:21:28 +03:00
|
|
|
restartAlice, err := net.SuspendNode(alice)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-06-12 11:44:22 +03:00
|
|
|
|
2019-05-08 10:31:14 +03:00
|
|
|
// Mine a block to confirm the two transactions (+ the coinbase).
|
2020-03-04 15:21:29 +03:00
|
|
|
block = mineBlocks(t, net, 1, expectedTxes)[0]
|
2020-10-26 16:21:09 +03:00
|
|
|
require.Len(t.t, block.Transactions, expectedTxes+1)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
2020-12-09 14:24:04 +03:00
|
|
|
var secondLevelMaturity uint32
|
|
|
|
switch c {
|
|
|
|
|
|
|
|
// If this is a channel of the anchor type, we will subtract one block
|
|
|
|
// from the default CSV, as the Sweeper will handle the input, and the Sweeper
|
|
|
|
// sweeps the input as soon as the lock expires.
|
|
|
|
case commitTypeAnchors:
|
|
|
|
secondLevelMaturity = defaultCSV - 1
|
|
|
|
|
|
|
|
// For non-anchor channel types, the nursery will handle sweeping the
|
|
|
|
// second level output, and it will wait one extra block before
|
|
|
|
// sweeping it.
|
|
|
|
default:
|
|
|
|
secondLevelMaturity = defaultCSV
|
|
|
|
}
|
|
|
|
|
2019-05-08 10:31:14 +03:00
|
|
|
// Keep track of the second level tx maturity.
|
2020-12-09 14:24:04 +03:00
|
|
|
carolSecondLevelCSV := secondLevelMaturity
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// When Bob notices Carol's second level transaction in the block, he
|
|
|
|
// will extract the preimage and broadcast a second level tx to claim
|
|
|
|
// the HTLC in his (already closed) channel with Alice.
|
2020-10-26 16:21:09 +03:00
|
|
|
bobSecondLvlTx, err := waitForTxInMempool(
|
|
|
|
net.Miner.Node, minerMempoolTimeout,
|
|
|
|
)
|
|
|
|
require.NoError(t.t, err)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// It should spend from the commitment in the channel with Alice.
|
|
|
|
tx, err := net.Miner.Node.GetRawTransaction(bobSecondLvlTx)
|
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.Equal(
|
|
|
|
t.t, *bobForceClose, tx.MsgTx().TxIn[0].PreviousOutPoint.Hash,
|
|
|
|
)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// At this point, Bob should have broadcast his second layer success
|
|
|
|
// transaction, and should have sent it to the nursery for incubation.
|
2020-10-26 16:21:09 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = waitForNumChannelPendingForceClose(
|
|
|
|
ctxt, bob, 1, func(c *lnrpcForceCloseChannel) error {
|
|
|
|
if c.Channel.LocalBalance != 0 {
|
|
|
|
return nil
|
2019-05-08 10:31:14 +03:00
|
|
|
}
|
|
|
|
|
2020-10-26 16:21:09 +03:00
|
|
|
if len(c.PendingHtlcs) != 1 {
|
|
|
|
return fmt.Errorf("bob should have pending " +
|
|
|
|
"htlc but doesn't")
|
2019-05-08 10:31:14 +03:00
|
|
|
}
|
2020-10-26 16:21:09 +03:00
|
|
|
|
|
|
|
if c.PendingHtlcs[0].Stage != 1 {
|
|
|
|
return fmt.Errorf("bob's htlc should have "+
|
2019-05-08 10:31:14 +03:00
|
|
|
"advanced to the first stage but was "+
|
2020-10-26 16:21:09 +03:00
|
|
|
"stage: %v", c.PendingHtlcs[0].Stage)
|
2019-05-08 10:31:14 +03:00
|
|
|
}
|
2020-10-26 16:21:09 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(t.t, err)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// We'll now mine a block which should confirm Bob's second layer
|
|
|
|
// transaction.
|
|
|
|
block = mineBlocks(t, net, 1, 1)[0]
|
2020-10-26 16:21:09 +03:00
|
|
|
require.Len(t.t, block.Transactions, 2)
|
2019-05-08 10:31:14 +03:00
|
|
|
assertTxInBlock(t, block, bobSecondLvlTx)
|
|
|
|
|
|
|
|
// Keep track of Bob's second level maturity, and decrement our track
|
|
|
|
// of Carol's.
|
2020-12-09 14:24:04 +03:00
|
|
|
bobSecondLevelCSV := secondLevelMaturity
|
2019-05-08 10:31:14 +03:00
|
|
|
carolSecondLevelCSV--
|
|
|
|
|
2019-06-12 11:44:22 +03:00
|
|
|
// Now that the preimage from Bob has hit the chain, restart Alice to
|
|
|
|
// ensure she'll pick it up.
|
2020-10-26 16:21:09 +03:00
|
|
|
err = restartAlice()
|
|
|
|
require.NoError(t.t, err)
|
2019-06-12 11:44:22 +03:00
|
|
|
|
2019-05-08 10:31:14 +03:00
|
|
|
// If we then mine 3 additional blocks, Carol's second level tx should
|
|
|
|
// mature, and she can pull the funds from it with a sweep tx.
|
2020-10-26 16:21:09 +03:00
|
|
|
_, err = net.Miner.Node.Generate(carolSecondLevelCSV)
|
|
|
|
require.NoError(t.t, err)
|
2019-05-08 10:31:14 +03:00
|
|
|
bobSecondLevelCSV -= carolSecondLevelCSV
|
|
|
|
|
|
|
|
carolSweep, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// Mining one additional block, Bob's second level tx is mature, and he
|
|
|
|
// can sweep the output.
|
|
|
|
block = mineBlocks(t, net, bobSecondLevelCSV, 1)[0]
|
|
|
|
assertTxInBlock(t, block, carolSweep)
|
|
|
|
|
|
|
|
bobSweep, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// Make sure it spends from the second level tx.
|
|
|
|
tx, err = net.Miner.Node.GetRawTransaction(bobSweep)
|
2020-10-26 16:21:09 +03:00
|
|
|
require.NoError(t.t, err)
|
|
|
|
require.Equal(
|
|
|
|
t.t, *bobSecondLvlTx, tx.MsgTx().TxIn[0].PreviousOutPoint.Hash,
|
|
|
|
)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// When we mine one additional block, that will confirm Bob's sweep.
|
|
|
|
// Now Bob should have no pending channels anymore, as this just
|
|
|
|
// resolved it by the confirmation of the sweep transaction.
|
|
|
|
block = mineBlocks(t, net, 1, 1)[0]
|
|
|
|
assertTxInBlock(t, block, bobSweep)
|
|
|
|
|
2020-10-26 16:21:09 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = waitForNumChannelPendingForceClose(ctxt, bob, 0, nil)
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
assertNodeNumChannels(t, bob, 0)
|
2019-05-08 10:31:14 +03:00
|
|
|
|
|
|
|
// Also Carol should have no channels left (open nor pending).
|
2020-10-26 16:21:09 +03:00
|
|
|
err = waitForNumChannelPendingForceClose(ctxt, carol, 0, nil)
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
assertNodeNumChannels(t, carol, 0)
|
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-05-08 10:31:14 +03:00
|
|
|
}
|