Hi Guest

More contents, please log on!

Bitmere.com 区块链技术 Content

solidity投票智能合约代码

BlockQ
76 0 0
    solidity投票智能合约代码,电子投票功能要解决的主要问题是如果分配投票权以及如何避免数据被篡改。本篇实现的合约思路是对于每次投票表决都创建一个合约,合约的创建者就是投票委员会的主席,可以给不同的账户投票的权利。拥有投票权的账户可以自己投票也可以委托给他所信任的人代理投票。

  1. pragma solidity >=0.7.0 <0.9.0;

  2. //投票实验
  3. contract Ballot{
  4.     struct Voter{
  5.         uint weight;//投票(单票)权重
  6.         bool voted;//是否投过票,true为投过票,bool类型默认值为false
  7.         address delegate;//想要委托投票的节点地址,address默认值为0x0
  8.         uint vote;//想要投票的节点的索引值(被投票节点信息用一维数组proposals存储)
  9.     }
  10.      
  11.     struct Proposal{//被投票节点的相关参数
  12.         bytes32 name;//被投票节点的姓名标识
  13.         uint voteCount;//累积被投票数
  14.     }

  15.     address public chairperson;//投票管理员地址
  16.     mapping(address => Voter) public voters;//地址对投票节点的信息的映射
  17.     Proposal[] public proposals;//一维数组存储被投票节点信息

  18.     //构造方法、构造函数
  19.     //solidity和其他语言不一样,构造函数仅在部署合约时调用一次,后续调用合约不调用其构造函数
  20.     //且一个合约只能有一个构造函数,不能进行构造函数重载
  21.     constructor(bytes32[] proposalNames) public{
  22.         chairperson = msg.sender;//将第一次调用该合约的节点设置为管理员
  23.         voters[chairperson].weight = 1;//将管理员投票权置为1

  24.         for(uint i=0; i<proposalNames.length; i++){
  25.             //将所有被投票人姓名初始化进一维数组proposals,并将其对应票数初始化为0票
  26.             //.push(),括号中内容需要强调数据类型,eg:arr.push(uint(6));
  27.             proposals.push(Proposal({
  28.                 name:proposalNames[i],
  29.                 voteCount:0
  30.             }));
  31.         }
  32.     }

  33.     //由管理员授权可投票节点
  34.     function giveRightToVote(address voter) public{
  35.         //require中判断条件为false时,输出字符串"xxx...",异常会被抛出,程序执行会被挂起,
  36.         //未消耗的gas会被退回,合约状态会回退到初始状态
  37.         require(
  38.             msg.sender == chairperson,"Only chairperson can give right to vote."
  39.         );//执行此function的节点一定为管理员节点
  40.         require(
  41.             !voters[voter].voted,"The voter already voted."
  42.         );//若voter没投过票
  43.         require(voters[voter].weight == 0);
  44.         //调用合约的人是管理员、待授权节点还没投过票、带授权节点投票权重为0时,进行授权
  45.         voters[voter].weight = 1;//上述三个require()均成立时,授权票数
  46.     }

  47.     //投票授权
  48.     function delegate(address to) public{
  49.         Voter storage sender = voters[msg.sender];
  50.         require(!sender.voted, "You already voted.");
  51.         require(to != msg.sender,"Self-delegation is disallowed.");
  52.         //sender满足的条件:要有投票权限、没有投过票、被授权节点不是自己

  53.         //判断代理节点地址是否为空:address(0)或者address(0x0)
  54.         while(voters[to].delegate != address(0)){
  55.             to = voters[to].delegate;//找到最终的代理节点
  56.             require(to != msg.sender,"Found loop in delegation.");//若代理节点最终是自己则回退到初始状态
  57.         }

  58.         sender.voted = true;//票权代理出去,状态改为已投票
  59.         sender.delegate = to;//票权代理地址
  60.         Voter storage delegate_ = voters[to];//取出代理节点状态

  61.         //若代理节点已投过票,将新代理的票权投出去,反之则将代理节点票权加和
  62.         if(delegate_.voted){
  63.             proposals[delegate_.vote].voteCount += sender.weight;
  64.         }else{
  65.             delegate_.weight += sender.weight;
  66.         }
  67.     }

  68.     function vote(uint proposal) public{
  69.         Voter storage sender = voters[msg.sender];//通过地址获取对应投票信息
  70.         require(!sender.voted,"Already voted.");//若sender未投过票
  71.         sender.voted = true;//更改投票状态为已投过票
  72.         sender.vote = proposal;//保存已投票节点
  73.         proposals[proposal].voteCount += sender.weight;//票权加和
  74.     }

  75.     //返回票数最多的节点在一维数组proposals中的索引
  76.     function winningProposal() public view returns(uint winningProposal_){
  77.         uint winningVoteCount = 0;
  78.         for(uint p=0;p<proposals.length;p++){
  79.             if(proposals[p].voteCount > winningVoteCount){
  80.                 winningVoteCount = proposals[p].voteCount;
  81.                 winningProposal_ = p;
  82.             }
  83.         }
  84.     }
  85.     //输出票数最多的节点name
  86.     function winnerName() public view returns(bytes32 winnerName_){
  87.         winnerName_ = proposals[winningProposal()].name;
  88.     }
  89. }
Copy the Code

BitMere.com is Information release platform,just provides information storage space services.
The opinions expressed are solely those of the author,Does not constitute advice, please treat with caution.
You have to log in before you can reply Login | 立即注册

Points Rules

Write the first review

BlockQ 小学生
  • Follow

    0

  • Following

    0

  • Articles

    3

Promoted