-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVotingPlus.sol
More file actions
248 lines (201 loc) · 8.7 KB
/
VotingPlus.sol
File metadata and controls
248 lines (201 loc) · 8.7 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
import "@openzeppelin/contracts/access/Ownable.sol";
contract VotingPlus is Ownable {
uint256 winningProposalId;
address[] registeredVoters;
Proposal[] proposals;
WorkflowStatus public currentStatus;
mapping(address => Voter) whitelist;
VoteRecord[] votes;
uint constant MIN_DESCRIPTION_LENGTH = 5;
uint constant MAX_DESCRIPTION_LENGTH = 100;
struct Voter {
bool isRegistered;
bool hasVoted;
uint votedProposalId;
}
struct Proposal {
string description;
uint256 voteCount;
bool isBlankVote;
}
struct VoteRecord {
address voter;
uint proposalId;
}
enum WorkflowStatus {
RegisteringVoters,
ProposalsRegistrationStarted,
ProposalsRegistrationEnded,
VotingSessionStarted,
VotingSessionEnded,
VotesTallied
}
event VoterRegistered(address voterAddress);
event WorkflowStatusChange(WorkflowStatus previousStatus, WorkflowStatus newStatus);
event ProposalRegistered(uint proposalId);
event Voted(address voter, uint proposalId);
event AllVotesAreBlank();
event TieDetected(uint256[] tiedProposalIds);
event TieResolved(uint256 winningProposalId);
modifier onlyVoters() {
require(whitelist[msg.sender].isRegistered, "Voter not registered or not allowed");
_;
}
constructor() Ownable(msg.sender) {
_registerVoter(msg.sender);
_addBlankVoteProposal();
}
function registerVoter(address _voter) external onlyOwner {
_registerVoter(_voter);
}
function _registerVoter(address _voter) private {
require(currentStatus == WorkflowStatus.RegisteringVoters, "Registration phase is not active.");
require(!whitelist[_voter].isRegistered, "Voter is already registered.");
whitelist[_voter] = Voter({
isRegistered: true,
hasVoted: false,
votedProposalId: 0
});
registeredVoters.push(_voter);
emit VoterRegistered(_voter);
}
function getWhitelist() external view onlyVoters returns (address[] memory) {
return registeredVoters;
}
function _addBlankVoteProposal() private {
proposals.push(Proposal({
description: "Blank vote",
voteCount: 0,
isBlankVote: true
}));
}
function startProposalsRegistration() external onlyOwner {
require(currentStatus == WorkflowStatus.RegisteringVoters, "Voter registration not completed.");
currentStatus = WorkflowStatus.ProposalsRegistrationStarted;
emit WorkflowStatusChange(WorkflowStatus.RegisteringVoters, WorkflowStatus.ProposalsRegistrationStarted);
}
function registerProposal(string memory proposalDescription) external onlyVoters {
require(currentStatus == WorkflowStatus.ProposalsRegistrationStarted, "Proposal registration not active");
require(bytes(proposalDescription).length >= MIN_DESCRIPTION_LENGTH && bytes(proposalDescription).length <= MAX_DESCRIPTION_LENGTH, "Invalid description length");
proposals.push(Proposal({
description: proposalDescription,
voteCount: 0,
isBlankVote: false
}));
emit ProposalRegistered(proposals.length - 1);
}
function getProposals() external view onlyVoters returns (Proposal[] memory) {
require(currentStatus >= WorkflowStatus.ProposalsRegistrationStarted, "Proposals session has not started yet.");
return proposals;
}
function endProposalsRegistration() external onlyOwner {
require(currentStatus == WorkflowStatus.ProposalsRegistrationStarted, "Cannot end proposals registration: it has not started yet.");
currentStatus = WorkflowStatus.ProposalsRegistrationEnded;
emit WorkflowStatusChange(WorkflowStatus.ProposalsRegistrationStarted, WorkflowStatus.ProposalsRegistrationEnded);
}
function startVotingSession() external onlyOwner {
require(currentStatus == WorkflowStatus.ProposalsRegistrationEnded, "Proposals registration not ended.");
currentStatus = WorkflowStatus.VotingSessionStarted;
emit WorkflowStatusChange(WorkflowStatus.ProposalsRegistrationEnded, WorkflowStatus.VotingSessionStarted);
}
function vote(uint proposalId) external onlyVoters {
require(currentStatus == WorkflowStatus.VotingSessionStarted,"Voting session not active");
require(!whitelist[msg.sender].hasVoted, "Voter has already voted");
require(proposalId < proposals.length, "Invalid proposal ID");
whitelist[msg.sender].hasVoted = true;
whitelist[msg.sender].votedProposalId = proposalId;
_recordVote(msg.sender, proposalId);
}
function endVotingSession() external onlyOwner {
require(currentStatus == WorkflowStatus.VotingSessionStarted, "Voting session not started");
currentStatus = WorkflowStatus.VotingSessionEnded;
emit WorkflowStatusChange(WorkflowStatus.VotingSessionStarted, WorkflowStatus.VotingSessionEnded);
}
function _recordVote(address voter, uint proposalId) private {
proposals[proposalId].voteCount++;
votes.push(VoteRecord({
voter: voter,
proposalId: proposalId
}));
emit Voted(voter, proposalId);
}
function getVoteHistory() external view onlyVoters returns (VoteRecord[] memory) {
require(currentStatus >= WorkflowStatus.VotingSessionStarted, "Voting has not yet started");
return votes;
}
function tallyVotes() external onlyOwner {
require(currentStatus == WorkflowStatus.VotingSessionEnded, "Voting session not ended");
(uint256 highestVoteCount, uint256 winningProposalIndex, bool isTie) = _calculateHighestVotesAndCheckTie();
if (highestVoteCount == 0) {
emit AllVotesAreBlank();
} else if (isTie) {
_handleTieCase(highestVoteCount);
} else {
winningProposalId = winningProposalIndex;
currentStatus = WorkflowStatus.VotesTallied;
emit WorkflowStatusChange(WorkflowStatus.VotingSessionEnded, WorkflowStatus.VotesTallied);
}
function _calculateHighestVotesAndCheckTie() private view returns (uint256 highestVoteCount, uint256 winningProposalIndex, bool isTie) {
highestVoteCount = 0;
winningProposalIndex = 0;
isTie = false;
for (uint i = 1; i < proposals.length; i++) {
if (proposals[i].voteCount > highestVoteCount) {
highestVoteCount = proposals[i].voteCount;
winningProposalIndex = i;
isTie = false;
} else if (proposals[i].voteCount == highestVoteCount) {
isTie = true;
}
}
return (highestVoteCount, winningProposalIndex, isTie);
}
function _determineWinner(bool isTie, uint256 highestVoteCount) private {
if (isTie) {
_handleTieCase(highestVoteCount);
} else {
winningProposalId = _findWinningProposal(highestVoteCount);
}
}
function _handleTieCase(uint256 highestVoteCount) private {
uint256[] memory tiedProposals = _findTiedProposals(highestVoteCount);
emit TieDetected(tiedProposals);
winningProposalId = _resolveTie(tiedProposals);
emit TieResolved(winningProposalId);
}
function _findTiedProposals(uint256 highestVoteCount) private view returns (uint256[] memory) {
uint256 tieCount = 0;
for (uint i = 1; i < proposals.length; i++) {
if (proposals[i].voteCount == highestVoteCount) {
tieCount++;
}
}
uint256[] memory tiedProposals = new uint256[](tieCount);
uint256 index = 0;
for (uint i = 1; i < proposals.length; i++) {
if (proposals[i].voteCount == highestVoteCount) {
tiedProposals[index] = i;
index++;
}
}
return tiedProposals;
}
function _resolveTie(uint256[] memory tiedProposals) private view returns (uint256) {
uint256 randomIndex = uint256(keccak256(abi.encodePacked(block.timestamp, block.prevrandao))) % tiedProposals.length;
return tiedProposals[randomIndex];
}
function _findWinningProposal(uint256 highestVoteCount) private view returns (uint256 winningProposalIndex) {
for (uint i = 1; i < proposals.length; i++) {
if (proposals[i].voteCount == highestVoteCount) {
return i;
}
}
return 0;
}
function getWinner() external view returns(uint256) {
require(currentStatus == WorkflowStatus.VotesTallied, "Votes not tallied yet");
return winningProposalId;
}
}