-
Notifications
You must be signed in to change notification settings - Fork 0
/
multihand.rsh
180 lines (167 loc) · 4.74 KB
/
multihand.rsh
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
'reach 0.1';
import 'rps.rsh'
//
// Rock Paper Scissors
//
// FEATURES
// - Make game fair
// 1. Alternate first moves each round
// - Make game more efficient O(3n/k)
// 1. optimize for case where there is no draw
// 2. play k hands in a single round
//
// TODO
// (1) maybe handle cases when alice attempts to wager more than she has
// and cases when bob tries to accept a wager for more than he has
//
// CHANGELOG
// - rps.rsh code separation
// - initial, Alice and Bob play fair and optimized game of
// rock paper scissors
//
const DEADLINE = 100;
const MAX_HANDS = 10;
const Player =
{
...hasRandom,
getHand: Fun([UInt], Object({
hands: Array(UInt, MAX_HANDS),
count: UInt
})),
seeOutcome: Fun([UInt], Null),
informTimeout: Fun([], Null),
};
const Alice =
{
...Player,
wager: UInt,
};
const Bob =
{
...Player,
acceptWager: Fun([UInt], Null),
};
export const main =
Reach.App(
{},
[Participant('Alice', Alice), Participant('Bob', Bob)],
(A, B) => {
/*
* informTimeout
* broadcasts timeout
*/
const informTimeout = () =>
each([A, B], () => {
interact.informTimeout();
});
/*
* isEvenTurn
* returns true if turn is even else false
*/
const isEvenTurn = state
=> state.turn % 2 === 0
/*
* isAWinner
* returns true if outcome is a wins else false
*/
const isAWinner = state
=> state.outcome == A_WINS
/*
* nextState
* returns state of next turn
*/
const nextState = state
=> (aHands, bHands)
=> ({
outcome: resolveWinner(aHands, bHands),
turn: state.turn + 1
})
/*
* playHands, main w/o wager
* returns next state
*/
const playHands = state => (C, D) => {
C.only(() => {
const _handA = interact.getHand(MAX_HANDS);
const [_commitA, _saltA] = makeCommitment(interact, _handA);
const commitA = declassify(_commitA);
});
C.publish(commitA)
.timeout(DEADLINE, () => closeTo(B, informTimeout));
commit();
unknowable(D, C(_handA, _saltA));
D.only(() => {
const handB = declassify(interact.getHand(MAX_HANDS))
});
D.publish(handB)
.timeout(DEADLINE, () => closeTo(C, informTimeout));
commit();
C.only(() => {
const [saltA, handA] = declassify([_saltA, _handA]);
});
C.publish(saltA, handA)
.timeout(DEADLINE, () => closeTo(D, informTimeout));
checkCommitment(commitA, saltA, handA);
return nextState(state)(handA, handB);
}
/*
* playRound, body of while loop
* returns next state
*/
const playRound = state => (C, D) => {
commit();
return playHands(state)(C, D);
}
/*
* adjustPartial
* returns function (a, b) => member of [a,b] depending on state
*/
const adjustPartial = state
=> ((evenTurn, aWins)
=> (a, b)
=> evenTurn ?
aWins ? b : a :
aWins ? a : b)(isEvenTurn(state), isAWinner(state))
/*main*/
A.only(() => {
const wager = declassify(interact.wager);
const _handA = interact.getHand(MAX_HANDS);
const [_commitA, _saltA] = makeCommitment(interact, _handA);
const commitA = declassify(_commitA);
});
A.publish(wager, commitA)
.pay(wager)
.timeout(DEADLINE, () => closeTo(B, informTimeout));
commit();
unknowable(B, A(_handA, _saltA));
B.only(() => {
interact.acceptWager(wager);
const handB = declassify(interact.getHand(MAX_HANDS))
});
B.publish(handB)
.pay(wager)
.timeout(DEADLINE, () => closeTo(A, informTimeout));
commit();
A.only(() => {
const [saltA, handA] = declassify([_saltA, _handA]);
});
A.publish(saltA, handA)
.timeout(DEADLINE, () => closeTo(B, informTimeout));
checkCommitment(commitA, saltA, handA);
var state = nextState({ turn: 0 })(handA, handB);
invariant(balance() == 2 * wager && isOutcome(state.outcome));
while (state.outcome == DRAW) {
// role switch
state = isEvenTurn(state)
? playRound(state)(A, B)
: playRound(state)(B, A)
continue;
}
assert(state.outcome == A_WINS || state.outcome == B_WINS);
transfer(2 * wager).to(adjustPartial(state)(A, B)); // handle case of outcome that Bob plays as Alice
commit();
each([A, B], () => {
interact.seeOutcome(adjustPartial(state)(A_WINS, B_WINS)); // handle case of outcome that Bob plays as Alice
});
exit();
});