首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这个编译后的函数不会超过整数53吗?

这个编译后的函数不会超过整数53吗?
EN

Stack Overflow用户
提问于 2021-10-09 21:28:15
回答 2查看 71关注 0票数 0

在Remix.Ethereum模块上使用LearnEth IDE。这个练习是关于错误处理的。我已经成功地编译并部署了errorHandling.sol脚本到JSVM。

但是,当我与IDE中的mint函数交互时(输入帐号和不同的整数)--我能够成功地从1-53中“mint”,但不能超过它--它失败并给出了错误消息:

对于我的生活,我不明白为什么其他数字会工作,而不是这个,考虑到限制?如有任何指导,我将不胜感激。

对不起,如果有明显的错误或误解-非常新的这里。

代码语言:javascript
复制
contract Coin {
    address public minter;
    mapping (address => uint) public balances;

    event Sent(address from, address to, uint amount);

    constructor() public {
        minter = msg.sender;
    }
    
    function mint(address receiver, uint amount) public {
        require(minter == receiver, 'Cannot Mint! Minter is not contract Creator!');
        require(amount < (1 * (10^60)), 'Amount requested too High for minting');
        balances[receiver] = amount;
    }
    
    function send(address receiver, uint amount) public {
        require(balances[minter] >= amount, 'Coin balance to low for transaction!');
        balances[minter] = -amount;
        balances[receiver] = amount;
        emit Sent(minter, receiver, amount);
    } 
    
}```

>  [vm] from: 0x5B3...eddC4to: Coin.mint(address,uint256)
> 0xEf9...10eBfvalue: 0 weidata: 0x40c...00036logs: 0hash: 0xc0a...40b8b
> transact to Coin.mint errored: VM error: revert.
> 
> revert    The transaction has been reverted to the initial state. Reason
> provided by the contract: "Amount requested too High for minting".
> Debug the transaction to get more information.
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-10-16 14:15:24

就是因为这种情况。

代码语言:javascript
复制
require(amount < (1 * (10^60)), 'Amount requested too High for minting');

如果amount的值大于或等于54,则抛出异常,有效地恢复事务。

您可以将(1 * (10^60))表达式简化为10^60。请注意,在稳健语法中,这是而不是 "10 to of 60“--它是"10 异或 60”(结果是54)。

如果要计算"10到60的幂“,语法是10 ** 60

票数 0
EN

Stack Overflow用户

发布于 2021-10-20 19:06:31

问题已经回答了。我想在这里记录逻辑操作的完整性:

当比较两个字节(10和60)在一个位级的XOR操作(即这两个位必须是不同的才能得到一个1)时,我得到了54:

255: 1 1 1 1 1 1 1 1 = (128 + 64 + 32 + 16 +8+4+2+ 1) = 255

60: 0 0 1 1 1 1 0 0 = (0 +0+ 32 + 16 +8+4+0+ 0) = 60

10: 0 0 0 0 1 0 1 0 = (0 +0+0+0+8+0+2+ 0) = 10

60 XOR 10: 0 0 1 1 0 1 1 0 = (0 +0+ 32 + 16 +0+4+2+ 0) = 54

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69510681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档