Skip to content

Commit

Permalink
reverts
Browse files Browse the repository at this point in the history
  • Loading branch information
novaknole committed Nov 4, 2024
1 parent e906746 commit def23ed
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
25 changes: 25 additions & 0 deletions packages/contracts/src/MajorityVotingBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ abstract contract MajorityVotingBase is
/// @param sender The sender address.
error ProposalCreationForbidden(address sender);

/// @notice Thrown when a proposal doesn't exist.
/// @param proposalId The ID of the proposal which doesn't exist.
error NonexistentProposal(uint256 proposalId);

/// @notice Thrown if an account is not allowed to cast a vote. This can be because the vote
/// - has not started,
/// - has ended,
Expand Down Expand Up @@ -387,18 +391,30 @@ abstract contract MajorityVotingBase is
address _voter,
VoteOption _voteOption
) public view virtual returns (bool) {
if (!_proposalExists(_proposalId)) {
revert NonexistentProposal(_proposalId);
}

return _canVote(_proposalId, _voter, _voteOption);
}

/// @inheritdoc IMajorityVoting
function canExecute(
uint256 _proposalId
) public view virtual override(IMajorityVoting) returns (bool) {
if (!_proposalExists(_proposalId)) {
revert NonexistentProposal(_proposalId);
}

return _canExecute(_proposalId);
}

/// @inheritdoc IProposal
function hasSucceeded(uint256 _proposalId) public view virtual returns (bool) {
if (!_proposalExists(_proposalId)) {
revert NonexistentProposal(_proposalId);
}

return _hasSucceeded(_proposalId);
}

Expand All @@ -413,6 +429,8 @@ abstract contract MajorityVotingBase is
proposal_.parameters.supportThreshold * proposal_.tally.no;
}

// is it possible that isSupportThresholdReachedEarly returns true, where isSupportThresholdReached returns false

/// @inheritdoc IMajorityVoting
function isSupportThresholdReachedEarly(
uint256 _proposalId
Expand Down Expand Up @@ -695,6 +713,13 @@ abstract contract MajorityVotingBase is
});
}

/// @notice Checks if proposal exists or not.
/// @param _proposalId The ID of the proposal.
/// @return Returns `true` if proposal exists, otherwise false.
function _proposalExists(uint256 _proposalId) private view returns (bool) {
return proposals[_proposalId].parameters.snapshotBlock != 0;
}

/// @notice Internal function to update minimal approval value.
/// @param _minApprovals The new minimal approval value.
function _updateMinApprovals(uint256 _minApprovals) internal virtual {
Expand Down
24 changes: 22 additions & 2 deletions packages/contracts/test/10_unit-testing/11_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1483,8 +1483,10 @@ describe('TokenVoting', function () {
expect(proposal.tally.no).to.equal(0);
expect(proposal.tally.abstain).to.equal(0);

expect(await plugin.canVote(1, alice.address, VoteOption.Yes)).to.equal(
false
expect(await plugin.canVote(id, alice.address, VoteOption.Yes)).to.be
.true;
expect(await plugin.getVoteOption(id, alice.address)).to.equal(
VoteOption.None
);

expect(proposal.actions.length).to.equal(1);
Expand Down Expand Up @@ -1649,6 +1651,24 @@ describe('TokenVoting', function () {
dummyMetadata: string;
}>
) {
it('reverts if proposal does not exist', async () => {
const {initializedPlugin: plugin} = await loadFixture(localFixture);

const id = 10;

await expect(plugin.canExecute(id))
.to.be.revertedWithCustomError(plugin, 'NonexistentProposal')
.withArgs(id);

await expect(plugin.canVote(id, plugin.address, VoteOption.Yes))
.to.be.revertedWithCustomError(plugin, 'NonexistentProposal')
.withArgs(id);

await expect(plugin.hasSucceeded(id))
.to.be.revertedWithCustomError(plugin, 'NonexistentProposal')
.withArgs(id);
});

it('does not allow voting, when the vote has not started yet', async () => {
const {
alice,
Expand Down

0 comments on commit def23ed

Please sign in to comment.