itest: flatten testMultiHopHtlcLocalChainClaim

This commit is contained in:
yyforyongyu 2024-10-21 13:05:41 +08:00
parent d7b2025248
commit 8dd73a08a9
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
3 changed files with 675 additions and 370 deletions

View File

@ -297,10 +297,6 @@ var allTestCases = []*lntest.TestCase{
Name: "REST API",
TestFunc: testRestAPI,
},
{
Name: "multi hop htlc local chain claim",
TestFunc: testMultiHopHtlcLocalChainClaim,
},
{
Name: "multi hop htlc remote chain claim",
TestFunc: testMultiHopHtlcRemoteChainClaim,

View File

@ -15,8 +15,11 @@ import (
)
const (
chanAmt = 1000000
htlcAmt = btcutil.Amount(300_000)
chanAmt = 1_000_000
invoiceAmt = 100_000
htlcAmt = btcutil.Amount(300_000)
incomingBroadcastDelta = lncfg.DefaultIncomingBroadcastDelta
)
var leasedType = lnrpc.CommitmentType_SCRIPT_ENFORCED_LEASE
@ -74,6 +77,18 @@ var multiHopForceCloseTestCases = []*lntest.TestCase{
Name: "multihop remote force close before timeout leased",
TestFunc: testRemoteForceCloseBeforeTimeoutLeased,
},
{
Name: "multihop local claim incoming htlc anchor",
TestFunc: testLocalClaimIncomingHTLCAnchor,
},
{
Name: "multihop local claim incoming htlc simple taproot",
TestFunc: testLocalClaimIncomingHTLCSimpleTaproot,
},
{
Name: "multihop local claim incoming htlc leased",
TestFunc: testLocalClaimIncomingHTLCLeased,
},
}
// testLocalClaimOutgoingHTLCAnchor tests `runLocalClaimOutgoingHTLC` with
@ -582,8 +597,6 @@ func runMultiHopReceiverPreimageClaim(ht *lntest.HarnessTest,
// 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
var preimage lntypes.Preimage
copy(preimage[:], ht.Random32Bytes())
payHash := preimage.Hash()
@ -634,7 +647,7 @@ func runMultiHopReceiverPreimageClaim(ht *lntest.HarnessTest,
// close her channel with Bob, broadcast the closing tx but keep it
// unconfirmed.
numBlocks := padCLTV(uint32(
invoiceReq.CltvExpiry - lncfg.DefaultIncomingBroadcastDelta,
invoiceReq.CltvExpiry - incomingBroadcastDelta,
))
// Now we'll mine enough blocks to prompt Carol to actually go to the
@ -1408,3 +1421,660 @@ func runRemoteForceCloseBeforeHtlcTimeout(ht *lntest.HarnessTest,
// correctly updated, and can no longer be settled.
ht.AssertInvoiceState(stream, lnrpc.Invoice_CANCELED)
}
// testLocalClaimIncomingHTLCAnchor tests `runLocalClaimIncomingHTLC` with
// anchor channel.
func testLocalClaimIncomingHTLCAnchor(ht *lntest.HarnessTest) {
success := ht.Run("no zero conf", func(t *testing.T) {
st := ht.Subtest(t)
// Create a three hop network: Alice -> Bob -> Carol, using
// anchor channels.
//
// Prepare params.
params := lntest.OpenChannelParams{Amt: chanAmt}
cfg := node.CfgAnchor
cfgs := [][]string{cfg, cfg, cfg}
runLocalClaimIncomingHTLC(st, cfgs, params)
})
if !success {
return
}
ht.Run("zero conf", func(t *testing.T) {
st := ht.Subtest(t)
// Create a three hop network: Alice -> Bob -> Carol, using
// zero-conf anchor channels.
//
// Prepare params.
params := lntest.OpenChannelParams{
Amt: chanAmt,
ZeroConf: true,
CommitmentType: lnrpc.CommitmentType_ANCHORS,
}
// Prepare Carol's node config to enable zero-conf and anchor.
cfg := node.CfgZeroConf
cfgs := [][]string{cfg, cfg, cfg}
runLocalClaimIncomingHTLC(st, cfgs, params)
})
}
// testLocalClaimIncomingHTLCSimpleTaproot tests `runLocalClaimIncomingHTLC`
// with simple taproot channel.
func testLocalClaimIncomingHTLCSimpleTaproot(ht *lntest.HarnessTest) {
c := lnrpc.CommitmentType_SIMPLE_TAPROOT
success := ht.Run("no zero conf", func(t *testing.T) {
st := ht.Subtest(t)
// Create a three hop network: Alice -> Bob -> Carol, using
// simple taproot channels.
//
// Prepare params.
params := lntest.OpenChannelParams{
Amt: chanAmt,
CommitmentType: c,
Private: true,
}
cfg := node.CfgSimpleTaproot
cfgs := [][]string{cfg, cfg, cfg}
runLocalClaimIncomingHTLC(st, cfgs, params)
})
if !success {
return
}
ht.Run("zero conf", func(t *testing.T) {
st := ht.Subtest(t)
// Create a three hop network: Alice -> Bob -> Carol, using
// zero-conf simple taproot channels.
//
// Prepare params.
params := lntest.OpenChannelParams{
Amt: chanAmt,
ZeroConf: true,
CommitmentType: c,
Private: true,
}
// Prepare Carol's node config to enable zero-conf and leased
// channel.
cfg := node.CfgSimpleTaproot
cfg = append(cfg, node.CfgZeroConf...)
cfgs := [][]string{cfg, cfg, cfg}
runLocalClaimIncomingHTLC(st, cfgs, params)
})
}
// runLocalClaimIncomingHTLC tests that in a multi-hop HTLC scenario, if 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.
func runLocalClaimIncomingHTLC(ht *lntest.HarnessTest,
cfgs [][]string, params lntest.OpenChannelParams) {
// Set the min relay feerate to be 10 sat/vbyte so the non-CPFP anchor
// is never swept.
//
// TODO(yy): delete this line once the normal anchor sweeping is
// removed.
ht.SetMinRelayFeerate(10_000)
// Create a three hop network: Alice -> Bob -> Carol.
chanPoints, nodes := ht.CreateSimpleNetwork(cfgs, params)
alice, bob, carol := nodes[0], nodes[1], nodes[2]
aliceChanPoint := chanPoints[0]
// Fund Carol one UTXO so she can sweep outputs.
ht.FundCoins(btcutil.SatoshiPerBitcoin, carol)
// If this is a taproot channel, then we'll need to make some manual
// route hints so Alice can actually find a route.
var routeHints []*lnrpc.RouteHint
if params.CommitmentType == lnrpc.CommitmentType_SIMPLE_TAPROOT {
routeHints = makeRouteHints(bob, carol, params.ZeroConf)
}
// 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.
preimage := ht.RandomPreimage()
payHash := preimage.Hash()
invoiceReq := &invoicesrpc.AddHoldInvoiceRequest{
Value: invoiceAmt,
CltvExpiry: finalCltvDelta,
Hash: payHash[:],
RouteHints: routeHints,
}
carolInvoice := carol.RPC.AddHoldInvoice(invoiceReq)
// Subscribe the invoice.
stream := carol.RPC.SubscribeSingleInvoice(payHash[:])
// 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.
req := &routerrpc.SendPaymentRequest{
PaymentRequest: carolInvoice.PaymentRequest,
TimeoutSeconds: 60,
FeeLimitMsat: noFeeLimitMsat,
}
alice.RPC.SendPayment(req)
// At this point, all 3 nodes should now have an active channel with
// the created HTLC pending on all of them.
ht.AssertActiveHtlcs(alice, payHash[:])
ht.AssertActiveHtlcs(bob, payHash[:])
ht.AssertActiveHtlcs(carol, payHash[:])
// 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.
ht.AssertInvoiceState(stream, lnrpc.Invoice_ACCEPTED)
// At this point, Bob decides that he wants to exit the channel
// Alice=>Bob immediately, so he force closes his commitment tx.
closeStream, _ := ht.CloseChannelAssertPending(
bob, aliceChanPoint, true,
)
// For anchor channels, the anchor won't be used for CPFP as there's no
// deadline pressure for Bob on the channel Alice->Bob at the moment.
// For Bob's local commitment tx, there's only one incoming HTLC which
// he doesn't have the preimage yet.
hasAnchorSweep := false
bobForceClose := ht.AssertStreamChannelForceClosed(
bob, aliceChanPoint, hasAnchorSweep, closeStream,
)
// Alice will offer her to_local and anchor outputs to her sweeper.
ht.AssertNumPendingSweeps(alice, 2)
// Bob will offer his anchor to his sweeper.
ht.AssertNumPendingSweeps(bob, 1)
// Assert the expected num of txns are found in the mempool.
//
// We expect to see only one sweeping tx to be published from Alice,
// which sweeps her to_local output (which is to to_remote on Bob's
// commit tx). Her anchor output won't be swept as it's uneconomical.
// For Bob, since his anchor is not used for CPFP, it'd be uneconomical
// to sweep so it will fail.
ht.AssertNumTxsInMempool(1)
// Mine a block to confirm Alice's sweeping tx.
ht.MineBlocksAndAssertNumTxes(1, 1)
// Suspend Bob to force Carol to go to chain.
restartBob := ht.SuspendNode(bob)
// 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.
carol.RPC.SettleInvoice(preimage[:])
// We now advance the block height to the point where Carol will force
// close her channel with Bob, broadcast the closing tx but keep it
// unconfirmed.
numBlocks := padCLTV(
uint32(invoiceReq.CltvExpiry - incomingBroadcastDelta),
)
// We've already mined 2 blocks at this point, so we only need to mine
// CLTV-2 blocks.
ht.MineBlocks(int(numBlocks - 2))
// Expect two txns in the mempool,
// - Carol's force close tx.
// - Carol's CPFP anchor sweeping tx.
// Mine a block to confirm them.
ht.MineBlocksAndAssertNumTxes(1, 2)
// After the force close tx is mined, Carol should offer her
// second-level success HTLC tx to her sweeper.
ht.AssertNumPendingSweeps(carol, 1)
// Restart bob again.
require.NoError(ht, restartBob())
// Once Bob is online and sees the force close tx Bob=>Carol, he will
// create a tx to sweep his commitment output. His anchor outputs will
// not be swept due to uneconomical. We expect to see three sweeping
// requests,
// - the commitment output.
// - the anchor output from channel Alice=>Bob.
// - the anchor output from channel Bob=>Carol.
ht.AssertNumPendingSweeps(bob, 3)
// Mine an empty block the for neutrino backend. We need this step to
// trigger Bob's chain watcher to detect the force close tx. Deep down,
// this happens because the notification system for neutrino is very
// different from others. Specifically, when a block contains the force
// close tx is notified, these two calls,
// - RegisterBlockEpochNtfn, will notify the block first.
// - RegisterSpendNtfn, will wait for the neutrino notifier to sync to
// the block, then perform a GetUtxo, which, by the time the spend
// details are sent, the blockbeat is considered processed in Bob's
// chain watcher.
//
// TODO(yy): refactor txNotifier to fix the above issue.
if ht.IsNeutrinoBackend() {
ht.MineEmptyBlocks(1)
}
// Assert txns can be found in the mempool.
//
// Carol will broadcast her sweeping tx and Bob will sweep his
// commitment anchor output, we'd expect to see two txns,
// - Carol's second level HTLC tx.
// - Bob's commitment output sweeping tx.
ht.AssertNumTxsInMempool(2)
// At this point we suspend Alice to make sure she'll handle the
// on-chain settle after a restart.
restartAlice := ht.SuspendNode(alice)
// Mine a block to confirm the sweeping txns made by Bob and Carol.
ht.MineBlocksAndAssertNumTxes(1, 2)
// When Bob notices Carol's second level tx 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, which means Bob has
// three sweeping requests,
// - the second level HTLC tx from channel Alice=>Bob.
// - the anchor output from channel Alice=>Bob.
// - the anchor output from channel Bob=>Carol.
ht.AssertNumPendingSweeps(bob, 3)
// Mine a block to trigger the sweep. This is needed because the
// preimage extraction logic from the link is not managed by the
// blockbeat, which means the preimage may be sent to the contest
// resolver after it's launched.
//
// TODO(yy): Expose blockbeat to the link layer.
ht.MineEmptyBlocks(1)
// At this point, Bob should have broadcast his second layer success
// tx, and should have sent it to his sweeper.
//
// Check Bob's second level tx.
bobSecondLvlTx := ht.GetNumTxsFromMempool(1)[0]
// It should spend from the commitment in the channel with Alice.
ht.AssertTxSpendFrom(bobSecondLvlTx, bobForceClose)
// We'll now mine a block which should confirm Bob's second layer tx.
ht.MineBlocksAndAssertNumTxes(1, 1)
// Bob should consider the channel Bob=>Carol closed, and channel
// Alice=>Bob pending close.
ht.AssertNumPendingForceClose(bob, 1)
// Now that the preimage from Bob has hit the chain, restart Alice to
// ensure she'll pick it up.
require.NoError(ht, restartAlice())
// If we then mine 1 additional block, Carol's second level tx should
// mature, and she can pull the funds from it with a sweep tx.
resp := ht.AssertNumPendingForceClose(carol, 1)[0]
require.Equal(ht, 1, len(resp.PendingHtlcs))
ht.Logf("Carol's timelock to_local output=%v, timelock on second "+
"stage htlc=%v", resp.BlocksTilMaturity,
resp.PendingHtlcs[0].BlocksTilMaturity)
ht.MineBlocks(int(resp.PendingHtlcs[0].BlocksTilMaturity))
// Carol should have one a sweep request for her second level tx.
ht.AssertNumPendingSweeps(carol, 1)
// Carol's sweep tx should be broadcast, assert it's in the mempool and
// mine it.
ht.MineBlocksAndAssertNumTxes(1, 1)
// We now mine blocks till the CSV lock on Bob's success HTLC on
// commitment Alice=>Bob expires.
resp = ht.AssertNumPendingForceClose(bob, 1)[0]
require.Equal(ht, 1, len(resp.PendingHtlcs))
ht.Logf("Bob's timelock to_local output=%v, timelock on second stage "+
"htlc=%v", resp.BlocksTilMaturity,
resp.PendingHtlcs[0].BlocksTilMaturity)
ht.MineBlocks(int(resp.PendingHtlcs[0].BlocksTilMaturity))
// Bob should have three requests in his sweeper.
// - the second level HTLC tx.
// - the anchor output from channel Alice=>Bob.
// - the anchor output from channel Bob=>Carol.
ht.AssertNumPendingSweeps(bob, 3)
// 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.
ht.MineBlocksAndAssertNumTxes(1, 1)
// All nodes should show zero pending and open channels.
for _, node := range []*node.HarnessNode{alice, bob, carol} {
ht.AssertNumPendingForceClose(node, 0)
ht.AssertNodeNumChannels(node, 0)
}
// Finally, check that the Alice's payment is correctly marked
// succeeded.
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_SUCCEEDED)
}
// testLocalClaimIncomingHTLCLeased tests `runLocalClaimIncomingHTLCLeased`
// with script enforced lease channel.
func testLocalClaimIncomingHTLCLeased(ht *lntest.HarnessTest) {
success := ht.Run("no zero conf", func(t *testing.T) {
st := ht.Subtest(t)
// Create a three hop network: Alice -> Bob -> Carol, using
// leased channels.
//
// Prepare params.
params := lntest.OpenChannelParams{
Amt: chanAmt,
CommitmentType: leasedType,
}
cfg := node.CfgLeased
cfgs := [][]string{cfg, cfg, cfg}
runLocalClaimIncomingHTLCLeased(st, cfgs, params)
})
if !success {
return
}
ht.Run("zero conf", func(t *testing.T) {
st := ht.Subtest(t)
// Create a three hop network: Alice -> Bob -> Carol, using
// zero-conf anchor channels.
//
// Prepare params.
params := lntest.OpenChannelParams{
Amt: chanAmt,
ZeroConf: true,
CommitmentType: leasedType,
}
// Prepare Carol's node config to enable zero-conf and leased
// channel.
cfg := node.CfgLeased
cfg = append(cfg, node.CfgZeroConf...)
cfgs := [][]string{cfg, cfg, cfg}
runLocalClaimIncomingHTLCLeased(st, cfgs, params)
})
}
// runLocalClaimIncomingHTLCLeased tests that in a multi-hop HTLC scenario, if
// 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.
//
// TODO(yy): simplify or remove this test as it's too complicated.
func runLocalClaimIncomingHTLCLeased(ht *lntest.HarnessTest,
cfgs [][]string, params lntest.OpenChannelParams) {
// Set the min relay feerate to be 5 sat/vbyte so the non-CPFP anchor
// is never swept.
//
// TODO(yy): delete this line once the normal anchor sweeping is
// removed.
ht.SetMinRelayFeerate(5000)
// Create a three hop network: Alice -> Bob -> Carol.
chanPoints, nodes := ht.CreateSimpleNetwork(cfgs, params)
alice, bob, carol := nodes[0], nodes[1], nodes[2]
aliceChanPoint, bobChanPoint := chanPoints[0], chanPoints[1]
// Fund Carol one UTXO so she can sweep outputs.
ht.FundCoins(btcutil.SatoshiPerBitcoin, carol)
// Carol should have enough wallet UTXOs here to sweep the HTLC in the
// end of this test. However, due to a known issue, Carol's wallet may
// report there's no UTXO available. For details,
// - https://github.com/lightningnetwork/lnd/issues/8786
//
// TODO(yy): remove this step once the issue is resolved.
ht.FundCoins(btcutil.SatoshiPerBitcoin, carol)
// 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.
preimage := ht.RandomPreimage()
payHash := preimage.Hash()
invoiceReq := &invoicesrpc.AddHoldInvoiceRequest{
Value: invoiceAmt,
CltvExpiry: finalCltvDelta,
Hash: payHash[:],
}
carolInvoice := carol.RPC.AddHoldInvoice(invoiceReq)
// Subscribe the invoice.
stream := carol.RPC.SubscribeSingleInvoice(payHash[:])
// 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.
req := &routerrpc.SendPaymentRequest{
PaymentRequest: carolInvoice.PaymentRequest,
TimeoutSeconds: 60,
FeeLimitMsat: noFeeLimitMsat,
}
alice.RPC.SendPayment(req)
// At this point, all 3 nodes should now have an active channel with
// the created HTLC pending on all of them.
ht.AssertActiveHtlcs(alice, payHash[:])
ht.AssertActiveHtlcs(bob, payHash[:])
ht.AssertActiveHtlcs(carol, payHash[:])
// 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.
ht.AssertInvoiceState(stream, lnrpc.Invoice_ACCEPTED)
// At this point, Bob decides that he wants to exit the channel
// Alice=>Bob immediately, so he force closes his commitment tx.
closeStream, _ := ht.CloseChannelAssertPending(
bob, aliceChanPoint, true,
)
// For anchor channels, the anchor won't be used for CPFP as there's no
// deadline pressure for Bob on the channel Alice->Bob at the moment.
// For Bob's local commitment tx, there's only one incoming HTLC which
// he doesn't have the preimage yet.
hasAnchorSweep := false
bobForceClose := ht.AssertStreamChannelForceClosed(
bob, aliceChanPoint, hasAnchorSweep, closeStream,
)
// Alice will offer her anchor output to her sweeper. Her commitment
// output cannot be swept yet as it has incurred an additional CLTV due
// to being the initiator of a script-enforced leased channel.
//
// This anchor output cannot be swept due to it being uneconomical.
ht.AssertNumPendingSweeps(alice, 1)
// Bob will offer his anchor to his sweeper.
//
// This anchor output cannot be swept due to it being uneconomical.
ht.AssertNumPendingSweeps(bob, 1)
// Suspend Bob to force Carol to go to chain.
restartBob := ht.SuspendNode(bob)
// 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.
carol.RPC.SettleInvoice(preimage[:])
// We now advance the block height to the point where Carol will force
// close her channel with Bob, broadcast the closing tx but keep it
// unconfirmed.
numBlocks := padCLTV(
uint32(invoiceReq.CltvExpiry - incomingBroadcastDelta),
)
ht.MineBlocks(int(numBlocks) - 1)
// Expect two txns in the mempool,
// - Carol's force close tx.
// - Carol's CPFP anchor sweeping tx.
// Mine a block to confirm them.
ht.MineBlocksAndAssertNumTxes(1, 2)
// After the force close tx is mined, Carol should offer her
// second-level success HTLC tx to her sweeper.
ht.AssertNumPendingSweeps(carol, 1)
// Restart bob again.
require.NoError(ht, restartBob())
// Once Bob is online and sees the force close tx Bob=>Carol, he will
// offer his commitment output to his sweeper, which will be skipped
// due to it being timelocked. His anchor outputs will not be swept due
// to uneconomical. We expect to see two sweeping requests,
// - the anchor output from channel Alice=>Bob.
// - the anchor output from channel Bob=>Carol.
ht.AssertNumPendingSweeps(bob, 2)
// Assert txns can be found in the mempool.
//
// Carol will broadcast her second-level HTLC sweeping txns. Bob canoot
// sweep his commitment anchor output yet due to it being CLTV locked.
ht.AssertNumTxsInMempool(1)
// At this point we suspend Alice to make sure she'll handle the
// on-chain settle after a restart.
restartAlice := ht.SuspendNode(alice)
// Mine a block to confirm the sweeping tx from Carol.
ht.MineBlocksAndAssertNumTxes(1, 1)
// When Bob notices Carol's second level tx 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, which means Bob has
// three sweeping requests,
// - the second level HTLC tx from channel Alice=>Bob.
// - the anchor output from channel Alice=>Bob.
// - the anchor output from channel Bob=>Carol.
ht.AssertNumPendingSweeps(bob, 3)
// Mine a block to trigger the sweep. This is needed because the
// preimage extraction logic from the link is not managed by the
// blockbeat, which means the preimage may be sent to the contest
// resolver after it's launched.
//
// TODO(yy): Expose blockbeat to the link layer.
ht.MineEmptyBlocks(1)
// At this point, Bob should have broadcast his second layer success
// tx, and should have sent it to his sweeper.
//
// Check Bob's second level tx.
bobSecondLvlTx := ht.GetNumTxsFromMempool(1)[0]
// It should spend from the commitment in the channel with Alice.
ht.AssertTxSpendFrom(bobSecondLvlTx, bobForceClose)
// The channel between Bob and Carol will still be pending force close
// if this is a leased channel. We'd also check the HTLC stages are
// correct in both channels.
ht.AssertNumPendingForceClose(bob, 2)
ht.AssertNumHTLCsAndStage(bob, aliceChanPoint, 1, 1)
ht.AssertNumHTLCsAndStage(bob, bobChanPoint, 1, 1)
// We'll now mine a block which should confirm Bob's second layer tx.
ht.MineBlocksAndAssertNumTxes(1, 1)
// Now that the preimage from Bob has hit the chain, restart Alice to
// ensure she'll pick it up.
require.NoError(ht, restartAlice())
// If we then mine 1 additional block, Carol's second level tx should
// mature, and she can pull the funds from it with a sweep tx.
resp := ht.AssertNumPendingForceClose(carol, 1)[0]
require.Equal(ht, 1, len(resp.PendingHtlcs))
ht.Logf("Carol's timelock to_local output=%v, timelock on second "+
"stage htlc=%v", resp.BlocksTilMaturity,
resp.PendingHtlcs[0].BlocksTilMaturity)
ht.MineBlocks(int(resp.PendingHtlcs[0].BlocksTilMaturity))
// Carol should have one a sweep request for her second level tx.
ht.AssertNumPendingSweeps(carol, 1)
// Carol's sweep tx should be broadcast, assert it's in the mempool and
// mine it.
ht.MineBlocksAndAssertNumTxes(1, 1)
// We now mine blocks till the CSV lock on Bob's success HTLC on
// commitment Alice=>Bob expires.
resp = ht.AssertChannelPendingForceClose(bob, aliceChanPoint)
require.Equal(ht, 1, len(resp.PendingHtlcs))
htlcExpiry := resp.PendingHtlcs[0].BlocksTilMaturity
ht.Logf("Bob's timelock to_local output=%v, timelock on second stage "+
"htlc=%v", resp.BlocksTilMaturity, htlcExpiry)
ht.MineBlocks(int(htlcExpiry))
// When we mine one additional block, that will confirm Bob's second
// level HTLC sweep on channel Alice=>Bob.
ht.MineBlocksAndAssertNumTxes(1, 1)
// We now mine blocks till the CLTV lock on Bob's to_local output HTLC
// on commitment Bob=>Carol expires.
resp = ht.AssertChannelPendingForceClose(bob, bobChanPoint)
require.Equal(ht, 1, len(resp.PendingHtlcs))
htlcExpiry = resp.PendingHtlcs[0].BlocksTilMaturity
ht.Logf("Bob's timelock to_local output=%v, timelock on second stage "+
"htlc=%v", resp.BlocksTilMaturity, htlcExpiry)
ht.MineBlocks(int(resp.BlocksTilMaturity))
// Bob should have three requests in his sweeper.
// - to_local output from channel Bob=>Carol.
// - the anchor output from channel Alice=>Bob, uneconomical.
// - the anchor output from channel Bob=>Carol, uneconomical.
ht.AssertNumPendingSweeps(bob, 3)
// Alice should have two requests in her sweeper,
// - the anchor output from channel Alice=>Bob, uneconomical.
// - her commitment output, now mature.
ht.AssertNumPendingSweeps(alice, 2)
// Mine a block to confirm Bob's to_local output sweep.
ht.MineBlocksAndAssertNumTxes(1, 2)
// All nodes should show zero pending and open channels.
for _, node := range []*node.HarnessNode{alice, bob, carol} {
ht.AssertNumPendingForceClose(node, 0)
ht.AssertNodeNumChannels(node, 0)
}
// Finally, check that the Alice's payment is correctly marked
// succeeded.
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_SUCCEEDED)
}

View File

@ -160,367 +160,6 @@ func runMultiHopHtlcClaimTest(ht *lntest.HarnessTest, tester caseRunner) {
}
}
// testMultiHopHtlcLocalChainClaim tests that in a multi-hop HTLC scenario, if
// 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.
func testMultiHopHtlcLocalChainClaim(ht *lntest.HarnessTest) {
runMultiHopHtlcClaimTest(ht, runMultiHopHtlcLocalChainClaim)
}
func runMultiHopHtlcLocalChainClaim(ht *lntest.HarnessTest,
alice, bob *node.HarnessNode, c lnrpc.CommitmentType, zeroConf bool) {
// First, we'll create a three hop network: Alice -> Bob -> Carol, with
// Carol refusing to actually settle or directly cancel any HTLC's
// self.
aliceChanPoint, bobChanPoint, carol := createThreeHopNetwork(
ht, alice, bob, false, c, zeroConf,
)
// For neutrino backend, we need to fund one more UTXO for Carol so she
// can sweep her outputs.
if ht.IsNeutrinoBackend() {
ht.FundCoins(btcutil.SatoshiPerBitcoin, carol)
}
// If this is a taproot channel, then we'll need to make some manual
// route hints so Alice can actually find a route.
var routeHints []*lnrpc.RouteHint
if c == lnrpc.CommitmentType_SIMPLE_TAPROOT {
routeHints = makeRouteHints(bob, carol, zeroConf)
}
// 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
var preimage lntypes.Preimage
copy(preimage[:], ht.Random32Bytes())
payHash := preimage.Hash()
invoiceReq := &invoicesrpc.AddHoldInvoiceRequest{
Value: invoiceAmt,
CltvExpiry: finalCltvDelta,
Hash: payHash[:],
RouteHints: routeHints,
}
carolInvoice := carol.RPC.AddHoldInvoice(invoiceReq)
// Subscribe the invoice.
stream := carol.RPC.SubscribeSingleInvoice(payHash[:])
// 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.
req := &routerrpc.SendPaymentRequest{
PaymentRequest: carolInvoice.PaymentRequest,
TimeoutSeconds: 60,
FeeLimitMsat: noFeeLimitMsat,
}
alice.RPC.SendPayment(req)
// At this point, all 3 nodes should now have an active channel with
// the created HTLC pending on all of them.
ht.AssertActiveHtlcs(alice, payHash[:])
ht.AssertActiveHtlcs(bob, payHash[:])
ht.AssertActiveHtlcs(carol, payHash[:])
// 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.
ht.AssertInvoiceState(stream, lnrpc.Invoice_ACCEPTED)
// blocksMined records how many blocks have mined after the creation of
// the invoice so it can be used to calculate how many more blocks need
// to be mined to trigger a force close later on.
var blocksMined uint32
// At this point, Bob decides that he wants to exit the channel
// immediately, so he force closes his commitment transaction.
closeStream, _ := ht.CloseChannelAssertPending(
bob, aliceChanPoint, true,
)
// For anchor channels, the anchor won't be used for CPFP as there's no
// deadline pressure for Bob on the channel Alice->Bob at the moment.
// For Bob's local commitment tx, there's only one incoming HTLC which
// he doesn't have the preimage yet. Thus this anchor won't be
// force-swept.
hasAnchorSweep := false
bobForceClose := ht.AssertStreamChannelForceClosed(
bob, aliceChanPoint, hasAnchorSweep, closeStream,
)
// Increase the blocks mined. At this step
// AssertStreamChannelForceClosed mines one block.
blocksMined++
var expectedTxes int
switch c {
// Alice will sweep her commitment and anchor output immediately. Bob
// will also offer his anchor to his sweeper.
case lnrpc.CommitmentType_ANCHORS, lnrpc.CommitmentType_SIMPLE_TAPROOT:
ht.AssertNumPendingSweeps(alice, 2)
ht.AssertNumPendingSweeps(bob, 1)
// We expect to see only one sweeping tx to be published from
// Alice, which sweeps her commit and anchor outputs in the
// same tx. For Bob, since his anchor is not used for CPFP,
// it'd be uneconomical to sweep so it will fail.
expectedTxes = 1
// Alice will offer her anchor output to her sweeper. Her commitment
// output cannot be swept yet as it has incurred an additional CLTV due
// to being the initiator of a script-enforced leased channel.
case lnrpc.CommitmentType_SCRIPT_ENFORCED_LEASE:
ht.AssertNumPendingSweeps(alice, 1)
ht.AssertNumPendingSweeps(bob, 1)
// We expect to see only no sweeping txns to be published,
// neither Alice's or Bob's anchor sweep can succeed due to
// it's uneconomical.
expectedTxes = 0
default:
ht.Fatalf("unhandled commitment type %v", c)
}
// Mine a block to trigger the sweeps.
ht.MineEmptyBlocks(1)
blocksMined++
// Assert the expected num of txns are found in the mempool.
ht.AssertNumTxsInMempool(expectedTxes)
// Mine a block to clean up the mempool for the rest of the test.
ht.MineBlocksAndAssertNumTxes(1, expectedTxes)
blocksMined++
// Suspend Bob to force Carol to go to chain.
restartBob := ht.SuspendNode(bob)
// 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.
carol.RPC.SettleInvoice(preimage[:])
// We now advance the block height to the point where Carol will force
// close her channel with Bob, broadcast the closing tx but keep it
// unconfirmed.
numBlocks := padCLTV(uint32(invoiceReq.CltvExpiry -
lncfg.DefaultIncomingBroadcastDelta))
ht.MineEmptyBlocks(int(numBlocks - blocksMined))
// Carol's commitment transaction should now be in the mempool.
ht.AssertNumTxsInMempool(1)
// Look up the closing transaction. It should be spending from the
// funding transaction,
closingTx := ht.AssertOutpointInMempool(
ht.OutPointFromChannelPoint(bobChanPoint),
)
closingTxid := closingTx.TxHash()
// Mine a block that should confirm the commit tx.
block := ht.MineBlocksAndAssertNumTxes(1, 1)[0]
ht.AssertTxInBlock(block, closingTxid)
// After the force close transaction is mined, Carol should offer her
// second-level success HTLC tx and anchor to the sweeper.
ht.AssertNumPendingSweeps(carol, 2)
// Restart bob again.
require.NoError(ht, restartBob())
// Lower the fee rate so Bob's two anchor outputs are economical to
// be swept in one tx.
ht.SetFeeEstimate(chainfee.FeePerKwFloor)
// After the force close transaction is mined, transactions will be
// broadcast by both Bob and Carol.
switch c {
// Carol will broadcast her sweeping txns and Bob will sweep his
// commitment and anchor outputs, we'd expect to see three txns,
// - Carol's second level HTLC transaction.
// - Carol's anchor sweeping txns since it's used for CPFP.
// - Bob's sweep tx spending his commitment output, and two anchor
// outputs, one from channel Alice to Bob and the other from channel
// Bob to Carol.
case lnrpc.CommitmentType_ANCHORS, lnrpc.CommitmentType_SIMPLE_TAPROOT:
ht.AssertNumPendingSweeps(bob, 3)
expectedTxes = 3
// Carol will broadcast her sweeping txns and Bob will sweep his
// anchor outputs. Bob can't sweep his commitment output yet as it has
// incurred an additional CLTV due to being the initiator of a
// script-enforced leased channel:
// - Carol's second level HTLC transaction.
// - Carol's anchor sweeping txns since it's used for CPFP.
// - Bob's sweep tx spending his two anchor outputs, one from channel
// Alice to Bob and the other from channel Bob to Carol.
case lnrpc.CommitmentType_SCRIPT_ENFORCED_LEASE:
ht.AssertNumPendingSweeps(bob, 2)
expectedTxes = 3
default:
ht.Fatalf("unhandled commitment type %v", c)
}
// Mine a block to trigger the sweeps.
ht.MineEmptyBlocks(1)
// Assert transactions can be found in the mempool.
ht.AssertNumTxsInMempool(expectedTxes)
// At this point we suspend Alice to make sure she'll handle the
// on-chain settle after a restart.
restartAlice := ht.SuspendNode(alice)
// Mine a block to confirm the expected transactions (+ the coinbase).
ht.MineBlocksAndAssertNumTxes(1, expectedTxes)
// For 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.
secondLevelMaturity := uint32(defaultCSV - 1)
// Keep track of the second level tx maturity.
carolSecondLevelCSV := secondLevelMaturity
// 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.
ht.AssertNumPendingSweeps(bob, 1)
// Mine a block to trigger the sweep of the second level tx.
ht.MineEmptyBlocks(1)
carolSecondLevelCSV--
// Check Bob's second level tx.
bobSecondLvlTx := ht.GetNumTxsFromMempool(1)[0]
// It should spend from the commitment in the channel with Alice.
ht.AssertTxSpendFrom(bobSecondLvlTx, bobForceClose)
// At this point, Bob should have broadcast his second layer success
// transaction, and should have sent it to the nursery for incubation.
ht.AssertNumHTLCsAndStage(bob, aliceChanPoint, 1, 1)
// The channel between Bob and Carol will still be pending force close
// if this is a leased channel. In that case, we'd also check the HTLC
// stages are correct in that channel.
if c == lnrpc.CommitmentType_SCRIPT_ENFORCED_LEASE {
ht.AssertNumPendingForceClose(bob, 2)
ht.AssertNumHTLCsAndStage(bob, bobChanPoint, 1, 1)
} else {
ht.AssertNumPendingForceClose(bob, 1)
}
// We'll now mine a block which should confirm Bob's second layer
// transaction.
ht.MineBlocksAndAssertNumTxes(1, 1)
// Keep track of Bob's second level maturity, and decrement our track
// of Carol's.
bobSecondLevelCSV := secondLevelMaturity
carolSecondLevelCSV--
// Now that the preimage from Bob has hit the chain, restart Alice to
// ensure she'll pick it up.
require.NoError(ht, restartAlice())
// If we then mine 1 additional blocks, Carol's second level tx should
// mature, and she can pull the funds from it with a sweep tx.
ht.MineEmptyBlocks(int(carolSecondLevelCSV))
bobSecondLevelCSV -= carolSecondLevelCSV
// Carol should have one a sweep request for her second level tx.
ht.AssertNumPendingSweeps(carol, 1)
// Mine a block to trigger the sweep.
ht.MineEmptyBlocks(1)
bobSecondLevelCSV--
// Carol's sweep tx should be broadcast.
carolSweep := ht.AssertNumTxsInMempool(1)[0]
// Bob should offer his second level tx to his sweeper.
ht.AssertNumPendingSweeps(bob, 1)
// Mining one additional block, Bob's second level tx is mature, and he
// can sweep the output.
block = ht.MineBlocksAndAssertNumTxes(bobSecondLevelCSV, 1)[0]
ht.AssertTxInBlock(block, carolSweep)
bobSweep := ht.GetNumTxsFromMempool(1)[0]
bobSweepTxid := bobSweep.TxHash()
// 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 = ht.MineBlocksAndAssertNumTxes(1, 1)[0]
ht.AssertTxInBlock(block, bobSweepTxid)
// With the script-enforced lease commitment type, Alice and Bob still
// haven't been able to sweep their respective commit outputs due to the
// additional CLTV. We'll need to mine enough blocks for the timelock to
// expire and prompt their sweep.
if c == lnrpc.CommitmentType_SCRIPT_ENFORCED_LEASE {
for _, node := range []*node.HarnessNode{alice, bob} {
ht.AssertNumPendingForceClose(node, 1)
}
// Due to the way the test is set up, Alice and Bob share the
// same CLTV for their commit outputs even though it's enforced
// on different channels (Alice-Bob and Bob-Carol).
resp := alice.RPC.PendingChannels()
require.Len(ht, resp.PendingForceClosingChannels, 1)
forceCloseChan := resp.PendingForceClosingChannels[0]
require.Positive(ht, forceCloseChan.BlocksTilMaturity)
// Mine enough blocks for the timelock to expire.
numBlocks := uint32(forceCloseChan.BlocksTilMaturity)
ht.MineEmptyBlocks(int(numBlocks))
// Both Alice and Bob should now offer their commit outputs to
// the sweeper. For Alice, she still has her anchor output as
// pending sweep as it's not used for CPFP, thus it's
// uneconomical to sweep it alone.
ht.AssertNumPendingSweeps(alice, 2)
ht.AssertNumPendingSweeps(bob, 1)
// Mine a block to trigger the sweeps.
ht.MineEmptyBlocks(1)
// Both Alice and Bob show broadcast their commit sweeps.
aliceCommitOutpoint := wire.OutPoint{
Hash: bobForceClose, Index: 3,
}
ht.AssertOutpointInMempool(
aliceCommitOutpoint,
).TxHash()
bobCommitOutpoint := wire.OutPoint{Hash: closingTxid, Index: 3}
ht.AssertOutpointInMempool(
bobCommitOutpoint,
).TxHash()
// Confirm their sweeps.
ht.MineBlocksAndAssertNumTxes(1, 2)
}
// All nodes should show zero pending and open channels.
for _, node := range []*node.HarnessNode{alice, bob, carol} {
ht.AssertNumPendingForceClose(node, 0)
ht.AssertNodeNumChannels(node, 0)
}
// Finally, check that the Alice's payment is correctly marked
// succeeded.
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_SUCCEEDED)
}
// 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