-
Notifications
You must be signed in to change notification settings - Fork 1
/
distributed.sol
65 lines (52 loc) · 1.7 KB
/
distributed.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
pragma solidity ^0.4.0;
contract Election {
address electionAuthority;
uint electionEndTime;
address[] candidates; // Registered candidates
mapping (address => uint) STVs; // Candidate address to number of single transferable votes
mapping (address => bool) voters; //Registered voters
mapping (address => mapping (address => bool) ) hasVoted; //If a registered voter has voted or not
function Election() {
electionAuthority = msg.sender;
}
modifier onlyElectionAuthority() {
require(msg.sender == electionAuthority);
_;
}
modifier onlyRegisteredVoters() {
require(voters[msg.sender]);
_;
}
modifier voteOnlyOnce(address _candidate) {
require(!hasVoted[msg.sender][_candidate]);
_;
}
modifier onlyDuringElectionTime() {
require(electionEndTime != 0 || electionEndTime < block.timestamp); // 0 can be replaced by the minimum duration of election
_;
}
modifier onlyAfterElectionTime() {
require(electionEndTime != 0 || electionEndTime > block.timestamp);
_;
}
function startElection(uint duration) onlyElectionAuthority {
electionEndTime = block.timestamp + duration;
}
function registerCandidate (address _candidate) onlyElectionAuthority {
candidates.push(_candidate);
}
function registerVoter(address voter) onlyElectionAuthority {
voters[voter] = true;
}
function vote(address _candidate) onlyRegisteredVoters voteOnlyOnce(_candidate) onlyDuringElectionTime {
STVs[_candidate] += 1;
hasVoted[msg.sender][_candidate] = true;
}
function getNumberOfCandidates() constant returns(uint) {
return candidates.length;
}
function getCandidate(uint i) constant returns(address _candidate, uint _STVs) {
_candidate = candidates[i];
_STVs = STVs[_candidate];
}
}