• solidity错误处理


    官方文档:

    https://solidity.readthedocs.io/en/develop/control-structures.html#error-handling-assert-require-revert-and-exceptions

    require和assert方法的区别:

    https://ethereum.stackexchange.com/questions/15166/difference-between-require-and-assert-and-the-difference-between-revert-and-thro

    程序示例:

    pragma solidity ^0.4.0;
    
    contract Sharer {
        function sendHalf(address addr) public payable returns (uint balance) {
            require(msg.value % 2 == 0); // Only allow even numbers
            uint balanceBeforeTransfer = this.balance;
            addr.transfer(msg.value / 2);
            // Since transfer throws an exception on failure and
            // cannot call back here, there should be no way for us to
            // still have half of the money.
            assert(this.balance == balanceBeforeTransfer - msg.value / 2);
            return this.balance;
        }
    }

    require和assert适用范围:

    An assert-style exception is generated in the following situations:

    1. If you access an array at a too large or negative index (i.e. x[i] where >= x.length or 0).
    2. If you access a fixed-length bytesN at a too large or negative index.
    3. If you divide or modulo by zero (e.g. 0 or 23 0).
    4. If you shift by a negative amount.
    5. If you convert a value too big or negative into an enum type.
    6. If you call a zero-initialized variable of internal function type.
    7. If you call assert with an argument that evaluates to false.

    require-style exception is generated in the following situations:

    1. Calling throw.
    2. Calling require with an argument that evaluates to false.
    3. If you call a function via a message call but it does not finish properly (i.e. it runs out of gas, has no matching function, or throws an exception itself), except when a low level operation callsenddelegatecall or callcode is used. The low level operations never throw exceptions but indicate failures by returning false.
    4. If you create a contract using the new keyword but the contract creation does not finish properly (see above for the definition of “not finish properly”).
    5. If you perform an external function call targeting a contract that contains no code.
    6. If your contract receives Ether via a public function without payable modifier (including the constructor and the fallback function).
    7. If your contract receives Ether via a public getter function.
    8. If a .transfer() fails.

    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. The reason for reverting is that there is no safe way to continue execution, because an expected effect did not occur. Because we want to retain the atomicity of transactions, the safest thing to do is to revert all changes and make the whole transaction (or at least call) without effect. 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.

    总得来说,两者都会使EVM回滚交易,不会造成任何变更。require适用于做输入检查,assert用来检查运行时内部错误(比如数组下标越界和除零错误)。require在Solidity Metropolis版本后将不消耗gas,而assert会消耗完所有的gas(gaslimit)。

    https://medium.com/taipei-ethereum-meetup/%E6%AF%94%E8%BC%83-require-assert-%E5%92%8C-revert-%E5%8F%8A%E5%85%B6%E9%81%8B%E4%BD%9C%E6%96%B9%E5%BC%8F-30c24d534ce4

  • 相关阅读:
    初始nginx(启动运行) 使用nginx做一个简单的静态资源服务器
    redis总结问题
    reids高可用(灾难备份-持久化)
    mybatis中将0识别为null的解决方法
    Spring MVC 通过反射将数据导出到excel
    spring mvc 中Uploadify插件的使用
    java搭建 SpringMVC+Mybatis(SMM)+mybatis-generate
    让ecshop用户名、手机号、email登陆方法
    ecmobile实现支付宝支付和百度云推送遇到的问题及解决方案(android)
    Oracle分页总汇
  • 原文地址:https://www.cnblogs.com/huahuayu/p/8556004.html
Copyright © 2020-2023  润新知