简体   繁体   English

Solidity Fallback Function Gas 限制

[英]Solidity Fallback Function Gas Limit

I am a bit confused with the fallback function in Solidity using Remix IDE, as far as I understand, the fallback function is limited to 2300 gas.我对使用 Remix IDE 的 Solidity 中的回退 function 有点困惑,据我所知,回退 function 仅限于 2300 gas。 However, when I call the fallback function by using the 'Transact' button in Remix, it does not fail even though it uses more than 2300 gas.但是,当我使用 Remix 中的“交易”按钮调用回退 function 时,即使它使用了超过 2300 gas,它也不会失败。

// let anyone send ether to the contract
fallback() external payable { 
    require(msg.data.length == 0);
}

Am I misunderstanding how fallback functions work and is my implementation of the fallback secure for when I want users to be able to send Ether to the contract?我是否误解了回退函数的工作原理,以及当我希望用户能够将 Ether 发送到合约时,我的回退实现是否安全? I also check the length of the data to ensure the function isn't used for other purposes.我还检查了数据的长度,以确保 function 没有被用于其他目的。

In the worst case, if a payable fallback function is also used in place of a receive function, it can only rely on 2300 gas being available (see receive Ether function for a brief description of the implications of this).在最坏的情况下,如果 payable fallback function 也被用来代替 receive function,它只能依赖 2300 gas 可用(请参阅 receive Ether function 以获得对此含义的简要描述)。

Source: https://docs.soliditylang.org/en/v0.8.17/contracts.html#fallback-function资料来源: https://docs.soliditylang.org/en/v0.8.17/contracts.html#fallback-function

There is no limit on the fallback function itself.回退 function 本身没有限制。 But if it's invoked from another contract though the transfer() native function, then the limit is applied.但如果它是通过transfer()本机 function 从另一个合约调用的,则应用该限制。

Example: If you invoke the fallback() directly, it succeeds even though it consumes more than 2300 gas.示例:如果您直接调用fallback() ,即使它消耗了超过 2300 gas,它也会成功。 If you invoke it through the transfer() function, it fails with "insufficient gas" error as it's only provided 2300 gas units.如果您通过transfer() function 调用它,它会失败并出现“gas 不足”错误,因为它只提供了 2300 个 gas 单位。 Comment out the number = 1 and it will work from the transfer() as well (because empty fallback uses less than 2300 gas).注释掉number = 1并且它也将在transfer()中起作用(因为空回退使用少于 2300 gas)。

pragma solidity ^0.8;

contract MyContract {
  uint256 number;

  fallback() external payable {
    number = 1;
  }
}

contract Sender {
  address myContract;

  constructor(address _myContract) {
    myContract = _myContract;
  }

  function invokeFallback() external {
    payable(myContract).transfer(0);
  } 
}

fallback function is invoked by the EVM under those 2 conditions EVM 在这 2 个条件下调用fallback function

  • a function that does not exist in contract, gets called, since function does not exist contract is falling back to fallback function合同中不存在的 function 被调用,因为 function 不存在合同正在回退到回fallback function

  • when contract receives ether (if you are on etherum, only native token whcih is ether, any other token does not count) fallback is called.当合约收到ether币时(如果你在以太币上,只有以太币是本机代币,任何其他代币都不算数) fallback被调用。 That is why we have payable modifier.这就是为什么我们有payable修饰符。

EVM provides 2300 gas. EVM 提供 2300 gas。 if fallback gets called by EVM but it costs more than 2300 gas, the function call will throw an exception and any state change will be reverted.如果fallback被 EVM 调用但它花费超过 2300 gas,则 function 调用将抛出异常并且任何 state 更改将被还原。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM