go-ethereum源码分析-consensus - 共识

源码包路径:github.com/ethereum/go-ethereum/consensus/

共识算法引擎接口的定义如下

type Engine interface {
   // Author retrieves the Ethereum address of the account that minted the given
 // block, which may be different from the header's coinbase if a consensus
 // engine is based on signatures.
 Author(header *types.Header) (common.Address, error)

   // VerifyHeader checks whether a header conforms to the consensus rules of a
 // given engine. Verifying the seal may be done optionally here, or explicitly
 // via the VerifySeal method.
 VerifyHeader(chain ChainReader, header *types.Header, seal bool) error

 // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
 // concurrently. The method returns a quit channel to abort the operations and
 // a results channel to retrieve the async verifications (the order is that of
 // the input slice).
 VerifyHeaders(chain ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error)

   // VerifyUncles verifies that the given block's uncles conform to the consensus
 // rules of a given engine.
 VerifyUncles(chain ChainReader, block *types.Block) error

 // VerifySeal checks whether the crypto seal on a header is valid according to
 // the consensus rules of the given engine.
 VerifySeal(chain ChainReader, header *types.Header) error

 // Prepare initializes the consensus fields of a block header according to the
 // rules of a particular engine. The changes are executed inline.
 Prepare(chain ChainReader, header *types.Header) error

 // Finalize runs any post-transaction state modifications (e.g. block rewards)
 // and assembles the final block.
 // Note: The block header and state database might be updated to reflect any
 // consensus rules that happen at finalization (e.g. block rewards).
 Finalize(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
 uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error)

   // Seal generates a new block for the given input block with the local miner's
 // seal place on top.
 Seal(chain ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error)

   // APIs returns the RPC APIs this consensus engine provides.
 APIs(chain ChainReader) []rpc.API
}

// PoW is a consensus engine based on proof-of-work.
type PoW interface {
   Engine

   // Hashrate returns the current mining hashrate of a PoW consensus engine.
 Hashrate() float64
}

其中最主要的是

prepare 预处理当前区块的头信息(比如 挖矿难度)(Homestead)

diff = (parent_diff + (parent_diff / 2048 * max(1 - (block_timestamp - parent_timestamp) / 10, -99)) ) + 2^(periodCount - 2)
  1. parent_diff :上一个区块的难度
  2.  block_timestamp :当前块的时间戳
  3. parent_timestamp:上一个块的时间戳
  4. periodCount :区块num/100000
  5. block_timestamp - parent_timestamp 差值小于10秒 变难

  6. block_timestamp - parent_timestamp 差值10-20秒 不变

  7. block_timestamp - parent_timestamp 差值大于20秒 变容易,并且大的越多,越容易,但是又上限
  8. 总体上块的难度是递增的

seal 开始做挖矿的事情,“解题”直到成功或者退出

  1. 根据挖矿难度计算目标值
  2. 选取随机数nonce+区块头(不包含nonce)的hash,再做一次hash,结果小于目标值,则退出,否则循环重试
  3. 如果外部退出了(比如已经收到这个块了),则立马放弃当前块的打包

Finalize 做挖矿成功后最后善后的事情

  1. 计算矿工的奖励:区块奖励,叔块奖励,
  2. 但是这里没有交易费~~


verifyHeader

  1. 校验extra大小
  2. 校验区块时间戳,跟当前时间比
  3. 校验难度值
  4. 校验gaslimit上线
  5. 校验区块的总gasuserd小于 gaslimit
  6. 校验区块的gaslimit 是在合理范围
  7. 特殊的校验,比如dao分叉后的几个块extra里面写了特殊数据,来判断一下

VerifySeal

  1. 校验难度的有效性
  2. nonce是否小于目标值(解题成功)



 

 


你可能感兴趣的:(golang,ethereum,区块链,源码分析,共识算法,以太坊,ethereum,源码分析)