boolean - 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;
}
}
如您所见,默认值为myBool
false我
可以通过调用函数来更改它setMyBoolean
。
如果我使用此参数并输入true,myBool
将设置为true
如果我使用此参数并输入false,myBool
将设置为false
但如果我输入任何其他字母组合,myBool
也将设置为true。这让我感到惊讶,因为默认设置myBool
为false。
为什么会这样?
解决方案
它按预期工作,因为这是混音和 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
}
推荐阅读
- sql - 从 postgresql 中的表中获取最大、最小时间戳值
- shell - 如何在 Sublime Text 3 构建系统中执行子 shell 命令?
- python - NoConvergence:WCS 全世界都可以像素化
- swift - 了解为 UICollectionViewCompositionalLayout 创建 NSCollectionLayoutGroup 的不同方法
- python - 用另一个字符替换尾随字符
- javascript - element#createTextRange() 方法是什么?
- typescript - 如何使用 AngularFire 查询 Firebase 时间戳字段?
- android - 联系内容提供者观察者在 android 中无法稳定工作
- django - Django - 使用 pre_save() 钩子拦截模型更改
- aws-lambda - 无服务器框架 - 参考层无版本,或始终为最新版本