小编Cen*_*ngo的帖子

为什么在这个智能合约上使用断言?

contract Sharer {
    function sendHalf(address payable addr) public payable returns (uint balance) {
        require(msg.value % 2 == 0, "Even value required.");
        uint balanceBeforeTransfer = address(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(address(this).balance == balanceBeforeTransfer - msg.value / 2);
        return address(this).balance;
    }
}
Run Code Online (Sandbox Code Playgroud)

对于上述合约,在什么情况下断言失败/address(this).balance 不会减少 (msg.value / 2)?为什么我们需要在这里断言?

assert solidity evm

4
推荐指数
1
解决办法
518
查看次数

标签 统计

assert ×1

evm ×1

solidity ×1