Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: avoid to cache block before wroten into db #2566

Merged
merged 1 commit into from
Jul 10, 2024

Conversation

buddh0
Copy link
Collaborator

@buddh0 buddh0 commented Jul 5, 2024

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 buddh0 marked this pull request as ready for review July 5, 2024 08:57
@zzzckck zzzckck merged commit 863fdea into bnb-chain:develop Jul 10, 2024
7 checks passed
jingjunLi added a commit to jingjunLi/bsc that referenced this pull request Jul 22, 2024
jingjunLi added a commit to jingjunLi/bsc that referenced this pull request Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants