-
Notifications
You must be signed in to change notification settings - Fork 14
/
Ballot.rho
207 lines (203 loc) · 8.49 KB
/
Ballot.rho
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// @title Voting with liquid delegation.
// originally from https://github.com/rchain-community/rchain-api/blob/master/examples/voting-locker/chairRole.rho
new Ballot,
deployId(`rho:io:stdout`),
trace(`rho:io:stdout`),
stdout(`rho:io:stdout`)
in {
new insertArbitrary(`rho:registry:insertArbitrary`), uriCh in {
insertArbitrary!(bundle+{*Ballot}, *uriCh) | for(@uri <- uriCh) {
stdout!(["#define $Ballot", uri])
|
deployId!(uri)
}
}
|
contract Ballot(@ballot , return, log) = {
new admin, tally, voterCh in {
log!("creating ballot") |
return!(bundle+{*admin}, bundle+{*tally}) |
voterCh!({}) |
contract admin(@"giveRightToVote", @name, return, log) = {
// voter names must be unique, a public key is suggested.
new voter, voteCh, delegateCh in {
log!({"admin": *admin, "gives right to vote": *voter}) |
voteCh!(Nil) |
delegateCh!(Nil) |
//TODO assign default deligate if any
for ( @voters <- voterCh ) {
if (voters.get(name) != Nil ) {
log!("user already added, returning existing voter capability.") |
voterCh!(voters) |
return!(bundle+{voters.get(name)})
} else {
return!(bundle+{*voter}) |
voterCh!(voters.union({name: *voter}))
}
} |
contract voter(@"vote", @votes, return, log) = {
for ( @oldvotes <- voteCh ) {
// TODO check for valid votes?
log!(["oldvotes were",oldvotes]) |
log!(["newvotes are",votes]) |
return!(["oldvotes were",oldvotes]) |
return!(["newvotes are",votes]) |
voteCh!(votes)
}
} |
contract voter(@"ballot", return, log) = {
return!(ballot)
} |
contract voter(@"choices", @priorDelegates, return, log) = {
for ( @votes <<- voteCh ) {
log!(["vote",votes])|
if ( votes == Nil ) {
for ( @delegate <<- delegateCh) {
for ( @voters <<- voterCh ) {
log!(["delegate",delegate]) |
if (delegate == Nil or priorDelegates.contains(delegate) or voters.get(delegate) == Nil ) {
return!(Nil)
} else {
log!([name, " -> ", delegate, voters.get(delegate)]) |
@{voters.get(delegate)}!("choices", priorDelegates.add(delegate), *return, *log)
}
}
}
} else
{
log!([name, votes]) |
return!(votes)
}
}
} |
contract voter(@"delegate", @delegate, ack, log) = {
// name s.b. unique, public key suggested.
for ( @prior <- delegateCh ) {
log!((name, "delegates to", delegate)) |
ack!(true) |
delegateCh!(delegate)
}
}
}
} |
contract tally(return, log) = {
new countCh, eachVoter, voterack in { countCh!({}) |
log!("running tally") |
for ( @voters <<- voterCh ) {
log!(["voters", voters]) |
eachVoter!(voters) |
contract eachVoter(@rest) = {
new ret, done in {
log!("eachVoter") |
match rest {
{} => {
// for (_ <- done) {
log!("end") |
for ( counts <<- countCh ) {
log!(["counts", *counts]) |
return!(["counts",*counts]) |
log!("done")
}
// }
}
{name: *voter, ...tail} => {
new eachIssue, ack, issueack in {
log!(["voter", name]) |
voter!("choices", Set(), *ret, *log) |
for (@votes <- ret ) {
eachIssue!(ballot.length()-1) |
contract eachIssue (@n) = {
if ( n >= 0) {
log!(["eachIssue",n,ballot.nth(n).get("label"),name,votes.nth(n)])|
for (@counts <- countCh) {
trace!(["updating counts for",name, ballot.nth(n).get("label"), votes.nth(n),votes.nth(n)]) |
log!(["precount", name, ballot.nth(n).get("label"), counts]) |
countCh!(counts.set(ballot.nth(n).get("label"),
counts.getOrElse(ballot.nth(n).get("label"),{})
.set(votes.nth(n),
counts.getOrElse(ballot.nth(n).get("label"),{}).getOrElse(votes.nth(n),0) + 1 ))
) |
for (post <<- countCh) {
log!(["postcount", name, ballot.nth(n).get("label"), *post])
} |
ack!(Nil)
} |
for ( _ <- ack) {
eachIssue!(n-1)
}
} else {
log!("eachIssue done")|
voterack!(Nil)
}
}
}
} |
for ( _ <- voterack) {
eachVoter!(tail)
}
}
_ => log!(["error",rest])
}
}
}
}
}
}
}
}
|
trace!("testing Ballot") |
new bCh, OwansVoterCh, jimscarverVoterCh, aviation_hackerVoterCh, momchilovVoterCh in {
trace!("creating Ballot") |
Ballot!([
{
"label": "Member Swag",
"shortDesc": "The Item of Business I want to propose is to provide all new members with stickers and t-shirts with the RChain logo on it as part of their membership onboarding package.",
"docLink": "https://gist.github.com/dckc/ca240e5336d0ee3e4f5cf31c4f629a30#member-swag",
"proposals": ["Oppose","Abstain","Support"],
"issue": {}
},
{
"label": "Board: DaD",
"shortDesc": "Daffy Duck for Board Member",
"docLink": "https://gist.github.com/dckc/ca240e5336d0ee3e4f5cf31c4f629a30#board-dad",
"proposals": ["Oppose","Abstain","Support"],
"issue": {}
},
{
"label": "Board: DoD",
"shortDesc": "Donald Duck for Board Member",
"docLink": "https://gist.github.com/dckc/ca240e5336d0ee3e4f5cf31c4f629a30#board-dod",
"proposals": ["Oppose","Abstain","Support"],
"issue": {}
}
], *bCh, *trace) |
for (admin, tally <- bCh) {
trace!(["Ballot returned", *admin, *tally]) |
admin!("giveRightToVote", "Owans", *OwansVoterCh, *trace) |
admin!("giveRightToVote", "jimscarver", *jimscarverVoterCh, *trace) |
admin!("giveRightToVote", "aviation_hacker", *aviation_hackerVoterCh, *trace) |
admin!("giveRightToVote", "momchilov", *momchilovVoterCh, *trace) |
for(Owans <- OwansVoterCh; jimscarver <- jimscarverVoterCh; aviation_hacker <- aviation_hackerVoterCh; momchilov <- momchilovVoterCh) {
aviation_hacker!("delegate", "jimscarver", *jimscarverVoterCh, *trace) |
jimscarver!("delegate", "Owans", *OwansVoterCh, *trace) |
jimscarver!("delegate", "momchilov", *momchilovVoterCh, *trace) |
for(_ <- jimscarverVoterCh; _ <- OwansVoterCh; _ <- momchilovVoterCh) {
trace!("voting.") |
// TODO multiple tallies should be performed since the delegate is set above
Owans!("vote", ["Oppose","Abstain","Support"], *OwansVoterCh, *trace) |
jimscarver!("vote", ["Abstain","Oppose","Support"], *jimscarverVoterCh, *trace) |
aviation_hacker!("vote", ["Support","Oppose","Abstain"], *aviation_hackerVoterCh, *trace) |
momchilov!("vote", ["Oppose","Abstain","Support"], *momchilovVoterCh, *trace) |
for(_ <- OwansVoterCh; _ <- jimscarverVoterCh; _ <- aviation_hackerVoterCh; _ <- momchilovVoterCh) {
trace!("votes done") |
tally!(*bCh, *trace) |
for(@w <- bCh) {
trace!({"tally": w})
}
}
}
}
}
}
}