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 .
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. 总体批注
本文件定义了两个接口。一个是用于验证区块的接口 Validator。另一个是用于处理区块的接口 Processor。需要注意,这些操作都需要用到状态数据库。
Appendix B. 详细批注
1. type Validator interface
接口 Validator 定义了区块验证的标准接口。它只负责验证区块内容,因为区块头验证由特定的共识引擎完成。
(1) ValidateBody(block *types.Block) error
方法 ValidateBody() 验证给定区块的内容。
参数:
- block *types.Block: 给定区块
返回值:
- error: 错误消息或 nil
(2) ValidateState(block, parent *types.Block, state *state.StateDB, receipts types.Receipts, usedGas uint64) error
方法 ValidateState() 验证给定的状态数据库以及可选的交易回执列表和使用的 Gas。
参数:
- block *types.Block: 区块
- parent *types.Block: 父区块
- state *state.StateDB: 状态数据库
- receipts types.Receipts: 交易回执列表
- usedGas uint64: 已使用的 Gas
返回值:
- error: 错误消息或 nil
2. type Processor interface
接口 Processor 使用一个给定的初始状态数据库处理区块。
(1) Process(block *types.Block, statedb state.StateDB, cfg vm.Config) (types.Receipts, []types.Log, uint64, error)
方法 Process() 基于给定的初始状态数据库处理给定的区块。它应该返回生成的交易回执列表,消耗的 Gas,如果任一内部规则失败返回错误。
参数:
- block *types.Block: 区块
- statedb *state.StateDB: 状态数据库
- cfg vm.Config: EVM 配置信息
返回值:
- types.Receipts: 交易回执列表
- []*types.Log: 日志列表
- uint64: 消耗的 Gas
- error: 错误消息或 nil
Reference
- https://github.com/ethereum/go-ethereum/blob/master/core/types.go
Contributor
- Windstamp, https://github.com/windstamp