Skip to content

Commit

Permalink
Make quadraticThreshold immutable for gas efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
duncancmt committed Mar 20, 2023
1 parent db23d97 commit f30eb88
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
9 changes: 4 additions & 5 deletions contracts/governance/src/ZeroExVotes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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;
}

/**
Expand Down
9 changes: 3 additions & 6 deletions contracts/governance/test/BaseTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)");
Expand Down Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion contracts/governance/test/ZeroExVotesMalicious.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions contracts/governance/test/ZeroExVotesTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();

Expand Down

0 comments on commit f30eb88

Please sign in to comment.