@@ -34,14 +34,9 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor, IERC721Receive
34
34
35
35
// solhint-disable var-name-mixedcase
36
36
struct ProposalCore {
37
- // --- start retyped from Timers.BlockNumber at offset 0x00 ---
38
- uint64 voteStart;
39
37
address proposer;
40
- bytes4 __gap_unused0;
41
- // --- start retyped from Timers.BlockNumber at offset 0x20 ---
42
- uint64 voteEnd;
43
- bytes24 __gap_unused1;
44
- // --- Remaining fields starting at offset 0x40 ---------------
38
+ uint48 voteStart;
39
+ uint32 voteDuration;
45
40
bool executed;
46
41
bool canceled;
47
42
}
@@ -166,7 +161,9 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor, IERC721Receive
166
161
* @dev See {IGovernor-state}.
167
162
*/
168
163
function state (uint256 proposalId ) public view virtual override returns (ProposalState) {
169
- ProposalCore storage proposal = _proposals[proposalId];
164
+ // ProposalCore is just one slot. We can load it from storage to memory with a single sload and use memory
165
+ // object as a cache. This avoid duplicating expensive sloads.
166
+ ProposalCore memory proposal = _proposals[proposalId];
170
167
171
168
if (proposal.executed) {
172
169
return ProposalState.Executed;
@@ -219,7 +216,7 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor, IERC721Receive
219
216
* @dev See {IGovernor-proposalDeadline}.
220
217
*/
221
218
function proposalDeadline (uint256 proposalId ) public view virtual override returns (uint256 ) {
222
- return _proposals[proposalId].voteEnd ;
219
+ return _proposals[proposalId].voteStart + _proposals[proposalId].voteDuration ;
223
220
}
224
221
225
222
/**
@@ -300,16 +297,14 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor, IERC721Receive
300
297
}
301
298
302
299
uint256 snapshot = currentTimepoint + votingDelay ();
303
- uint256 deadline = snapshot + votingPeriod ();
300
+ uint256 duration = votingPeriod ();
304
301
305
302
_proposals[proposalId] = ProposalCore ({
306
303
proposer: proposer,
307
- voteStart: SafeCast.toUint64 (snapshot),
308
- voteEnd : SafeCast.toUint64 (deadline ),
304
+ voteStart: SafeCast.toUint48 (snapshot),
305
+ voteDuration : SafeCast.toUint32 (duration ),
309
306
executed: false ,
310
- canceled: false ,
311
- __gap_unused0: 0 ,
312
- __gap_unused1: 0
307
+ canceled: false
313
308
});
314
309
315
310
emit ProposalCreated (
@@ -320,7 +315,7 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor, IERC721Receive
320
315
new string [](targets.length ),
321
316
calldatas,
322
317
snapshot,
323
- deadline ,
318
+ snapshot + duration ,
324
319
description
325
320
);
326
321
@@ -592,13 +587,12 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor, IERC721Receive
592
587
string memory reason ,
593
588
bytes memory params
594
589
) internal virtual returns (uint256 ) {
595
- ProposalCore storage proposal = _proposals[proposalId];
596
590
ProposalState currentState = state (proposalId);
597
591
if (currentState != ProposalState.Active) {
598
592
revert GovernorUnexpectedProposalState (proposalId, currentState, _encodeStateBitmap (ProposalState.Active));
599
593
}
600
594
601
- uint256 weight = _getVotes (account, proposal.voteStart , params);
595
+ uint256 weight = _getVotes (account, proposalSnapshot (proposalId) , params);
602
596
_countVote (proposalId, account, support, weight, params);
603
597
604
598
if (params.length == 0 ) {
0 commit comments