源碼
GitHub地址 https://github.com/TTCECO/gttc
目錄
基于以太坊go-ethereum的DPOS實(shí)現(xiàn)(一)源碼及測(cè)試運(yùn)行
基于以太坊go-ethereum的DPOS實(shí)現(xiàn)(二)簡(jiǎn)要說明
基于以太坊go-ethereum的DPOS實(shí)現(xiàn)(三)創(chuàng)世塊
基于以太坊go-ethereum的DPOS實(shí)現(xiàn)(四)共識(shí)接口
基于以太坊go-ethereum的DPOS實(shí)現(xiàn)(五)定時(shí)出塊
基于以太坊go-ethereum的DPOS實(shí)現(xiàn)(六)代表選擇
共識(shí)接口
在go-ethereum中添加一種共識(shí)機(jī)制,最方便的方式是實(shí)現(xiàn)consensus.go中Engine接口所定義的方法亩码。
// Engine is an algorithm agnostic consensus engine.
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)
// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
// that a new block should have.
CalcDifficulty(chain ChainReader, time uint64, parent *types.Header) *big.Int
// APIs returns the RPC APIs this consensus engine provides.
APIs(chain ChainReader) []rpc.API
}
在本篇及接下來的幾篇關(guān)于DPOS實(shí)現(xiàn)的文章中瞬场,所有的敘述都是圍繞著這些方法的實(shí)現(xiàn)展開的,所以在這里先對(duì)這些方法做下說明句喜。
Author
// 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)
對(duì)于以太坊的POW共識(shí)來說Author返回的就是header.coinbase的地址预愤,可以認(rèn)為是礦工地址或受益人的地址,這個(gè)地址并不需要用簽名來證明咳胃。但對(duì)于DPOS共識(shí)機(jī)制來說植康,必須證明出塊地址必須是當(dāng)前有權(quán)限出塊的地址,所以在alien當(dāng)中展懈,Author的返回值是根據(jù)簽名計(jì)算而來的销睁,方式和clique中的完全相同。
VerifyHeader & VerifyHeaders
// 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)
這兩個(gè)方法都是用來驗(yàn)證頭部信息的存崖,可以調(diào)用同一個(gè)方法實(shí)現(xiàn)冻记,其中驗(yàn)證的內(nèi)容會(huì)單獨(dú)一篇敘述。
VerifyUncles
// VerifyUncles verifies that the given block's uncles conform to the consensus
// rules of a given engine.
VerifyUncles(chain ChainReader, block *types.Block) error
叔塊(Uncles)在DPOS共識(shí)當(dāng)中沒有意義来惧,所以可以直接返回nil冗栗,也可以驗(yàn)證如果叔塊存在則報(bào)錯(cuò),需要注意的是挖礦獎(jiǎng)金的計(jì)算處也不需要計(jì)算叔塊供搀。
VerifySeal
// 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
注釋中敘述的很清晰隅居,但是在開發(fā)過程中,因?yàn)閹讉€(gè)Verify方法會(huì)逐層調(diào)用趁曼,所以具體功能上的區(qū)分并不清晰军浆。
Prepare
// 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
在DPOS共識(shí)當(dāng)中,如果啟動(dòng)后還未達(dá)到genesisTimestamp中所定義的首輪開始的出塊時(shí)間挡闰,則等待乒融。
Finalize
// 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)
Finalize是DPOS實(shí)現(xiàn)中非常重要的一個(gè)方法,除了計(jì)算header.extra中簽名的部分以外摄悯,幾乎所有的數(shù)據(jù)的計(jì)算和組裝都在這個(gè)方法中完成赞季。比如獲取代表投票,提議奢驯,表決等的交易及根據(jù)快照所做的計(jì)算入出塊順序確認(rèn)等申钩。
Seal
// 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)
DPOS實(shí)現(xiàn)中,Seal方法根據(jù)已經(jīng)unlock的賬戶信息瘪阁,在header.extra中為block添加簽名撒遣。
CalcDifficulty
// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
// that a new block should have.
CalcDifficulty(chain ChainReader, time uint64, parent *types.Header) *big.Int
直接設(shè)置為1邮偎,DPOS不需要工作量證明。
APIs
// APIs returns the RPC APIs this consensus engine provides.
APIs(chain ChainReader) []rpc.API
對(duì)外提供的rpc接口定義义黎。