Go-ethereum Source Analysis-consensus-consensus

Source: Internet
Author: User
Tags bool current time hash pow prepare

SOURCE Package Path: github.com/ethereum/go-ethereum/consensus/

The definition of the consensus algorithm engine interface is as follows

Type Engine Interface {//Author retrieves the Ethereum address of the account that minted the given//block, which
May was different from the header ' s coinbase if a consensus//engine was 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 is 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//Concurren tly. The method returns a quit channel to abort the operations and//a results channel to retrieve the async verifications (t
He 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 con form to theConsensus//rules of a given engine.  verifyuncles (Chain Chainreader, Block *types.  Block) Error  //Verifyseal checks whether the crypto seal on a header was 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 PARTICU Lar engine.
The changes is 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 is updated to reflect any//consensus rules which happen at Finalizati
On (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.  hashra Te () float64}

The most important is prepare preprocessing the current chunk header information (such as mining difficulty) (Homestead)

diff = (Parent_diff + (parent_diff/2048 * MAX (1-(Block_timestamp-parent_timestamp)/ten, -99)) + 2^ (Periodcount- 2)
Parent_diff: The difficulty of the previous block
Block_timestamp: Timestamp of the current block Parent_timestamp: Timestamp of the previous block Periodcount: Chunk num/100000

Block_timestamp-parent_timestamp difference of less than 10 seconds becomes difficult

Block_timestamp-parent_timestamp difference of 10-20 seconds constant block_timestamp-parent_timestamp difference is greater than 20 seconds to become easier, and the larger the more, the easier, but also the upper limit The overall difficulty of the block is the increment of the seal start to do mining things, "solve the problem" until the success or exit according to the mining difficulty calculation target value Select random number nonce+ chunk head (not including nonce) hash, then do a hash, the result is less than the target value, then exit, Otherwise, the loop retries if the external exit (such as already received this block), then immediately discard the current block of packaging finalize to do mining success after the last thing to calculate the miners reward: block reward, tertiary block reward, but there is no transaction fee ~ ~


verifyheader Check extra size check block timestamp, with the current time than the check difficulty value check gaslimit on-line check block total gasuserd less than gaslimit check block Gaslimit is a reasonable range of special checks, For example DAO Fork after several blocks extra inside write special data, to judge the validity of verifyseal Check difficulty nonce is less than the target value (solve the problem successfully)




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.