// Body is a simple (mutable, non-safe) data container for storing and moving // a block's data contents (transactions and uncles) together. type Body struct { Transactions []*Transaction Uncles []*Header }
// source: core/types/block.go // // NewBlock creates a new block. The input data is copied, // changes to header and to the field values will not affect the // block. // // The values of TxHash, UncleHash, ReceiptHash and Bloom in header // are ignored and set to values derived from the given txs, uncles // and receipts. funcNewBlock(header *Header, txs []*Transaction, uncles []*Header, receipts []*Receipt) *Block { b := &Block{header: CopyHeader(header), td: new(big.Int)}
// source: miner/miner.go // // PendingBlock returns the currently pending block. // // Note, to access both the pending block and the pending state // simultaneously, please use Pending(), as the pending state can // change between multiple method calls func(self *Miner)PendingBlock() *types.Block { return self.worker.pendingBlock() }
mu sync.RWMutex // global mutex for locking chain operations chainmu sync.RWMutex // blockchain insertion lock procmu sync.RWMutex // block processor lock
checkpoint int// checkpoint counts towards the new checkpoint currentBlock atomic.Value // Current head of the block chain currentFastBlock atomic.Value // Current head of the fast-sync chain (may be above the block chain!)
stateCache state.Database // State database to reuse between imports (contains state cache) bodyCache *lru.Cache // Cache for the most recent block bodies bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format blockCache *lru.Cache // Cache for the most recent entire blocks futureBlocks *lru.Cache // future blocks are blocks added for later processing
quit chanstruct{} // blockchain quit channel running int32// running must be called atomically // procInterrupt must be atomically called procInterrupt int32// interrupt signaler for block processing wg sync.WaitGroup // chain processing wait group for shutting down
// 共识算法的引擎 engine consensus.Engine
// 当需要导入 blocks 时需要根据两个阶段的 Validator 的规则集进行校验 // 分别为 block validator(区块验证器) 和 state validator(状态验证器) processor Processor // block processor interface validator Validator // block and state validator interface
// HeaderChain implements the basic block header chain logic that is shared by // core.BlockChain and light.LightChain. It is not usable in itself, only as // a part of either structure. // It is not thread safe either, the encapsulating chain structures should do // the necessary mutex locking/unlocking. type HeaderChain struct { config *params.ChainConfig
// Current head of the header chain (may be above the block chain!) currentHeader atomic.Value // 当前区块头
// Hash of the current head of the header chain (prevent recomputing all the time) currentHeaderHash common.Hash // 当前区块头的 hash(避免重新计算)
headerCache *lru.Cache // Cache for the most recent block headers tdCache *lru.Cache // Cache for the most recent block total difficulties numberCache *lru.Cache // Cache for the most recent block numbers