基于以太坊go-ethereum的DPOS實(shí)現(xiàn)(四)共識(shí)接口

源碼

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接口定義义黎。

下一節(jié) 基于以太坊go-ethereum的DPOS實(shí)現(xiàn)(五)定時(shí)出塊

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末禾进,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子廉涕,更是在濱河造成了極大的恐慌泻云,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,941評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件狐蜕,死亡現(xiàn)場(chǎng)離奇詭異宠纯,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)层释,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門婆瓜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人贡羔,你說我怎么就攤上這事勃救。” “怎么了治力?”我有些...
    開封第一講書人閱讀 165,345評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵蒙秒,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我宵统,道長(zhǎng)晕讲,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,851評(píng)論 1 295
  • 正文 為了忘掉前任马澈,我火速辦了婚禮瓢省,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘痊班。我一直安慰自己勤婚,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評(píng)論 6 392
  • 文/花漫 我一把揭開白布涤伐。 她就那樣靜靜地躺著馒胆,像睡著了一般。 火紅的嫁衣襯著肌膚如雪凝果。 梳的紋絲不亂的頭發(fā)上祝迂,一...
    開封第一講書人閱讀 51,688評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音器净,去河邊找鬼型雳。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的纠俭。 我是一名探鬼主播沿量,決...
    沈念sama閱讀 40,414評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼冤荆!你這毒婦竟也來了欧瘪?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,319評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤匙赞,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后妖碉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體涌庭,經(jīng)...
    沈念sama閱讀 45,775評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年欧宜,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了坐榆。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,096評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡冗茸,死狀恐怖席镀,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情夏漱,我是刑警寧澤豪诲,帶...
    沈念sama閱讀 35,789評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站挂绰,受9級(jí)特大地震影響屎篱,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜葵蒂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評(píng)論 3 331
  • 文/蒙蒙 一交播、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧践付,春花似錦秦士、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至命爬,卻和暖如春次洼,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背遇骑。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評(píng)論 1 271
  • 我被黑心中介騙來泰國(guó)打工卖毁, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,308評(píng)論 3 372
  • 正文 我出身青樓亥啦,卻偏偏與公主長(zhǎng)得像炭剪,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子翔脱,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評(píng)論 2 355