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. 總體批注
本文件定義了兩個接口。一個是用于驗證區(qū)塊的接口 Validator谣拣。另一個是用于處理區(qū)塊的接口 Processor募寨。需要注意,這些操作都需要用到狀態(tài)數(shù)據(jù)庫森缠。
Appendix B. 詳細批注
1. type Validator interface
接口 Validator 定義了區(qū)塊驗證的標準接口拔鹰。它只負責驗證區(qū)塊內(nèi)容,因為區(qū)塊頭驗證由特定的共識引擎完成贵涵。
(1) ValidateBody(block *types.Block) error
方法 ValidateBody() 驗證給定區(qū)塊的內(nèi)容列肢。
參數(shù):
- block *types.Block: 給定區(qū)塊
返回值:
- error: 錯誤消息或 nil
(2) ValidateState(block, parent *types.Block, state *state.StateDB, receipts types.Receipts, usedGas uint64) error
方法 ValidateState() 驗證給定的狀態(tài)數(shù)據(jù)庫以及可選的交易回執(zhí)列表和使用的 Gas。
參數(shù):
- block *types.Block: 區(qū)塊
- parent *types.Block: 父區(qū)塊
- state *state.StateDB: 狀態(tài)數(shù)據(jù)庫
- receipts types.Receipts: 交易回執(zhí)列表
- usedGas uint64: 已使用的 Gas
返回值:
- error: 錯誤消息或 nil
2. type Processor interface
接口 Processor 使用一個給定的初始狀態(tài)數(shù)據(jù)庫處理區(qū)塊宾茂。
(1) Process(block *types.Block, statedb state.StateDB, cfg vm.Config) (types.Receipts, []types.Log, uint64, error)
方法 Process() 基于給定的初始狀態(tài)數(shù)據(jù)庫處理給定的區(qū)塊瓷马。它應(yīng)該返回生成的交易回執(zhí)列表,消耗的 Gas跨晴,如果任一內(nèi)部規(guī)則失敗返回錯誤欧聘。
參數(shù):
- block *types.Block: 區(qū)塊
- statedb *state.StateDB: 狀態(tài)數(shù)據(jù)庫
- cfg vm.Config: EVM 配置信息
返回值:
- types.Receipts: 交易回執(zhí)列表
- []*types.Log: 日志列表
- uint64: 消耗的 Gas
- error: 錯誤消息或 nil
Reference
Contributor
- Windstamp, https://github.com/windstamp