Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
nucleo
Manage
Activity
Members
Labels
Plan
Issues
4
Issue boards
Milestones
Wiki
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
blockchain
nucleo
Commits
a84e4f89
Commit
a84e4f89
authored
6 years ago
by
Robert Martin-Legene
Browse files
Options
Downloads
Patches
Plain Diff
Fixed syntax errors in Ballot
parent
d0ef1cdd
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Ballot.sol
+56
-31
56 additions, 31 deletions
src/Ballot.sol
with
56 additions
and
31 deletions
src/Ballot.sol
+
56
−
31
View file @
a84e4f89
...
...
@@ -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
.00000
000001
// specify 50000001
// TWO-THIRDS:
// specify 67
.77777
777777
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 =>
u
int ) 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;
u
int 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];
u
int 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.
proposal
s
[ uint(formervote) ].voteCount--;
proposal
List
[ uint(formervote) ].voteCount--;
}
proposal
s
[ proposal
].voteCount++;
voterList[ u
int(
idx
)
].vote =
u
int(proposal);
proposal
List
[ proposal ].voteCount++;
voterList[ uidx ].vote
= int(proposal);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment