Skip to content

Commit

Permalink
Added new OrCandidate methods to return any block, guarded old GetHea…
Browse files Browse the repository at this point in the history
…der and GetBlock methods
  • Loading branch information
kiltsonfire authored and gameofpointers committed Mar 13, 2023
1 parent f34833d commit 13a2592
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
20 changes: 20 additions & 0 deletions core/bodydb.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ func (bc *BodyDb) Engine() consensus.Engine {
// GetBlock retrieves a block from the database by hash and number,
// caching it if found.
func (bc *BodyDb) GetBlock(hash common.Hash, number uint64) *types.Block {
td := rawdb.ReadTd(bc.db, hash, number)
if td == nil {
return nil
}
// Short circuit if the block's already in the cache, retrieve otherwise
if block, ok := bc.blockCache.Get(hash); ok {
return block.(*types.Block)
}
block := rawdb.ReadBlock(bc.db, hash, number)
if block == nil {
return nil
}
// Cache the found block for next time and return
bc.blockCache.Add(block.Hash(), block)
return block
}

// GetBlockOrCandidate retrieves any known block from the database by hash and number,
// caching it if found.
func (bc *BodyDb) GetBlockOrCandidate(hash common.Hash, number uint64) *types.Block {
// Short circuit if the block's already in the cache, retrieve otherwise
if block, ok := bc.blockCache.Get(hash); ok {
return block.(*types.Block)
Expand Down
17 changes: 17 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ func (c *Core) GetBlockByHash(hash common.Hash) *types.Block {
return c.sl.hc.GetBlockByHash(hash)
}

// GetBlockOrCandidateByHash retrieves a block from the database by hash, caching it if found.
func (c *Core) GetBlockOrCandidateByHash(hash common.Hash) *types.Block {
return c.sl.hc.GetBlockOrCandidateByHash(hash)
}

// GetHeaderByNumber retrieves a block header from the database by number,
// caching it (associated with its hash) if found.
func (c *Core) GetHeaderByNumber(number uint64) *types.Header {
Expand Down Expand Up @@ -348,6 +353,18 @@ func (c *Core) GetHeaderByHash(hash common.Hash) *types.Header {
return c.sl.hc.GetHeaderByHash(hash)
}

// GetHeaderOrCandidate retrieves a block header from the database by hash and number,
// caching it if found.
func (c *Core) GetHeaderOrCandidate(hash common.Hash, number uint64) *types.Header {
return c.sl.hc.GetHeaderOrCandidate(hash, number)
}

// GetHeaderOrCandidateByHash retrieves a block header from the database by hash, caching it if
// found.
func (c *Core) GetHeaderOrCandidateByHash(hash common.Hash) *types.Header {
return c.sl.hc.GetHeaderOrCandidateByHash(hash)
}

// HasHeader checks if a block header is present in the database or not, caching
// it if present.
func (c *Core) HasHeader(hash common.Hash, number uint64) bool {
Expand Down
45 changes: 45 additions & 0 deletions core/headerchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,10 @@ func (hc *HeaderChain) GetTdByHash(hash common.Hash) *big.Int {
// GetHeader retrieves a block header from the database by hash and number,
// caching it if found.
func (hc *HeaderChain) GetHeader(hash common.Hash, number uint64) *types.Header {
td := hc.GetTdByHash(hash)
if td == nil {
return nil
}
// Short circuit if the header's already in the cache, retrieve otherwise
if header, ok := hc.headerCache.Get(hash); ok {
return header.(*types.Header)
Expand All @@ -490,13 +494,45 @@ func (hc *HeaderChain) GetHeader(hash common.Hash, number uint64) *types.Header
// GetHeaderByHash retrieves a block header from the database by hash, caching it if
// found.
func (hc *HeaderChain) GetHeaderByHash(hash common.Hash) *types.Header {
td := hc.GetTdByHash(hash)
if td == nil {
return nil
}
number := hc.GetBlockNumber(hash)
if number == nil {
return nil
}

return hc.GetHeader(hash, *number)
}

// GetHeaderOrCandidate retrieves a block header from the database by hash and number,
// caching it if found.
func (hc *HeaderChain) GetHeaderOrCandidate(hash common.Hash, number uint64) *types.Header {
// Short circuit if the header's already in the cache, retrieve otherwise
if header, ok := hc.headerCache.Get(hash); ok {
return header.(*types.Header)
}
header := rawdb.ReadHeader(hc.headerDb, hash, number)
if header == nil {
return nil
}
// Cache the found header for next time and return
hc.headerCache.Add(hash, header)
return header
}

// GetHeaderOrCandidateByHash retrieves a block header from the database by hash, caching it if
// found.
func (hc *HeaderChain) GetHeaderOrCandidateByHash(hash common.Hash) *types.Header {
number := hc.GetBlockNumber(hash)
if number == nil {
return nil
}

return hc.GetHeaderOrCandidate(hash, *number)
}

// HasHeader checks if a block header is present in the database or not.
// In theory, if header is present in the database, all relative components
// like td and hash->number should be present too.
Expand Down Expand Up @@ -640,6 +676,15 @@ func (hc *HeaderChain) GetBlockByHash(hash common.Hash) *types.Block {
return hc.GetBlock(hash, *number)
}

// GetBlockOrCandidateByHash retrieves any block from the database by hash, caching it if found.
func (hc *HeaderChain) GetBlockOrCandidateByHash(hash common.Hash) *types.Block {
number := hc.GetBlockNumber(hash)
if number == nil {
return nil
}
return hc.bc.GetBlockOrCandidate(hash, *number)
}

// GetBlockByNumber retrieves a block from the database by number, caching it
// (associated with its hash) if found.
func (hc *HeaderChain) GetBlockByNumber(number uint64) *types.Block {
Expand Down
8 changes: 4 additions & 4 deletions eth/protocols/eth/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ func answerGetBlockHeadersQuery(backend Backend, query *GetBlockHeadersPacket, p
if hashMode {
if first {
first = false
origin = backend.Core().GetHeaderByHash(query.Origin.Hash)
origin = backend.Core().GetHeaderOrCandidateByHash(query.Origin.Hash)
if origin != nil {
query.Origin.Number = origin.NumberU64()
}
} else {
origin = backend.Core().GetHeader(query.Origin.Hash, query.Origin.Number)
origin = backend.Core().GetHeaderOrCandidate(query.Origin.Hash, query.Origin.Number)
}
} else {
origin = backend.Core().GetHeaderByNumber(query.Origin.Number)
Expand Down Expand Up @@ -217,7 +217,7 @@ func handleGetBlock(backend Backend, msg Decoder, peer *Peer) error {
}
log.Info("Got a block fetch request eth/65: ", "Hash", query.Hash)
// check if we have the requested block in the database.
response := backend.Core().GetBlockByHash(query.Hash)
response := backend.Core().GetBlockOrCandidateByHash(query.Hash)
if response != nil {
return peer.SendNewBlock(response)
}
Expand All @@ -233,7 +233,7 @@ func handleGetBlock66(backend Backend, msg Decoder, peer *Peer) error {
}
log.Debug("Got a block fetch request eth/66: ", "Hash", query.Hash)
// check if we have the requested block in the database.
response := backend.Core().GetBlockByHash(query.Hash)
response := backend.Core().GetBlockOrCandidateByHash(query.Hash)
if response != nil {
return peer.SendNewBlock(response)
}
Expand Down

0 comments on commit 13a2592

Please sign in to comment.