bsc icon indicating copy to clipboard operation
bsc copied to clipboard

core: avoid to cache block before wroten into db

Open buddh0 opened this issue 7 months ago • 0 comments

Description

core: avoid to cache block before wroten into db

Rationale

let's see the GetBlock interface, it's used to get block from db after add func cacheBlock, block not wroten into db also will be add into bc.blockCache. so this destroy the semitics of GetBlock, and will lead to potential issue

// GetBlock retrieves a block from the database by hash and number,
// caching it if found.
func (bc *BlockChain) GetBlock(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
	}
	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
}

for example, if a block can be retrieved by calling GetBlock, then the Td can be retrieved by GetTd, image in the above code, no need to check GetTd will return nil if no exist of cacheBlock but now, it lead to crash.

so this PR suggest to remove cacheBlock to recover the semitics of GetBlock

Example

add an example CLI or API response...

Changes

Notable changes:

  • add each change in a bullet point here
  • ...

buddh0 avatar Jul 05 '24 08:07 buddh0