Skip to content

Commit

Permalink
rescan: wait till current or end height reached
Browse files Browse the repository at this point in the history
Let the rescan function wait until the filter headers have either caught
up to the back end chain or until they have caught up to the specified
rescan end block. This lets the rescan operation take advantage of doing
batch filter fetching during rescan making the operation a lot faster
since filters can be fetched in batches of 1000 instead of one at a
time.
  • Loading branch information
ellemouton committed May 5, 2023
1 parent af6ae1f commit 9a49d03
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion rescan.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type ChainSource interface {
// GetCFilter returns the filter of the given type for the block with
// the given hash.
GetCFilter(chainhash.Hash, wire.FilterType,
...QueryOption) (*gcs.Filter, error)
...QueryOption) (*gcs.Filter, error)

// Subscribe returns a block subscription that delivers block
// notifications in order. The bestHeight parameter can be used to
Expand Down Expand Up @@ -440,6 +440,39 @@ func (rs *rescanState) rescan() error {
return err
}

// To ensure that we batch as many filter queries as possible, we also
// wait for the header chain to either be current or for it to at least
// have caught up with the specified end block.
r, ok := chain.(*RescanChainSource)
if ok {
log.Debugf("Waiting for the chain source to be current or " +
"for the rescan end height to be reached.")

if err := rs.waitForBlocks(func(hash chainhash.Hash,
height uint32) bool {

// If the header chain is current, then there is no need
// to wait.
if r.IsCurrent() {
return true
}

// If an end height was specified then we wait until the
// notification corresponding to that block height.
if ro.endBlock.Height > 0 &&
height >= uint32(ro.endBlock.Height) {

return true
}

// If a block hash was specified, check if the
// notification is for that block.
return hash == ro.endBlock.Hash
}); err != nil {
return err
}
}

log.Debugf("Starting rescan from known block %d (%s)",
rs.curStamp.Height, rs.curStamp.Hash)

Expand Down

0 comments on commit 9a49d03

Please sign in to comment.