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

Ask a question about the code in func SetHead #20224

Closed
james-ray opened this issue Oct 31, 2019 · 4 comments
Closed

Ask a question about the code in func SetHead #20224

james-ray opened this issue Oct 31, 2019 · 4 comments
Labels

Comments

@james-ray
Copy link

james-ray commented Oct 31, 2019

func (bc *BlockChain) SetHead(head uint64) error {
	log.Warn("Rewinding blockchain", "target", head)

	bc.chainmu.Lock()
	defer bc.chainmu.Unlock()

	updateFn := func(db ethdb.KeyValueWriter, header *types.Header) {
		// Rewind the block chain, ensuring we don't end up with a stateless head block
		if currentBlock := bc.CurrentBlock(); currentBlock != nil && header.Number.Uint64() < currentBlock.NumberU64() {
			newHeadBlock := bc.GetBlock(header.Hash(), header.Number.Uint64())
			if newHeadBlock == nil {
				newHeadBlock = bc.genesisBlock
			} else {
				if _, err := state.New(newHeadBlock.Root(), bc.stateCache); err != nil {
					// Rewound state missing, rolled back to before pivot, reset to genesis
					newHeadBlock = bc.genesisBlock
				}
			}
			rawdb.WriteHeadBlockHash(db, newHeadBlock.Hash())
			bc.currentBlock.Store(newHeadBlock)
			headBlockGauge.Update(int64(newHeadBlock.NumberU64()))
		}
...

I want to ask, why here not try to repair the chain (call the func repair)?. Just reset to genesisBlock?

@karalabe
Copy link
Member

This is a valid issue. The code originates form before we had (in-memory) state pruning, so a missing state meant fast sync + pre-sync reset. Now, with pruning, it can indeed just happen that the state is missing for this particular block, but not for some previous ones.

@james-ray
Copy link
Author

james-ray commented Oct 31, 2019

Please allow me to fix this

@karalabe
Copy link
Member

Sure!

@karalabe
Copy link
Member

Fixed by #21409.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
@karalabe @adamschmideg @james-ray and others