Skip to content

Commit 9bcff34

Browse files
jflatowhayesgm
authored andcommitted
Comp and Gov Formatting and Dependency Update
This patch is a very small update to our Comp and Gov contracts to improve formatting and grammar. We also: 1. Remove SafeMath in place of inlining the two SafeMath functions that were being used. 2. Move Interfaces to the bottom to clean up flattened code viewing 3. Add StartBlock and EndBlock to the ProposalCreated event.
1 parent 7f396aa commit 9bcff34

File tree

4 files changed

+61
-41
lines changed

4 files changed

+61
-41
lines changed

contracts/Governance/Comp.sol

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
pragma solidity ^0.5.16;
22
pragma experimental ABIEncoderV2;
33

4-
import "../EIP20Interface.sol";
5-
6-
/**
7-
* @title Compound's Governance Token Contract
8-
* @notice Immutable tokens for Compound Governors
9-
* @author Compound
10-
*/
11-
contract Comp is EIP20Interface {
4+
contract Comp {
125
/// @notice EIP-20 token name for this token
136
string public constant name = "Compound";
147

@@ -57,6 +50,12 @@ contract Comp is EIP20Interface {
5750
/// @notice An event thats emitted when a delegate account's vote balance changes
5851
event DelegateVotesChanged(address indexed delegate, uint previousBalance, uint newBalance);
5952

53+
/// @notice The standard EIP-20 transfer event
54+
event Transfer(address indexed from, address indexed to, uint256 amount);
55+
56+
/// @notice The standard EIP-20 approval event
57+
event Approval(address indexed owner, address indexed spender, uint256 amount);
58+
6059
/**
6160
* @notice Construct a new Comp token
6261
* @param account The initial account to grant all the tokens
@@ -291,8 +290,7 @@ contract Comp is EIP20Interface {
291290

292291
function sub96(uint96 a, uint96 b, string memory errorMessage) internal pure returns (uint96) {
293292
require(b <= a, errorMessage);
294-
uint96 c = a - b;
295-
return c;
293+
return a - b;
296294
}
297295

298296
function getChainId() internal pure returns (uint) {

contracts/Governance/GovernorAlpha.sol

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,7 @@
11
pragma solidity ^0.5.16;
22
pragma experimental ABIEncoderV2;
33

4-
import "../SafeMath.sol";
5-
6-
interface TimelockInterface {
7-
function delay() external view returns (uint);
8-
function GRACE_PERIOD() external view returns (uint);
9-
function acceptAdmin() external;
10-
function queuedTransactions(bytes32 hash) external view returns (bool);
11-
function queueTransaction(address target, uint value, string calldata signature, bytes calldata data, uint eta) external returns (bytes32);
12-
function cancelTransaction(address target, uint value, string calldata signature, bytes calldata data, uint eta) external;
13-
function executeTransaction(address target, uint value, string calldata signature, bytes calldata data, uint eta) external payable returns (bytes memory);
14-
}
15-
16-
interface CompInterface {
17-
function getPriorVotes(address account, uint blockNumber) external view returns (uint96);
18-
}
19-
204
contract GovernorAlpha {
21-
using SafeMath for uint;
22-
235
/// @notice The name of this contract
246
string public constant name = "Compound Governor Alpha";
257

@@ -41,7 +23,7 @@ contract GovernorAlpha {
4123
/// @notice The address of the Compound Protocol Timelock
4224
TimelockInterface public timelock;
4325

44-
/// @notice The address of the Compound Governance Token
26+
/// @notice The address of the Compound governance token
4527
CompInterface public comp;
4628

4729
/// @notice The address of the Governor Guardian
@@ -131,7 +113,7 @@ contract GovernorAlpha {
131113
bytes32 public constant BALLOT_TYPEHASH = keccak256("Ballot(uint256 proposalId,bool support)");
132114

133115
/// @notice An event emitted when a new proposal is created
134-
event ProposalCreated(uint256 id, address proposer, address[] targets, uint256[] values, string[] signatures, bytes[] calldatas, string description);
116+
event ProposalCreated(uint id, address proposer, address[] targets, uint[] values, string[] signatures, bytes[] calldatas, uint startBlock, uint endBlock, string description);
135117

136118
/// @notice An event emitted when a vote has been cast on a proposal
137119
event VoteCast(address voter, uint proposalId, bool support, uint votes);
@@ -152,7 +134,7 @@ contract GovernorAlpha {
152134
}
153135

154136
function propose(address[] memory targets, uint[] memory values, string[] memory signatures, bytes[] memory calldatas, string memory description) public returns (uint) {
155-
require(comp.getPriorVotes(msg.sender, block.number.sub(1)) > proposalThreshold(), "GovernorAlpha::propose: proposer votes below proposal threshold");
137+
require(comp.getPriorVotes(msg.sender, sub256(block.number, 1)) > proposalThreshold(), "GovernorAlpha::propose: proposer votes below proposal threshold");
156138
require(targets.length == values.length && targets.length == signatures.length && targets.length == calldatas.length, "GovernorAlpha::propose: proposal function information arity mismatch");
157139
require(targets.length != 0, "GovernorAlpha::propose: must provide actions");
158140
require(targets.length <= proposalMaxOperations(), "GovernorAlpha::propose: too many actions");
@@ -164,6 +146,9 @@ contract GovernorAlpha {
164146
require(proposersLatestProposalState != ProposalState.Pending, "GovernorAlpha::propose: one live proposal per proposer, found an already pending proposal");
165147
}
166148

149+
uint startBlock = add256(block.number, votingDelay());
150+
uint endBlock = add256(startBlock, votingPeriod());
151+
167152
proposalCount++;
168153
Proposal memory newProposal = Proposal({
169154
id: proposalCount,
@@ -173,8 +158,8 @@ contract GovernorAlpha {
173158
values: values,
174159
signatures: signatures,
175160
calldatas: calldatas,
176-
startBlock: block.number.add(votingDelay()),
177-
endBlock: block.number.add(votingDelay()).add(votingPeriod()),
161+
startBlock: startBlock,
162+
endBlock: endBlock,
178163
forVotes: 0,
179164
againstVotes: 0,
180165
canceled: false,
@@ -184,14 +169,14 @@ contract GovernorAlpha {
184169
proposals[newProposal.id] = newProposal;
185170
latestProposalIds[newProposal.proposer] = newProposal.id;
186171

187-
emit ProposalCreated(newProposal.id, msg.sender, targets, values, signatures, calldatas, description);
172+
emit ProposalCreated(newProposal.id, msg.sender, targets, values, signatures, calldatas, startBlock, endBlock, description);
188173
return newProposal.id;
189174
}
190175

191176
function queue(uint proposalId) public {
192177
require(state(proposalId) == ProposalState.Succeeded, "GovernorAlpha::queue: proposal can only be queued if it is succeeded");
193178
Proposal storage proposal = proposals[proposalId];
194-
uint eta = block.timestamp.add(timelock.delay());
179+
uint eta = add256(block.timestamp, timelock.delay());
195180
for (uint i = 0; i < proposal.targets.length; i++) {
196181
_queueOrRevert(proposal.targets[i], proposal.values[i], proposal.signatures[i], proposal.calldatas[i], eta);
197182
}
@@ -219,7 +204,7 @@ contract GovernorAlpha {
219204
require(state != ProposalState.Executed, "GovernorAlpha::cancel: cannot cancel executed proposal");
220205

221206
Proposal storage proposal = proposals[proposalId];
222-
require(msg.sender == guardian || comp.getPriorVotes(proposal.proposer, block.number.sub(1)) < proposalThreshold(), "GovernorAlpha::cancel: proposer above threshold");
207+
require(msg.sender == guardian || comp.getPriorVotes(proposal.proposer, sub256(block.number, 1)) < proposalThreshold(), "GovernorAlpha::cancel: proposer above threshold");
223208

224209
proposal.canceled = true;
225210
for (uint i = 0; i < proposal.targets.length; i++) {
@@ -253,7 +238,7 @@ contract GovernorAlpha {
253238
return ProposalState.Succeeded;
254239
} else if (proposal.executed) {
255240
return ProposalState.Executed;
256-
} else if (block.timestamp >= proposal.eta.add(timelock.GRACE_PERIOD())) {
241+
} else if (block.timestamp >= add256(proposal.eta, timelock.GRACE_PERIOD())) {
257242
return ProposalState.Expired;
258243
} else {
259244
return ProposalState.Queued;
@@ -281,9 +266,9 @@ contract GovernorAlpha {
281266
uint96 votes = comp.getPriorVotes(voter, proposal.startBlock);
282267

283268
if (support) {
284-
proposal.forVotes = proposal.forVotes.add(votes);
269+
proposal.forVotes = add256(proposal.forVotes, votes);
285270
} else {
286-
proposal.againstVotes = proposal.againstVotes.add(votes);
271+
proposal.againstVotes = add256(proposal.againstVotes, votes);
287272
}
288273

289274
receipt.hasVoted = true;
@@ -313,9 +298,34 @@ contract GovernorAlpha {
313298
timelock.executeTransaction(address(timelock), 0, "setPendingAdmin(address)", abi.encode(newPendingAdmin), eta);
314299
}
315300

301+
function add256(uint256 a, uint256 b) internal pure returns (uint) {
302+
uint c = a + b;
303+
require(c >= a, "addition overflow");
304+
return c;
305+
}
306+
307+
function sub256(uint256 a, uint256 b) internal pure returns (uint) {
308+
require(b <= a, "subtraction underflow");
309+
return a - b;
310+
}
311+
316312
function getChainId() internal pure returns (uint) {
317-
uint256 chainId;
313+
uint chainId;
318314
assembly { chainId := chainid() }
319315
return chainId;
320316
}
321317
}
318+
319+
interface TimelockInterface {
320+
function delay() external view returns (uint);
321+
function GRACE_PERIOD() external view returns (uint);
322+
function acceptAdmin() external;
323+
function queuedTransactions(bytes32 hash) external view returns (bool);
324+
function queueTransaction(address target, uint value, string calldata signature, bytes calldata data, uint eta) external returns (bytes32);
325+
function cancelTransaction(address target, uint value, string calldata signature, bytes calldata data, uint eta) external;
326+
function executeTransaction(address target, uint value, string calldata signature, bytes calldata data, uint eta) external payable returns (bytes memory);
327+
}
328+
329+
interface CompInterface {
330+
function getPriorVotes(address account, uint blockNumber) external view returns (uint96);
331+
}

spec/scenario/Governor/Propose.scen

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Test "Propose 💍 [1 Action]"
1919
Assert Log ProposalCreated (targets [(Address Counter)])
2020
Assert Log ProposalCreated (values [1])
2121
Assert Log ProposalCreated (signatures ["increment(uint256)"])
22+
Assert Log ProposalCreated (startBlock 9)
23+
Assert Log ProposalCreated (endBlock 17289)
2224
Assert Log ProposalCreated (calldatas ["0x0000000000000000000000000000000000000000000000000000000000000005"])
2325
Assert Log ProposalCreated (proposer (Address Root))
2426
Assert Equal (Governor LegitGov Proposal LastProposal Id) 1

tests/Governance/GovernorAlpha/ProposeTest.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,17 @@ describe('GovernorAlpha#propose/5', () => {
137137

138138
expect(
139139
await send(gov, 'propose', [targets, values, signatures, callDatas, "second proposal"], { from: accounts[3] })
140-
).toHaveLog("ProposalCreated", { id: nextProposalId, targets: targets, values: values, signatures: signatures, calldatas: callDatas, description: "second proposal", proposer: accounts[3] });
140+
).toHaveLog("ProposalCreated", {
141+
id: nextProposalId,
142+
targets: targets,
143+
values: values,
144+
signatures: signatures,
145+
calldatas: callDatas,
146+
startBlock: 14,
147+
endBlock: 17294,
148+
description: "second proposal",
149+
proposer: accounts[3]
150+
});
141151
});
142152
});
143153
});

0 commit comments

Comments
 (0)