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

Reducing Safe Contract Code Size #726

Merged
merged 13 commits into from Jan 12, 2024
Merged

Reducing Safe Contract Code Size #726

merged 13 commits into from Jan 12, 2024

Conversation

remedcu
Copy link
Member

@remedcu remedcu commented Jan 8, 2024

TODO:

  • Using uint256 for v. (Saved around 9 bytes)
  • Using storage slot directly where possible. (Saved around 30+ bytes)
  • Using if with revert instead of require where possible. (Saved around 20 bytes)
    • Using assembly to revert in Safe.sol and inherited files. (Saved around 1900 bytes 2700 bytes)

Closes #713

@remedcu remedcu self-assigned this Jan 8, 2024
@coveralls
Copy link

coveralls commented Jan 8, 2024

Pull Request Test Coverage Report for Build 7503450091

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.2%) to 94.397%

Totals Coverage Status
Change from base Build 7503409586: -0.2%
Covered Lines: 396
Relevant Lines: 404

💛 - Coveralls

@@ -292,7 +292,7 @@ contract Safe is
// Load threshold to avoid multiple storage loads
uint256 _threshold = threshold;
// Check that a threshold is set
require(_threshold > 0, "GS001");
if (_threshold == 0) revertWithError("GS001");
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is such a surprise to me that changing require to if affects the code size...

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see now, its because of how the require was encoding the revert error.

} else if (v > 30) {
// If v > 30 then default va (27,28) has been adjusted for eth_sign flow
// To support eth_sign and similar we adjust v and hash the messageHash with the Ethereum message prefix before applying ecrecover
currentOwner = ecrecover(keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", dataHash)), v - 4, r, s);
currentOwner = ecrecover(keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", dataHash)), uint8(v - 4), r, s);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice! I've also noticed that keeping uintN for N < 256 tends to generate more code.

Copy link
Member Author

Choose a reason for hiding this comment

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

Learned from the best: #707 (comment)

* @author Shebin John - @remedcu
* @notice The aim is to save gas using assembly to revert with custom error message.
*/
abstract contract ErrorMessage {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Personally, I'm fine with this assembly, it is fairly simple, and removing all the additional string encoding saves non-trivial amount of space.

We should probably have a discussion with @rmeissner about what our appetite for assembly in the contracts is.

Copy link
Member

Choose a reason for hiding this comment

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

I think the benefits of the approach outweigh the cognitive overhead of assembly code

@remedcu remedcu marked this pull request as ready for review January 9, 2024 15:18
contracts/Safe.sol Outdated Show resolved Hide resolved
Copy link
Collaborator

@nlordell nlordell left a comment

Choose a reason for hiding this comment

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

LGTM! Sorry for the confusion around the uint8 stuff.

@remedcu remedcu merged commit ac0eda0 into main Jan 12, 2024
20 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jan 12, 2024
@mmv08 mmv08 deleted the contract-size branch January 12, 2024 14:51
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Evaluate strategies for reducing Safe code size
4 participants