Skip to content
Snippets Groups Projects
Commit a84e4f89 authored by Robert Martin-Legene's avatar Robert Martin-Legene
Browse files

Fixed syntax errors in Ballot

parent d0ef1cdd
No related branches found
No related tags found
No related merge requests found
...@@ -22,17 +22,28 @@ contract Ballot { ...@@ -22,17 +22,28 @@ contract Ballot {
address chairman; address chairman;
uint voteAtOrAfter; uint voteAtOrAfter;
uint voteAtOrBefore; uint voteAtOrBefore;
// A NOTE ON PERCENTAGES
// At present floats do not exist. Since we merely use
// our floats to later present to the outside world,
// and then let them handle the math, this scenario will
// be used:
//
// A percentage will be multiplied with one million. Thus:
// 100% is represented as 100 million. I.e. "100000000".
// 2/3 is represented as "67777778" (see note later on
// two-thirds
// Which percentage of the registered voters must // Which percentage of the registered voters must
// vote in order for the vote to be considered valid. // vote in order for the vote to be considered valid.
// e.g. 0.0 // e.g. 0
float percentOfRegisteredVotersReqToBeValid; uint percentOfRegisteredVotersReqToBeValid;
// Which percentage of the cast votes are required // Which percentage of the cast votes are required
// for the motion/title to pass? // for the motion/title to pass?
// MAJORITY VOTE: // MAJORITY VOTE:
// specify 50.00000000001 // specify 50000001
// TWO-THIRDS: // TWO-THIRDS:
// specify 67.77777777777 // specify 67777777
float percentOfVotesCastToWin; uint percentOfVotesCastToWin;
// Counting registered voters who do not vote as blank // Counting registered voters who do not vote as blank
// votes, has the effect that it is more difficult // votes, has the effect that it is more difficult
// to acquire the desired votes. // to acquire the desired votes.
...@@ -40,7 +51,7 @@ contract Ballot { ...@@ -40,7 +51,7 @@ contract Ballot {
} }
Votingrules public rules; Votingrules public rules;
mapping( address => int ) public voterMap; mapping( address => uint ) public voterMap;
Voter[] public voterList; Voter[] public voterList;
uint public numvoters; uint public numvoters;
Proposal[] public proposalList; Proposal[] public proposalList;
...@@ -51,11 +62,13 @@ contract Ballot { ...@@ -51,11 +62,13 @@ contract Ballot {
string ballotTitle, string ballotTitle,
uint voteAtOrAfter, uint voteAtOrAfter,
uint voteAtOrBefore, uint voteAtOrBefore,
float percentOfRegisteredVotersReqToBeValid, uint percentOfRegisteredVotersReqToBeValid,
float percentOfVotesCastToWin, uint percentOfVotesCastToWin,
bool countNonvotesAsBlanks, bool countNonvotesAsBlanks,
bytes32[] proposalNames bytes32[] proposalNames
) { )
public
{
require( voteAtOrBefore > now ); require( voteAtOrBefore > now );
// chairman can not automatically vote. Chairman must // chairman can not automatically vote. Chairman must
// giveRightToVote to himself if he wants to vote. // giveRightToVote to himself if he wants to vote.
...@@ -63,31 +76,36 @@ contract Ballot { ...@@ -63,31 +76,36 @@ contract Ballot {
rules.title = ballotTitle; rules.title = ballotTitle;
rules.voteAtOrAfter = voteAtOrAfter; rules.voteAtOrAfter = voteAtOrAfter;
rules.voteAtOrBefore = voteAtOrBefore; rules.voteAtOrBefore = voteAtOrBefore;
rules.percentOfRegisteredVotersReqToBeValid = percentOfRegisteredVotersReqToBeValid, rules.percentOfRegisteredVotersReqToBeValid = percentOfRegisteredVotersReqToBeValid;
rules.percentOfVotesCastToWin = percentOfVotesCastToWin, rules.percentOfVotesCastToWin = percentOfVotesCastToWin;
rules.countNonvotesAsBlanks = countNonvotesAsBlanks, rules.countNonvotesAsBlanks = countNonvotesAsBlanks;
// For each of the provided proposal names, // For each of the provided proposal names,
// create a new proposal object and add it // create a new proposal object and add it
// to the end of the array. // to the end of the array.
numproposals = proposalNames.length; numproposals = proposalNames.length;
int i = 0; uint i = 0;
while ( i < numproposals ) while ( i < numproposals )
{ {
Proposal newprop; proposalList.push(
newprop.name = proposalNames[i]; Proposal(
newprop.voteCount = 0; {
proposalList.push( newprop ); name: proposalNames[i],
voteCount: 0
}
)
);
i++; i++;
} }
} }
// Give `voter` the right to vote on this ballot. // Give `voter` the right to vote on this ballot.
function giveRightToVote( address voter ) function giveRightToVote( address voter )
public
{ {
// May only be called by chairman. // May only be called by chairman.
require( msg.sender == chairman ); require( msg.sender == rules.chairman );
require( voteAtOrBefore <= now ); require( rules.voteAtOrBefore <= now );
uint idx = voterMap[voter]; uint idx = voterMap[voter];
// Can't add voters more than once. // Can't add voters more than once.
require( idx == 0 ); require( idx == 0 );
...@@ -97,21 +115,26 @@ contract Ballot { ...@@ -97,21 +115,26 @@ contract Ballot {
// If the voter's address doesn't match, it is because // If the voter's address doesn't match, it is because
// he doesn't exist (and then we always have idx=0). // he doesn't exist (and then we always have idx=0).
// So we push him onto the voterList. // So we push him onto the voterList.
Voter newvoter; idx = voterList.push(
newvoter.voter = voter; Voter(
newvoter.vote = -1; {
idx = voterList.push( newvoter ) - 1; voter: voter,
vote: -1
}
)
) - 1;
voterMap[voter] = idx; voterMap[voter] = idx;
numvoters++; numvoters++;
} }
function getVoterIdx( address voter ) function getVoterIdx( address voter )
readonly public
returns int view
returns( int )
{ {
int idx = voterMap[voter]; uint idx = voterMap[voter];
if ( idx > 0 ) if ( idx > 0 )
return idx; return int(idx);
if ( voterList[0].voter == voter ) if ( voterList[0].voter == voter )
return 0; return 0;
return -1; return -1;
...@@ -119,20 +142,22 @@ contract Ballot { ...@@ -119,20 +142,22 @@ contract Ballot {
/// Give your vote to proposal `proposals[proposal].name`. /// Give your vote to proposal `proposals[proposal].name`.
function vote( uint proposal ) function vote( uint proposal )
public
{ {
require( proposal < numproposals ); require( proposal < numproposals );
require( rules.voteAtOrAfter >= now ); require( rules.voteAtOrAfter >= now );
require( rules.voteAtOrBefore <= now ); require( rules.voteAtOrBefore <= now );
int idx = getVoterIdx( msg.sender ); int idx = getVoterIdx( msg.sender );
require( idx > -1 ); require( idx > -1 );
int formervote = voterList[idx].vote; uint uidx = uint( idx );
int formervote = voterList[uidx].vote;
require( formervote != int(proposal) ); require( formervote != int(proposal) );
if ( formervote > -1 ) if ( formervote > -1 )
{ {
// He changed his vote - this is normal for politicians, too. // He changed his vote - this is normal for politicians, too.
proposals[ uint(formervote) ].voteCount--; proposalList[ uint(formervote) ].voteCount--;
} }
proposals[ proposal ].voteCount++; proposalList[ proposal ].voteCount++;
voterList[ uint(idx) ].vote = uint(proposal); voterList[ uidx ].vote = int(proposal);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment