solidity - Require() 函数在下面的代码中无法正常工作
问题描述
在 Rinkeyby 测试网络的 InjectedWeb3 环境中的 Remix IDE 上部署此合约。
我尝试删除 require 中的 error msg 语句,然后它不会抛出错误但仍然无法正常工作,即无论任何 require 条件如何,该函数都会被执行。
pragma solidity >=0.4.22 <0.7.0;
contract RegisterLand{
struct land{
uint area;
string location;
uint floorsAllowed;
mapping(uint => address) owner;
uint count;
bool idExists;
}
mapping(uint => land) lands;
function Register(uint id,uint area, string memory location, uint
floorsAllowed) public
{
require(
!lands[id].idExists,
"ID already exists"
);
lands[id] = land(area, location, floorsAllowed,0,true);
lands[id].owner[lands[id].count] = msg.sender;
}
function ViewLand(uint id) public view returns(address currentOwner,
uint
landArea, string memory landLocation, uint landFloors )
{
require(lands[id].idExists,
"Id doesn't exist.");
currentOwner = lands[id].owner[lands[id].count];
landArea = lands[id].area;
landLocation = lands[id].location;
landFloors = lands[id].floorsAllowed;
}
}
错误:
解码输出失败:错误:溢出(操作=“setValue”,故障=“溢出”,详细信息=“数字最多只能安全存储53位”,版本=4.0.32)
解决方案
有一个已知问题要求查看/纯功能不会在公共网络上恢复: https ://forum.openzeppelin.com/t/require-in-view-pure-functions-dont-revert-on-public-网络/1211
如果您使用 Remix JavaScript VM,则ViewLand
使用不存在的调用id
会按预期恢复。
如果您对 dapp 开发有任何疑问,也可以在 OpenZeppelin 社区论坛中提问:https ://forum.openzeppelin.com/
披露:我是 OpenZeppelin 的社区经理
推荐阅读
- c# - ITextSharp PdfCleanUpProcessor 类清理方法已删除整行
- python - GeoJson 和传单
- simulation - AnyLogic_Parking_Simulation
- python - 如何模拟由'__init__'的方法调用的方法
- java - 在接口 VS 中定义的匿名类。接口外的具体类
- python - 如何在列中仅放置部分字符串值?
- excel - Excel VBA,比较两个不同列中的值并保留整行以获得共同值
- javascript - 尝试使用 getElementById 更改 innerHTML 时,ng-repeat 不起作用
- arduino - 错误:没有匹配函数调用 'UdpContext::listen(const ip_addr_t&, int&)' if(!_udp_ota->listen(*IP_ADDR_ANY, _port))
- angular - 创建新 Angular 项目后的多个 karma.conf.js 文件