diff --git a/src/Ballot.sol b/src/Ballot.sol index 14cb9c0f0404daefc1dd6362685afe3f688fe423..72fcd4840a410a5b7dd5d8b8ac1124c206ec5aac 100644 --- a/src/Ballot.sol +++ b/src/Ballot.sol @@ -22,17 +22,28 @@ contract Ballot { address chairman; uint voteAtOrAfter; 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 // vote in order for the vote to be considered valid. - // e.g. 0.0 - float percentOfRegisteredVotersReqToBeValid; + // e.g. 0 + uint percentOfRegisteredVotersReqToBeValid; // Which percentage of the cast votes are required // for the motion/title to pass? // MAJORITY VOTE: - // specify 50.00000000001 + // specify 50000001 // TWO-THIRDS: - // specify 67.77777777777 - float percentOfVotesCastToWin; + // specify 67777777 + uint percentOfVotesCastToWin; // Counting registered voters who do not vote as blank // votes, has the effect that it is more difficult // to acquire the desired votes. @@ -40,7 +51,7 @@ contract Ballot { } Votingrules public rules; - mapping( address => int ) public voterMap; + mapping( address => uint ) public voterMap; Voter[] public voterList; uint public numvoters; Proposal[] public proposalList; @@ -51,11 +62,13 @@ contract Ballot { string ballotTitle, uint voteAtOrAfter, uint voteAtOrBefore, - float percentOfRegisteredVotersReqToBeValid, - float percentOfVotesCastToWin, + uint percentOfRegisteredVotersReqToBeValid, + uint percentOfVotesCastToWin, bool countNonvotesAsBlanks, bytes32[] proposalNames - ) { + ) + public + { require( voteAtOrBefore > now ); // chairman can not automatically vote. Chairman must // giveRightToVote to himself if he wants to vote. @@ -63,31 +76,36 @@ contract Ballot { rules.title = ballotTitle; rules.voteAtOrAfter = voteAtOrAfter; rules.voteAtOrBefore = voteAtOrBefore; - rules.percentOfRegisteredVotersReqToBeValid = percentOfRegisteredVotersReqToBeValid, - rules.percentOfVotesCastToWin = percentOfVotesCastToWin, - rules.countNonvotesAsBlanks = countNonvotesAsBlanks, + rules.percentOfRegisteredVotersReqToBeValid = percentOfRegisteredVotersReqToBeValid; + rules.percentOfVotesCastToWin = percentOfVotesCastToWin; + rules.countNonvotesAsBlanks = countNonvotesAsBlanks; // For each of the provided proposal names, // create a new proposal object and add it // to the end of the array. numproposals = proposalNames.length; - int i = 0; + uint i = 0; while ( i < numproposals ) { - Proposal newprop; - newprop.name = proposalNames[i]; - newprop.voteCount = 0; - proposalList.push( newprop ); + proposalList.push( + Proposal( + { + name: proposalNames[i], + voteCount: 0 + } + ) + ); i++; } } // Give `voter` the right to vote on this ballot. function giveRightToVote( address voter ) + public { // May only be called by chairman. - require( msg.sender == chairman ); - require( voteAtOrBefore <= now ); + require( msg.sender == rules.chairman ); + require( rules.voteAtOrBefore <= now ); uint idx = voterMap[voter]; // Can't add voters more than once. require( idx == 0 ); @@ -97,21 +115,26 @@ contract Ballot { // If the voter's address doesn't match, it is because // he doesn't exist (and then we always have idx=0). // So we push him onto the voterList. - Voter newvoter; - newvoter.voter = voter; - newvoter.vote = -1; - idx = voterList.push( newvoter ) - 1; + idx = voterList.push( + Voter( + { + voter: voter, + vote: -1 + } + ) + ) - 1; voterMap[voter] = idx; numvoters++; } function getVoterIdx( address voter ) - readonly - returns int + public + view + returns( int ) { - int idx = voterMap[voter]; + uint idx = voterMap[voter]; if ( idx > 0 ) - return idx; + return int(idx); if ( voterList[0].voter == voter ) return 0; return -1; @@ -119,20 +142,22 @@ contract Ballot { /// Give your vote to proposal `proposals[proposal].name`. function vote( uint proposal ) + public { require( proposal < numproposals ); require( rules.voteAtOrAfter >= now ); require( rules.voteAtOrBefore <= now ); int idx = getVoterIdx( msg.sender ); require( idx > -1 ); - int formervote = voterList[idx].vote; + uint uidx = uint( idx ); + int formervote = voterList[uidx].vote; require( formervote != int(proposal) ); if ( formervote > -1 ) { // He changed his vote - this is normal for politicians, too. - proposals[ uint(formervote) ].voteCount--; + proposalList[ uint(formervote) ].voteCount--; } - proposals[ proposal ].voteCount++; - voterList[ uint(idx) ].vote = uint(proposal); + proposalList[ proposal ].voteCount++; + voterList[ uidx ].vote = int(proposal); } }