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

Standardize on Custom errors #143

Merged
merged 7 commits into from
Nov 4, 2024
Merged

Conversation

ControlCplusControlV
Copy link
Contributor

@ControlCplusControlV ControlCplusControlV commented Oct 28, 2024

Description

Set a clear standard on how to handle errors for OP Smart Contracts, using only custom errors and reverts.

Tests

No tests needed

Additional context

Carried over from discord, but the idea is to stop bikeshedding around using both require with error strings and custom errors and just standardized on custom errors, which I lay out in the doc. There was some concern around using require in scripts + tests, but after some discussion on discord we agreed assertEq should be able to cover that in most cases

Metadata
optimism issue #12478

@tynes
Copy link
Contributor

tynes commented Oct 29, 2024

There will be many opinions on this, it is important to not bikeshed so not going to die on a hill over this but I do think there is value in common error types like Unauthorized, under this we will get SystemConfigUnauthorized and OptimismPortalUnauthorized which is a different 4 byte selector for the same thing, I suppose that is fine given that the ABIs are uploaded to 4byte directories.

I also prefer to have a _ in between the contract name and the error name, making it a bit easier to read. I do not think that we have any conditional logic based on returndata so there should be no internal backwards compatibility concerns. We have changed the returndata so many times at this point and nobody has complained, so i doubt that people are currently consuming the revert errors. The only time that I have every observed a revert personally was a deposit oog, with no good error message.

@smartcontracts
Copy link
Contributor

I do think there is value in common error types like Unauthorized

One thing to note is that providing the contract name in the error means it's immediately obvious where an error was thrown.

I also prefer to have a _ in between the contract name and the error name, making it a bit easier to read.

Yep totally down with this, easier to read.

@ControlCplusControlV
Copy link
Contributor Author

I will pushback against common errors solely because it is nice to not have to run everything with -vvv in order to determine where an occurred, since a contract name in 90% of cases will make it immediately obvious where failure occurred

@tynes
Copy link
Contributor

tynes commented Oct 29, 2024

Ok your arguments against common errors are good and I am on board with this standard


# Proposed Solution - Revert and Custom Errors

All errors returned from Optimism Smart Contracts should use `revert` with custom errors for both efficiency and safety. In the case of scripts and tests which currently use `require` frequently, they should be instead replaced with `assertEq` or similar `assert*` where appropriate, including a string as a revert message. This will allow for semgrep rules to more easily enforce the no `require` constraint. Custom errors should also include the contract name as a prefix, followed by an underscore, and `Error` as a suffix for clarity and consistency.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we verified there are no forge issues with assertEq when used in setUp() or constructors?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked through open foundry issues and didn't see anything? Are there anything specific concerns you had in mind?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the original issue where assertions didn't work in setUp and I think in constructors too: foundry-rs/foundry#1291

That was a long time ago, but I'd suggest setting up a quick test to verify that the assertEq methods still cause failures when hit in both setUp and constructors, just to be safe

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-ran your POC test

Ran 1 test suite in 124.97ms (414.42µs CPU time): 0 tests passed, 1 failed, 0 skipped (1 total tests)

Failing tests:
Encountered 1 failing test in test/Counter.t.sol:SetUpFailureTest
[FAIL. Reason: setup failed: assertion failed] setUp() (gas: 0)

Test now fails in the setUp process which is as expect and doesn't run the other tests, LGTM?

@ControlCplusControlV
Copy link
Contributor Author

Discussed in meeting #150 , consensus is approved but PR's should be done file by file with prior discussion with the protocol team

@ControlCplusControlV ControlCplusControlV merged commit 91a6d3b into main Nov 4, 2024
@ControlCplusControlV ControlCplusControlV deleted the controlc/custom-errors branch November 4, 2024 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants