Skip to content

Commit

Permalink
Add out of bounds error to CRVoting
Browse files Browse the repository at this point in the history
  • Loading branch information
ßingen committed May 16, 2019
1 parent d31488c commit bd0932f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 13 additions & 3 deletions contracts/CRVoting.sol
Expand Up @@ -11,6 +11,7 @@ contract CRVoting is ICRVoting {
string internal constant ERROR_ALREADY_VOTED = "CRV_ALREADY_VOTED";
string internal constant ERROR_INVALID_VOTE = "CRV_INVALID_VOTE";
string internal constant ERROR_FAILURE_COMMITMENT_CHECK = "CRV_FAILURE_COMMITMENT_CHECK";
string internal constant ERROR_OUT_OF_BOUNDS = "CRV_OUT_OF_BOUNDS";

struct CastVote {
bytes32 commitment;
Expand All @@ -31,9 +32,9 @@ contract CRVoting is ICRVoting {
// ruling options are dispute specific
}

ICRVotingOwner owner;
mapping (uint256 => Vote) votes;
uint256 votesLength;
ICRVotingOwner public owner;
mapping (uint256 => Vote) internal votes;
uint256 public votesLength;

event VoteCommitted(uint256 indexed voteId, address indexed voter, bytes32 commitment);
event VoteRevealed(uint256 indexed voteId, address indexed voter, uint8 ruling);
Expand Down Expand Up @@ -63,6 +64,7 @@ contract CRVoting is ICRVoting {
* @notice Commit juror vote for vote #`_voteId`
*/
function commitVote(uint256 _voteId, bytes32 _commitment) external {
require(_voteId < votesLength, ERROR_OUT_OF_BOUNDS);
require(owner.canCommit(_voteId, msg.sender) > 0, ERROR_NOT_ALLOWED_BY_OWNER);
CastVote storage castVote = votes[_voteId].castVotes[msg.sender];
require(castVote.commitment == bytes32(0) && castVote.ruling == uint8(Ruling.Missing), ERROR_ALREADY_VOTED);
Expand All @@ -76,6 +78,7 @@ contract CRVoting is ICRVoting {
* @notice Leak vote for `_voter` in vote #`_voteId`
*/
function leakVote(uint256 _voteId, address _voter, uint8 _leakedRuling, bytes32 _salt) external {
require(_voteId < votesLength, ERROR_OUT_OF_BOUNDS);
uint256 weight = owner.canCommit(_voteId, _voter);
require(weight > 0, ERROR_NOT_ALLOWED_BY_OWNER);

Expand All @@ -97,6 +100,7 @@ contract CRVoting is ICRVoting {
* @notice Reveal juror `_ruling` vote in dispute #`_disputeId` (round #`_roundId`)
*/
function revealVote(uint256 _voteId, uint8 _ruling, bytes32 _salt) external {
require(_voteId < votesLength, ERROR_OUT_OF_BOUNDS);
uint256 weight = owner.canReveal(_voteId, msg.sender);
require(weight > 0, ERROR_NOT_ALLOWED_BY_OWNER);

Expand All @@ -114,6 +118,8 @@ contract CRVoting is ICRVoting {
}

function getVote(uint256 _voteId) external view returns (uint8 ruling, uint256 winningVoters) {
require(_voteId < votesLength, ERROR_OUT_OF_BOUNDS);

Vote storage vote = votes[_voteId];
ruling = vote.winningRuling;
if (Ruling(ruling) == Ruling.Missing) {
Expand All @@ -123,10 +129,14 @@ contract CRVoting is ICRVoting {
}

function getCastVote(uint256 _voteId, address _voter) external view returns (uint8) {
require(_voteId < votesLength, ERROR_OUT_OF_BOUNDS);

return votes[_voteId].castVotes[_voter].ruling;
}

function getRulingVotes(uint256 _voteId, uint8 _ruling) external view returns (uint256) {
require(_voteId < votesLength, ERROR_OUT_OF_BOUNDS);

return votes[_voteId].rulingVotes[_ruling];
}

Expand Down
4 changes: 1 addition & 3 deletions test/court-disputes.js
Expand Up @@ -186,9 +186,7 @@ contract('Court: Disputes', ([ poor, rich, governor, juror1, juror2, juror3, arb
assert.equal(ruling, 0, `juror #${draftId} vote`)
}

// TODO:
//await assertRevert(this.voting.getCastVote(voteId + 1, juror1)) // out of bounds
//await assertRevert(this.voting.getCastVote(voteId, other)) // out of bounds
await assertRevert(this.voting.getCastVote(voteId + 1, juror1)) // out of bounds
})

it('fails to draft a second time', async () => {
Expand Down

0 comments on commit bd0932f

Please sign in to comment.