首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SafeMath .add()施加无限气体

SafeMath .add()施加无限气体
EN

Ethereum用户
提问于 2018-11-14 03:55:57
回答 1查看 773关注 0票数 1

当我从Solidity库中使用SafeMath函数时,在混合中得到无限的气体需求(这反映在无法通过的事务中)。

简化示例:

代码语言:javascript
复制
function someFunction() internal view returns (bool) {
    uint256 a = 23;
    uint256 b = 48;
    return (a.add(1) > b);
}

包括SafeMath。

怎么啦?

下面是包含SafeMath的粘贴:

https://pastebin.com/C4FVyBFa

所用版本:

普拉格玛坚实度^0.4.24;

EN

回答 1

Ethereum用户

发布于 2018-11-14 04:32:47

我的意思是,我们真的需要看到导致这个问题的整个合同,这样我们才能发现一些东西。

这里有一个稍微修改过的函数版本(因此它可以从外部调用),它可以在一个超级基本的契约外壳中正常工作。

代码语言:javascript
复制
// 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。

我们需要看到一些编译但不按预期工作的东西。

希望能帮上忙。

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/62295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档