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

Mint re-entrancy guard with optimizations. #131

Merged
merged 6 commits into from
Feb 26, 2022

Conversation

Vectorized
Copy link
Collaborator

@Vectorized Vectorized commented Feb 23, 2022

Ok, so as we know, the _safeMint function is NOT safe from re-entrancy. #92

Ideally, most contracts should use _mint.

Although we can warn about it in the comments / docs, many devs won't read it and will happily use _safeMint as it is.
For security reasons, I won't cite any examples here.

Furthermore, the README example shows _safeMint being used in an unprotected manner.

From a usability point of view, I suggest that we make the _mint function re-entrancy safe regardless, to be foolproof.

This will also reduce the cognitive dissonance of _safeMint being unsafe, giving a cleaner API design.

The overhead is only 100 gas for an extra warm SLOAD.
Assuming a function using _safeMint costs ~60K gas, this is only 0.2% overhead that can protect against catastrophic consequences. For users who use _mint, the gas overhead is as good as none.

To ease the concerns for the increase in gas for _safeMint, the function is optimized even further, such that we can save more gas to cover for the overhead.

In practice, implementations that need to use _safeMint for some reason can save 2k gas (cold SLOAD) with this functionality, as they don't need to use OpenZepplin's Reentrancy guard.

Before:

·------------------------------------------|---------------------------|-------------|
|           Solc version: 0.8.11           ·  Optimizer enabled: true  ·  Runs: 800  ·
···········································|···························|·············|
|  Methods                                                                            
···························|···············|·············|·············|·············|
|  Contract                ·  Method       ·  Min        ·  Max        ·  Avg        ·
···························|···············|·············|·············|·············|
|  ERC721AGasReporterMock  ·  mintOne      ·      56502  ·      90702  ·      57186  ·
···························|···············|·············|·············|·············|
|  ERC721AGasReporterMock  ·  mintTen      ·      74696  ·     108896  ·      75380  ·
···························|···············|·············|·············|·············|
|  ERC721AGasReporterMock  ·  safeMintOne  ·      59256  ·      93456  ·      59940  ·
···························|···············|·············|·············|·············|
|  ERC721AGasReporterMock  ·  safeMintTen  ·      79246  ·     113446  ·      79930  ·
···························|···············|·············|·············|·············|
|  Deployments                             ·                                         ·
···········································|·············|·············|·············|
|  ERC721AGasReporterMock                  ·          -  ·          -  ·    1146497  ·
···········································|·············|·············|·············|

After re-entrancy guard + gas optimizations:

·------------------------------------------|---------------------------|-------------|
|           Solc version: 0.8.11           ·  Optimizer enabled: true  ·  Runs: 800  ·
···········································|···························|·············|
|  Methods                                                                            
···························|···············|·············|·············|·············|
|  Contract                ·  Method       ·  Min        ·  Max        ·  Avg        ·
···························|···············|·············|·············|·············|
|  ERC721AGasReporterMock  ·  mintOne      ·      56462  ·      90662  ·      57146  ·
···························|···············|·············|·············|·············|
|  ERC721AGasReporterMock  ·  mintTen      ·      74116  ·     108316  ·      74800  ·
···························|···············|·············|·············|·············|
|  ERC721AGasReporterMock  ·  safeMintOne  ·      59143  ·      93343  ·      59827  ·
···························|···············|·············|·············|·············|
|  ERC721AGasReporterMock  ·  safeMintTen  ·      76775  ·     110975  ·      77459  ·
···························|···············|·············|·············|·············|
|  Deployments                             ·                                         ·
···········································|·············|·············|·············|
|  ERC721AGasReporterMock                  ·          -  ·          -  ·    1168285  ·
·------------------------------------------|-------------|-------------|-------------|

@cygaar @ahbanavi

Copy link
Collaborator

@cygaar cygaar left a comment

Choose a reason for hiding this comment

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

I like it, let's land this after the start index PR like you said

@liarco
Copy link

liarco commented Feb 25, 2022

Hi, thank you all for the great job with this project.

I just tried to get in touch with @Vectorized from other places but I couldn't, so I hope not to bother you asking this question here.

About what you said here:

Ideally, most contracts should use _mint.

Although we can warn about it in the comments / docs, many devs won't read it and will happily use _safeMint as it is.

Do you mean that using _safeMint() instead of _mint() is a bad practice in general, or do you refer to just using it without considering potential re-entrancy attacks?

Would you mind explaining why you would suggest using _mint() in most situations?

Thank you for your time.

@Vectorized
Copy link
Collaborator Author

@liarco Messaged you on twitter. I forgot to turn on direct messages. You can contact me there from now.

https://ethereum.stackexchange.com/questions/115280/mint-vs-safemint-which-is-best-for-erc721 <- by the lead author of ERC721.

@cygaar cygaar merged commit e03a377 into chiru-labs:main Feb 26, 2022
@Vectorized Vectorized deleted the feature/mintGuard branch April 12, 2022 13:52
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

Successfully merging this pull request may close these issues.

None yet

3 participants