Last active
November 5, 2018 01:59
-
-
Save atvanguard/46ac6b02533d26bf4ddb059aeb597bcd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
event Winner(int proposal, int votes); | |
function findWinningProposal() public returns(int, int) { | |
uint numVoters = validVoters.length; | |
bool[] memory visited = new bool[](numVoters); | |
int maxVotes = -1; | |
int winningProposal = -1; | |
// loop through the validVoters array | |
for (uint i = 0; i < numVoters; i++) { | |
if (visited[i]) continue; | |
uint voter = i; // start processing the voter | |
// accumulate total voting power | |
uint totalVotes = 1; | |
visited[voter] = true; | |
// traverse the delegation chain until we reach a vanilla voter - add the voting power and mark voters as visited | |
while(voters[validVoters[voter]].state == State.Delegated) { | |
voter = voters[validVoters[voter]].delegatedTo; | |
if (!visited[voter]) { | |
totalVotes += 1; | |
visited[voter] = true; | |
} | |
} | |
// reached a voter who has voted or hasn't participated in voting/delegation | |
if (voters[validVoters[voter]].state == State.Voted) { | |
Proposal storage proposal = proposals[voters[validVoters[voter]].vote]; | |
proposal.numVotes += totalVotes; | |
if (maxVotes < int(proposal.numVotes)) { | |
maxVotes = int(proposal.numVotes); | |
winningProposal = int(voters[validVoters[voter]].vote); | |
} | |
} | |
} | |
emit Winner(winningProposal, maxVotes); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment