From ce888ed941af9f614d506da3c72d1456f2dd4268 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Wed, 13 May 2020 08:54:23 -0700 Subject: [PATCH] wallet: use GetBlockHeaderVerbose to retrieve block heights in GetTransactions There's no need to retrieve the full block as we're only interesting in retrieve its corresponding height, which can be done with GetBlockHeaderVerbose. --- wallet/wallet.go | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/wallet/wallet.go b/wallet/wallet.go index cf2990f..ad16ab9 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -20,7 +20,6 @@ import ( "github.com/btcsuite/btcd/btcjson" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" - "github.com/btcsuite/btcd/rpcclient" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" @@ -2202,7 +2201,6 @@ func (w *Wallet) GetTransactions(startBlock, endBlock *BlockIdentifier, cancel < // TODO: Fetching block heights by their hashes is inherently racy // because not all block headers are saved but when they are for SPV the // db can be queried directly without this. - var startResp, endResp rpcclient.FutureGetBlockVerboseResult if startBlock != nil { if startBlock.hash == nil { start = startBlock.height @@ -2212,7 +2210,13 @@ func (w *Wallet) GetTransactions(startBlock, endBlock *BlockIdentifier, cancel < } switch client := chainClient.(type) { case *chain.RPCClient: - startResp = client.GetBlockVerboseTxAsync(startBlock.hash) + startHeader, err := client.GetBlockHeaderVerbose( + startBlock.hash, + ) + if err != nil { + return nil, err + } + start = startHeader.Height case *chain.BitcoindClient: var err error start, err = client.GetBlockHeight(startBlock.hash) @@ -2237,7 +2241,19 @@ func (w *Wallet) GetTransactions(startBlock, endBlock *BlockIdentifier, cancel < } switch client := chainClient.(type) { case *chain.RPCClient: - endResp = client.GetBlockVerboseTxAsync(endBlock.hash) + endHeader, err := client.GetBlockHeaderVerbose( + endBlock.hash, + ) + if err != nil { + return nil, err + } + end = endHeader.Height + case *chain.BitcoindClient: + var err error + start, err = client.GetBlockHeight(endBlock.hash) + if err != nil { + return nil, err + } case *chain.NeutrinoClient: var err error end, err = client.GetBlockHeight(endBlock.hash) @@ -2247,20 +2263,6 @@ func (w *Wallet) GetTransactions(startBlock, endBlock *BlockIdentifier, cancel < } } } - if startResp != nil { - resp, err := startResp.Receive() - if err != nil { - return nil, err - } - start = int32(resp.Height) - } - if endResp != nil { - resp, err := endResp.Receive() - if err != nil { - return nil, err - } - end = int32(resp.Height) - } var res GetTransactionsResult err := walletdb.View(w.db, func(dbtx walletdb.ReadTx) error {