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

  • 本文关键字:整数 编译 函数 solidity
  • 更新时间 :
  • 英文 :


在LearnEath模块上使用Remix.以太坊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异或60〃;(结果为54)。

如果要计算";10的60〃的幂;,语法为CCD_ 4。

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

当在逐位XOR运算中比较两个字节(10和60)时(即,两个比特必须不同才能得到1),我得到54:

255:1 1 1 1 1 1 1 1=(128+64+32+16+8+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+2+0)=10


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

最新更新