go-ethereum源碼分析-consensus - 共識

來源:互聯網
上載者:User

源碼包路徑: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)
parent_diff :上一個區塊的難度
 block_timestamp :當前塊的時間戳記 parent_timestamp:上一個塊的時間戳記 periodCount :區塊num/100000

block_timestamp - parent_timestamp 差值小於10秒 變難

block_timestamp - parent_timestamp 差值10-20秒 不變 block_timestamp - parent_timestamp 差值大於20秒 變容易,並且大的越多,越容易,但是又上限 總體上塊的難度是遞增的 seal 開始做挖礦的事情,“解題”直到成功或者退出 根據挖礦難度計算目標值 選取隨機數nonce+區塊頭(不包含nonce)的hash,再做一次hash,結果小於目標值,則退出,否則迴圈重試 如果外部退出了(比如已經收到這個塊了),則立馬放棄當前塊的打包 Finalize 做挖礦成功後最後善後的事情 計算礦工的獎勵:區塊獎勵,叔塊獎勵, 但是這裡沒有交易費~~


verifyHeader 校正extra大小 校正區塊時間戳記,跟目前時間比 校正難度值 校正gaslimit上線 校正區塊的總gasuserd小於 gaslimit 校正區塊的gaslimit 是在合理範圍 特殊的校正,比如dao分叉後的幾個塊extra裡面寫了特殊資料,來判斷一下 VerifySeal 校正難度的有效性 nonce是否小於目標值(解題成功)



 

 


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.