Skip to content
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
189 changes: 127 additions & 62 deletions chainntnfs/btcdnotify/btcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,60 @@ out:
}
}(msg)

case *chainntnfs.HistoricalSpendDispatch:
// In order to ensure we don't block the caller
// on what may be a long rescan, we'll launch a
// goroutine to do so in the background.
//
// TODO(wilmer): add retry logic if rescan fails?
b.wg.Add(1)

//nolint:ll
go func(msg *chainntnfs.HistoricalSpendDispatch) {
defer b.wg.Done()

spendDetails, err := b.historicalSpendDetails(
msg.SpendRequest,
msg.StartHeight, msg.EndHeight,
)
if err != nil {
chainntnfs.Log.Errorf("Rescan to "+
"determine the spend "+
"details of %v within "+
"range %d-%d failed: %v",
msg.SpendRequest,
msg.StartHeight,
msg.EndHeight, err)

return
}

chainntnfs.Log.Infof("Historical "+
"spend dispatch finished "+
"for request %v (start=%v "+
"end=%v) with details: %v",
msg.SpendRequest,
msg.StartHeight, msg.EndHeight,
spendDetails)

// If the historical dispatch finished
// without error, we will invoke
// UpdateSpendDetails even if none were
// found. This allows the notifier to
// begin safely updating the height hint
// cache at tip, since any pending
// rescans have now completed.
err = b.txNotifier.UpdateSpendDetails(
msg.SpendRequest, spendDetails,
)
if err != nil {
chainntnfs.Log.Errorf("Unable "+
"to update spend "+
"details of %v: %v",
msg.SpendRequest, err)
}
}(msg)

case *blockEpochRegistration:
chainntnfs.Log.Infof("New block epoch subscription")

Expand Down Expand Up @@ -698,6 +752,66 @@ func (b *BtcdNotifier) confDetailsManually(confRequest chainntnfs.ConfRequest,
return nil, chainntnfs.TxNotFoundManually, nil
}

// historicalSpendDetails attempts to manually scan the chain within the given
// height range for a transaction that spends the given outpoint/output script.
// If one is found, the spend details are assembled and returned to the caller.
// If the spend is not found, a nil spend detail will be returned.
func (b *BtcdNotifier) historicalSpendDetails(
spendRequest chainntnfs.SpendRequest, startHeight, endHeight uint32) (
*chainntnfs.SpendDetail, error) {

// Begin scanning blocks at every height to determine if the outpoint
// was spent.
for height := endHeight; height >= startHeight && height > 0; height-- {
// Ensure we haven't been requested to shut down before
// processing the next height.
select {
case <-b.quit:
return nil, chainntnfs.ErrChainNotifierShuttingDown
default:
}

// First, we'll fetch the block for the current height.
blockHash, err := b.chainConn.GetBlockHash(int64(height))
if err != nil {
return nil, fmt.Errorf("unable to retrieve hash for "+
"block with height %d: %v", height, err)
}
block, err := b.GetBlock(blockHash)
if err != nil {
return nil, fmt.Errorf("unable to retrieve block "+
"with hash %v: %v", blockHash, err)
}

// Then, we'll manually go over every input in every transaction
// in it and determine whether it spends the request in
// question. If we find one, we'll dispatch the spend details.
for _, tx := range block.Transactions {
matches, inputIdx, err := spendRequest.MatchesTx(tx)
if err != nil {
return nil, err
}
if !matches {
continue
}

txCopy := tx.Copy()
txHash := txCopy.TxHash()
spendOutPoint := &txCopy.TxIn[inputIdx].PreviousOutPoint

return &chainntnfs.SpendDetail{
SpentOutPoint: spendOutPoint,
SpenderTxHash: &txHash,
SpendingTx: txCopy,
SpenderInputIndex: inputIdx,
SpendingHeight: int32(height),
}, nil
}
}

return nil, nil
}

// handleBlockConnected applies a chain update for a new block. Any watched
// transactions included this block will processed to either send notifications
// now or after numConfirmations confs.
Expand Down Expand Up @@ -834,32 +948,12 @@ func (b *BtcdNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
// rather than an outpoint, as there aren't any additional checks we can
// make for scripts.
if outpoint == nil || *outpoint == chainntnfs.ZeroOutPoint {
startHash, err := b.chainConn.GetBlockHash(
int64(ntfn.HistoricalDispatch.StartHeight),
)
if err != nil {
return nil, err
}

// TODO(wilmer): add retry logic if rescan fails?
_, addrs, _, err := txscript.ExtractPkScriptAddrs(
pkScript, b.chainParams,
)
if err != nil {
return nil, fmt.Errorf("unable to parse address: %w",
err)
select {
case b.notificationRegistry <- ntfn.HistoricalDispatch:
case <-b.quit:
return nil, chainntnfs.ErrChainNotifierShuttingDown
}

asyncResult := b.chainConn.RescanAsync(startHash, addrs, nil)
go func() {
if rescanErr := asyncResult.Receive(); rescanErr != nil {
chainntnfs.Log.Errorf("Rescan to determine "+
"the spend details of %v failed: %v",
ntfn.HistoricalDispatch.SpendRequest,
rescanErr)
}
}()

return ntfn.Event, nil
}

Expand Down Expand Up @@ -888,16 +982,8 @@ func (b *BtcdNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
}

// Since the outpoint was spent, as it no longer exists within the UTXO
// set, we'll determine when it happened by scanning the chain. We'll
// begin by fetching the block hash of our starting height.
startHash, err := b.chainConn.GetBlockHash(
int64(ntfn.HistoricalDispatch.StartHeight),
)
if err != nil {
return nil, fmt.Errorf("unable to get block hash for height "+
"%d: %v", ntfn.HistoricalDispatch.StartHeight, err)
}

// set, we'll determine when it happened by scanning the chain.
//
// As a minimal optimization, we'll query the backend's transaction
// index (if enabled) to determine if we have a better rescan starting
// height. We can do this as the GetRawTransaction call will return the
Expand Down Expand Up @@ -942,38 +1028,17 @@ func (b *BtcdNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
// now update the starting height to be the spent height to make
// sure we won't miss it in the rescan.
if spentHeight != ntfn.HistoricalDispatch.StartHeight {
startHash, err = b.chainConn.GetBlockHash(
int64(spentHeight),
)
if err != nil {
return nil, fmt.Errorf("unable to get block "+
"hash for height %d: %v",
blockHeader.Height, err)
}

ntfn.HistoricalDispatch.StartHeight = spentHeight
}
}

// Now that we've determined the best starting point for our rescan,
// we can go ahead and dispatch it.
//
// In order to ensure that we don't block the caller on what may be a
// long rescan, we'll launch a new goroutine to handle the async result
// of the rescan. We purposefully prevent from adding this goroutine to
// the WaitGroup as we cannot wait for a quit signal due to the
// asyncResult channel not being exposed.
//
// TODO(wilmer): add retry logic if rescan fails?
asyncResult := b.chainConn.RescanAsync(
startHash, nil, []*wire.OutPoint{outpoint},
)
go func() {
if rescanErr := asyncResult.Receive(); rescanErr != nil {
chainntnfs.Log.Errorf("Rescan to determine the spend "+
"details of %v failed: %v", outpoint, rescanErr)
}
}()
// Now that we've determined the starting point of our rescan, we can
// dispatch it and return.
select {
case b.notificationRegistry <- ntfn.HistoricalDispatch:
case <-b.quit:
return nil, chainntnfs.ErrChainNotifierShuttingDown
}

return ntfn.Event, nil
}
Expand Down
121 changes: 121 additions & 0 deletions chainntnfs/btcdnotify/btcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package btcdnotify
import (
"bytes"
"testing"
"time"

"github.com/btcsuite/btcd/chainhash/v2"
"github.com/btcsuite/btcd/integration/rpctest"
Expand Down Expand Up @@ -207,3 +208,123 @@ func TestHistoricalConfDetailsNoTxIndex(t *testing.T) {
"scanning the chain, but did not")
}
}

// TestHistoricalSpendDetails ensures that we correctly retrieve historical
// spend details of outpoints and output scripts by manually scanning the
// chain.
func TestHistoricalSpendDetails(t *testing.T) {
t.Parallel()

harness := unittest.NewMiner(t, unittest.NetParams, nil, true, 25)

notifier := setUpNotifier(t, harness)

// We'll create an output and note the height it confirmed at to use as
// the start of the scanned range.
outpoint, output, privKey := chainntnfs.CreateSpendableOutput(
t, harness,
)
_, startHeight, err := harness.Client.GetBestBlock()
require.NoError(t, err, "unable to retrieve current height")

opReq, err := chainntnfs.NewSpendRequest(outpoint, output.PkScript)
require.NoError(t, err, "unable to create spend request")
scriptReq, err := chainntnfs.NewSpendRequest(nil, output.PkScript)
require.NoError(t, err, "unable to create spend request")

// Since the output has yet to be spent, no spend details should be
// found for either request.
for _, req := range []chainntnfs.SpendRequest{opReq, scriptReq} {
details, err := notifier.historicalSpendDetails(
req, uint32(startHeight), uint32(startHeight),
)
require.NoError(t, err, "unable to retrieve spend details")
require.Nil(t, details)
}

// We'll now spend the output, confirm the spend and bury it under a
// few more blocks.
spendTx := chainntnfs.CreateSpendTx(t, outpoint, output, privKey)
spendTxid, err := harness.Client.SendRawTransaction(spendTx, true)
require.NoError(t, err, "unable to broadcast tx")
err = chainntnfs.WaitForMempoolTx(harness, spendTxid)
require.NoError(t, err, "tx not relayed to miner")

_, err = harness.Client.Generate(1)
require.NoError(t, err, "unable to generate block")
_, spendHeight, err := harness.Client.GetBestBlock()
require.NoError(t, err, "unable to retrieve current height")

_, err = harness.Client.Generate(3)
require.NoError(t, err, "unable to generate blocks")
_, endHeight, err := harness.Client.GetBestBlock()
require.NoError(t, err, "unable to retrieve current height")

// The spend should now be found for both requests.
for _, req := range []chainntnfs.SpendRequest{opReq, scriptReq} {
details, err := notifier.historicalSpendDetails(
req, uint32(startHeight), uint32(endHeight),
)
require.NoError(t, err, "unable to retrieve spend details")
require.NotNil(t, details)
require.Equal(t, *outpoint, *details.SpentOutPoint)
require.Equal(t, *spendTxid, *details.SpenderTxHash)
require.Equal(t, uint32(0), details.SpenderInputIndex)
require.Equal(t, spendHeight, details.SpendingHeight)
}

// A range that ends before the spend should not find it.
details, err := notifier.historicalSpendDetails(
scriptReq, uint32(startHeight), uint32(spendHeight)-1,
)
require.NoError(t, err, "unable to retrieve spend details")
require.Nil(t, details)
}

// TestHistoricalSpendDispatchUnspent ensures that a historical spend dispatch
// for an output script that has not been spent completes, so that the spend
// hint of the request is committed.
func TestHistoricalSpendDispatchUnspent(t *testing.T) {
t.Parallel()

harness := unittest.NewMiner(t, unittest.NetParams, nil, true, 25)

notifier := setUpNotifier(t, harness)

epochClient, err := notifier.RegisterBlockEpochNtfn(nil)
require.NoError(t, err, "unable to register for block epoch")
defer epochClient.Cancel()

_, output, _ := chainntnfs.CreateSpendableOutput(t, harness)
_, height, err := harness.Client.GetBestBlock()
require.NoError(t, err, "unable to retrieve current height")

// Wait for the notifier to process the output's block, so that
// registering with its height as the hint dispatches a historical
// rescan.
timeout := time.After(10 * time.Second)
for caughtUp := false; !caughtUp; {
select {
case epoch := <-epochClient.Epochs:
caughtUp = epoch.Height == height

case <-timeout:
t.Fatalf("notifier did not reach height %d", height)
}
}

_, err = notifier.RegisterSpendNtfn(
nil, output.PkScript, uint32(height),
)
require.NoError(t, err, "unable to register spend ntfn")

req, err := chainntnfs.NewSpendRequest(nil, output.PkScript)
require.NoError(t, err, "unable to create spend request")

// Once the historical dispatch completes without finding a spend, the
// spend hint of the request is committed at the current height.
require.Eventually(t, func() bool {
hint, err := notifier.spendHintCache.QuerySpendHint(req)
return err == nil && hint == uint32(height)
}, 10*time.Second, 50*time.Millisecond)
}
12 changes: 12 additions & 0 deletions docs/release-notes/unreleased/11583.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Technical and Architectural Updates

## Code Health

* The btcd chain notifier now [determines historical spends by scanning the
chain's blocks](https://github.com/lightningnetwork/lnd/pull/11583) instead
of issuing the deprecated `rescan` command, as the bitcoind notifier already
does. Btcd-compatible backends that do not implement `rescan` are now
supported. A historical spend dispatch now also completes when no spend is
found, so the spend hint of an unspent output script is committed and the
dispatch is no longer repeated from the original height hint after every
restart.
Loading