首页 > 解决方案 > Solidity:为什么错误的输入会将我的布尔值设置为“真”?

问题描述

我正在使用 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;
    }

}

如您所见,默认值为myBoolfalse 可以通过调用函数来更改它setMyBoolean

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

但如果我输入任何其他字母组合,myBool也将设置为true。这让我感到惊讶,因为默认设置myBoolfalse

为什么会这样?

标签: booleanethereumsoliditysmartcontractsremix

解决方案


它按预期工作,因为这是混音和 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


推荐阅读