首页 > 解决方案 > 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)

标签: soliditysmartcontracts

解决方案


有一个已知问题要求查看/纯功能不会在公共网络上恢复: 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 的社区经理


推荐阅读