Skip to content

Commit c094b55

Browse files
committed
feat: add tests
1 parent 4b59e66 commit c094b55

File tree

4 files changed

+66
-27
lines changed

4 files changed

+66
-27
lines changed

src/XanV1.sol

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ contract XanV1 is
115115
function castVote(address proposedImpl) external override {
116116
address voter = msg.sender;
117117

118-
Voting.Data storage $ = _getVotingData();
119-
Voting.Ballot storage ballot = $.ballots[proposedImpl];
118+
Voting.Data storage data = _getVotingData();
119+
Voting.Ballot storage ballot = data.ballots[proposedImpl];
120120

121121
if (!ballot.exists) {
122-
$.assignWorstRank(proposedImpl);
122+
data.assignWorstRank(proposedImpl);
123123
}
124124

125125
// Cache the old votum of the voter.
@@ -147,7 +147,7 @@ contract XanV1 is
147147
ballot.totalVotes += delta;
148148

149149
// Bubble the proposed implementation up in the ranking.
150-
$.bubbleUp(proposedImpl);
150+
data.bubbleUp(proposedImpl);
151151

152152
emit VoteCast({voter: voter, implementation: proposedImpl, value: delta});
153153
}
@@ -156,8 +156,8 @@ contract XanV1 is
156156
function revokeVote(address proposedImpl) external override {
157157
address voter = msg.sender;
158158

159-
Voting.Data storage $ = _getVotingData();
160-
Voting.Ballot storage ballot = $.ballots[proposedImpl];
159+
Voting.Data storage data = _getVotingData();
160+
Voting.Ballot storage ballot = data.ballots[proposedImpl];
161161

162162
// Cache the old votum of the voter.
163163
uint256 oldVotum = ballot.vota[voter];
@@ -174,7 +174,7 @@ contract XanV1 is
174174
ballot.totalVotes -= oldVotum;
175175

176176
// Bubble the proposed implementation down in the ranking.
177-
$.bubbleDown(proposedImpl);
177+
data.bubbleDown(proposedImpl);
178178

179179
emit VoteRevoked({voter: voter, implementation: proposedImpl, value: oldVotum});
180180
}
@@ -184,24 +184,24 @@ contract XanV1 is
184184
// Check that all upgrade criteria are met before starting the delay.
185185
_checkVoterBodyUpgradeCriteria(proposedImpl);
186186

187-
Voting.Data storage $ = _getVotingData();
187+
Voting.Data storage data = _getVotingData();
188188

189189
uint48 currentTime = Time.timestamp();
190190

191191
// Check that the delay period hasn't been started yet by
192192
// ensuring that no end time and implementation has been set.
193-
if ($.delayEndTime != 0 && $.delayedUpgradeImpl != address(0)) {
194-
revert DelayPeriodAlreadyStarted($.delayedUpgradeImpl);
193+
if (data.delayEndTime != 0 && data.delayedUpgradeImpl != address(0)) {
194+
revert DelayPeriodAlreadyStarted(data.delayedUpgradeImpl);
195195
}
196196

197197
// Set the end time and emit the associated event.
198-
$.delayEndTime = currentTime + Parameters.DELAY_DURATION;
199-
$.delayedUpgradeImpl = proposedImpl;
198+
data.delayEndTime = currentTime + Parameters.DELAY_DURATION;
199+
data.delayedUpgradeImpl = proposedImpl;
200200

201201
emit VoterBodyUpgradeDelayStarted({
202202
implementation: proposedImpl,
203203
startTime: currentTime,
204-
endTime: $.delayEndTime
204+
endTime: data.delayEndTime
205205
});
206206
}
207207

@@ -383,18 +383,29 @@ contract XanV1 is
383383
revert ImplementationZero();
384384
}
385385

386-
// TODO optimize fetching? Data is also used in Criteria checks // No, happens only once.
387-
address councilProposedImpl = _getCouncilData().proposedImpl;
386+
Council.Data storage council = _getCouncilData();
387+
Voting.Data storage voting = _getVotingData();
388388

389-
// TODO! What if the voter body votes for the same impl as the council?
389+
// TODO REFACTOR
390+
if (newImpl != council.proposedImpl) {
391+
_checkVoterBodyDelayCriterion(newImpl);
392+
_checkVoterBodyUpgradeCriteria(newImpl);
393+
return;
394+
}
390395

391-
if (newImpl == councilProposedImpl) {
392-
_checkCouncilDelayCriterion( /*TODO! //councilProposedImpl*/ );
396+
// TODO! rename delayedUpgradeImpl to scheduled impl!!!
397+
if (newImpl != voting.delayedUpgradeImpl) {
398+
_checkCouncilDelayCriterion();
399+
_checkCouncilUpgradeCriteria(newImpl);
400+
return;
401+
}
393402

394-
_checkCouncilUpgradeCriteria(councilProposedImpl);
403+
// The same implementation has been proposed by both
404+
if (council.delayEndTime < voting.delayEndTime) {
405+
_checkCouncilDelayCriterion();
406+
_checkCouncilUpgradeCriteria(newImpl);
395407
} else {
396408
_checkVoterBodyDelayCriterion(newImpl);
397-
398409
_checkVoterBodyUpgradeCriteria(newImpl);
399410
}
400411
}

test/XanV1.council.t.sol

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,19 @@ contract XanV1CouncilTest is Test {
149149
assertEq(_xanProxy.councilProposedImplementation(), _NEW_IMPL);
150150
}
151151

152-
function test_councilProposedImplementation_returns_address_0_if_no_upgrade_delay_has_been_started() public view {
152+
function test_councilDelayEndTime_returns_the_end_time_if_an_upgrade_delay_has_been_started() public {
153+
uint256 expectedEndTime = block.timestamp + Parameters.DELAY_DURATION;
154+
vm.prank(_COUNCIL);
155+
_xanProxy.proposeCouncilUpgrade(_NEW_IMPL);
156+
157+
assertEq(_xanProxy.councilDelayEndTime(), expectedEndTime);
158+
}
159+
160+
function test_councilProposedImplementation_returns_0_if_no_upgrade_delay_has_been_started() public view {
153161
assertEq(_xanProxy.councilProposedImplementation(), address(0));
154162
}
163+
164+
function test_councilDelayEndTime_returns_0_if_no_upgrade_delay_has_been_started() public view {
165+
assertEq(_xanProxy.councilDelayEndTime(), 0);
166+
}
155167
}

test/XanV1.upgrade.t.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,10 @@ contract XanV1UpgradeTest is Test {
176176
vm.prank(_COUNCIL);
177177
_xanProxy.proposeCouncilUpgrade(_voterProposedImpl);
178178

179+
assertLt(_xanProxy.voterBodyDelayEndTime(), _xanProxy.councilDelayEndTime());
180+
179181
// Advance time after the delay end of the implementation proposed by the voter body.
180-
skip(_xanProxy.voterBodyDelayEndTime());
182+
skip(_xanProxy.voterBodyDelayEndTime() - block.timestamp);
181183

182184
_xanProxy.upgradeToAndCall({newImplementation: _voterProposedImpl, data: ""});
183185
}

test/XanV1.voting.t.sol

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,16 +369,30 @@ contract XanV1VotingTest is Test {
369369
vm.startPrank(_defaultSender);
370370
_xanProxy.lock(Parameters.MIN_LOCKED_SUPPLY);
371371
_xanProxy.castVote(_NEW_IMPL);
372-
_xanProxy.startVoterBodyUpgradeDelay(_NEW_IMPL);
373372
vm.stopPrank();
374373

374+
_xanProxy.startVoterBodyUpgradeDelay(_NEW_IMPL);
375+
375376
assertEq(_xanProxy.voterBodyProposedImplementation(), _NEW_IMPL);
376377
}
377378

378-
function test_voterBodyProposedImplementation_returns_address_0_if_no_upgrade_delay_has_been_started()
379-
public
380-
view
381-
{
379+
function test_councilDelayEndTime_returns_the_end_time_if_an_upgrade_delay_has_been_started() public {
380+
vm.startPrank(_defaultSender);
381+
_xanProxy.lock(Parameters.MIN_LOCKED_SUPPLY);
382+
_xanProxy.castVote(_NEW_IMPL);
383+
vm.stopPrank();
384+
385+
uint256 expectedEndTime = block.timestamp + Parameters.DELAY_DURATION;
386+
_xanProxy.startVoterBodyUpgradeDelay(_NEW_IMPL);
387+
388+
assertEq(_xanProxy.voterBodyDelayEndTime(), expectedEndTime);
389+
}
390+
391+
function test_voterBodyProposedImplementation_returns_0_if_no_upgrade_delay_has_been_started() public view {
382392
assertEq(_xanProxy.voterBodyProposedImplementation(), address(0));
383393
}
394+
395+
function test_voterBodyDelayEndTime_returns_0_if_no_upgrade_delay_has_been_started() public view {
396+
assertEq(_xanProxy.voterBodyDelayEndTime(), 0);
397+
}
384398
}

0 commit comments

Comments
 (0)