欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

用 solidity 编程语言开发以太应用区块链投票示例的智能合约

最编程 2024-03-04 16:49:47
...
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 delegation weight在代表投票过程中会累积 bool voted; // if true, that person already voted 如果值为true,代表这个投票人已经投过票 address delegate; // person delegated to 投票人地址 uint 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`. 传入议案名称来定义一个投票对象 function Ballot(bytes32[] 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 reference 找出委托发起人,如果已经投票,终止程序 Voter 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 votes 投票成功,投票总数加上相应的weight proposals[delegate_.vote].voteCount += sender.weight; } else { // If the delegate did not vote yet, // add to her weight. 如果还没投票,发起人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 view 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; } } } // 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 view returns (bytes32 winnerName_) { winnerName_ = proposals[winningProposal()].name; } }