Merge pull request #2759 from nspcc-dev/avoid-db-lookup-in-hasblock

core: optimize HasBlock check for recent blocks
This commit is contained in:
Roman Khimov 2022-10-24 14:33:38 +07:00 committed by GitHub
commit a17d9f80a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1714,6 +1714,16 @@ func (bc *Blockchain) HasTransaction(hash util.Uint256) bool {
// HasBlock returns true if the blockchain contains the given
// block hash.
func (bc *Blockchain) HasBlock(hash util.Uint256) bool {
var height = bc.BlockHeight()
bc.headerHashesLock.RLock()
for i := int(height); i >= int(height)-4 && i >= 0; i-- {
if hash.Equals(bc.headerHashes[i]) {
bc.headerHashesLock.RUnlock()
return true
}
}
bc.headerHashesLock.RUnlock()
if header, err := bc.GetHeader(hash); err == nil {
return header.Index <= bc.BlockHeight()
}