Skip to content

itest: RegisterSpendNtfn detects reorgs #10083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions itest/list_on_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ var allTestCases = []*lntest.TestCase{
Name: "send selected coins channel reserve",
TestFunc: testSendSelectedCoinsChannelReserve,
},
{
Name: "reorg notifications",
TestFunc: testReorgNotifications,
},
{
Name: "disconnecting target peer",
TestFunc: testDisconnectingTargetPeer,
Expand Down
204 changes: 204 additions & 0 deletions itest/lnd_misc_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package itest

import (
"context"
"encoding/hex"
"fmt"
"os"

"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/wallet"
"github.com/lightningnetwork/lnd/funding"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/chainrpc"
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
"github.com/lightningnetwork/lnd/lntest"
"github.com/lightningnetwork/lnd/lntest/node"
"github.com/lightningnetwork/lnd/lntest/rpc"
"github.com/lightningnetwork/lnd/lntest/wait"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire"
Expand Down Expand Up @@ -1391,3 +1395,203 @@ func testGRPCNotFound(ht *lntest.HarnessTest) {
RHash: rHash,
}, notFoundErr)
}

// receiveSpendNotification tries to receive a spend notification from a spend
// client until the context expires.
func receiveSpendNotification(ctx context.Context,
spendClient rpc.SpendClient) (*chainrpc.SpendEvent, error) {

var (
msg *chainrpc.SpendEvent
recvErr error
)

received := make(chan struct{})
go func() {
msg, recvErr = spendClient.Recv()
close(received)
}()
select {
case <-ctx.Done():
return nil, fmt.Errorf("spending notification expired")

case <-received:
return msg, recvErr
}
}

// testReorgNotifications tests that RegisterSpendNtfn behaves as expected
// during a reorg. A reorg notification is produced after a reorg affects the
// block which has produced a spending notification for this registration.
func testReorgNotifications(ht *lntest.HarnessTest) {
ctxb := context.Background()
const timeout = wait.DefaultTimeout

alice := ht.NewNodeWithCoins("Alice", nil)
bob := ht.NewNode("Bob", nil)

const tx1Amount = 1_000_000

// Alice will send coins to herself, Bob will watch spending and
// confirmation of the transaction. We make sure that a node can watch
// transactions which are not a part of its wallet.
respAddr := alice.RPC.NewAddress(&lnrpc.NewAddressRequest{
Type: lnrpc.AddressType_TAPROOT_PUBKEY,
})
txid1Str := alice.RPC.SendCoins(&lnrpc.SendCoinsRequest{
Addr: respAddr.Address,
Amount: tx1Amount,
SatPerVbyte: 2,
}).Txid
txid1, err := chainhash.NewHashFromStr(txid1Str)
require.NoError(ht, err)
tx1 := ht.AssertTxInMempool(*txid1)

// Find the output of tx1.
tx1OutIndex := -1
for i, txOut := range tx1.TxOut {
if txOut.Value == tx1Amount {
tx1OutIndex = i
}
}
require.NotEqual(ht, -1, tx1OutIndex)
tx1op := wire.OutPoint{
Hash: *txid1,
Index: uint32(tx1OutIndex),
}
tx1opLnrpc := &lnrpc.OutPoint{
TxidStr: txid1Str,
OutputIndex: uint32(tx1OutIndex),
}
tx1opChainrpc := &chainrpc.Outpoint{
Hash: txid1[:],
Index: uint32(tx1OutIndex),
}
pkscript := tx1.TxOut[tx1OutIndex].PkScript

// Now fee bump the output of the first transaction.
alice.RPC.BumpFee(&walletrpc.BumpFeeRequest{
Outpoint: tx1opLnrpc,
Immediate: true,
SatPerVbyte: 20,
})

// Now find the fee bump tx.
listSweepsReq := &walletrpc.ListSweepsRequest{
Verbose: true,

// startHeight -1 means include only unconfirmed.
StartHeight: -1,
}

var tx2aLnrpc *lnrpc.Transaction
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: imo readability would improve with a few added new lines. Right now code blocks feel a bit too tight.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added few new lines.

require.NoError(ht, wait.NoError(func() error {
sweepsResp := alice.RPC.ListSweeps(listSweepsReq)
sweepsDetails := sweepsResp.GetTransactionDetails()
if sweepsDetails == nil {
return fmt.Errorf("no sweep details")
}
if len(sweepsDetails.Transactions) != 1 {
return fmt.Errorf("got %d sweeps, want %d",
len(sweepsDetails.Transactions), 1)
}
tx2aLnrpc = sweepsDetails.Transactions[0]

return nil
}, defaultTimeout))

require.Len(ht, tx2aLnrpc.PreviousOutpoints, 1)
require.Equal(
ht, tx1op.String(), tx2aLnrpc.PreviousOutpoints[0].Outpoint,
)
txid2a, err := chainhash.NewHashFromStr(tx2aLnrpc.TxHash)
require.NoError(ht, err)
tx2a := ht.AssertTxInMempool(*txid2a)

// Fee bump the output of the first transaction again with a higher fee
// rate to get RBF transaction tx2b.
alice.RPC.BumpFee(&walletrpc.BumpFeeRequest{
Outpoint: tx1opLnrpc,
Immediate: true,
SatPerVbyte: 200,
})

var tx2bLnrpc *lnrpc.Transaction
require.NoError(ht, wait.NoError(func() error {
sweepsResp := alice.RPC.ListSweeps(listSweepsReq)
sweepsDetails := sweepsResp.GetTransactionDetails()
if sweepsDetails == nil {
return fmt.Errorf("no sweep details")
}
for _, tx := range sweepsDetails.Transactions {
if tx.TxHash != tx2aLnrpc.TxHash {
tx2bLnrpc = tx
break
}
}
if tx2bLnrpc == nil {
return fmt.Errorf("tx2aLnrpc hasn't been replaced yet")
}

return nil
}, defaultTimeout))

require.Len(ht, tx2bLnrpc.PreviousOutpoints, 1)
require.Equal(
ht, tx1op.String(), tx2bLnrpc.PreviousOutpoints[0].Outpoint,
)
txid2b, err := chainhash.NewHashFromStr(tx2bLnrpc.TxHash)
require.NoError(ht, err)
tx2b := ht.AssertTxInMempool(*txid2b)

// Mine tx1 only.
ht.Miner().MineBlockWithTxes([]*btcutil.Tx{btcutil.NewTx(tx1)})

// Bob starts watching spending of tx1op.
spendClient := bob.RPC.RegisterSpendNtfn(&chainrpc.SpendRequest{
Outpoint: tx1opChainrpc,
Script: pkscript,
HeightHint: ht.CurrentHeight(),
})

// Mine tx2b.
block1 := ht.Miner().MineBlockWithTxes(
[]*btcutil.Tx{btcutil.NewTx(tx2b)},
)

// Make sure RegisterSpendNtfn noticed the spending.
ctx, cancel := context.WithTimeout(ctxb, timeout)
defer cancel()
spendMsg, err := receiveSpendNotification(ctx, spendClient)
require.NoError(ht, err)
spendDetails := spendMsg.GetSpend()
require.NotNil(ht, spendDetails)
require.Equal(ht, txid2b[:], spendDetails.SpendingTxHash)

// Reorg block1.
blockHash1 := block1.Header.BlockHash()
require.NoError(ht, ht.Miner().Client.InvalidateBlock(&blockHash1))

// Mine empty blocks to evict block1 in bitcoin backend (e.g. bitcoind).
ht.Miner().MineEmptyBlocks(2)

// Make sure RegisterSpendNtfn noticed the reorg. Transaction tx2b was
// just unconfirmed.
ctx, cancel = context.WithTimeout(ctxb, timeout)
defer cancel()
spendMsg, err = receiveSpendNotification(ctx, spendClient)
require.NoError(ht, err)
require.NotNil(ht, spendMsg.GetReorg())

// Mine tx2a to confirm a different version of spending.
ht.Miner().MineBlockWithTxes([]*btcutil.Tx{btcutil.NewTx(tx2a)})

// Make sure RegisterSpendNtfn noticed the spending.
ctx, cancel = context.WithTimeout(ctxb, timeout)
defer cancel()
spendMsg, err = receiveSpendNotification(ctx, spendClient)
require.NoError(ht, err)
spendDetails = spendMsg.GetSpend()
require.NotNil(ht, spendDetails)
require.Equal(ht, txid2a[:], spendDetails.SpendingTxHash)
}
Loading