Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SafeMath - failed assert reason. #1506

Closed
robertmagier opened this issue Nov 20, 2018 · 4 comments
Closed

SafeMath - failed assert reason. #1506

robertmagier opened this issue Nov 20, 2018 · 4 comments

Comments

@robertmagier
Copy link

I would like to propose changing SafeMath library to use require instead of assert and also describe the reason why require failed. It will make testing in truffle much easier.

For example current sub function is using assert.

    function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {
        assert(_b <= _a);
        return _a - _b;
    }

We can change it to require and provide error description which will be then displayed truffle test.

    function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {
        require(_b <= _a,"SafeMath library subtraction error");
        return _a - _b;
    }
@maxsam4
Copy link

maxsam4 commented Nov 20, 2018

A) internal functions of libraries are inlined in the implementing smart contract.
B) Strings cannot be compressed and hence take a good chunk of allowed bytecode size for deployment.

As a result, Adding error strings to SafeMath will considerably increase the size of contracts implementing it. I would avoid making such basic functions so heavy.

I won't mind a separate SafeMathWithReasonStrings library though. Just don't add reason strings to the default one.

@robertmagier
Copy link
Author

robertmagier commented Nov 20, 2018 via email

@maxsam4
Copy link

maxsam4 commented Nov 20, 2018

I think libraries address is inlined in the contract using it.

Library address is used for public functions.
internal functions are directly inlined.

As address cannot be compressed either, making SafeMath functions public will increase deployment cost as well as (more importantly) execution cost as everytime the contract will have to make an external call.

As I said earlier, I won't mind a separate SafeMathWithReasonStrings library. Just don't add reason strings to the default one.

@nventuro
Copy link
Contributor

@maxsam4 is right in that SafeMath is intended to be used as an internal library, and will be inlined in your source code (it'd be way too expensive to use otherwise). We are considering adding revert reasons, but we're not yet sure on how they should look like (there was some discussion in #888), and lack tooling to make sure they've been correctly implemented.

Regarding require, that's already part of OpenZeppelin! It was added in #1187.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants