Skip to content

Commit

Permalink
Merge pull request #2759 from nspcc-dev/avoid-db-lookup-in-hasblock
Browse files Browse the repository at this point in the history
core: optimize HasBlock check for recent blocks
  • Loading branch information
roman-khimov authored Oct 24, 2022
2 parents f42b8e7 + 0c3b036 commit a17d9f8
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit a17d9f8

Please sign in to comment.