【问题标题】:Solidity: Why does a faulty input set my boolean to "true"?Solidity:为什么错误的输入会将我的布尔值设置为“真”?
【发布时间】:2020-05-27 15:40:31
【问题描述】:

我正在使用 remix.ethereum.org

我写了这个非常简单的智能合约:

pragma solidity ^0.4.19;

contract TicTacToe {
    bool myBool = false;

    uint8 myUint8;
    uint256 myUint256;

    string myString = "myString";
    bytes myBytes = "myString";

    function setMyBoolean(bool myBoolArgument) public {
        myBool = myBoolArgument;
    }

    function getMyBoolean() public view returns(bool) {
      return myBool;
    }

}

如您所见,myBool 的默认值为 false 我可以通过调用函数setMyBoolean 来改变它。

如果我使用这个参数并输入 truemyBool 将被设置为 true 如果我使用这个参数并输入 falsemyBool 将被设置为 false

但如果我输入任何其他字母组合,myBool 也将设置为 true。这让我很吃惊,因为myBool 的默认设置是false

为什么会这样?

【问题讨论】:

    标签: boolean ethereum solidity smartcontracts remix


    【解决方案1】:

    我假设输入的值被评估为字符串,并且由于字符串存在并且它不为空,因此它被评估为真。

    【讨论】:

      【解决方案2】:

      它按预期工作,因为这是 remix 和 abi 编码器决定处理布尔值的方式

        // "false" will be converting to `false` and "true" will be working
        // fine as abiCoder assume anything in quotes as `true`
        if (type === 'bool' && args[i] === 'false') {
           args[i] = false
        }
      

      https://github.com/ethereum/remix/blob/807ffd9772b07dafb343c08faf44c78ee456de77/remix-lib/src/execution/txHelper.js#L18

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-07
        • 2022-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-01
        相关资源
        最近更新 更多