Go-ethereum: Ask a question about the code in func SetHead

Created on 31 Oct 2019  路  4Comments  路  Source: ethereum/go-ethereum

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?

backlog bug

All 4 comments

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.

Please allow me to fix this

Sure!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

362228416 picture 362228416  路  3Comments

prene picture prene  路  3Comments

AdrianScott picture AdrianScott  路  3Comments

leonzhao picture leonzhao  路  3Comments

freshonline picture freshonline  路  3Comments