今天在做以太坊开发时,突然想到一个问题,以太坊一个区块包含的交易个数由什么决定?
Today, in the development of Etheria, a question comes to mind: What determines the number of transactions included in an Etheraya block?
如果交易池中有足够的交易,一个区块最多可容纳多少交易?
If there are enough transactions in the pool, how many transactions can a block accommodate?
带着这个问题,我阅读了一下go-ethereum中关于挖矿的源代码,找到了答案。
With this question, I read the source code of the go-etheium for mining, and I found the answer.
先抛出结论:
drops its conclusion:
影响一个区块交易个数的因素有两个:
Two factors influence the number of blocks traded:
- 交易池中的交易个数。这个很好理解,交易池的交易个数直接决定了一个矿工可以打包多少交易到区块中。
- 区块允许的GasLimit。GasLimit又由父块GasLimit、GasFloor和GasCeil共同决定(1.8版本以前只受父块GasLimit影响),当交易的gas总计大于GasLimit时,交易将不在打包的区块中。
其中gasFloor和gasCeil是在geth的中mine的两个配置项。详见文档:https://geth.ethereum.org/docs/interface/command-line-options
Of these, GasFloor and GasCeil are two configurations for centralmine in Geth. For details, see the document:
结论主要从"go-ethereum/miner/worker.go"源文件中得出。worker相当于一个挖矿工人,负责具体的挖矿工作流程。在worker对象中有一个“commitNewWork”方法,是创建一个新的区块,其中计算了该区块的GasLimit和需要处理的交易池中的交易。相关代码如下:
The conclusions are drawn mainly from the source document ".
“commitNewWork”方法中获取到交易池中的交易后将交易提交给了“commitTransactions”方法对交易进行验证。
Once the transaction in the trading pool was captured in the “committeeNewWork” method, the transaction was submitted to the “committeeTransactions” method for validation of the transaction.
将交易提交给“commitTransaction”方法后,回调用
Call back when the transaction is submitted to the "committeeTransation" method
在ApplyTransaction方法中会将gasPool中的gas值减去该笔交易的gas,当gasPool的gas值小于21000时,剩下的交易将不在打包的该区块中。
In the ApplyTransaction method, the gas value in GasPool is subtracted from the gas in the transaction, and when the gasPool value is less than 21,000, the remainder of the transaction will not be in the packaged block.
回头在来看一下区块中的GasLimit是怎么计算的。
Turn around and see what GasLimit calculates in the block.
core.CalcGasLimit(parent, w.config.GasFloor, w.config.GasCeil)方法在“go-ethereum/core/block_validator.go”,代码如下:
CalcGasLimit (parent, w.config. GasFloor, w.config. GasCeil) at ), code as follows:
计算GasLimit的策略很有意思,受父块的GasLimit、挖矿的gas上限gasCeil和gas下限gasFloor三者共同决定。
The strategy for calculating GasLimit is interesting and is determined jointly by the fatherless GasLimit, the mining cap GasCeil and the lower limit GasFloor.
当父块的交易里的gas总和大于父块GasLimit的2/3时,则增加当前块的GasLimit,反之减少当前块的GasLimit,同时,保证GasLimit的范围在gasFloor和gasCeil之间。
When the sum of the gass in the patrilineal transaction is more than two thirds of the patrilineal GasLimit, the current section is added to GasLimit, while the current section is reduced to GasLimit, while the scope of GasLimit is guaranteed to be between GasFloor and GasCeil.
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论