这个编译函数不会超过整数 53?

This compiled function won't mint beyond integer 53?

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

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

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

抱歉,如果有明显的错误或误解——这里很新。


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.

就是因为这个条件。

require(amount < (1 * (10^60)), 'Amount requested too High for minting');

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

您可以将 (1 * (10^60)) 表达式简化为 10^60。请注意,在 Solidity 语法中,这是 而不是 “10 的 60 次方”——它是“10 XOR 60”(结果为 54)。

如果要计算“10的60次方”,语法为10 ** 60

问题已回答。为了完整起见,我想在这里记录逻辑操作:

在按位异或运算中比较两个字节(10 和 60)时(即两个位必须不同才能得到 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