Created
March 26, 2016 16:22
-
-
Save goodjoon/3674f7873a0b885a69ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| contract Ballot { | |
| // 하나의 투표자를 나타내기 위한 Complex Type | |
| struct Voter { | |
| uint weight; | |
| bool voted; | |
| address delegate; | |
| uint vote; | |
| } | |
| // 하나의 무기명 투표 후보 (Proposal) | |
| struct Proposal { | |
| bytes32 name; // 짧은 이름 | |
| uint voteCount; // 누적 투표 수 | |
| } | |
| address public chairperson; | |
| // 각 가능한 주소의 Voter 구조체를 저장하는 state 변수 | |
| mapping(address => Voter) public voters; | |
| Proposal[] public proposals; | |
| // proposalNames 로 무기명 투표 만듦 | |
| function Ballot(bytes32[] proposalNames) { | |
| chairperson = msg.sender; | |
| voters[chairperson].weight = 1; | |
| // 각 제공된 후보 이름에 대해 새로운 후보 객체를 생성하고 | |
| // 배열에 push 한다 | |
| for(uint i = 0; i < proposalNames.length; i++) { | |
| proposals.push(Proposal({ | |
| name: proposalNames[i], | |
| voteCount: 0 })); | |
| } | |
| } | |
| // 투표자에게 이 투표에 대해 권한을 부여한다. | |
| // chairperson 만이 부를 수 있다 | |
| function giveRightToVote(address voter) { | |
| if (msg.sender != chairperson || voters[voter].voted) { | |
| // `throw` 는 state 와 Ether Balance 를 모두 되돌리고 | |
| // 종료한다. function 이 부적합하게 호출 | |
| // 되었을 때 throw 를 날리는건 좋은 생각임. | |
| // 그러나 이러면 모든 제공된 gas 가 소진 됨. | |
| throw; | |
| } | |
| voters[voter].weight = 1; | |
| } | |
| // 투표를 `to` 에게 위임 하다 | |
| function delegate(address to) { | |
| // Reference Assign | |
| Voter sender = voters[msg.sender]; | |
| if (sender.voted) | |
| throw; | |
| // 위임을 `to` 가 또 위임하기 전까지 위임한다 | |
| while(voters[to].delegate != address(0) && | |
| voters[to].delegate != msg.sender) | |
| to = voters[to].delegate; | |
| if (to == msg.sender) | |
| throw; | |
| // sender 는 Voter 의 reference 이므로 | |
| // voters[msg.sender].voted 를 true 로 설정한다 | |
| sender.voted = true; | |
| sender.delegate = to; | |
| Voter delegate = voters[to]; | |
| if (delegate.voted) | |
| // 위임자가 이미 투표 하였다면 | |
| // 직접 투표한 수를 올린다 | |
| proposals[delegate.vote].voteCount += sender.weight; | |
| else | |
| // 위임자가 아직 투표 안했으면, | |
| // 위임자에게 weight 를 하나 더 추가해준다 | |
| delegate.weight += sender.weight; | |
| } | |
| function vote(uint proposal) { | |
| Voter sender = voters[msg.sender]; | |
| if (sender.voted) throw; | |
| sender.voted = true; | |
| sender.vote = proposal; | |
| // 만약 proposal 이 배열 범위 넘어가면 | |
| // 자동적으로 throw 한다 | |
| proposals[proposal].voteCount += sender.weight; | |
| } | |
| 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; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment