Changeset View
Changeset View
Standalone View
Standalone View
autotests/html/test.sol.html
- This file was added.
1 | <!DOCTYPE html> | ||||
---|---|---|---|---|---|
2 | <html><head> | ||||
3 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> | ||||
4 | <title>test.sol</title> | ||||
5 | <meta name="generator" content="KF5::SyntaxHighlighting (Solidity)"/> | ||||
6 | </head><body style="color:#1f1c1b"><pre> | ||||
7 | <span style="color:#898887;">// Solidity Sample File</span> | ||||
8 | <span style="color:#898887;">// https://solidity.readthedocs.io/en/latest/solidity-by-example.html#voting</span> | ||||
9 | | ||||
10 | <span style="font-weight:bold;">pragma</span> <span style="color:#644a9b;font-weight:bold;">solidity</span> >=0.4.22 <0.7.0; | ||||
11 | | ||||
12 | <span style="color:#898887;">/// </span><span style="color:#ca60ca;">@title</span><span style="color:#898887;"> Voting with delegation.</span> | ||||
13 | <span style="font-weight:bold;">contract</span> Ballot { | ||||
14 | <span style="color:#898887;">// This declares a new complex type which will</span> | ||||
15 | <span style="color:#898887;">// be used for variables later.</span> | ||||
16 | <span style="color:#898887;">// It will represent a single voter.</span> | ||||
17 | <span style="font-weight:bold;">struct</span> Voter { | ||||
18 | <span style="color:#0057ae;">uint</span> weight; <span style="color:#898887;">// weight is accumulated by delegation</span> | ||||
19 | <span style="color:#0057ae;">bool</span> voted; <span style="color:#898887;">// if true, that person already voted</span> | ||||
20 | <span style="color:#0057ae;">address</span> delegate; <span style="color:#898887;">// person delegated to</span> | ||||
21 | <span style="color:#0057ae;">uint</span> vote; <span style="color:#898887;">// index of the voted proposal</span> | ||||
22 | } | ||||
23 | | ||||
24 | <span style="color:#898887;">// This is a type for a single proposal.</span> | ||||
25 | <span style="font-weight:bold;">struct</span> Proposal { | ||||
26 | <span style="color:#0057ae;">bytes32</span> name; <span style="color:#898887;">// short name (up to 32 bytes)</span> | ||||
27 | <span style="color:#0057ae;">uint</span> voteCount; <span style="color:#898887;">// number of accumulated votes</span> | ||||
28 | } | ||||
29 | | ||||
30 | <span style="color:#0057ae;">address</span> <span style="font-weight:bold;">public</span> chairperson; | ||||
31 | | ||||
32 | <span style="color:#898887;">// This declares a state variable that</span> | ||||
33 | <span style="color:#898887;">// stores a `Voter` struct for each possible address.</span> | ||||
34 | <span style="color:#0057ae;">mapping</span>(<span style="color:#0057ae;">address</span> => Voter) <span style="font-weight:bold;">public</span> voters; | ||||
35 | | ||||
36 | <span style="color:#898887;">// A dynamically-sized array of `Proposal` structs.</span> | ||||
37 | Proposal[] <span style="font-weight:bold;">public</span> proposals; | ||||
38 | | ||||
39 | <span style="color:#898887;">/// Create a new ballot to choose one of `proposalNames`.</span> | ||||
40 | <span style="font-weight:bold;">constructor</span>(<span style="color:#0057ae;">bytes32</span>[] <span style="font-weight:bold;">memory</span> proposalNames) <span style="font-weight:bold;">public</span> { | ||||
41 | chairperson = <span style="color:#644a9b;font-weight:bold;">msg</span>.sender; | ||||
42 | voters[chairperson].weight = <span style="color:#b08000;">1</span>; | ||||
43 | | ||||
44 | <span style="color:#898887;">// For each of the provided proposal names,</span> | ||||
45 | <span style="color:#898887;">// create a new proposal object and add it</span> | ||||
46 | <span style="color:#898887;">// to the end of the array.</span> | ||||
47 | <span style="font-weight:bold;">for</span> (<span style="color:#0057ae;">uint</span> i = <span style="color:#b08000;">0</span>; i < proposalNames.length; i<span style="color:#644a9b;font-weight:bold;">++</span>) { | ||||
48 | <span style="color:#898887;">// `Proposal({...})` creates a temporary</span> | ||||
49 | <span style="color:#898887;">// Proposal object and `proposals.push(...)`</span> | ||||
50 | <span style="color:#898887;">// appends it to the end of `proposals`.</span> | ||||
51 | proposals.push(Proposal({ | ||||
52 | name: proposalNames[i], | ||||
53 | voteCount: <span style="color:#b08000;">0</span> | ||||
54 | })); | ||||
55 | } | ||||
56 | } | ||||
57 | | ||||
58 | <span style="color:#898887;">// Give `voter` the right to vote on this ballot.</span> | ||||
59 | <span style="color:#898887;">// May only be called by `chairperson`.</span> | ||||
60 | <span style="font-weight:bold;">function</span> giveRightToVote(<span style="color:#0057ae;">address</span> voter) <span style="font-weight:bold;">public</span> { | ||||
61 | <span style="color:#898887;">// If the first argument of `require` evaluates</span> | ||||
62 | <span style="color:#898887;">// to `false`, execution terminates and all</span> | ||||
63 | <span style="color:#898887;">// changes to the state and to Ether balances</span> | ||||
64 | <span style="color:#898887;">// are reverted.</span> | ||||
65 | <span style="color:#898887;">// This used to consume all gas in old EVM versions, but</span> | ||||
66 | <span style="color:#898887;">// not anymore.</span> | ||||
67 | <span style="color:#898887;">// It is often a good idea to use `require` to check if</span> | ||||
68 | <span style="color:#898887;">// functions are called correctly.</span> | ||||
69 | <span style="color:#898887;">// As a second argument, you can also provide an</span> | ||||
70 | <span style="color:#898887;">// explanation about what went wrong.</span> | ||||
71 | <span style="color:#644a9b;font-weight:bold;">require</span>( | ||||
72 | <span style="color:#644a9b;font-weight:bold;">msg</span>.sender == chairperson, | ||||
73 | <span style="color:#bf0303;">"Only chairperson can give right to vote."</span> | ||||
74 | ); | ||||
75 | <span style="color:#644a9b;font-weight:bold;">require</span>( | ||||
76 | !voters[voter].voted, | ||||
77 | <span style="color:#bf0303;">"The voter already voted."</span> | ||||
78 | ); | ||||
79 | <span style="color:#644a9b;font-weight:bold;">require</span>(voters[voter].weight == <span style="color:#b08000;">0</span>); | ||||
80 | voters[voter].weight = <span style="color:#b08000;">1</span>; | ||||
81 | } | ||||
82 | | ||||
83 | <span style="color:#898887;">/// Delegate your vote to the voter `to`.</span> | ||||
84 | <span style="font-weight:bold;">function</span> delegate(<span style="color:#0057ae;">address</span> to) <span style="font-weight:bold;">public</span> { | ||||
85 | <span style="color:#898887;">// assigns reference</span> | ||||
86 | Voter <span style="font-weight:bold;">storage</span> sender = voters[<span style="color:#644a9b;font-weight:bold;">msg</span>.sender]; | ||||
87 | <span style="color:#644a9b;font-weight:bold;">require</span>(!sender.voted, <span style="color:#bf0303;">"You already voted."</span>); | ||||
88 | | ||||
89 | <span style="color:#644a9b;font-weight:bold;">require</span>(to != <span style="color:#644a9b;font-weight:bold;">msg</span>.sender, <span style="color:#bf0303;">"Self-delegation is disallowed."</span>); | ||||
90 | | ||||
91 | <span style="color:#898887;">// Forward the delegation as long as</span> | ||||
92 | <span style="color:#898887;">// `to` also delegated.</span> | ||||
93 | <span style="color:#898887;">// In general, such loops are very dangerous,</span> | ||||
94 | <span style="color:#898887;">// because if they run too long, they might</span> | ||||
95 | <span style="color:#898887;">// need more gas than is available in a block.</span> | ||||
96 | <span style="color:#898887;">// In this case, the delegation will not be executed,</span> | ||||
97 | <span style="color:#898887;">// but in other situations, such loops might</span> | ||||
98 | <span style="color:#898887;">// cause a contract to get "stuck" completely.</span> | ||||
99 | <span style="font-weight:bold;">while</span> (voters[to].delegate != <span style="color:#0057ae;">address</span>(<span style="color:#b08000;">0</span>)) { | ||||
100 | to = voters[to].delegate; | ||||
101 | | ||||
102 | <span style="color:#898887;">// We found a loop in the delegation, not allowed.</span> | ||||
103 | <span style="color:#644a9b;font-weight:bold;">require</span>(to != <span style="color:#644a9b;font-weight:bold;">msg</span>.sender, <span style="color:#bf0303;">"Found loop in delegation."</span>); | ||||
104 | } | ||||
105 | | ||||
106 | <span style="color:#898887;">// Since `sender` is a reference, this</span> | ||||
107 | <span style="color:#898887;">// modifies `voters[msg.sender].voted`</span> | ||||
108 | sender.voted = <span style="font-weight:bold;">true</span>; | ||||
109 | sender.delegate = to; | ||||
110 | Voter <span style="font-weight:bold;">storage</span> delegate_ = voters[to]; | ||||
111 | <span style="font-weight:bold;">if</span> (delegate_.voted) { | ||||
112 | <span style="color:#898887;">// If the delegate already voted,</span> | ||||
113 | <span style="color:#898887;">// directly add to the number of votes</span> | ||||
114 | proposals[delegate_.vote].voteCount += sender.weight; | ||||
115 | } <span style="font-weight:bold;">else</span> { | ||||
116 | <span style="color:#898887;">// If the delegate did not vote yet,</span> | ||||
117 | <span style="color:#898887;">// add to her weight.</span> | ||||
118 | delegate_.weight += sender.weight; | ||||
119 | } | ||||
120 | } | ||||
121 | | ||||
122 | <span style="color:#898887;">/// Give your vote (including votes delegated to you)</span> | ||||
123 | <span style="color:#898887;">/// to proposal `proposals[proposal].name`.</span> | ||||
124 | <span style="font-weight:bold;">function</span> vote(<span style="color:#0057ae;">uint</span> proposal) <span style="font-weight:bold;">public</span> { | ||||
125 | Voter <span style="font-weight:bold;">storage</span> sender = voters[<span style="color:#644a9b;font-weight:bold;">msg</span>.sender]; | ||||
126 | <span style="color:#644a9b;font-weight:bold;">require</span>(sender.weight != <span style="color:#b08000;">0</span>, <span style="color:#bf0303;">"Has no right to vote"</span>); | ||||
127 | <span style="color:#644a9b;font-weight:bold;">require</span>(!sender.voted, <span style="color:#bf0303;">"Already voted."</span>); | ||||
128 | sender.voted = <span style="font-weight:bold;">true</span>; | ||||
129 | sender.vote = proposal; | ||||
130 | | ||||
131 | <span style="color:#898887;">// If `proposal` is out of the range of the array,</span> | ||||
132 | <span style="color:#898887;">// this will throw automatically and revert all</span> | ||||
133 | <span style="color:#898887;">// changes.</span> | ||||
134 | proposals[proposal].voteCount += sender.weight; | ||||
135 | } | ||||
136 | | ||||
137 | <span style="color:#898887;">/// </span><span style="color:#ca60ca;">@dev</span><span style="color:#898887;"> Computes the winning proposal taking all</span> | ||||
138 | <span style="color:#898887;">/// previous votes into account.</span> | ||||
139 | <span style="font-weight:bold;">function</span> winningProposal() <span style="font-weight:bold;">public</span> <span style="font-weight:bold;">view</span> | ||||
140 | <span style="font-weight:bold;">returns</span> (<span style="color:#0057ae;">uint</span> winningProposal_) | ||||
141 | { | ||||
142 | <span style="color:#0057ae;">uint</span> winningVoteCount = <span style="color:#b08000;">0</span>; | ||||
143 | <span style="font-weight:bold;">for</span> (<span style="color:#0057ae;">uint</span> p = <span style="color:#b08000;">0</span>; p < proposals.length; p<span style="color:#644a9b;font-weight:bold;">++</span>) { | ||||
144 | <span style="font-weight:bold;">if</span> (proposals[p].voteCount > winningVoteCount) { | ||||
145 | winningVoteCount = proposals[p].voteCount; | ||||
146 | winningProposal_ = p; | ||||
147 | } | ||||
148 | } | ||||
149 | } | ||||
150 | | ||||
151 | <span style="color:#898887;">// Calls winningProposal() function to get the index</span> | ||||
152 | <span style="color:#898887;">// of the winner contained in the proposals array and then</span> | ||||
153 | <span style="color:#898887;">// returns the name of the winner</span> | ||||
154 | <span style="font-weight:bold;">function</span> winnerName() <span style="font-weight:bold;">public</span> <span style="font-weight:bold;">view</span> | ||||
155 | <span style="font-weight:bold;">returns</span> (<span style="color:#0057ae;">bytes32</span> winnerName_) | ||||
156 | { | ||||
157 | winnerName_ = proposals[winningProposal()].name; | ||||
158 | } | ||||
159 | } | ||||
160 | </pre></body></html> |