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

Explain order of operations in _useUnorderedNonce #217

Open
PaulRBerg opened this issue May 23, 2023 · 0 comments
Open

Explain order of operations in _useUnorderedNonce #217

PaulRBerg opened this issue May 23, 2023 · 0 comments

Comments

@PaulRBerg
Copy link
Contributor

PaulRBerg commented May 23, 2023

In SignatureTransfer._userUnorderedNonce, there is this line:

uint256 flipped = nonceBitmap[from][wordPos] ^= bit;

Based on the discussion in #120, I understand that the double assignment was implemented for gas efficiency purposes (that is, for the happy execution paths). However, this is an uncommon design choice, so it would be helpful to explain the order of operations, i.e. that the storage assignment is performed first, and then the resulting value is assigned to the flipped variable (generally speaking, it's not obvious that y = z is performed first in x = y = z).

Also, the NatSpec comments mention the check before the storage update, although in practice the reverse order of operations applies:

/// @notice Checks whether a nonce is taken and sets the bit at the bit position in the bitmap at the word position

Side note: this design deviates from the Checks-Effects-Interactions pattern, which is not a bad thing in and of itself, but this is worth mentioning FYI.

This was referenced May 23, 2023
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

1 participant