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

Various changes to code clarity (Fix N-07) #5317

Merged
merged 11 commits into from
Nov 27, 2024
14 changes: 7 additions & 7 deletions contracts/account/utils/draft-ERC4337Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ library ERC4337Utils {
function parseValidationData(
uint256 validationData
) internal pure returns (address aggregator, uint48 validAfter, uint48 validUntil) {
validAfter = uint48(bytes32(validationData).extract_32_6(0x00));
validUntil = uint48(bytes32(validationData).extract_32_6(0x06));
aggregator = address(bytes32(validationData).extract_32_20(0x0c));
validAfter = uint48(bytes32(validationData).extract_32_6(0));
validUntil = uint48(bytes32(validationData).extract_32_6(6));
aggregator = address(bytes32(validationData).extract_32_20(12));
if (validUntil == 0) validUntil = type(uint48).max;
}

Expand Down Expand Up @@ -110,22 +110,22 @@ library ERC4337Utils {

/// @dev Returns `verificationGasLimit` from the {PackedUserOperation}.
function verificationGasLimit(PackedUserOperation calldata self) internal pure returns (uint256) {
return uint128(self.accountGasLimits.extract_32_16(0x00));
return uint128(self.accountGasLimits.extract_32_16(0));
}

/// @dev Returns `accountGasLimits` from the {PackedUserOperation}.
function callGasLimit(PackedUserOperation calldata self) internal pure returns (uint256) {
return uint128(self.accountGasLimits.extract_32_16(0x10));
return uint128(self.accountGasLimits.extract_32_16(16));
}

/// @dev Returns the first section of `gasFees` from the {PackedUserOperation}.
function maxPriorityFeePerGas(PackedUserOperation calldata self) internal pure returns (uint256) {
return uint128(self.gasFees.extract_32_16(0x00));
return uint128(self.gasFees.extract_32_16(0));
}

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

/// @dev Returns the total gas price for the {PackedUserOperation} (ie. `maxFeePerGas` or `maxPriorityFeePerGas + basefee`).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ abstract contract GovernorCountingOverridable is GovernorVotes {
revert GovernorAlreadyOverridenVote(account);
}

uint256 proposalSnapshot = proposalSnapshot(proposalId);
uint256 overridenWeight = VotesExtended(address(token())).getPastBalanceOf(account, proposalSnapshot);
address delegate = VotesExtended(address(token())).getPastDelegate(account, proposalSnapshot);
uint256 snapshot = proposalSnapshot(proposalId);
uint256 overridenWeight = VotesExtended(address(token())).getPastBalanceOf(account, snapshot);
address delegate = VotesExtended(address(token())).getPastDelegate(account, snapshot);
uint8 delegateCasted = proposalVote.voteReceipt[delegate].casted;

proposalVote.voteReceipt[account].hasOverriden = true;
Expand Down
14 changes: 7 additions & 7 deletions contracts/governance/utils/VotesExtended.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ abstract contract VotesExtended is Votes {
using Checkpoints for Checkpoints.Trace160;
using Checkpoints for Checkpoints.Trace208;

mapping(address delegator => Checkpoints.Trace160) private _delegateCheckpoints;
mapping(address account => Checkpoints.Trace208) private _balanceOfCheckpoints;
mapping(address delegator => Checkpoints.Trace160) private _userDelegationCheckpoints;
mapping(address account => Checkpoints.Trace208) private _userVotingWeightCheckpoints;
arr00 marked this conversation as resolved.
Show resolved Hide resolved
arr00 marked this conversation as resolved.
Show resolved Hide resolved

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

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

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

_delegateCheckpoints[account].push(clock(), uint160(delegatee));
_userDelegationCheckpoints[account].push(clock(), uint160(delegatee));
}

/// @inheritdoc Votes
function _transferVotingUnits(address from, address to, uint256 amount) internal virtual override {
super._transferVotingUnits(from, to, amount);
if (from != to) {
if (from != address(0)) {
_balanceOfCheckpoints[from].push(clock(), SafeCast.toUint208(_getVotingUnits(from)));
_userVotingWeightCheckpoints[from].push(clock(), SafeCast.toUint208(_getVotingUnits(from)));
}
if (to != address(0)) {
_balanceOfCheckpoints[to].push(clock(), SafeCast.toUint208(_getVotingUnits(to)));
_userVotingWeightCheckpoints[to].push(clock(), SafeCast.toUint208(_getVotingUnits(to)));
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions contracts/proxy/Clones.sol
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ library Clones {
* function should only be used to check addresses that are known to be clones.
*/
function fetchCloneArgs(address instance) internal view returns (bytes memory) {
bytes memory result = new bytes(instance.code.length - 0x2d); // revert if length is too short
bytes memory result = new bytes(instance.code.length - 45); // revert if length is too short
assembly ("memory-safe") {
extcodecopy(instance, add(result, 0x20), 0x2d, mload(result))
extcodecopy(instance, add(result, 0x20), 45, mload(result))
}
return result;
}
Expand All @@ -248,11 +248,11 @@ library Clones {
address implementation,
bytes memory args
) private pure returns (bytes memory) {
if (args.length > 0x5fd3) revert CloneArgumentsTooLong();
if (args.length > 24531) revert CloneArgumentsTooLong();
return
abi.encodePacked(
hex"61",
uint16(args.length + 0x2d),
uint16(args.length + 45),
hex"3d81600a3d39f3363d3d373d3d3d363d73",
implementation,
hex"5af43d82803e903d91602b57fd5bf3",
Expand Down
4 changes: 4 additions & 0 deletions contracts/utils/NoncesKeyed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import {Nonces} from "./Nonces.sol";
* @dev Alternative to {Nonces}, that supports key-ed nonces.
*
* Follows the https://eips.ethereum.org/EIPS/eip-4337#semi-abstracted-nonce-support[ERC-4337's semi-abstracted nonce system].
*
* NOTE: This contract inherits from {Nonces} and reuses its storage for the first nonce key (i.e. `0`). This
* makes upgrading from {Nonces} to {NoncesKeyed} safe. Doing so will NOT reset the current state of nonces to
* avoid replay attacks where a nonce is reused after the upgrade.
arr00 marked this conversation as resolved.
Show resolved Hide resolved
*/
abstract contract NoncesKeyed is Nonces {
mapping(address owner => mapping(uint192 key => uint64)) private _nonces;
Expand Down