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.
This commit is contained in:
Wilmer Paulino 2020-05-13 08:54:23 -07:00
parent b07494fc2d
commit ce888ed941
No known key found for this signature in database
GPG key ID: 6DF57B9F9514972F

View file

@ -20,7 +20,6 @@ import (
"github.com/btcsuite/btcd/btcjson" "github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "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 // TODO: Fetching block heights by their hashes is inherently racy
// because not all block headers are saved but when they are for SPV the // because not all block headers are saved but when they are for SPV the
// db can be queried directly without this. // db can be queried directly without this.
var startResp, endResp rpcclient.FutureGetBlockVerboseResult
if startBlock != nil { if startBlock != nil {
if startBlock.hash == nil { if startBlock.hash == nil {
start = startBlock.height start = startBlock.height
@ -2212,7 +2210,13 @@ func (w *Wallet) GetTransactions(startBlock, endBlock *BlockIdentifier, cancel <
} }
switch client := chainClient.(type) { switch client := chainClient.(type) {
case *chain.RPCClient: 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: case *chain.BitcoindClient:
var err error var err error
start, err = client.GetBlockHeight(startBlock.hash) start, err = client.GetBlockHeight(startBlock.hash)
@ -2237,7 +2241,19 @@ func (w *Wallet) GetTransactions(startBlock, endBlock *BlockIdentifier, cancel <
} }
switch client := chainClient.(type) { switch client := chainClient.(type) {
case *chain.RPCClient: 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: case *chain.NeutrinoClient:
var err error var err error
end, err = client.GetBlockHeight(endBlock.hash) 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 var res GetTransactionsResult
err := walletdb.View(w.db, func(dbtx walletdb.ReadTx) error { err := walletdb.View(w.db, func(dbtx walletdb.ReadTx) error {