实现一个电子投票系统,基于智能合约该电子投票系统的一个主要问题是如何分配合理的权限给正确的人,并且要防止篡改。这个例子不能解决所有问题,但是实现了如何去委托投票,整个投票计数过程是自动且完全透明的。

功能上首先要为投票设立一个简称创建一个合约,发起者作为主席来给每一个独立的地址分配权限。每一个参与者可以自己投票或者委托给信任的人。程序最后会返回得票数最多的那个提议。

程序构造了voter和candidate两种结构,在voter中含有投票者的地址(address),是否完成投票(bool)和票数(uint)(本来想完成委托投票的),candidate中则含有地址(address),是否获胜(bool)和得到票数(uint),下一步是完成投票人和候选人的初始化,函数vote则是完成投票的过程,其中值得注意的是应该排除无效的候选人,最后则是返回胜利的地址选出投票的胜利者。

pragma solidity ^0.4.22;/// @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.struct Voter {uint weight; // weight is accumulated by delegationbool voted;  // if true, that person already votedaddress delegate; // person delegated touint vote;   // index of the voted proposal}// This is a type for a single proposal.struct Proposal {bytes32 name;   // short name (up to 32 bytes)uint voteCount; // number of accumulated votes}
address public chairperson;// This declares a state variable that
// stores a `Voter` struct for each possible address.
mapping(address => Voter) public voters;// A dynamically-sized array of `Proposal` structs.
Proposal[] public proposals;/// Create a new ballot to choose one of `proposalNames`.
constructor(bytes32[] memory proposalNames) public {chairperson = msg.sender;voters[chairperson].weight = 1;// 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++) {// `Proposal({...})` creates a temporary// Proposal object and `proposals.push(...)`// appends it to the end of `proposals`.proposals.push(Proposal({name: proposalNames[i],voteCount: 0}));}
}// Give `voter` the right to vote on this ballot.
// May only be called by `chairperson`.
function giveRightToVote(address voter) public {// If the first argument of `require` evaluates// to `false`, execution terminates and all// changes to the state and to Ether balances// are reverted.// This used to consume all gas in old EVM versions, but// not anymore.// It is often a good idea to use `require` to check if// functions are called correctly.// As a second argument, you can also provide an// explanation about what went wrong.require(msg.sender == chairperson,"Only chairperson can give right to vote.");require(!voters[voter].voted,"The voter already voted.");require(voters[voter].weight == 0);voters[voter].weight = 1;
}/// Delegate your vote to the voter `to`.
function delegate(address to) public {// assigns referenceVoter storage sender = voters[msg.sender];require(!sender.voted, "You already voted.");require(to != msg.sender, "Self-delegation is disallowed.");// Forward the delegation as long as// `to` also delegated.// 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)) {to = voters[to].delegate;// We found a loop in the delegation, not allowed.require(to != msg.sender, "Found loop in delegation.");}// Since `sender` is a reference, this// modifies `voters[msg.sender].voted`sender.voted = true;sender.delegate = to;Voter storage delegate_ = voters[to];if (delegate_.voted) {// If the delegate already voted,// directly add to the number of votesproposals[delegate_.vote].voteCount += sender.weight;} else {// If the delegate did not vote yet,// add to her weight.delegate_.weight += sender.weight;}
}/// Give your vote (including votes delegated to you)
/// to proposal `proposals[proposal].name`.
function vote(uint proposal) public {Voter storage sender = voters[msg.sender];require(!sender.voted, "Already voted.");sender.voted = true;sender.vote = proposal;// If `proposal` is out of the range of the array,// this will throw automatically and revert all// changes.proposals[proposal].voteCount += sender.weight;
}/// @dev Computes the winning proposal taking all
/// previous votes into account.
function winningProposal() public viewreturns (uint winningProposal_)
{uint winningVoteCount = 0;for (uint p = 0; p < proposals.length; p++) {if (proposals[p].voteCount > winningVoteCount) {winningVoteCount = proposals[p].voteCount;winningProposal_ = p;}}
}// 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() public viewreturns (bytes32 winnerName_)
{winnerName_ = proposals[winningProposal()].name;
}

}

以太坊智能合约solidity去中心化投票系统相关推荐

  1. 以太坊智能合约部署——一个简单的投票系统

    首先在Remix上进行测试 代码如下 pragma solidity ^0.4.16;/// @title Voting with delegation. contract Ballot {// Th ...

  2. 精通以太坊:开发智能合约和去中心化应用

    为什么微软.纳斯达克以及其他数百个组织都在尝试以太坊? 因为以太坊打开了通向去中心化计算的大门,在这个平台上,我们可以运行去中心化的应用程序(DApp)和智能合约.以太坊是WEB 3.0 和 NFT的 ...

  3. 区块链入门文章二《以太坊:下一代智能合约和去中心化应用平台》

    以太坊:下一代智能合约和去中心化应用平台 以太坊基金会 著 李志阔(网名:面神护法) 赵海涛 焦锋 译 中本聪2009年发明的比特币经常被视作货币和通货领域内一次激进的发展,这种激进首先表现为一种没有 ...

  4. 附录2 以太坊:下一代智能合约和去中心化应用平台(选译)

    以太坊基金会 著 李志阔(网名:面神护法) 赵海涛 焦锋 译 中本聪2009年发明的比特币经常被视作货币和通货领域内一次激进的发展,这种激进首先表现为一种没有资产担保或内生价值[1],也没有中央发行者 ...

  5. 以太坊:下一代智能合约和去中心化应用平台

    以太坊基金会 著 李志阔(网名:面神护法) 赵海涛 焦锋 译 中本聪2009年发明的比特币经常被视作货币和通货领域内一次激进的发展,这种激进首先表现为一种没有资产担保或内生价值[1],也没有中央发行者 ...

  6. 以太坊区块链实现去中心化购物功能

    在当今的中国,网上购物已经成为了我们不可或缺的一部分,通过电商网站查看商品,下单购物,支付,付款到支付宝,买家收货确认后,货款自动打入卖家的账户,这些购物的体验多数人每天都可能发生.大家都知道,淘宝的 ...

  7. 币图网以太坊开发实例_去中心化概念模型与架构设计

    IM 去中心化概念模型与架构设计 今天打算写写关于 IM 去中心化涉及的架构模型变化和设计思路,去中心化的概念就是说用户的访问不是集中在一个数据中心,这里的去中心是针对数据中心而言的. 站在这个角度而 ...

  8. 3步! 老司机教你如何在以太坊上构建基于Token去中心化投票系统!

    作者 | Doug Crescenzi 译者 | 王柯凝 出品 | CSDN.区块链大本营 如果想在以太坊平台上构建一个去中心化的自治系统,其实有很多种不同的方法可供你选择.其中,最常用的方法之一就是 ...

  9. 白皮书 | 以太坊 (Ethereum ):下一代智能合约和去中心化应用平台

    当中本聪在2009年1月启动比特币区块链时,他同时向世界引入了两种未经测试的革命性的新概念.第一种就是比特币(bitcoin),一种去中心化的点对点的网上货币,在没有任何资产担保.内在价值或者中心发行 ...

最新文章

  1. Ubuntu软件包管理相关部分命令
  2. servlet中的几个路径有关的方法
  3. OpenCV计算机视觉编程之三种图像像素的遍历方法
  4. easyScholar——文献数据库插件
  5. 【REACT NATIVE 系列教程之十二】REACT NATIVE(JS/ES)与IOS(OBJECT-C)交互通信
  6. Chrome插件(扩展)
  7. window环境下安装Python2和Python3
  8. 登录窗体与主窗体的关闭
  9. 【KDD20】深度图神经网络专题
  10. 机器学习面试-模型融合和提升的算法
  11. php设计模式 -- 迭代器模式
  12. 一键安装Tengine服务器,TengineRPM(LTMP)构建高效、稳定、安全、易用的Web平台
  13. 程序员简洁简历模板分享
  14. windows7 上搭建NFS服务器--haneWIN
  15. 定义一个list对象数组 java_javascript定义一个list
  16. 项目选题报告答辩总结——日不落战队
  17. 非线性最小二乘求解方法详解
  18. 五线谱软件测试初学者,学习五线谱(初学者专用).pdf
  19. 麻省理工学院公开课:MBA金融学、字幕、去时间轴版
  20. 大功率双路直流电机驱动板的设计源文件

热门文章

  1. 5G超级上行【5G技术篇,转自微信公众号网优雇佣军】
  2. centos的wget无法解析域名
  3. tomcat跨域请求
  4. AutoCAD:The miracle created by Autodesk
  5. 织梦网站在空间怎么转服务器,织梦网站换空间
  6. Answer Set Programming 回答集编程
  7. 未来,大数据行业工资会断崖式下滑吗?
  8. PPT:立体仓库详解及出入库效率计算
  9. 项目管理之-项目评估
  10. 《大话数据结构》笔记——第8章 查找(四)