Merge pull request #4779 from halseth/anchor-htlc-aggregation
[anchors] HTLC second level aggregation in the sweeper
This commit is contained in:
27 files changed
+3128
-436
No files matched your search
@@ -0,0 +1,427 @@
|
||||
package itest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/lightningnetwork/lnd"
|
||||
"github.com/lightningnetwork/lnd/lncfg"
|
||||
"github.com/lightningnetwork/lnd/lnrpc"
|
||||
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
|
||||
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
|
||||
"github.com/lightningnetwork/lnd/lntest"
|
||||
"github.com/lightningnetwork/lnd/lntest/wait"
|
||||
"github.com/lightningnetwork/lnd/lntypes"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// testMultiHopHtlcAggregation tests that in a multi-hop HTLC scenario, if we
|
||||
// force close a channel with both incoming and outgoing HTLCs, we can properly
|
||||
// resolve them using the second level timeout and success transactions. In
|
||||
// case of anchor channels, the second-level spends can also be aggregated and
|
||||
// properly feebumped, so we'll check that as well.
|
||||
func testMultiHopHtlcAggregation(net *lntest.NetworkHarness, t *harnessTest,
|
||||
alice, bob *lntest.HarnessNode, c commitType) {
|
||||
|
||||
const finalCltvDelta = 40
|
||||
ctxb := context.Background()
|
||||
|
||||
// First, we'll create a three hop network: Alice -> Bob -> Carol.
|
||||
aliceChanPoint, bobChanPoint, carol := createThreeHopNetwork(
|
||||
t, net, alice, bob, false, c,
|
||||
)
|
||||
defer shutdownAndAssert(net, t, carol)
|
||||
|
||||
// To ensure we have capacity in both directions of the route, we'll
|
||||
// make a fairly large payment Alice->Carol and settle it.
|
||||
const reBalanceAmt = 500_000
|
||||
invoice := &lnrpc.Invoice{
|
||||
Value: reBalanceAmt,
|
||||
}
|
||||
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
||||
resp, err := carol.AddInvoice(ctxt, invoice)
|
||||
require.NoError(t.t, err)
|
||||
|
||||
sendReq := &routerrpc.SendPaymentRequest{
|
||||
PaymentRequest: resp.PaymentRequest,
|
||||
TimeoutSeconds: 60,
|
||||
FeeLimitMsat: noFeeLimitMsat,
|
||||
}
|
||||
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
||||
stream, err := alice.RouterClient.SendPaymentV2(ctxt, sendReq)
|
||||
require.NoError(t.t, err)
|
||||
|
||||
result, err := getPaymentResult(stream)
|
||||
require.NoError(t.t, err)
|
||||
require.Equal(t.t, result.Status, lnrpc.Payment_SUCCEEDED)
|
||||
|
||||
// With the network active, we'll now add a new hodl invoices at both
|
||||
// Alice's and Carol's end. Make sure the cltv expiry delta is large
|
||||
// enough, otherwise Bob won't send out the outgoing htlc.
|
||||
const numInvoices = 5
|
||||
const invoiceAmt = 50_000
|
||||
|
||||
var (
|
||||
carolInvoices []*invoicesrpc.AddHoldInvoiceResp
|
||||
aliceInvoices []*invoicesrpc.AddHoldInvoiceResp
|
||||
alicePreimages []lntypes.Preimage
|
||||
payHashes [][]byte
|
||||
alicePayHashes [][]byte
|
||||
carolPayHashes [][]byte
|
||||
)
|
||||
|
||||
// Add Carol invoices.
|
||||
for i := 0; i < numInvoices; i++ {
|
||||
preimage := lntypes.Preimage{1, 1, 1, byte(i)}
|
||||
payHash := preimage.Hash()
|
||||
invoiceReq := &invoicesrpc.AddHoldInvoiceRequest{
|
||||
Value: invoiceAmt,
|
||||
CltvExpiry: finalCltvDelta,
|
||||
Hash: payHash[:],
|
||||
}
|
||||
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
|
||||
defer cancel()
|
||||
carolInvoice, err := carol.AddHoldInvoice(ctxt, invoiceReq)
|
||||
require.NoError(t.t, err)
|
||||
|
||||
carolInvoices = append(carolInvoices, carolInvoice)
|
||||
payHashes = append(payHashes, payHash[:])
|
||||
carolPayHashes = append(carolPayHashes, payHash[:])
|
||||
}
|
||||
|
||||
// We'll give Alice's invoices a longer CLTV expiry, to ensure the
|
||||
// channel Bob<->Carol will be closed first.
|
||||
for i := 0; i < numInvoices; i++ {
|
||||
preimage := lntypes.Preimage{2, 2, 2, byte(i)}
|
||||
payHash := preimage.Hash()
|
||||
invoiceReq := &invoicesrpc.AddHoldInvoiceRequest{
|
||||
Value: invoiceAmt,
|
||||
CltvExpiry: 2 * finalCltvDelta,
|
||||
Hash: payHash[:],
|
||||
}
|
||||
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
|
||||
defer cancel()
|
||||
aliceInvoice, err := alice.AddHoldInvoice(ctxt, invoiceReq)
|
||||
require.NoError(t.t, err)
|
||||
|
||||
aliceInvoices = append(aliceInvoices, aliceInvoice)
|
||||
alicePreimages = append(alicePreimages, preimage)
|
||||
payHashes = append(payHashes, payHash[:])
|
||||
alicePayHashes = append(alicePayHashes, payHash[:])
|
||||
}
|
||||
|
||||
// Now that we've created the invoices, we'll pay them all from
|
||||
// Alice<->Carol, going through Bob. We won't wait for the response
|
||||
// however, as neither will immediately settle the payment.
|
||||
ctx, cancel := context.WithCancel(ctxb)
|
||||
defer cancel()
|
||||
|
||||
// Alice will pay all of Carol's invoices.
|
||||
for _, carolInvoice := range carolInvoices {
|
||||
_, err = alice.RouterClient.SendPaymentV2(
|
||||
ctx, &routerrpc.SendPaymentRequest{
|
||||
PaymentRequest: carolInvoice.PaymentRequest,
|
||||
TimeoutSeconds: 60,
|
||||
FeeLimitMsat: noFeeLimitMsat,
|
||||
},
|
||||
)
|
||||
require.NoError(t.t, err)
|
||||
}
|
||||
|
||||
// And Carol will pay Alice's.
|
||||
for _, aliceInvoice := range aliceInvoices {
|
||||
_, err = carol.RouterClient.SendPaymentV2(
|
||||
ctx, &routerrpc.SendPaymentRequest{
|
||||
PaymentRequest: aliceInvoice.PaymentRequest,
|
||||
TimeoutSeconds: 60,
|
||||
FeeLimitMsat: noFeeLimitMsat,
|
||||
},
|
||||
)
|
||||
require.NoError(t.t, err)
|
||||
}
|
||||
|
||||
// At this point, all 3 nodes should now the HTLCs active on their
|
||||
// channels.
|
||||
nodes := []*lntest.HarnessNode{alice, bob, carol}
|
||||
err = wait.NoError(func() error {
|
||||
return assertActiveHtlcs(nodes, payHashes...)
|
||||
}, defaultTimeout)
|
||||
require.NoError(t.t, err)
|
||||
|
||||
// Wait for Alice and Carol to mark the invoices as accepted. There is
|
||||
// a small gap to bridge between adding the htlc to the channel and
|
||||
// executing the exit hop logic.
|
||||
for _, payHash := range carolPayHashes {
|
||||
h := lntypes.Hash{}
|
||||
copy(h[:], payHash)
|
||||
waitForInvoiceAccepted(t, carol, h)
|
||||
}
|
||||
|
||||
for _, payHash := range alicePayHashes {
|
||||
h := lntypes.Hash{}
|
||||
copy(h[:], payHash)
|
||||
waitForInvoiceAccepted(t, alice, h)
|
||||
}
|
||||
|
||||
// Increase the fee estimate so that the following force close tx will
|
||||
// be cpfp'ed.
|
||||
net.SetFeeEstimate(30000)
|
||||
|
||||
// We'll now mine enough blocks to trigger Bob's broadcast of his
|
||||
// commitment transaction due to the fact that the Carol's HTLCs are
|
||||
// about to timeout. With the default outgoing broadcast delta of zero,
|
||||
// this will be the same height as the htlc expiry height.
|
||||
numBlocks := padCLTV(
|
||||
uint32(finalCltvDelta - lncfg.DefaultOutgoingBroadcastDelta),
|
||||
)
|
||||
_, err = net.Miner.Node.Generate(numBlocks)
|
||||
require.NoError(t.t, err)
|
||||
|
||||
// Bob's force close transaction should now be found in the mempool. If
|
||||
// there are anchors, we also expect Bob's anchor sweep.
|
||||
expectedTxes := 1
|
||||
if c == commitTypeAnchors {
|
||||
expectedTxes = 2
|
||||
}
|
||||
|
||||
bobFundingTxid, err := lnd.GetChanPointFundingTxid(bobChanPoint)
|
||||
require.NoError(t.t, err)
|
||||
_, err = waitForNTxsInMempool(
|
||||
net.Miner.Node, expectedTxes, minerMempoolTimeout,
|
||||
)
|
||||
require.NoError(t.t, err)
|
||||
closeTx := getSpendingTxInMempool(
|
||||
t, net.Miner.Node, minerMempoolTimeout, wire.OutPoint{
|
||||
Hash: *bobFundingTxid,
|
||||
Index: bobChanPoint.OutputIndex,
|
||||
},
|
||||
)
|
||||
closeTxid := closeTx.TxHash()
|
||||
|
||||
// Go through the closing transaction outputs, and make an index for the HTLC outputs.
|
||||
successOuts := make(map[wire.OutPoint]struct{})
|
||||
timeoutOuts := make(map[wire.OutPoint]struct{})
|
||||
for i, txOut := range closeTx.TxOut {
|
||||
op := wire.OutPoint{
|
||||
Hash: closeTxid,
|
||||
Index: uint32(i),
|
||||
}
|
||||
|
||||
switch txOut.Value {
|
||||
// If this HTLC goes towards Carol, Bob will claim it with a
|
||||
// timeout Tx. In this case the value will be the invoice
|
||||
// amount.
|
||||
case invoiceAmt:
|
||||
timeoutOuts[op] = struct{}{}
|
||||
|
||||
// If the HTLC has direction towards Alice, Bob will
|
||||
// claim it with the success TX when he learns the preimage. In
|
||||
// this case one extra sat will be on the output, because of
|
||||
// the routing fee.
|
||||
case invoiceAmt + 1:
|
||||
successOuts[op] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// Mine a block to confirm the closing transaction.
|
||||
mineBlocks(t, net, 1, expectedTxes)
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
// Let Alice settle her invoices. When Bob now gets the preimages, he
|
||||
// has no other option than to broadcast his second-level transactions
|
||||
// to claim the money.
|
||||
for _, preimage := range alicePreimages {
|
||||
ctx, cancel = context.WithTimeout(ctxb, defaultTimeout)
|
||||
defer cancel()
|
||||
_, err = alice.SettleInvoice(ctx, &invoicesrpc.SettleInvoiceMsg{
|
||||
Preimage: preimage[:],
|
||||
})
|
||||
require.NoError(t.t, err)
|
||||
}
|
||||
|
||||
// With the closing transaction confirmed, we should expect Bob's HTLC
|
||||
// timeout transactions to be broadcast due to the expiry being reached.
|
||||
// We will also expect the success transactions, since he learnt the
|
||||
// preimages from Alice. We also expect Carol to sweep her commitment
|
||||
// output.
|
||||
expectedTxes = 2*numInvoices + 1
|
||||
|
||||
// In case of anchors, all success transactions will be aggregated into
|
||||
// one, the same is the case for the timeout transactions. In this case
|
||||
// Carol will also sweep her anchor output in a separate tx (since it
|
||||
// will be low fee).
|
||||
if c == commitTypeAnchors {
|
||||
expectedTxes = 4
|
||||
}
|
||||
|
||||
txes, err := getNTxsFromMempool(
|
||||
net.Miner.Node, expectedTxes, minerMempoolTimeout,
|
||||
)
|
||||
require.NoError(t.t, err)
|
||||
|
||||
// Since Bob can aggregate the transactions, we expect a single
|
||||
// transaction, that have multiple spends from the commitment.
|
||||
var (
|
||||
timeoutTxs []*chainhash.Hash
|
||||
successTxs []*chainhash.Hash
|
||||
)
|
||||
for _, tx := range txes {
|
||||
txid := tx.TxHash()
|
||||
|
||||
for i := range tx.TxIn {
|
||||
prevOp := tx.TxIn[i].PreviousOutPoint
|
||||
if _, ok := successOuts[prevOp]; ok {
|
||||
successTxs = append(successTxs, &txid)
|
||||
break
|
||||
}
|
||||
|
||||
if _, ok := timeoutOuts[prevOp]; ok {
|
||||
timeoutTxs = append(timeoutTxs, &txid)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// In case of anchor we expect all the timeout and success second
|
||||
// levels to be aggregated into one tx. For earlier channel types, they
|
||||
// will be separate transactions.
|
||||
if c == commitTypeAnchors {
|
||||
require.Len(t.t, timeoutTxs, 1)
|
||||
require.Len(t.t, successTxs, 1)
|
||||
} else {
|
||||
require.Len(t.t, timeoutTxs, numInvoices)
|
||||
require.Len(t.t, successTxs, numInvoices)
|
||||
|
||||
}
|
||||
|
||||
// All mempool transactions should be spending from the commitment
|
||||
// transaction.
|
||||
assertAllTxesSpendFrom(t, txes, closeTxid)
|
||||
|
||||
// Mine a block to confirm the transactions.
|
||||
block := mineBlocks(t, net, 1, expectedTxes)[0]
|
||||
require.Len(t.t, block.Transactions, expectedTxes+1)
|
||||
|
||||
// At this point, Bob should have broadcast his second layer success
|
||||
// transaction, and should have sent it to the nursery for incubation,
|
||||
// or to the sweeper for sweeping.
|
||||
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
||||
err = waitForNumChannelPendingForceClose(
|
||||
ctxt, bob, 1, func(c *lnrpcForceCloseChannel) error {
|
||||
if c.Channel.LocalBalance != 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(c.PendingHtlcs) != 1 {
|
||||
return fmt.Errorf("bob should have pending " +
|
||||
"htlc but doesn't")
|
||||
}
|
||||
|
||||
if c.PendingHtlcs[0].Stage != 1 {
|
||||
return fmt.Errorf("bob's htlc should have "+
|
||||
"advanced to the first stage but was "+
|
||||
"stage: %v", c.PendingHtlcs[0].Stage)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
require.NoError(t.t, err)
|
||||
|
||||
// If we then mine additional blocks, Bob can sweep his commitment
|
||||
// output.
|
||||
_, err = net.Miner.Node.Generate(defaultCSV - 2)
|
||||
require.NoError(t.t, err)
|
||||
|
||||
// Find the commitment sweep.
|
||||
bobCommitSweepHash, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
||||
require.NoError(t.t, err)
|
||||
bobCommitSweep, err := net.Miner.Node.GetRawTransaction(bobCommitSweepHash)
|
||||
require.NoError(t.t, err)
|
||||
|
||||
require.Equal(
|
||||
t.t, closeTxid, bobCommitSweep.MsgTx().TxIn[0].PreviousOutPoint.Hash,
|
||||
)
|
||||
|
||||
// Also ensure it is not spending from any of the HTLC output.
|
||||
for _, txin := range bobCommitSweep.MsgTx().TxIn {
|
||||
for _, timeoutTx := range timeoutTxs {
|
||||
if *timeoutTx == txin.PreviousOutPoint.Hash {
|
||||
t.Fatalf("found unexpected spend of timeout tx")
|
||||
}
|
||||
}
|
||||
|
||||
for _, successTx := range successTxs {
|
||||
if *successTx == txin.PreviousOutPoint.Hash {
|
||||
t.Fatalf("found unexpected spend of success tx")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch {
|
||||
|
||||
// Mining one additional block, Bob's second level tx is mature, and he
|
||||
// can sweep the output.
|
||||
case c == commitTypeAnchors:
|
||||
_ = mineBlocks(t, net, 1, 1)
|
||||
|
||||
// In case this is a non-anchor channel type, we must mine 2 blocks, as
|
||||
// the nursery waits an extra block before sweeping.
|
||||
default:
|
||||
_ = mineBlocks(t, net, 2, 1)
|
||||
}
|
||||
|
||||
bobSweep, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
||||
require.NoError(t.t, err)
|
||||
|
||||
// Make sure it spends from the second level tx.
|
||||
secondLevelSweep, err := net.Miner.Node.GetRawTransaction(bobSweep)
|
||||
require.NoError(t.t, err)
|
||||
|
||||
// It should be sweeping all the second-level outputs.
|
||||
var secondLvlSpends int
|
||||
for _, txin := range secondLevelSweep.MsgTx().TxIn {
|
||||
for _, timeoutTx := range timeoutTxs {
|
||||
if *timeoutTx == txin.PreviousOutPoint.Hash {
|
||||
secondLvlSpends++
|
||||
}
|
||||
}
|
||||
|
||||
for _, successTx := range successTxs {
|
||||
if *successTx == txin.PreviousOutPoint.Hash {
|
||||
secondLvlSpends++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
require.Equal(t.t, 2*numInvoices, secondLvlSpends)
|
||||
|
||||
// When we mine one additional block, that will confirm Bob's second
|
||||
// level 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)
|
||||
|
||||
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
||||
err = waitForNumChannelPendingForceClose(ctxt, bob, 0, nil)
|
||||
require.NoError(t.t, err)
|
||||
|
||||
// THe channel with Alice is still open.
|
||||
assertNodeNumChannels(t, bob, 1)
|
||||
|
||||
// Carol should have no channels left (open nor pending).
|
||||
err = waitForNumChannelPendingForceClose(ctxt, carol, 0, nil)
|
||||
require.NoError(t.t, err)
|
||||
assertNodeNumChannels(t, carol, 0)
|
||||
|
||||
// Coop close channel, expect no anchors.
|
||||
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
||||
closeChannelAndAssertType(
|
||||
ctxt, t, net, alice, aliceChanPoint, false, false,
|
||||
)
|
||||
}
|
||||
@@ -184,8 +184,24 @@ func testMultiHopHtlcLocalChainClaim(net *lntest.NetworkHarness, t *harnessTest,
|
||||
block = mineBlocks(t, net, 1, expectedTxes)[0]
|
||||
require.Len(t.t, block.Transactions, expectedTxes+1)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// Keep track of the second level tx maturity.
|
||||
carolSecondLevelCSV := uint32(defaultCSV)
|
||||
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
|
||||
@@ -236,7 +252,7 @@ func testMultiHopHtlcLocalChainClaim(net *lntest.NetworkHarness, t *harnessTest,
|
||||
|
||||
// Keep track of Bob's second level maturity, and decrement our track
|
||||
// of Carol's.
|
||||
bobSecondLevelCSV := uint32(defaultCSV)
|
||||
bobSecondLevelCSV := secondLevelMaturity
|
||||
carolSecondLevelCSV--
|
||||
|
||||
// Now that the preimage from Bob has hit the chain, restart Alice to
|
||||
|
||||
@@ -43,7 +43,7 @@ func testMultiHopHtlcLocalTimeout(net *lntest.NetworkHarness, t *harnessTest,
|
||||
// while the second will be a proper fully valued HTLC.
|
||||
const (
|
||||
dustHtlcAmt = btcutil.Amount(100)
|
||||
htlcAmt = btcutil.Amount(30000)
|
||||
htlcAmt = btcutil.Amount(300_000)
|
||||
finalCltvDelta = 40
|
||||
)
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ func testMultiHopLocalForceCloseOnChainHtlcTimeout(net *lntest.NetworkHarness,
|
||||
// opens up the base for out tests.
|
||||
const (
|
||||
finalCltvDelta = 40
|
||||
htlcAmt = btcutil.Amount(30000)
|
||||
htlcAmt = btcutil.Amount(300_000)
|
||||
)
|
||||
ctx, cancel := context.WithCancel(ctxb)
|
||||
defer cancel()
|
||||
|
||||
@@ -61,6 +61,11 @@ func testMultiHopHtlcClaims(net *lntest.NetworkHarness, t *harnessTest) {
|
||||
name: "remote chain claim",
|
||||
test: testMultiHopHtlcRemoteChainClaim,
|
||||
},
|
||||
{
|
||||
// bob: outgoing and incoming, sweep all on chain
|
||||
name: "local htlc aggregation",
|
||||
test: testMultiHopHtlcAggregation,
|
||||
},
|
||||
}
|
||||
|
||||
commitTypes := []commitType{
|
||||
|
||||
+132
-70
@@ -3640,6 +3640,16 @@ func channelForceClosureTest(net *lntest.NetworkHarness, t *harnessTest,
|
||||
htlcCsvMaturityHeight = padCLTV(startHeight + defaultCLTV + 1 + defaultCSV)
|
||||
)
|
||||
|
||||
// If we are dealing with an anchor channel type, the sweeper will
|
||||
// sweep the HTLC second level output one block earlier (than the
|
||||
// nursery that waits an additional block, and handles non-anchor
|
||||
// channels). So we set a maturity height that is one less.
|
||||
if channelType == commitTypeAnchors {
|
||||
htlcCsvMaturityHeight = padCLTV(
|
||||
startHeight + defaultCLTV + defaultCSV,
|
||||
)
|
||||
}
|
||||
|
||||
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
||||
aliceChan, err := getChanInfo(ctxt, alice)
|
||||
if err != nil {
|
||||
@@ -4157,79 +4167,125 @@ func channelForceClosureTest(net *lntest.NetworkHarness, t *harnessTest,
|
||||
|
||||
// Since Alice had numInvoices (6) htlcs extended to Carol before force
|
||||
// closing, we expect Alice to broadcast an htlc timeout txn for each
|
||||
// one. Wait for them all to show up in the mempool.
|
||||
htlcTxIDs, err := waitForNTxsInMempool(net.Miner.Node, numInvoices,
|
||||
minerMempoolTimeout)
|
||||
// one.
|
||||
expectedTxes = numInvoices
|
||||
|
||||
// In case of anchors, the timeout txs will be aggregated into one.
|
||||
if channelType == commitTypeAnchors {
|
||||
expectedTxes = 1
|
||||
}
|
||||
|
||||
// Wait for them all to show up in the mempool.
|
||||
htlcTxIDs, err := waitForNTxsInMempool(
|
||||
net.Miner.Node, expectedTxes, minerMempoolTimeout,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to find htlc timeout txns in mempool: %v", err)
|
||||
}
|
||||
|
||||
// Retrieve each htlc timeout txn from the mempool, and ensure it is
|
||||
// well-formed. This entails verifying that each only spends from
|
||||
// output, and that that output is from the commitment txn. We do not
|
||||
// the sweeper check for these timeout transactions because they are
|
||||
// not swept by the sweeper; the nursery broadcasts the pre-signed
|
||||
// transaction.
|
||||
// output, and that that output is from the commitment txn. In case
|
||||
// this is an anchor channel, the transactions are aggregated by the
|
||||
// sweeper into one.
|
||||
numInputs := 1
|
||||
if channelType == commitTypeAnchors {
|
||||
numInputs = numInvoices + 1
|
||||
}
|
||||
|
||||
// Construct a map of the already confirmed htlc timeout outpoints,
|
||||
// that will count the number of times each is spent by the sweep txn.
|
||||
// We prepopulate it in this way so that we can later detect if we are
|
||||
// spending from an output that was not a confirmed htlc timeout txn.
|
||||
var htlcTxOutpointSet = make(map[wire.OutPoint]int)
|
||||
|
||||
var htlcLessFees uint64
|
||||
for _, htlcTxID := range htlcTxIDs {
|
||||
// Fetch the sweep transaction, all input it's spending should
|
||||
// be from the commitment transaction which was broadcast
|
||||
// on-chain.
|
||||
// on-chain. In case of an anchor type channel, we expect one
|
||||
// extra input that is not spending from the commitment, that
|
||||
// is added for fees.
|
||||
htlcTx, err := net.Miner.Node.GetRawTransaction(htlcTxID)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to fetch sweep tx: %v", err)
|
||||
}
|
||||
// Ensure the htlc transaction only has one input.
|
||||
|
||||
// Ensure the htlc transaction has the expected number of
|
||||
// inputs.
|
||||
inputs := htlcTx.MsgTx().TxIn
|
||||
if len(inputs) != 1 {
|
||||
t.Fatalf("htlc transaction should only have one txin, "+
|
||||
"has %d", len(htlcTx.MsgTx().TxIn))
|
||||
}
|
||||
// Ensure the htlc transaction is spending from the commitment
|
||||
// transaction.
|
||||
txIn := inputs[0]
|
||||
if !closingTxID.IsEqual(&txIn.PreviousOutPoint.Hash) {
|
||||
t.Fatalf("htlc transaction not spending from commit "+
|
||||
"tx %v, instead spending %v",
|
||||
closingTxID, txIn.PreviousOutPoint)
|
||||
if len(inputs) != numInputs {
|
||||
t.Fatalf("htlc transaction should only have %d txin, "+
|
||||
"has %d", numInputs, len(htlcTx.MsgTx().TxIn))
|
||||
}
|
||||
|
||||
// The number of outputs should be the same.
|
||||
outputs := htlcTx.MsgTx().TxOut
|
||||
if len(outputs) != 1 {
|
||||
t.Fatalf("htlc transaction should only have one "+
|
||||
"txout, has: %v", len(outputs))
|
||||
if len(outputs) != numInputs {
|
||||
t.Fatalf("htlc transaction should only have %d"+
|
||||
"txout, has: %v", numInputs, len(outputs))
|
||||
}
|
||||
|
||||
// For each htlc timeout transaction, we expect a resolver
|
||||
// report recording this on chain resolution for both alice and
|
||||
// carol.
|
||||
outpoint := txIn.PreviousOutPoint
|
||||
resolutionOutpoint := &lnrpc.OutPoint{
|
||||
TxidBytes: outpoint.Hash[:],
|
||||
TxidStr: outpoint.Hash.String(),
|
||||
OutputIndex: outpoint.Index,
|
||||
}
|
||||
// Ensure all the htlc transaction inputs are spending from the
|
||||
// commitment transaction, except if this is an extra input
|
||||
// added to pay for fees for anchor channels.
|
||||
nonCommitmentInputs := 0
|
||||
for i, txIn := range inputs {
|
||||
if !closingTxID.IsEqual(&txIn.PreviousOutPoint.Hash) {
|
||||
nonCommitmentInputs++
|
||||
|
||||
// We expect alice to have a timeout tx resolution with an
|
||||
// amount equal to the payment amount.
|
||||
aliceReports[outpoint.String()] = &lnrpc.Resolution{
|
||||
ResolutionType: lnrpc.ResolutionType_OUTGOING_HTLC,
|
||||
Outcome: lnrpc.ResolutionOutcome_FIRST_STAGE,
|
||||
SweepTxid: htlcTx.Hash().String(),
|
||||
Outpoint: resolutionOutpoint,
|
||||
AmountSat: uint64(paymentAmt),
|
||||
}
|
||||
if nonCommitmentInputs > 1 {
|
||||
t.Fatalf("htlc transaction not "+
|
||||
"spending from commit "+
|
||||
"tx %v, instead spending %v",
|
||||
closingTxID,
|
||||
txIn.PreviousOutPoint)
|
||||
}
|
||||
|
||||
// We expect carol to have a resolution with an incoming htlc
|
||||
// timeout which reflects the full amount of the htlc. It has
|
||||
// no spend tx, because carol stops monitoring the htlc once
|
||||
// it has timed out.
|
||||
carolReports[outpoint.String()] = &lnrpc.Resolution{
|
||||
ResolutionType: lnrpc.ResolutionType_INCOMING_HTLC,
|
||||
Outcome: lnrpc.ResolutionOutcome_TIMEOUT,
|
||||
SweepTxid: "",
|
||||
Outpoint: resolutionOutpoint,
|
||||
AmountSat: uint64(paymentAmt),
|
||||
// This was an extra input added to pay fees,
|
||||
// continue to the next one.
|
||||
continue
|
||||
}
|
||||
|
||||
// For each htlc timeout transaction, we expect a
|
||||
// resolver report recording this on chain resolution
|
||||
// for both alice and carol.
|
||||
outpoint := txIn.PreviousOutPoint
|
||||
resolutionOutpoint := &lnrpc.OutPoint{
|
||||
TxidBytes: outpoint.Hash[:],
|
||||
TxidStr: outpoint.Hash.String(),
|
||||
OutputIndex: outpoint.Index,
|
||||
}
|
||||
|
||||
// We expect alice to have a timeout tx resolution with
|
||||
// an amount equal to the payment amount.
|
||||
aliceReports[outpoint.String()] = &lnrpc.Resolution{
|
||||
ResolutionType: lnrpc.ResolutionType_OUTGOING_HTLC,
|
||||
Outcome: lnrpc.ResolutionOutcome_FIRST_STAGE,
|
||||
SweepTxid: htlcTx.Hash().String(),
|
||||
Outpoint: resolutionOutpoint,
|
||||
AmountSat: uint64(paymentAmt),
|
||||
}
|
||||
|
||||
// We expect carol to have a resolution with an
|
||||
// incoming htlc timeout which reflects the full amount
|
||||
// of the htlc. It has no spend tx, because carol stops
|
||||
// monitoring the htlc once it has timed out.
|
||||
carolReports[outpoint.String()] = &lnrpc.Resolution{
|
||||
ResolutionType: lnrpc.ResolutionType_INCOMING_HTLC,
|
||||
Outcome: lnrpc.ResolutionOutcome_TIMEOUT,
|
||||
SweepTxid: "",
|
||||
Outpoint: resolutionOutpoint,
|
||||
AmountSat: uint64(paymentAmt),
|
||||
}
|
||||
|
||||
// Recorf the HTLC outpoint, such that we can later
|
||||
// check whether it gets swept
|
||||
op := wire.OutPoint{
|
||||
Hash: *htlcTxID,
|
||||
Index: uint32(i),
|
||||
}
|
||||
htlcTxOutpointSet[op] = 0
|
||||
}
|
||||
|
||||
// We record the htlc amount less fees here, so that we know
|
||||
@@ -4260,7 +4316,13 @@ func channelForceClosureTest(net *lntest.NetworkHarness, t *harnessTest,
|
||||
}
|
||||
|
||||
// Advance the chain until just before the 2nd-layer CSV delays expire.
|
||||
blockHash, err = net.Miner.Node.Generate(defaultCSV - 1)
|
||||
// For anchor channels thhis is one block earlier.
|
||||
numBlocks := uint32(defaultCSV - 1)
|
||||
if channelType == commitTypeAnchors {
|
||||
numBlocks = defaultCSV - 2
|
||||
|
||||
}
|
||||
_, err = net.Miner.Node.Generate(numBlocks)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to generate block: %v", err)
|
||||
}
|
||||
@@ -4327,15 +4389,6 @@ func channelForceClosureTest(net *lntest.NetworkHarness, t *harnessTest,
|
||||
t.Fatalf("failed to get sweep tx from mempool: %v", err)
|
||||
}
|
||||
|
||||
// Construct a map of the already confirmed htlc timeout txids, that
|
||||
// will count the number of times each is spent by the sweep txn. We
|
||||
// prepopulate it in this way so that we can later detect if we are
|
||||
// spending from an output that was not a confirmed htlc timeout txn.
|
||||
var htlcTxIDSet = make(map[chainhash.Hash]int)
|
||||
for _, htlcTxID := range htlcTxIDs {
|
||||
htlcTxIDSet[*htlcTxID] = 0
|
||||
}
|
||||
|
||||
// Fetch the htlc sweep transaction from the mempool.
|
||||
htlcSweepTx, err := net.Miner.Node.GetRawTransaction(htlcSweepTxID)
|
||||
if err != nil {
|
||||
@@ -4353,19 +4406,19 @@ func channelForceClosureTest(net *lntest.NetworkHarness, t *harnessTest,
|
||||
"%v", outputCount)
|
||||
}
|
||||
|
||||
// Ensure that each output spends from exactly one htlc timeout txn.
|
||||
// Ensure that each output spends from exactly one htlc timeout output.
|
||||
for _, txIn := range htlcSweepTx.MsgTx().TxIn {
|
||||
outpoint := txIn.PreviousOutPoint.Hash
|
||||
outpoint := txIn.PreviousOutPoint
|
||||
// Check that the input is a confirmed htlc timeout txn.
|
||||
if _, ok := htlcTxIDSet[outpoint]; !ok {
|
||||
if _, ok := htlcTxOutpointSet[outpoint]; !ok {
|
||||
t.Fatalf("htlc sweep output not spending from htlc "+
|
||||
"tx, instead spending output %v", outpoint)
|
||||
}
|
||||
// Increment our count for how many times this output was spent.
|
||||
htlcTxIDSet[outpoint]++
|
||||
htlcTxOutpointSet[outpoint]++
|
||||
|
||||
// Check that each is only spent once.
|
||||
if htlcTxIDSet[outpoint] > 1 {
|
||||
if htlcTxOutpointSet[outpoint] > 1 {
|
||||
t.Fatalf("htlc sweep tx has multiple spends from "+
|
||||
"outpoint %v", outpoint)
|
||||
}
|
||||
@@ -4386,6 +4439,13 @@ func channelForceClosureTest(net *lntest.NetworkHarness, t *harnessTest,
|
||||
}
|
||||
}
|
||||
|
||||
// Check that each HTLC output was spent exactly onece.
|
||||
for op, num := range htlcTxOutpointSet {
|
||||
if num != 1 {
|
||||
t.Fatalf("HTLC outpoint %v was spent %v times", op, num)
|
||||
}
|
||||
}
|
||||
|
||||
// Check that we can find the htlc sweep in our set of sweeps using
|
||||
// the verbose output of the listsweeps output.
|
||||
assertSweepFound(ctxb, t.t, alice, htlcSweepTx.Hash().String(), true)
|
||||
@@ -11019,11 +11079,12 @@ func assertActiveHtlcs(nodes []*lntest.HarnessNode, payHashes ...[]byte) error {
|
||||
// Record all payment hashes active for this channel.
|
||||
htlcHashes := make(map[string]struct{})
|
||||
for _, htlc := range channel.PendingHtlcs {
|
||||
_, ok := htlcHashes[string(htlc.HashLock)]
|
||||
h := hex.EncodeToString(htlc.HashLock)
|
||||
_, ok := htlcHashes[h]
|
||||
if ok {
|
||||
return fmt.Errorf("duplicate HashLock")
|
||||
}
|
||||
htlcHashes[string(htlc.HashLock)] = struct{}{}
|
||||
htlcHashes[h] = struct{}{}
|
||||
}
|
||||
|
||||
// Channel should have exactly the payHashes active.
|
||||
@@ -11035,12 +11096,13 @@ func assertActiveHtlcs(nodes []*lntest.HarnessNode, payHashes ...[]byte) error {
|
||||
|
||||
// Make sure all the payHashes are active.
|
||||
for _, payHash := range payHashes {
|
||||
if _, ok := htlcHashes[string(payHash)]; ok {
|
||||
h := hex.EncodeToString(payHash)
|
||||
if _, ok := htlcHashes[h]; ok {
|
||||
continue
|
||||
}
|
||||
return fmt.Errorf("node %x didn't have the "+
|
||||
"payHash %v active", node.PubKey[:],
|
||||
payHash)
|
||||
h)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user