Go-ethereum 源碼解析之 go-ethereum/core/types.go

Go-ethereum 源碼解析之 go-ethereum/core/types.go


Source code

// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package core

import (
    "github.com/ethereum/go-ethereum/core/state"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/core/vm"
)

// Validator is an interface which defines the standard for block validation. It
// is only responsible for validating block contents, as the header validation is
// done by the specific consensus engines.
//
type Validator interface {
    // ValidateBody validates the given block's content.
    ValidateBody(block *types.Block) error

    // ValidateState validates the given statedb and optionally the receipts and
    // gas used.
    ValidateState(block, parent *types.Block, state *state.StateDB, receipts types.Receipts, usedGas uint64) error
}

// Processor is an interface for processing blocks using a given initial state.
//
// Process takes the block to be processed and the statedb upon which the
// initial state is based. It should return the receipts generated, amount
// of gas used in the process and return an error if any of the internal rules
// failed.
type Processor interface {
    Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error)
}


Appendix A. 總體批注

本文件定義了兩個(gè)接口。一個(gè)是用于驗(yàn)證區(qū)塊的接口 Validator。另一個(gè)是用于處理區(qū)塊的接口 Processor。需要注意,這些操作都需要用到狀態(tài)數(shù)據(jù)庫(kù)。


Appendix B. 詳細(xì)批注

1. type Validator interface

接口 Validator 定義了區(qū)塊驗(yàn)證的標(biāo)準(zhǔn)接口。它只負(fù)責(zé)驗(yàn)證區(qū)塊內(nèi)容,因?yàn)閰^(qū)塊頭驗(yàn)證由特定的共識(shí)引擎完成。

(1) ValidateBody(block *types.Block) error

方法 ValidateBody() 驗(yàn)證給定區(qū)塊的內(nèi)容。

參數(shù):

  • block *types.Block: 給定區(qū)塊

返回值:

  • error: 錯(cuò)誤消息或 nil

(2) ValidateState(block, parent *types.Block, state *state.StateDB, receipts types.Receipts, usedGas uint64) error

方法 ValidateState() 驗(yàn)證給定的狀態(tài)數(shù)據(jù)庫(kù)以及可選的交易回執(zhí)列表和使用的 Gas。

參數(shù):

  • block *types.Block: 區(qū)塊
  • parent *types.Block: 父區(qū)塊
  • state *state.StateDB: 狀態(tài)數(shù)據(jù)庫(kù)
  • receipts types.Receipts: 交易回執(zhí)列表
  • usedGas uint64: 已使用的 Gas

返回值:

  • error: 錯(cuò)誤消息或 nil

2. type Processor interface

接口 Processor 使用一個(gè)給定的初始狀態(tài)數(shù)據(jù)庫(kù)處理區(qū)塊。

(1) Process(block *types.Block, statedb state.StateDB, cfg vm.Config) (types.Receipts, []types.Log, uint64, error)

方法 Process() 基于給定的初始狀態(tài)數(shù)據(jù)庫(kù)處理給定的區(qū)塊。它應(yīng)該返回生成的交易回執(zhí)列表,消耗的 Gas,如果任一內(nèi)部規(guī)則失敗返回錯(cuò)誤。

參數(shù):

  • block *types.Block: 區(qū)塊
  • statedb *state.StateDB: 狀態(tài)數(shù)據(jù)庫(kù)
  • cfg vm.Config: EVM 配置信息

返回值:

  • types.Receipts: 交易回執(zhí)列表
  • []*types.Log: 日志列表
  • uint64: 消耗的 Gas
  • error: 錯(cuò)誤消息或 nil

Reference

  1. https://github.com/ethereum/go-ethereum/blob/master/core/types.go

Contributor

  1. Windstamp, https://github.com/windstamp
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容