Skip to content

Commit 653963b

Browse files
Amxxernestognwarr00
authored
Various changes to code clarity (Fix N-07) (#5317)
Co-authored-by: Ernesto García <[email protected]> Co-authored-by: Arr00 <[email protected]>
1 parent fdf7012 commit 653963b

File tree

5 files changed

+27
-22
lines changed

5 files changed

+27
-22
lines changed

contracts/account/utils/draft-ERC4337Utils.sol

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ library ERC4337Utils {
2424
function parseValidationData(
2525
uint256 validationData
2626
) internal pure returns (address aggregator, uint48 validAfter, uint48 validUntil) {
27-
validAfter = uint48(bytes32(validationData).extract_32_6(0x00));
28-
validUntil = uint48(bytes32(validationData).extract_32_6(0x06));
29-
aggregator = address(bytes32(validationData).extract_32_20(0x0c));
27+
validAfter = uint48(bytes32(validationData).extract_32_6(0));
28+
validUntil = uint48(bytes32(validationData).extract_32_6(6));
29+
aggregator = address(bytes32(validationData).extract_32_20(12));
3030
if (validUntil == 0) validUntil = type(uint48).max;
3131
}
3232

@@ -59,7 +59,8 @@ library ERC4337Utils {
5959
(address aggregator1, uint48 validAfter1, uint48 validUntil1) = parseValidationData(validationData1);
6060
(address aggregator2, uint48 validAfter2, uint48 validUntil2) = parseValidationData(validationData2);
6161

62-
bool success = aggregator1 == address(0) && aggregator2 == address(0);
62+
bool success = aggregator1 == address(uint160(SIG_VALIDATION_SUCCESS)) &&
63+
aggregator2 == address(uint160(SIG_VALIDATION_SUCCESS));
6364
uint48 validAfter = uint48(Math.max(validAfter1, validAfter2));
6465
uint48 validUntil = uint48(Math.min(validUntil1, validUntil2));
6566
return packValidationData(success, validAfter, validUntil);
@@ -110,22 +111,22 @@ library ERC4337Utils {
110111

111112
/// @dev Returns `verificationGasLimit` from the {PackedUserOperation}.
112113
function verificationGasLimit(PackedUserOperation calldata self) internal pure returns (uint256) {
113-
return uint128(self.accountGasLimits.extract_32_16(0x00));
114+
return uint128(self.accountGasLimits.extract_32_16(0));
114115
}
115116

116117
/// @dev Returns `accountGasLimits` from the {PackedUserOperation}.
117118
function callGasLimit(PackedUserOperation calldata self) internal pure returns (uint256) {
118-
return uint128(self.accountGasLimits.extract_32_16(0x10));
119+
return uint128(self.accountGasLimits.extract_32_16(16));
119120
}
120121

121122
/// @dev Returns the first section of `gasFees` from the {PackedUserOperation}.
122123
function maxPriorityFeePerGas(PackedUserOperation calldata self) internal pure returns (uint256) {
123-
return uint128(self.gasFees.extract_32_16(0x00));
124+
return uint128(self.gasFees.extract_32_16(0));
124125
}
125126

126127
/// @dev Returns the second section of `gasFees` from the {PackedUserOperation}.
127128
function maxFeePerGas(PackedUserOperation calldata self) internal pure returns (uint256) {
128-
return uint128(self.gasFees.extract_32_16(0x10));
129+
return uint128(self.gasFees.extract_32_16(16));
129130
}
130131

131132
/// @dev Returns the total gas price for the {PackedUserOperation} (ie. `maxFeePerGas` or `maxPriorityFeePerGas + basefee`).

contracts/governance/extensions/GovernorCountingOverridable.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ abstract contract GovernorCountingOverridable is GovernorVotes {
144144
revert GovernorAlreadyOverridenVote(account);
145145
}
146146

147-
uint256 proposalSnapshot = proposalSnapshot(proposalId);
148-
uint256 overridenWeight = VotesExtended(address(token())).getPastBalanceOf(account, proposalSnapshot);
149-
address delegate = VotesExtended(address(token())).getPastDelegate(account, proposalSnapshot);
147+
uint256 snapshot = proposalSnapshot(proposalId);
148+
uint256 overridenWeight = VotesExtended(address(token())).getPastBalanceOf(account, snapshot);
149+
address delegate = VotesExtended(address(token())).getPastDelegate(account, snapshot);
150150
uint8 delegateCasted = proposalVote.voteReceipt[delegate].casted;
151151

152152
proposalVote.voteReceipt[account].hasOverriden = true;

contracts/governance/utils/VotesExtended.sol

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ abstract contract VotesExtended is Votes {
3434
using Checkpoints for Checkpoints.Trace160;
3535
using Checkpoints for Checkpoints.Trace208;
3636

37-
mapping(address delegator => Checkpoints.Trace160) private _delegateCheckpoints;
38-
mapping(address account => Checkpoints.Trace208) private _balanceOfCheckpoints;
37+
mapping(address delegator => Checkpoints.Trace160) private _userDelegationCheckpoints;
38+
mapping(address account => Checkpoints.Trace208) private _userVotingUnitsCheckpoints;
3939

4040
/**
4141
* @dev Returns the delegate of an `account` at a specific moment in the past. If the `clock()` is
@@ -46,7 +46,7 @@ abstract contract VotesExtended is Votes {
4646
* - `timepoint` must be in the past. If operating using block numbers, the block must be already mined.
4747
*/
4848
function getPastDelegate(address account, uint256 timepoint) public view virtual returns (address) {
49-
return address(_delegateCheckpoints[account].upperLookupRecent(_validateTimepoint(timepoint)));
49+
return address(_userDelegationCheckpoints[account].upperLookupRecent(_validateTimepoint(timepoint)));
5050
}
5151

5252
/**
@@ -58,25 +58,25 @@ abstract contract VotesExtended is Votes {
5858
* - `timepoint` must be in the past. If operating using block numbers, the block must be already mined.
5959
*/
6060
function getPastBalanceOf(address account, uint256 timepoint) public view virtual returns (uint256) {
61-
return _balanceOfCheckpoints[account].upperLookupRecent(_validateTimepoint(timepoint));
61+
return _userVotingUnitsCheckpoints[account].upperLookupRecent(_validateTimepoint(timepoint));
6262
}
6363

6464
/// @inheritdoc Votes
6565
function _delegate(address account, address delegatee) internal virtual override {
6666
super._delegate(account, delegatee);
6767

68-
_delegateCheckpoints[account].push(clock(), uint160(delegatee));
68+
_userDelegationCheckpoints[account].push(clock(), uint160(delegatee));
6969
}
7070

7171
/// @inheritdoc Votes
7272
function _transferVotingUnits(address from, address to, uint256 amount) internal virtual override {
7373
super._transferVotingUnits(from, to, amount);
7474
if (from != to) {
7575
if (from != address(0)) {
76-
_balanceOfCheckpoints[from].push(clock(), SafeCast.toUint208(_getVotingUnits(from)));
76+
_userVotingUnitsCheckpoints[from].push(clock(), SafeCast.toUint208(_getVotingUnits(from)));
7777
}
7878
if (to != address(0)) {
79-
_balanceOfCheckpoints[to].push(clock(), SafeCast.toUint208(_getVotingUnits(to)));
79+
_userVotingUnitsCheckpoints[to].push(clock(), SafeCast.toUint208(_getVotingUnits(to)));
8080
}
8181
}
8282
}

contracts/proxy/Clones.sol

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ library Clones {
227227
* function should only be used to check addresses that are known to be clones.
228228
*/
229229
function fetchCloneArgs(address instance) internal view returns (bytes memory) {
230-
bytes memory result = new bytes(instance.code.length - 0x2d); // revert if length is too short
230+
bytes memory result = new bytes(instance.code.length - 45); // revert if length is too short
231231
assembly ("memory-safe") {
232-
extcodecopy(instance, add(result, 0x20), 0x2d, mload(result))
232+
extcodecopy(instance, add(result, 32), 45, mload(result))
233233
}
234234
return result;
235235
}
@@ -248,11 +248,11 @@ library Clones {
248248
address implementation,
249249
bytes memory args
250250
) private pure returns (bytes memory) {
251-
if (args.length > 0x5fd3) revert CloneArgumentsTooLong();
251+
if (args.length > 24531) revert CloneArgumentsTooLong();
252252
return
253253
abi.encodePacked(
254254
hex"61",
255-
uint16(args.length + 0x2d),
255+
uint16(args.length + 45),
256256
hex"3d81600a3d39f3363d3d373d3d3d363d73",
257257
implementation,
258258
hex"5af43d82803e903d91602b57fd5bf3",

contracts/utils/NoncesKeyed.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import {Nonces} from "./Nonces.sol";
77
* @dev Alternative to {Nonces}, that supports key-ed nonces.
88
*
99
* Follows the https://eips.ethereum.org/EIPS/eip-4337#semi-abstracted-nonce-support[ERC-4337's semi-abstracted nonce system].
10+
*
11+
* NOTE: This contract inherits from {Nonces} and reuses its storage for the first nonce key (i.e. `0`). This
12+
* makes upgrading from {Nonces} to {NoncesKeyed} safe when using their upgradeable versions (e.g. `NoncesKeyedUpgradeable`).
13+
* Doing so will NOT reset the current state of nonces, avoiding replay attacks where a nonce is reused after the upgrade.
1014
*/
1115
abstract contract NoncesKeyed is Nonces {
1216
mapping(address owner => mapping(uint192 key => uint64)) private _nonces;

0 commit comments

Comments
 (0)