-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVotingSys.sol
More file actions
128 lines (111 loc) · 4.89 KB
/
VotingSys.sol
File metadata and controls
128 lines (111 loc) · 4.89 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Election {
struct Candidate {
string name;
uint age;
bool isAlive;
uint netWorth; // Total net worth of the candidate
bool hasBadCriminalRecord;
uint numVotes;
}
struct Voter {
string name;
uint age;
bool isAlive;
bool isAuthorized;
bool hasVoted;
bool verifiedByZKP;
uint vote;
uint netWorth;
bool hasBadCriminalRecord;
bool runningForElect;
}
bool isFinished = false;
uint256 private t1;
uint256 private t2;
address private owner;
string public electionName;
Candidate public winner;
mapping(address => Voter) public voters;
Candidate[] public candidates;
uint public totalVotes;
mapping(uint => bool) private usedZKPs; // Mapping to track used ZKPs
modifier onlyOwner() {
require(msg.sender == owner, "Only the owner can call this function.");
_;
}
constructor(string memory _name) {
owner = msg.sender;
electionName = _name;
t1 = block.timestamp;
winner.name = "Election is under process!";
}
function runForElection() public {
require(!isFinished,"Election has ended");
require(voters[msg.sender].age >= 25, "Candidate must be at least 25 years old.");
require(voters[msg.sender].isAlive, "Deceased individuals are not allowed to become candidates.");
require(!voters[msg.sender].hasBadCriminalRecord, "Candidates with bad criminal records are not allowed.");
require(voters[msg.sender].isAuthorized, "You are not allowed to run for the election.");
candidates.push(Candidate(voters[msg.sender].name, voters[msg.sender].age, voters[msg.sender].isAlive, voters[msg.sender].netWorth, voters[msg.sender].hasBadCriminalRecord, 0));
}
function numOfCandidates() public view returns(uint) {
return candidates.length;
}
function validateZKP(uint zkp) pure private returns (bool) {
if (zkp > 0) return true;
return false;
}
function authorizeUser(string memory _name, uint _age, bool _isAlive, bool _hasBadCriminalRecord, uint netWorth, uint zkp) public {
require(!isFinished,"Election has ended");
require(_age >= 18, "Voter must be at least 18 years old.");
require(_age < 150, "Voter must be alive.");
require(!_hasBadCriminalRecord, "Voters with bad criminal records are not allowed.");
require(!voters[msg.sender].isAuthorized, "You have already been registered as a voter.");
require(!usedZKPs[zkp], "This ZKP has already been used."); // Check if the ZKP has been used before
require(validateZKP(zkp), "You are not an authorised Indian citizen.");
require(!voters[msg.sender].runningForElect,"You have already been registered for election");
address voterAddress = msg.sender;
voters[voterAddress] = Voter(_name, _age, _isAlive, true, false, true, 9999, netWorth, _hasBadCriminalRecord,true);
usedZKPs[zkp] = true; // Mark the ZKP as used
}
function vote(uint _voteIndex) public {
require(!isFinished,"Election has ended");
require(!voters[msg.sender].hasVoted, "You have already voted.");
require(voters[msg.sender].isAuthorized, "You are not authorized to vote.");
require(voters[msg.sender].age >= 18, "You must be at least 18 years old to vote.");
require(voters[msg.sender].isAlive, "Deceased individuals are not allowed to vote.");
require(voters[msg.sender].verifiedByZKP, "Voter data must be verified with Aadhar database.");
voters[msg.sender].vote = _voteIndex;
voters[msg.sender].hasVoted = true;
candidates[_voteIndex].numVotes += 1;
totalVotes += 1;
}
function winningCandidate() private view returns (uint _winningCandidate) {
uint winningVoteCount = 0;
uint winningCandidateIndex = 9999;
for (uint i = 0; i < numOfCandidates(); i++) {
if (candidates[i].numVotes > winningVoteCount) {
winningVoteCount = candidates[i].numVotes;
winningCandidateIndex = i;
} else if (candidates[i].numVotes == winningVoteCount && i != winningCandidateIndex) {
winningCandidateIndex = 9999;
}
}
_winningCandidate = winningCandidateIndex;
// Candidate[_winningCandidate].name;
}
uint256 public TotalTimeElapsed;
function endElection() public onlyOwner {
require(!isFinished,"Election has already ended");
uint winningCandidateIndex = winningCandidate();
t2 = block.timestamp;
TotalTimeElapsed = t2 - t1;
if (winningCandidateIndex == 9999) {
winner.name = "It's a tie!";
} else {
winner = candidates[winningCandidateIndex];
}
isFinished = true;
}
}