MCPcopy Create free account
hub / github.com/CPChain/chain / insert

Method insert

core/blockchain.go:620–637  ·  view source on GitHub ↗

insert injects a new head block into the current block chain. This method assumes that the block is indeed a true head. It will also reset the head header and the head fast sync block to this very same block if they are older or if they are on a different side chain. Note, this function assumes tha

(block *types.Block)

Source from the content-addressed store, hash-verified

618//
619// Note, this function assumes that the `mu` mutex is held!
620func (bc *BlockChain) insert(block *types.Block) {
621 // If the block is on a side chain or an unknown one, force other heads onto it too
622 updateHeads := rawdb.ReadCanonicalHash(bc.db, block.NumberU64()) != block.Hash()
623
624 // Add the block to the canonical chain number scheme and mark as the head
625 rawdb.WriteCanonicalHash(bc.db, block.Hash(), block.NumberU64())
626 rawdb.WriteHeadBlockHash(bc.db, block.Hash())
627
628 bc.currentBlock.Store(block)
629
630 // If the block is better than our head or is on a different chain, force update heads
631 if updateHeads {
632 bc.hc.SetCurrentHeader(block.Header())
633 rawdb.WriteHeadFastBlockHash(bc.db, block.Hash())
634
635 bc.currentFastBlock.Store(block)
636 }
637}
638
639// Genesis retrieves the chain's genesis block.
640func (bc *BlockChain) Genesis() *types.Block {

Callers 3

ResetWithGenesisBlockMethod · 0.95
WriteBlockWithStateMethod · 0.95
reorgMethod · 0.95

Calls 4

NumberU64Method · 0.80
SetCurrentHeaderMethod · 0.80
HeaderMethod · 0.80
HashMethod · 0.65

Tested by

no test coverage detected