Skip to content

Commit

Permalink
Calculation of unlockable gold (#10731)
Browse files Browse the repository at this point in the history
* Fix for historical vote records

* contractkit fix

* lint fix

* Governance version update

* PR comments

* extended test

* Remove console.log

* Test

* lint fix

* version fix

* version fix

---------

Co-authored-by: Martín Volpe <[email protected]>
  • Loading branch information
pahor167 and martinvol authored Jan 30, 2024
1 parent e0e3578 commit b167121
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
18 changes: 16 additions & 2 deletions packages/protocol/contracts/governance/Governance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1471,14 +1471,20 @@ contract Governance is

uint256 maxUsed = 0;
for (uint256 index = 0; index < dequeued.length; index = index.add(1)) {
Proposals.Proposal storage proposal = proposals[dequeued[index]];
uint256 proposalId = dequeued[index];
Proposals.Proposal storage proposal = proposals[proposalId];
bool isVotingReferendum = (getProposalDequeuedStage(proposal) == Proposals.Stage.Referendum);

if (!isVotingReferendum) {
continue;
}

VoteRecord storage voteRecord = voter.referendumVotes[index];
// skip if vote record is not for this proposal
if (voteRecord.proposalId != proposalId) {
continue;
}

uint256 votesCast = voteRecord.yesVotes.add(voteRecord.noVotes).add(voteRecord.abstainVotes);
maxUsed = Math.max(
maxUsed,
Expand Down Expand Up @@ -1514,14 +1520,22 @@ contract Governance is
Voter storage voter = voters[account];

for (uint256 index = 0; index < dequeued.length; index = index.add(1)) {
Proposals.Proposal storage proposal = proposals[dequeued[index]];
uint256 proposalId = dequeued[index];
Proposals.Proposal storage proposal = proposals[proposalId];
bool isVotingReferendum = (getProposalDequeuedStage(proposal) == Proposals.Stage.Referendum);

if (!isVotingReferendum) {
continue;
}

VoteRecord storage voteRecord = voter.referendumVotes[index];

// skip if vote record is not for this proposal
if (voteRecord.proposalId != proposalId) {
delete voter.referendumVotes[index];
continue;
}

uint256 sumOfVotes = voteRecord.yesVotes.add(voteRecord.noVotes).add(voteRecord.abstainVotes);

if (sumOfVotes > newVotingPower) {
Expand Down
33 changes: 29 additions & 4 deletions packages/protocol/test-sol/governance/network/Governance.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ contract GovernanceForTest is Governance(true) {
_removeVotesWhenRevokingDelegatedVotes(account, maxAmountAllowed);
}

function setDeprecatedWeight(address voterAddress, uint256 proposalIndex, uint256 weight)
external
{
function setDeprecatedWeight(
address voterAddress,
uint256 proposalIndex,
uint256 weight,
uint256 proposalId
) external {
Voter storage voter = voters[voterAddress];
VoteRecord storage voteRecord = voter.referendumVotes[proposalIndex];
voteRecord.deprecated_weight = weight;
voteRecord.proposalId = proposalId;
}
}

Expand Down Expand Up @@ -3477,7 +3481,7 @@ contract GovernanceGetAmountOfGoldUsedForVoting is GovernanceBaseTest {
vm.warp(block.timestamp + governance.dequeueFrequency());
vm.prank(accApprover);
governance.approve(proposalId, 0);
governance.setDeprecatedWeight(accVoter, 0, 100);
governance.setDeprecatedWeight(accVoter, 0, 100, 1);
assertEq(governance.getAmountOfGoldUsedForVoting(accVoter), 100);
}

Expand Down Expand Up @@ -3506,6 +3510,27 @@ contract GovernanceGetAmountOfGoldUsedForVoting is GovernanceBaseTest {
assertEq(governance.getAmountOfGoldUsedForVoting(accVoter), 0);
}

function test_return0Votes_WhenIndexOfProposalGetsReused() public {
uint256 proposalId = makeValidProposal();
vm.warp(block.timestamp + governance.dequeueFrequency());
vm.prank(accApprover);
governance.approve(proposalId, 0);
vm.prank(accVoter);
governance.votePartially(proposalId, 0, 10, 30, 0);
vm.warp(block.timestamp + REFERENDUM_STAGE_DURATION);
governance.execute(proposalId, 0);
vm.warp(block.timestamp + governance.getExecutionStageDuration() + 1);
assertEq(governance.getAmountOfGoldUsedForVoting(accVoter), 0);

governance.dequeueProposalsIfReady();
proposalId = makeValidProposal();

vm.warp(block.timestamp + governance.dequeueFrequency() + 1);
vm.prank(accApprover);
governance.approve(proposalId, 0);
assertEq(governance.getAmountOfGoldUsedForVoting(accVoter), 0);
}

function test_returnFullWeightWhenUpvoting_WhenProposalInQueue() public {
vm.prank(accOwner);
governance.setConcurrentProposals(3);
Expand Down

0 comments on commit b167121

Please sign in to comment.