pragma solidity ^0.4.0;
contract Ballot {

struct Voter {
uint weight;
bool voted;
uint8 vote;
address delegate;
}
struct Proposal {
uint voteCount;
}

address chairperson;
mapping(address => Voter) voters;
Proposal[] proposals;

/// Create a new ballot with $(_numProposals) different proposals.
function Ballot(uint8 _numProposals) public {
chairperson = msg.sender;
voters[chairperson].weight = 1;
proposals.length = _numProposals;
}

/// Give $(toVoter) the right to vote on this ballot.
/// May only be called by $(chairperson).
function giveRightToVote(address toVoter) public {
if (msg.sender != chairperson || voters[toVoter].voted) return;
voters[toVoter].weight = 1;
}

/// Delegate your vote to the voter $(to).
function delegate(address to) public {
Voter storage sender = voters[msg.sender]; // assigns reference
if (sender.voted) return;
while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender)
to = voters[to].delegate;
if (to == msg.sender) return;
sender.voted = true;
sender.delegate = to;
Voter storage delegateTo = voters[to];
if (delegateTo.voted)
proposals[delegateTo.vote].voteCount += sender.weight;
else
delegateTo.weight += sender.weight;
}

/// Give a single vote to proposal $(toProposal).
function vote(uint8 toProposal) public {
Voter storage sender = voters[msg.sender];
if (sender.voted || toProposal >= proposals.length) return;
sender.voted = true;
sender.vote = toProposal;
proposals[toProposal].voteCount += sender.weight;
}

function winningProposal() public constant returns (uint8 _winningProposal) {
uint256 winningVoteCount = 0;
for (uint8 prop = 0; prop < proposals.length; prop++)
if (proposals[prop].voteCount > winningVoteCount) {
winningVoteCount = proposals[prop].voteCount;
_winningProposal = prop;
}
}
}

ethcode的更多相关文章

随机推荐

  1. RocketMQ详解

    原文链接:http://www.cnblogs.com/xiaodf/p/5075167.html 简介 官方简介: RocketMQ是一款分布式.队列模型的消息中间件,具有以下特点:  能够保证严格 ...

  2. C#通过反射执行C#dll所有函数

    C# 反射(Reflection) 反射指程序可以访问.检测和修改它本身状态或行为的一种能力. 程序集包含模块,而模块包含类型,类型又包含成员.反射则提供了封装程序集.模块和类型的对象. 您可以使用反 ...

  3. C# string 特殊的引用类型

    .Net 框架程序设计(修订版)中有这样一段描述:String类型直接继承自Object,这使得它成为一个引用类型,也就是说线程上的堆栈上不会驻留有任何字符串.(译注:注意这里的“直接继承”.直接继承 ...

  4. JWT的介绍解析

    JWT的介绍解析 一.什么是JWT?了解JWT,认知JWT 首先jwt其实是三个英语单词JSON Web Token的缩写.通过全名你可能就有一个基本的认知了.token一般都是用来认证的,比如我们系 ...

  5. Spring Boot遇到的某些问题

    Spring Boot遇到的某些问题 1.关于templates的html包格式问题: <properties> <project.build.sourceEncoding>U ...

  6. 第1章—Spring之旅—容纳你的Bean

    容纳你的Bean 在基于Spring的应用中,你的应用对象生存于Spring容器中.Spring负责创建对象,装配他们,配置他们并管理他们整个生命周期,从生存到死亡(在这里 可能是new 到 fina ...

  7. 配置Linux本地源镜像

    今天看到同事做了一个公司本地的apache镜像源,感觉好叼的样子.然后就自己上网找些资料,尝试自己搭建一套出来.然后就有了这篇博文... 声明:本文中充满了浓浓的技术嫉妒的心理,阅读需谨慎. 本文以 ...

  8. Hibernate5.1+Sqlserver2000分页查询

    前几天改到一个bug:从MS SQLserver上面同步表结构并且采集数据写入其他库.然后用的核心技术是用的Hibernate. 其中bug出在SQLServer2000版本上.排查下来发现2000版 ...

  9. Java 中 String 的常用方法(二)

    本文介绍剩下的一些常用的 String 中的方法. 1.replace 方法 .replaceFirst 方法和 replaceAll 方法 replace(char oldChar, char ne ...

  10. 【转】Emgu CV on C# (五) —— Emgu CV on 局部自适应阈值二值化

    局部自适应阈值二值化 相对全局阈值二值化,自然就有局部自适应阈值二值化,本文利用Emgu CV实现局部自适应阈值二值化算法,并通过调节block大小,实现图像的边缘检测. 一.理论概述(转载自< ...