Require() 函数在下面的代码中不能正常工作
Require() function is not working properly in below code
在 Rinkeyby 测试网络上的 InjectedWeb3 环境中的 Remix IDE 上部署了此合约。
我尝试删除 require 中的错误消息语句,然后它没有抛出错误,但仍然无法正常工作,即无论任何 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;
}
}
错误:
Failed to decode output: Error: overflow (operation="setValue",
fault="overflow", details="Number can only safely store up to 53
bits", version=4.0.32)
存在一个已知问题,要求 view/pure 函数不能在 public 网络上还原:
https://forum.openzeppelin.com/t/require-in-view-pure-functions-dont-revert-on-public-networks/1211
如果您使用 Remix JavaScript 虚拟机,则使用不存在的 id
调用 ViewLand
会按预期恢复。
如果你有关于 dapp 开发的问题,你也可以在 OpenZeppelin 社区论坛中提问:https://forum.openzeppelin.com/
披露:我是 OpenZeppelin 的社区经理
在 Rinkeyby 测试网络上的 InjectedWeb3 环境中的 Remix IDE 上部署了此合约。
我尝试删除 require 中的错误消息语句,然后它没有抛出错误,但仍然无法正常工作,即无论任何 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;
}
}
错误:
Failed to decode output: Error: overflow (operation="setValue", fault="overflow", details="Number can only safely store up to 53 bits", version=4.0.32)
存在一个已知问题,要求 view/pure 函数不能在 public 网络上还原: https://forum.openzeppelin.com/t/require-in-view-pure-functions-dont-revert-on-public-networks/1211
如果您使用 Remix JavaScript 虚拟机,则使用不存在的 id
调用 ViewLand
会按预期恢复。
如果你有关于 dapp 开发的问题,你也可以在 OpenZeppelin 社区论坛中提问:https://forum.openzeppelin.com/
披露:我是 OpenZeppelin 的社区经理