当我从Solidity库中使用SafeMath函数时,在混合中得到无限的气体需求(这反映在无法通过的事务中)。
简化示例:
function someFunction() internal view returns (bool) {
uint256 a = 23;
uint256 b = 48;
return (a.add(1) > b);
}包括SafeMath。
怎么啦?
下面是包含SafeMath的粘贴:
所用版本:
普拉格玛坚实度^0.4.24;
发布于 2018-11-14 04:32:47
我的意思是,我们真的需要看到导致这个问题的整个合同,这样我们才能发现一些东西。
这里有一个稍微修改过的函数版本(因此它可以从外部调用),它可以在一个超级基本的契约外壳中正常工作。
// File: zeppelin-solidity/contracts/math/SafeMath.sol
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
/**
* @dev Multiplies two numbers, throws on overflow.
*/
function mul(uint256 _a, uint256 _b) internal pure returns (uint256 c) {
// Gas optimization: this is cheaper than asserting 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (_a == 0) {
return 0;
}
c = _a * _b;
assert(c / _a == _b);
return c;
}
/**
* @dev Integer division of two numbers, truncating the quotient.
*/
function div(uint256 _a, uint256 _b) internal pure returns (uint256) {
// assert(_b > 0); // Solidity automatically throws when dividing by 0
// uint256 c = _a / _b;
// assert(_a == _b * c + _a % _b); // There is no case in which this doesn't hold
return _a / _b;
}
/**
* @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {
assert(_b <= _a);
return _a - _b;
}
/**
* @dev Adds two numbers, throws on overflow.
*/
function add(uint256 _a, uint256 _b) internal pure returns (uint256 c) {
c = _a + _b;
assert(c >= _a);
return c;
}
}
contract Try {
using SafeMath for uint256;
function someFunction() public view returns (bool) {
uint256 a = 23;
uint256 b = 48;
return (a.add(1) > b);
}
}如预期的那样,它返回false。

我们需要看到一些编译但不按预期工作的东西。
希望能帮上忙。
https://ethereum.stackexchange.com/questions/62295
复制相似问题