以太坊go-ethereum源码研读(一)从Process函数相关自定义类型和结构体开始

本文主要是介绍以太坊go-ethereum源码研读(一)从Process函数相关自定义类型和结构体开始,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

  • 写在前面
  • 一些自定义类型
  • 一些结构体
    • Receipt
    • Log
    • Header
    • ChainConfig
    • BlockChain
    • Transaction
    • Block
    • StateProcessor
    • BlockContext
    • evm
    • Message
    • StateDB
    • accessList
    • Engine
  • 相关函数
    • Process
    • NewEVMBlockContext
    • NewEVM
    • NewEVMInterpreter
    • AsMessage
    • Prepare
    • Finalize

写在前面

现在自己对其中一些代码的理解还不够,等我逐渐深入学习后回回来再修改的。
其中对于一些代码的理解参考了以太坊黄皮书的内容。
链接: https://ethereum.github.io/yellowpaper/paper.pdf

一些自定义类型

类型名位置定义
Hashcommon/types.go32byte
Addresscommon/types.go20byte
Bloomcore/types/bloom9.go256byte(filter)
GasPoolcore/gaspool.gouint64

一些结构体

Receipt

位于core\types\receipt.go
为了方便索引、搜索交易和对交易的零知识证明,将交易执行中的某些信息进行编码形成了Receipt
在这里插入图片描述
参考黄皮书4.3.1

名字定义
Type交易类型
PostStateStateDB的MPT树根,相当于当前Block事务执行后所有账户状态
Status当前交易的状态
CumulativeGasUsed累积的Gas使用量
Bloom布隆过滤器,用来快速验证给定Log是否为这个事务生成
Logs交易执行过程中生成的log集合
TxHash交易hash值
ContractAddress交易对应智能合约地址
GasUsed使用的Gas量
BlockHash区块hash
BlockNumber区块号
TransactionIndex交易索引

Log

位于core\types\log.go
以太坊中定义了event和log机制,用于表示一个合约的日志。
在这里插入图片描述

名字定义
Address对应事件的合约地址
Topics用于检索日志时使用
Data由合约提供的ABI编码的内容
BlockNumber区块号
TxHash交易hash
TxIndex该Log对应的交易在区块中的索引
BlockHash该Log对应的交易所在的区块hash
Index该Log在区块中的索引
Removed当发生了链重组导致log被恢复,该字段为真。故如果通过过滤器查询log时,要多注意该字段。

Header

在这里插入图片描述
位于core\types\block.go
表示区块头

名字定义
ParentHash父区块的hash值
UncleHash叔区块RLP编码hash
Coinbase矿工地址
Root世界状态的根hash
TxHash交易信息的根hash
ReceiptHash收据信息的根hash
Bloom布隆过滤器
Difficulty挖矿的难度系数
Number区块序号
GasLimit区块内Gas消耗上限
GasUsed区块交易完成后消耗Gas总量
Time区块生成的时间(貌似并不太精准)
Extra区块创建者(矿工)记录的信息
MixDigesthashimotoFull函数生成后的digest生成的hash值,可用于结合nonce进行工作量证明
NoncePow枚举猜测的值
BaseFeeEIP-1559新增的区块头可选项,允许协议强制执行最低费用,而不会激励矿工和交易方在链下交易,形成链外市场。

ChainConfig

位于params\config.go
代表区块链的配置
在这里插入图片描述

名字定义
DAOForkBlockDAO硬分叉的区块号
DAOForkSupport当前节点是否支持DAO硬分叉

BlockChain

在这里插入图片描述

位于core\blockchain.go
待补充

Transaction

在这里插入图片描述
在这里插入图片描述
位于core\types\transaction.go
其中txdata是一个接口,定义如下,位于相同位置
在这里插入图片描述

位于core\types\transaction.go

名字定义
inner交易相关共识内容
time交易时间
hash交易hash值
size交易大小
from交易发起方

Block


位于core\types\block.go
表示区块

名字定义
ParentHash父区块的hash值
UncleHash叔区块RLP编码hash
Coinbase矿工地址
Root世界状态的根hash
TxHash交易信息的根hash
ReceiptHash收据信息的根hash
Bloom布隆过滤器
Difficulty挖矿的难度系数

StateProcessor

在这里插入图片描述
位于core\state_processor.go

名字定义
config区块配置
bc区块链
engine用于区块奖励的共识引擎

BlockContext

位于core\vm\evm.go
表示区块上下文,很多属性和前面是重复的。
在这里插入图片描述

名字定义
CoinBase矿工地址
GasLimitGas的限制量
BlockNumber区块号
Time时间
Difficulty难度
BaseFee协议执行最低费用

evm

位于core\vm\evm.go
待补充
在这里插入图片描述

名字定义
interpreter解释编译程序

Message

在这里插入图片描述
位于core\types\transaction.go
派生的事务(待补充)

名字定义
nonce即为交易中的nonce,用来交易排序,交易校验以避免双花
gasLimit当前消息gas最大限制
gasPrice油价
gasFeeCap用户所能支付给矿工的最大单价限额
gasTipCap小费,即在网络拥堵的情况下支付给矿工的小费,这个也意味着矿工有优先选择权。支付该费用,则优先打包区块

StateDB

在这里插入图片描述
位于core\state\statedb.go
StateDB结构用于存储所有的与Merkle trie相关的存储, 包括一些循环state结构

accessList

在这里插入图片描述
位于core\state\access_list.go
每个事务的访问列表,在某些形式的 EVM 执行过程中会触及的账户和合约存储位置的列表

Engine

在这里插入图片描述
位于consensus\consensus.go
engine是一个算法无关的用作共识层面的引擎

相关函数

Process

位于core\state_processor.go

func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) {var (receipts    types.ReceiptsusedGas     = new(uint64)header      = block.Header()blockHash   = block.Hash()blockNumber = block.Number()allLogs     []*types.Loggp          = new(GasPool).AddGas(block.GasLimit()))// Mutate the block and state according to any hard-fork specsif p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {misc.ApplyDAOHardFork(statedb)}blockContext := NewEVMBlockContext(header, p.bc, nil)vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, p.config, cfg)// Iterate over and process the individual transactionsfor i, tx := range block.Transactions() {msg, err := tx.AsMessage(types.MakeSigner(p.config, header.Number), header.BaseFee)if err != nil {return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)}statedb.Prepare(tx.Hash(), i)receipt, err := applyTransaction(msg, p.config, p.bc, nil, gp, statedb, blockNumber, blockHash, tx, usedGas, vmenv)if err != nil {return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)}receipts = append(receipts, receipt)allLogs = append(allLogs, receipt.Logs...)}// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles())return receipts, allLogs, *usedGas, nil
}

首先该函数判断当前区块是否为DAO硬分叉的区块,若是则调用ApplyDAOHardFork函数(待补充)。
然后调用NewEVMBlockContext函数为当前区块创立一个运行上下文。
随后调用NewEVM函数建立一个以太坊虚拟机,准备编译执行程序。
然后枚举区块中的交易并把它转换为消息格式,随后调用Prepare函数设置当前状态的交易hash和序号,随后调用applyTransaction在虚拟机中执行交易相关指令(见以太坊go-ethereum源码研读(二)进一步分析),得到返回的收据后,加入到列表中,并获取其中的日志加入到列表中,最后调用共识引擎的Finalize函数计算区块奖励并加入到最终状态中。

NewEVMBlockContext

位于core\evm.go
根据区块头信息,建立并返回一个BlockContext区块上下文信息结构体

func NewEVMBlockContext(header *types.Header, chain ChainContext, author *common.Address) vm.BlockContext {var (beneficiary common.AddressbaseFee     *big.Int)// If we don't have an explicit author (i.e. not mining), extract from the headerif author == nil {beneficiary, _ = chain.Engine().Author(header) // Ignore error, we're past header validation} else {beneficiary = *author}if header.BaseFee != nil {baseFee = new(big.Int).Set(header.BaseFee)}return vm.BlockContext{CanTransfer: CanTransfer,Transfer:    Transfer,GetHash:     GetHashFn(header, chain),Coinbase:    beneficiary,BlockNumber: new(big.Int).Set(header.Number),Time:        new(big.Int).SetUint64(header.Time),Difficulty:  new(big.Int).Set(header.Difficulty),BaseFee:     baseFee,GasLimit:    header.GasLimit,}
}

NewEVM

位于core/vm/evm.go
根据之前的上下文信息,以及其他的配置和内容建立虚拟机,同时调用NewEVMInterpreter函数建立对应解释器。

// NewEVM returns a new EVM. The returned EVM is not thread safe and should
// only ever be used *once*.
func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig *params.ChainConfig, config Config) *EVM {evm := &EVM{Context:     blockCtx,TxContext:   txCtx,StateDB:     statedb,Config:      config,chainConfig: chainConfig,chainRules:  chainConfig.Rules(blockCtx.BlockNumber, blockCtx.Random != nil),}evm.interpreter = NewEVMInterpreter(evm, config)return evm
}

NewEVMInterpreter

根据采取的不同链规则不同来建立对应EVM解释器

// NewEVMInterpreter returns a new instance of the Interpreter.
func NewEVMInterpreter(evm *EVM, cfg Config) *EVMInterpreter {// If jump table was not initialised we set the default one.if cfg.JumpTable == nil {switch {case evm.chainRules.IsLondon:cfg.JumpTable = &londonInstructionSetcase evm.chainRules.IsBerlin:cfg.JumpTable = &berlinInstructionSetcase evm.chainRules.IsIstanbul:cfg.JumpTable = &istanbulInstructionSetcase evm.chainRules.IsConstantinople:cfg.JumpTable = &constantinopleInstructionSetcase evm.chainRules.IsByzantium:cfg.JumpTable = &byzantiumInstructionSetcase evm.chainRules.IsEIP158:cfg.JumpTable = &spuriousDragonInstructionSetcase evm.chainRules.IsEIP150:cfg.JumpTable = &tangerineWhistleInstructionSetcase evm.chainRules.IsHomestead:cfg.JumpTable = &homesteadInstructionSetdefault:cfg.JumpTable = &frontierInstructionSet}for i, eip := range cfg.ExtraEips {copy := *cfg.JumpTableif err := EnableEIP(eip, &copy); err != nil {// Disable it, so caller can check if it's activated or notcfg.ExtraEips = append(cfg.ExtraEips[:i], cfg.ExtraEips[i+1:]...)log.Error("EIP activation failed", "eip", eip, "error", err)}cfg.JumpTable = &copy}}return &EVMInterpreter{evm: evm,cfg: cfg,}
}

AsMessage

位于core\types\transaction.go
将交易返回为消息格式

// AsMessage returns the transaction as a core.Message.
func (tx *Transaction) AsMessage(s Signer, baseFee *big.Int) (Message, error) {msg := Message{nonce:      tx.Nonce(),gasLimit:   tx.Gas(),gasPrice:   new(big.Int).Set(tx.GasPrice()),gasFeeCap:  new(big.Int).Set(tx.GasFeeCap()),gasTipCap:  new(big.Int).Set(tx.GasTipCap()),to:         tx.To(),amount:     tx.Value(),data:       tx.Data(),accessList: tx.AccessList(),isFake:     false,}// If baseFee provided, set gasPrice to effectiveGasPrice.if baseFee != nil {msg.gasPrice = math.BigMin(msg.gasPrice.Add(msg.gasTipCap, baseFee), msg.gasFeeCap)}var err errormsg.from, err = Sender(s, tx)return msg, err
}

Prepare

位于core\state\statedb.go
在EVM需要生成新的状态时调用此函数来设置当前交易的hash和序号

// Prepare sets the current transaction hash and index which are
// used when the EVM emits new state logs.
func (s *StateDB) Prepare(thash common.Hash, ti int) {s.thash = thashs.txIndex = tis.accessList = newAccessList()
}

Finalize

// Finalize implements consensus.Engine, accumulating the block and uncle rewards,
// setting the final state on the header
func (ethash *Ethash) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) {// Accumulate any block and uncle rewards and commit the final state rootaccumulateRewards(chain.Config(), state, header, uncles)header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
}

位于consensus\ethash\consensus.go
该函数累积区块和叔块的奖励并设置在头部的最终状态中。

这篇关于以太坊go-ethereum源码研读(一)从Process函数相关自定义类型和结构体开始的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/203784

相关文章

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

go 指针接收者和值接收者的区别小结

《go指针接收者和值接收者的区别小结》在Go语言中,值接收者和指针接收者是方法定义中的两种接收者类型,本文主要介绍了go指针接收者和值接收者的区别小结,文中通过示例代码介绍的非常详细,需要的朋友们下... 目录go 指针接收者和值接收者的区别易错点辨析go 指针接收者和值接收者的区别指针接收者和值接收者的

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

MySQL高级查询之JOIN、子查询、窗口函数实际案例

《MySQL高级查询之JOIN、子查询、窗口函数实际案例》:本文主要介绍MySQL高级查询之JOIN、子查询、窗口函数实际案例的相关资料,JOIN用于多表关联查询,子查询用于数据筛选和过滤,窗口函... 目录前言1. JOIN(连接查询)1.1 内连接(INNER JOIN)1.2 左连接(LEFT JOI

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2