chain: prevent filtering blocks when BitcoindClient has an empty filter

This commit is contained in:
Wilmer Paulino 2019-08-09 17:52:24 -07:00
parent 7a3a3e82cb
commit 5124978a9b
No known key found for this signature in database
GPG key ID: 6DF57B9F9514972F

View file

@ -378,6 +378,12 @@ func (c *BitcoindClient) RescanBlocks(
continue
}
// Prevent fetching the block completely if we know we shouldn't
// filter it.
if !c.shouldFilterBlock(time.Unix(header.Time, 0)) {
continue
}
block, err := c.GetBlock(&hash)
if err != nil {
log.Warnf("Unable to get block %s from bitcoind: %s",
@ -1128,14 +1134,25 @@ func (c *BitcoindClient) rescan(start chainhash.Hash) error {
return nil
}
// shouldFilterBlock determines whether we should filter a block based on its
// timestamp or our watch list.
func (c *BitcoindClient) shouldFilterBlock(blockTimestamp time.Time) bool {
c.watchMtx.RLock()
hasEmptyFilter := len(c.watchedAddresses) == 0 &&
len(c.watchedOutPoints) == 0 && len(c.watchedTxs) == 0
c.watchMtx.RUnlock()
return !(blockTimestamp.Before(c.birthday) || hasEmptyFilter)
}
// filterBlock filters a block for watched outpoints and addresses, and returns
// any matching transactions, sending notifications along the way.
func (c *BitcoindClient) filterBlock(block *wire.MsgBlock, height int32,
notify bool) []*wtxmgr.TxRecord {
// If this block happened before the client's birthday, then we'll skip
// it entirely.
if block.Header.Timestamp.Before(c.birthday) {
// If this block happened before the client's birthday or we have
// nothing to filter for, then we'll skip it entirely.
if !c.shouldFilterBlock(block.Header.Timestamp) {
return nil
}