-
Notifications
You must be signed in to change notification settings - Fork 3
/
Ballot.rho
150 lines (146 loc) · 5.04 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
/// @title Voting with liquid delegation.
new Ballot,
trace(`rho:io:stderr`),
stdout(`rho:io:stdout`)
in {
new insertArbitrary(`rho:registry:insertArbitrary`), uriCh in {
insertArbitrary!(bundle+{*Ballot}, *uriCh) | for(@uri <- uriCh) {
stdout!(["#define $Ballot", uri])
}
}
|
// See example / demo below.
//contract Ballot(@{proposalNames /\ Set(hd, ...tail)}, return) = {
contract Ballot(@proposalNames , return) = {
new admin, tally, voterCh in {
stdout!("creating ballot") |
return!(bundle+{*admin}, bundle+{*tally}) |
voterCh!({}) |
contract admin(@"giveRightToVote", @name, return) = {
// voter names must be unique, a public key is suggested.
new voter, voteCh, delegateCh in {
trace!({"admin": *admin, "gives right to vote": *voter}) |
voteCh!(Nil) |
delegateCh!(Nil) |
//TODO assign default deligate if any
for ( @voters <- voterCh ) {
//trace!(voters.union({name: *voter}).keys()) |
voterCh!(voters.union({name: *voter}))
} |
return!(bundle+{*voter}) |
contract voter(@"vote", @proposal, eject, ack) = {
trace!({ "for": proposal}) |
if(proposalNames.contains(proposal) == false) {
eject!({"unknown proposal": proposal}) |
ack!(Nil)
}
else {
trace!("valid proposal") |
for ( @oldvote <- voteCh ) {
trace!(["vote was",oldvote]) |
ack!(Nil) |
voteCh!(proposal)
}
}
} |
contract voter(@"choice", @priorDelegates, return) = {
trace!(name) |
for ( @vote <- voteCh ) {
voteCh!(vote) |
if ( vote == Nil ) {
for ( @delegate <- delegateCh; @voters <- voterCh ) {
delegateCh!(delegate) |
voterCh!(voters) |
//trace!(voters) |
if (delegate == Nil or priorDelegates.contains(delegate) or voters.get(delegate) == Nil ) {
return!(Nil)
} else {
trace!([name ++ " -> " ++ delegate, voters.get(delegate)]) |
@{voters.get(delegate)}!("choice", priorDelegates.add(delegate), *return)
}
}
} else {
trace!(name ++ " " ++ vote) |
return!(vote)
}
}
} |
contract voter(@"delegate", @delegate, ack) = {
// name s.b. unique, public key suggested.
for ( @prior <- delegateCh ) {
trace!((name, "delegates to", delegate)) |
ack!(true) |
delegateCh!(delegate)
}
}
}
} |
contract tally(return) = {
new countCh, loop in { countCh!({}) |
//trace!("tally") |
for ( @voters <- voterCh ) {
trace!(voters) |
voterCh!(voters) |
loop!(voters, *return) |
contract loop(@rest, ack) = {
new ret in {
//trace!("loop") |
match rest {
{} => {
//trace!("end") |
for ( counts <- countCh ) {
trace!(*counts) |
return!(*counts)
}
}
{name: *voter, ...tail} => {
//trace!(name) |
voter!("choice", Set(), *ret) |
for (@vote <- ret; @counts <- countCh ) {
trace!([name,vote]) |
//trace!(counts) |
countCh!(counts.set(vote,counts.getOrElse(vote,0) + 1 ) ) |
loop!(tail, *ack)
}
}
_ => trace!(["junk",rest])
}
}
}
}
}
}
}
}
|
new bCh, v1Ch, v2Ch, v3Ch, v4Ch in {
trace!("creating Ballot") |
Ballot!(Set("Lincoln", "Douglas"), *bCh) |
for (admin, tally <- bCh) {
trace!({"Ballot returned": *admin}) |
admin!("giveRightToVote", "Owans", *v1Ch) |
admin!("giveRightToVote", "jimscarver", *v2Ch) |
admin!("giveRightToVote", "aviation_hacker", *v3Ch) |
admin!("giveRightToVote", "momchilov", *v4Ch) |
for(v1 <- v1Ch; v2 <- v2Ch; v3 <- v3Ch; v4 <- v4Ch) {
v3!("delegate", "jimscarver", *v3Ch) |
v2!("delegate", "Owens", *v3Ch) |
v2!("delegate", "Owans", *v3Ch) |
for(_ <- v3Ch; _ <- v3Ch ) {
trace!("voting.") |
v1!("vote", "Lincoln", *trace, *v1Ch) |
v1!("vote", "Douglas", *trace, *v1Ch) |
v1!("vote", "Abe Lincoln", *trace, *v1Ch) |
v4!("vote", "Lincoln", *trace, *v2Ch) |
for(_ <- v1Ch; _ <- v1Ch; _ <- v1Ch; _ <- v2Ch) {
trace!("votes done") |
tally!(*bCh) |
for(@w <- bCh) {
trace!({"tally": w})
}
}
}
}
}
}
}