Merge pull request #636 from wpaulino/empty-filter-block

chain: prevent filtering blocks when BitcoindClient has an empty filter
This commit is contained in:
Olaoluwa Osuntokun 2019-08-13 19:34:31 -07:00 committed by GitHub
commit 505acf5150
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -378,6 +378,12 @@ func (c *BitcoindClient) RescanBlocks(
continue 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) block, err := c.GetBlock(&hash)
if err != nil { if err != nil {
log.Warnf("Unable to get block %s from bitcoind: %s", log.Warnf("Unable to get block %s from bitcoind: %s",
@ -1128,14 +1134,25 @@ func (c *BitcoindClient) rescan(start chainhash.Hash) error {
return nil 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 // filterBlock filters a block for watched outpoints and addresses, and returns
// any matching transactions, sending notifications along the way. // any matching transactions, sending notifications along the way.
func (c *BitcoindClient) filterBlock(block *wire.MsgBlock, height int32, func (c *BitcoindClient) filterBlock(block *wire.MsgBlock, height int32,
notify bool) []*wtxmgr.TxRecord { notify bool) []*wtxmgr.TxRecord {
// If this block happened before the client's birthday, then we'll skip // If this block happened before the client's birthday or we have
// it entirely. // nothing to filter for, then we'll skip it entirely.
if block.Header.Timestamp.Before(c.birthday) { if !c.shouldFilterBlock(block.Header.Timestamp) {
return nil return nil
} }