You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
4.8 KiB
144 lines
4.8 KiB
7 years ago
|
pragma solidity ^0.4.0;
|
||
|
|
||
8 years ago
|
/// @title Voting with delegation.
|
||
|
contract Ballot {
|
||
|
// This declares a new complex type which will
|
||
|
// be used for variables later.
|
||
|
// It will represent a single voter.
|
||
7 years ago
|
struct Voter {
|
||
8 years ago
|
uint weight; // weight is accumulated by delegation
|
||
|
bool voted; // if true, that person already voted
|
||
|
address delegate; // person delegated to
|
||
|
uint vote; // index of the voted proposal
|
||
7 years ago
|
}
|
||
8 years ago
|
|
||
|
// This is a type for a single proposal.
|
||
5 years ago
|
struct Proposal {
|
||
8 years ago
|
bytes32 name; // short name (up to 32 bytes)
|
||
|
uint voteCount; // number of accumulated votes
|
||
7 years ago
|
}
|
||
|
|
||
8 years ago
|
address public chairperson;
|
||
7 years ago
|
|
||
8 years ago
|
// This declares a state variable that
|
||
8 years ago
|
// stores a \`Voter\` struct for each possible address.
|
||
8 years ago
|
mapping(address => Voter) public voters;
|
||
|
|
||
8 years ago
|
// A dynamically-sized array of \`Proposal\` structs.
|
||
8 years ago
|
Proposal[] public proposals;
|
||
|
|
||
8 years ago
|
/// Create a new ballot to choose one of \`proposalNames\`.
|
||
8 years ago
|
function Ballot(bytes32[] proposalNames) {
|
||
7 years ago
|
chairperson = msg.sender;
|
||
|
voters[chairperson].weight = 1;
|
||
8 years ago
|
|
||
|
// For each of the provided proposal names,
|
||
|
// create a new proposal object and add it
|
||
|
// to the end of the array.
|
||
|
for (uint i = 0; i < proposalNames.length; i++) {
|
||
8 years ago
|
// \`Proposal({...})\` creates a temporary
|
||
|
// Proposal object and \`proposals.push(...)\`
|
||
|
// appends it to the end of \`proposals\`.
|
||
8 years ago
|
proposals.push(Proposal({
|
||
|
name: proposalNames[i],
|
||
|
voteCount: 0
|
||
|
}));
|
||
|
}
|
||
7 years ago
|
}
|
||
|
|
||
8 years ago
|
// Give \`voter\` the right to vote on this ballot.
|
||
|
// May only be called by \`chairperson\`.
|
||
7 years ago
|
function giveRightToVote(address voter) {
|
||
8 years ago
|
if (msg.sender != chairperson || voters[voter].voted) {
|
||
8 years ago
|
// \`throw\` terminates and reverts all changes to
|
||
8 years ago
|
// the state and to Ether balances. It is often
|
||
|
// a good idea to use this if functions are
|
||
|
// called incorrectly. But watch out, this
|
||
|
// will also consume all provided gas.
|
||
|
throw;
|
||
|
}
|
||
7 years ago
|
voters[voter].weight = 1;
|
||
|
}
|
||
|
|
||
8 years ago
|
/// Delegate your vote to the voter \`to\`.
|
||
7 years ago
|
function delegate(address to) {
|
||
8 years ago
|
// assigns reference
|
||
|
Voter sender = voters[msg.sender];
|
||
|
if (sender.voted)
|
||
|
throw;
|
||
|
|
||
|
// Forward the delegation as long as
|
||
8 years ago
|
// \`to\` also delegated.
|
||
8 years ago
|
// In general, such loops are very dangerous,
|
||
|
// because if they run too long, they might
|
||
|
// need more gas than is available in a block.
|
||
|
// In this case, the delegation will not be executed,
|
||
|
// but in other situations, such loops might
|
||
|
// cause a contract to get "stuck" completely.
|
||
|
while (
|
||
|
voters[to].delegate != address(0) &&
|
||
|
voters[to].delegate != msg.sender
|
||
|
) {
|
||
7 years ago
|
to = voters[to].delegate;
|
||
8 years ago
|
}
|
||
|
|
||
|
// We found a loop in the delegation, not allowed.
|
||
|
if (to == msg.sender) {
|
||
|
throw;
|
||
|
}
|
||
|
|
||
8 years ago
|
// Since \`sender\` is a reference, this
|
||
|
// modifies \`voters[msg.sender].voted\`
|
||
7 years ago
|
sender.voted = true;
|
||
|
sender.delegate = to;
|
||
|
Voter delegate = voters[to];
|
||
8 years ago
|
if (delegate.voted) {
|
||
|
// If the delegate already voted,
|
||
|
// directly add to the number of votes
|
||
7 years ago
|
proposals[delegate.vote].voteCount += sender.weight;
|
||
8 years ago
|
} else {
|
||
|
// If the delegate did not vote yet,
|
||
|
// add to her weight.
|
||
7 years ago
|
delegate.weight += sender.weight;
|
||
8 years ago
|
}
|
||
7 years ago
|
}
|
||
|
|
||
8 years ago
|
/// Give your vote (including votes delegated to you)
|
||
8 years ago
|
/// to proposal \`proposals[proposal].name\`.
|
||
8 years ago
|
function vote(uint proposal) {
|
||
7 years ago
|
Voter sender = voters[msg.sender];
|
||
8 years ago
|
if (sender.voted)
|
||
|
throw;
|
||
7 years ago
|
sender.voted = true;
|
||
|
sender.vote = proposal;
|
||
8 years ago
|
|
||
8 years ago
|
// If \`proposal\` is out of the range of the array,
|
||
8 years ago
|
// this will throw automatically and revert all
|
||
|
// changes.
|
||
7 years ago
|
proposals[proposal].voteCount += sender.weight;
|
||
|
}
|
||
|
|
||
8 years ago
|
/// @dev Computes the winning proposal taking all
|
||
|
/// previous votes into account.
|
||
|
function winningProposal() constant
|
||
|
returns (uint winningProposal)
|
||
|
{
|
||
|
uint winningVoteCount = 0;
|
||
|
for (uint p = 0; p < proposals.length; p++) {
|
||
|
if (proposals[p].voteCount > winningVoteCount) {
|
||
|
winningVoteCount = proposals[p].voteCount;
|
||
|
winningProposal = p;
|
||
7 years ago
|
}
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// Calls winningProposal() function to get the index
|
||
|
// of the winner contained in the proposals array and then
|
||
|
// returns the name of the winner
|
||
|
function winnerName() constant
|
||
|
returns (bytes32 winnerName)
|
||
|
{
|
||
|
winnerName = proposals[winningProposal()].name;
|
||
7 years ago
|
}
|
||
|
}
|