From 274687471a7e894209f4a8c8f3110d7980660daa Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 18 Jul 2018 01:31:49 -0700 Subject: [PATCH] chainntnfs/btcdnotify: switch to async rescan call for historical spend ntfns In this commit, we modify the way to handle historical spend dispatches to ensure that we don't block the client for very old rescans. Rather than blocking and waiting for the rescan to finish (which may take minutes in the worst case), we'll now instead launch a goroutine to handle the async response of the rescan. --- chainntnfs/btcdnotify/btcd.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/chainntnfs/btcdnotify/btcd.go b/chainntnfs/btcdnotify/btcd.go index 8e134020..e594ce36 100644 --- a/chainntnfs/btcdnotify/btcd.go +++ b/chainntnfs/btcdnotify/btcd.go @@ -7,12 +7,12 @@ import ( "sync/atomic" "time" - "github.com/lightningnetwork/lnd/chainntnfs" "github.com/btcsuite/btcd/btcjson" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/rpcclient" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" + "github.com/lightningnetwork/lnd/chainntnfs" ) const ( @@ -765,12 +765,21 @@ func (b *BtcdNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint, // and only arrives in the mempool after the getxout call. if blockHash != nil { ops := []*wire.OutPoint{outpoint} - err = b.chainConn.Rescan(blockHash, nil, ops) - if err != nil { - chainntnfs.Log.Errorf("Rescan for spend "+ - "notification txout failed: %v", err) - return nil, err - } + + // 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. + asyncResult := b.chainConn.RescanAsync( + blockHash, nil, ops, + ) + go func() { + rescanErr := asyncResult.Receive() + if rescanErr != nil { + chainntnfs.Log.Errorf("Rescan for spend "+ + "notification txout(%x) "+ + "failed: %v", outpoint, rescanErr) + } + }() } }