From f30eb880093a1ffae4cdac523bb4a1ed9a085660 Mon Sep 17 00:00:00 2001 From: Duncan Townsend Date: Mon, 20 Mar 2023 09:39:51 -0400 Subject: [PATCH] Make quadraticThreshold `immutable` for gas efficiency --- contracts/governance/src/ZeroExVotes.sol | 9 ++++----- contracts/governance/test/BaseTest.t.sol | 9 +++------ contracts/governance/test/ZeroExVotesMalicious.sol | 2 +- contracts/governance/test/ZeroExVotesTest.t.sol | 4 ++-- 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/contracts/governance/src/ZeroExVotes.sol b/contracts/governance/src/ZeroExVotes.sol index 1dfec72790..7c8a70fd8e 100644 --- a/contracts/governance/src/ZeroExVotes.sol +++ b/contracts/governance/src/ZeroExVotes.sol @@ -29,14 +29,15 @@ import "./IZeroExVotes.sol"; contract ZeroExVotes is IZeroExVotes, Initializable, OwnableUpgradeable, UUPSUpgradeable { address public immutable token; - uint256 public quadraticThreshold; + uint256 public immutable quadraticThreshold; mapping(address => Checkpoint[]) private _checkpoints; Checkpoint[] private _totalSupplyCheckpoints; - constructor(address _token) { + constructor(address _token, uint256 _quadraticThreshold) { require(_token != address(0), "ZeroExVotes: token cannot be 0"); token = _token; + quadraticThreshold = _quadraticThreshold; _disableInitializers(); } @@ -47,11 +48,9 @@ contract ZeroExVotes is IZeroExVotes, Initializable, OwnableUpgradeable, UUPSUpg _; } - function initialize(uint256 _quadraticThreshold) public onlyProxy initializer { + function initialize() public onlyProxy initializer { __Ownable_init(); __UUPSUpgradeable_init(); - - quadraticThreshold = _quadraticThreshold; } /** diff --git a/contracts/governance/test/BaseTest.t.sol b/contracts/governance/test/BaseTest.t.sol index 21504edc62..47d33ce58f 100644 --- a/contracts/governance/test/BaseTest.t.sol +++ b/contracts/governance/test/BaseTest.t.sol @@ -41,7 +41,7 @@ contract BaseTest is Test { address payable internal account3 = payable(vm.addr(3)); address payable internal account4 = payable(vm.addr(4)); address payable internal securityCouncil = payable(vm.addr(5)); - uint256 internal quadraticThreshold = 1000000e18; + uint256 public constant quadraticThreshold = 1000000e18; bytes32 internal constant DELEGATION_TYPEHASH = keccak256("Delegation(address delegatee,uint256 nonce,uint256 expiry)"); @@ -105,11 +105,8 @@ contract BaseTest is Test { zrxToken := create(0, add(_bytecode, 0x20), mload(_bytecode)) } address wTokenPrediction = predict(account1, vm.getNonce(account1) + 2); - ZeroExVotes votesImpl = new ZeroExVotes(wTokenPrediction); - ERC1967Proxy votesProxy = new ERC1967Proxy( - address(votesImpl), - abi.encodeCall(votesImpl.initialize, (quadraticThreshold)) - ); + ZeroExVotes votesImpl = new ZeroExVotes(wTokenPrediction, quadraticThreshold); + ERC1967Proxy votesProxy = new ERC1967Proxy(address(votesImpl), abi.encodeCall(votesImpl.initialize, ())); ZRXWrappedToken wToken = new ZRXWrappedToken(zrxToken, ZeroExVotes(address(votesProxy))); vm.stopPrank(); diff --git a/contracts/governance/test/ZeroExVotesMalicious.sol b/contracts/governance/test/ZeroExVotesMalicious.sol index 8381a7b617..bfbcc2b7d1 100644 --- a/contracts/governance/test/ZeroExVotesMalicious.sol +++ b/contracts/governance/test/ZeroExVotesMalicious.sol @@ -21,7 +21,7 @@ pragma solidity ^0.8.19; import "../src/ZeroExVotes.sol"; contract ZeroExVotesMalicious is ZeroExVotes { - constructor(address _token) ZeroExVotes(_token) {} + constructor(address _token, uint256 _quadraticThreshold) ZeroExVotes(_token, _quadraticThreshold) {} function writeCheckpointTotalSupplyBurn( uint256 amount, diff --git a/contracts/governance/test/ZeroExVotesTest.t.sol b/contracts/governance/test/ZeroExVotesTest.t.sol index e4bd8c62ed..d04e528b69 100644 --- a/contracts/governance/test/ZeroExVotesTest.t.sol +++ b/contracts/governance/test/ZeroExVotesTest.t.sol @@ -43,7 +43,7 @@ contract ZeroExVotesTest is BaseTest { function testShouldNotBeAbleToReinitialise() public { vm.expectRevert("Initializable: contract is already initialized"); - votes.initialize(quadraticThreshold); + votes.initialize(); } function testShouldNotBeAbleToStopBurn() public { @@ -57,7 +57,7 @@ contract ZeroExVotesTest is BaseTest { // malicious upgrade vm.startPrank(account1); - IZeroExVotes maliciousImpl = new ZeroExVotesMalicious(votes.token()); + IZeroExVotes maliciousImpl = new ZeroExVotesMalicious(votes.token(), votes.quadraticThreshold()); votes.upgradeTo(address(maliciousImpl)); vm.stopPrank();