Skip to content

Commit 02722fc

Browse files
z0r0zfrangioAmxx
authored
Use unchecked in ERC20Votes and fix typo (#3748)
Co-authored-by: Francisco Giordano <frangio.1@gmail.com> Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
1 parent eb03304 commit 02722fc

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
* `ReentrancyGuard`: Add a `_reentrancyGuardEntered` function to expose the guard status. ([#3714](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3714))
6+
* `ERC20Votes`: optimize by using unchecked arithmetic. ([#3748](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3748))
67

78
## Unreleased
89

contracts/token/ERC20/extensions/ERC20Votes.sol

+18-10
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ abstract contract ERC20Votes is IVotes, ERC20Permit {
6363
*/
6464
function getVotes(address account) public view virtual override returns (uint256) {
6565
uint256 pos = _checkpoints[account].length;
66-
return pos == 0 ? 0 : _checkpoints[account][pos - 1].votes;
66+
unchecked {
67+
return pos == 0 ? 0 : _checkpoints[account][pos - 1].votes;
68+
}
6769
}
6870

6971
/**
@@ -80,7 +82,7 @@ abstract contract ERC20Votes is IVotes, ERC20Permit {
8082

8183
/**
8284
* @dev Retrieve the `totalSupply` at the end of `blockNumber`. Note, this value is the sum of all balances.
83-
* It is but NOT the sum of all the delegated votes!
85+
* It is NOT the sum of all the delegated votes!
8486
*
8587
* Requirements:
8688
*
@@ -130,7 +132,9 @@ abstract contract ERC20Votes is IVotes, ERC20Permit {
130132
}
131133
}
132134

133-
return high == 0 ? 0 : _unsafeAccess(ckpts, high - 1).votes;
135+
unchecked {
136+
return high == 0 ? 0 : _unsafeAccess(ckpts, high - 1).votes;
137+
}
134138
}
135139

136140
/**
@@ -243,15 +247,19 @@ abstract contract ERC20Votes is IVotes, ERC20Permit {
243247
) private returns (uint256 oldWeight, uint256 newWeight) {
244248
uint256 pos = ckpts.length;
245249

246-
Checkpoint memory oldCkpt = pos == 0 ? Checkpoint(0, 0) : _unsafeAccess(ckpts, pos - 1);
250+
unchecked {
251+
Checkpoint memory oldCkpt = pos == 0 ? Checkpoint(0, 0) : _unsafeAccess(ckpts, pos - 1);
247252

248-
oldWeight = oldCkpt.votes;
249-
newWeight = op(oldWeight, delta);
253+
oldWeight = oldCkpt.votes;
254+
newWeight = op(oldWeight, delta);
250255

251-
if (pos > 0 && oldCkpt.fromBlock == block.number) {
252-
_unsafeAccess(ckpts, pos - 1).votes = SafeCast.toUint224(newWeight);
253-
} else {
254-
ckpts.push(Checkpoint({fromBlock: SafeCast.toUint32(block.number), votes: SafeCast.toUint224(newWeight)}));
256+
if (pos > 0 && oldCkpt.fromBlock == block.number) {
257+
_unsafeAccess(ckpts, pos - 1).votes = SafeCast.toUint224(newWeight);
258+
} else {
259+
ckpts.push(
260+
Checkpoint({fromBlock: SafeCast.toUint32(block.number), votes: SafeCast.toUint224(newWeight)})
261+
);
262+
}
255263
}
256264
}
257265

0 commit comments

Comments
 (0)