gpt4 book ai didi

Ethereum Solidity - require() 是否使用任何气体?

转载 作者:行者123 更新时间:2023-12-03 23:17:20 25 4
gpt4 key购买 nike

谷歌没有给我一个具体的答案,使用 require()函数在 Solidity用完任何气体?即使函数中的语句被评估为真?

最佳答案

我不太确定您是在问 OPCODE 本身是否消耗了 gas,或者如果语句的计算结果为 true,则是否消耗了 gas。

如果您问的是 OPCODE 本身,我同意您的看法,即答案尚不清楚。我没有看到 REVERT操作码(这是 require() 被编译成的)(现在非常不推荐使用)Google OPCODE gas usage spreadsheet或在 yellowpaper (附录 G)。

在 Remix 中运行测试,看起来它确实消耗了很少的 gas。只需添加一个 require(true)调用此方法的顶部会增加 23 的气体使用量。

contract GasUsage {
uint val;

function someFunc() public returns (bool) {
require(true);

delete val;
}
}

包含时的执行成本:5230

注释掉时的执行成本:5207

如果您在 require 之前询问天然气消耗量声明,那么答案是肯定的。截至 Byzantium释放,直到 require 为止消耗的所有气体语句被消耗,但返回任何剩余的气体。在拜占庭之前, require()assert()是相同的,所有的气体都会被消耗掉。

来自 Solidity docs :

Internally, Solidity performs a revert operation (instruction 0xfd) for a require-style exception and executes an invalid operation (instruction 0xfe) to throw an assert-style exception. In both cases, this causes the EVM to revert all changes made to the state...Note that assert-style exceptions consume all gas available to the call, while require-style exceptions will not consume any gas starting from the Metropolis release.

关于Ethereum Solidity - require() 是否使用任何气体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48077773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com