Slightly optimize GenerateInitialIndex.
Rather than fetching the hash of each block individually 2k+ times, make use of the FetchHeightRange function so all of the most recent hashes can be fetched at once.
This commit is contained in:
parent
d7e057a020
commit
c88338d227
1 changed files with 30 additions and 7 deletions
29
chain.go
29
chain.go
|
@ -347,13 +347,31 @@ func (b *BlockChain) GenerateInitialIndex() error {
|
||||||
|
|
||||||
// Loop forwards through each block loading the node into the index for
|
// Loop forwards through each block loading the node into the index for
|
||||||
// the block.
|
// the block.
|
||||||
for i := startHeight; i <= endHeight; i++ {
|
//
|
||||||
hash, err := b.db.FetchBlockShaByHeight(i)
|
// Due to a bug in the SQLite btcdb driver, the FetchBlockBySha call is
|
||||||
|
// limited to a maximum number of hashes per invocation. Since SQLite
|
||||||
|
// is going to be nuked eventually, the bug isn't being fixed in the
|
||||||
|
// driver. In the mean time, work around the issue by calling
|
||||||
|
// FetchBlockBySha multiple times with the appropriate indices as needed.
|
||||||
|
for start := startHeight; start < endHeight; {
|
||||||
|
hashList, err := b.db.FetchHeightRange(start, endHeight+1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
node, err := b.loadBlockNode(hash)
|
// The database did not return any further hashes. Break out of
|
||||||
|
// the loop now.
|
||||||
|
if len(hashList) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loop forwards through each block loading the node into the
|
||||||
|
// index for the block.
|
||||||
|
for _, hash := range hashList {
|
||||||
|
// Make a copy of the hash to make sure there are no references
|
||||||
|
// into the list so it can be freed.
|
||||||
|
hashCopy := hash
|
||||||
|
node, err := b.loadBlockNode(&hashCopy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -362,6 +380,11 @@ func (b *BlockChain) GenerateInitialIndex() error {
|
||||||
b.bestChain = node
|
b.bestChain = node
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start at the next block after the latest one on the next loop
|
||||||
|
// iteration.
|
||||||
|
start += int64(len(hashList))
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue