github.com/Cleverse/go-ethereum@v0.0.0-20220927095127-45113064e7f2/core/blockchain_arbitrum.go (about)

     1  // Copyright 2021 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Package core implements the Ethereum consensus protocol.
    18  package core
    19  
    20  import (
    21  	"time"
    22  
    23  	"github.com/ethereum/go-ethereum/core/state"
    24  	"github.com/ethereum/go-ethereum/core/types"
    25  	"github.com/ethereum/go-ethereum/log"
    26  	"github.com/ethereum/go-ethereum/rpc"
    27  )
    28  
    29  // WriteBlockAndSetHeadWithTime also counts processTime, which will cause intermittent TrieDirty cache writes
    30  func (bc *BlockChain) WriteBlockAndSetHeadWithTime(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB, emitHeadEvent bool, processTime time.Duration) (status WriteStatus, err error) {
    31  	if !bc.chainmu.TryLock() {
    32  		return NonStatTy, errChainStopped
    33  	}
    34  	defer bc.chainmu.Unlock()
    35  	bc.gcproc += processTime
    36  	return bc.writeBlockAndSetHead(block, receipts, logs, state, emitHeadEvent)
    37  }
    38  
    39  func (bc *BlockChain) ReorgToOldBlock(newHead *types.Block) error {
    40  	bc.wg.Add(1)
    41  	defer bc.wg.Done()
    42  	bc.chainmu.MustLock()
    43  	defer bc.chainmu.Unlock()
    44  	oldHead := bc.CurrentBlock()
    45  	if oldHead.Hash() == newHead.Hash() {
    46  		return nil
    47  	}
    48  	bc.writeHeadBlock(newHead)
    49  	err := bc.reorg(oldHead, newHead)
    50  	if err != nil {
    51  		return err
    52  	}
    53  	bc.chainHeadFeed.Send(ChainHeadEvent{Block: newHead})
    54  	return nil
    55  }
    56  
    57  func (bc *BlockChain) ClipToPostNitroGenesis(blockNum rpc.BlockNumber) (rpc.BlockNumber, rpc.BlockNumber) {
    58  	currentBlock := rpc.BlockNumber(bc.CurrentBlock().NumberU64())
    59  	nitroGenesis := rpc.BlockNumber(bc.Config().ArbitrumChainParams.GenesisBlockNum)
    60  	if blockNum == rpc.LatestBlockNumber || blockNum == rpc.PendingBlockNumber {
    61  		blockNum = currentBlock
    62  	}
    63  	if blockNum > currentBlock {
    64  		blockNum = currentBlock
    65  	}
    66  	if blockNum < nitroGenesis {
    67  		blockNum = nitroGenesis
    68  	}
    69  	return blockNum, currentBlock
    70  }
    71  
    72  func (bc *BlockChain) RecoverState(block *types.Block) error {
    73  	if bc.HasState(block.Root()) {
    74  		return nil
    75  	}
    76  	log.Warn("recovering block state", "num", block.Number(), "hash", block.Hash(), "root", block.Root())
    77  	_, err := bc.recoverAncestors(block)
    78  	return err
    79  }