-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.rsh
157 lines (126 loc) · 4.55 KB
/
index.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
'reach 0.1';
'use strict';
const COUNTDOWN = 20/5; //this is the number of blocks instead seconds
const shared = {
showTime: Fun([UInt], Null), //show the countdown
...hasConsoleLogger,
informTimeout: Fun([], Null),
showStillHere: Fun([Bool], Null), //show the variable stillHere
}
// NOTE: I do not display the outcome of the balance of EACH round, but only on the initial and final part
export const main = Reach.App(() => {
const A = Participant('Alice', {
...shared,
inherit: UInt,
getChoice: Fun([], Bool),
});
const Bob = Participant('Bob',{
...shared,
acceptTerms: Fun([UInt], Bool),
});
const informTimeout = () => {
each([A, Bob], () => {
interact.informTimeout();
});
}
init();
A.only(() =>{
const value = declassify(interact.inherit);
});
A.publish();
commit();
A.publish(value)
.pay(value)
commit();
Bob.only(() => {
const terms = declassify(interact.acceptTerms(value));
})
Bob.publish(terms);
if(terms){ //Bob has accepted the terms
commit();
A.publish();
const END_PROGRAM = lastConsensusTime() + COUNTDOWN; //a time in the future. At this time the program must stop.
commit();
// //SHOW the countdown
each([A, Bob], () => {
interact.showTime(COUNTDOWN);
});
//choice phase
A.only(() => {
const stillHere = declassify(interact.getChoice());
});
A.publish(stillHere)
.timeout(relativeTime(COUNTDOWN), () => closeTo(Bob, informTimeout));
each([A, Bob], () => {
interact.showStillHere(stillHere);
});
var stillHereAlice = stillHere;
invariant(balance() == balance());
while(stillHereAlice){
// END_PROGRAM - lastConsensusTime() is my counter variable
if ( END_PROGRAM - lastConsensusTime() <= 0 && stillHereAlice){ //If timer reaches “zero” and Alice is “still here”.
transfer(balance()).to(A); // transfer the balance of the contract to Alice
commit();
exit();
}
commit();
A.only(() => {
const stillHereAgain = declassify(interact.getChoice());
});
A.publish(stillHereAgain)
.timeout(relativeTime(COUNTDOWN), () => closeTo(Bob, informTimeout));
each([A, Bob], () => { //information about Alice status
interact.showStillHere(stillHereAgain);
});
stillHereAlice = stillHereAgain;
continue;
}
if (balance()> 0){ //if we are here, stillHere is false
transfer(balance()).to(Bob);
}
}else{ //bob does not accept the terms
transfer(value).to(A);
}
commit();
exit();
});
/**-----------------------------------------------------
* ----------------- FOR REACH'S TEAM: -----------------
*
* an other version: it was a different interpetation of specification
*
*
*/
//var [count, end_program2] = [END_PROGRAM - lastConsensusTime(), lastConsensusTime() + COUNTDOWN];
//invariant(balance() == balance());
// while(count>0){
// commit();
// //SHOW the countdown
// each([A, Bob], () => {
// interact.showTime(count);
// });
// //choice phase
// A.only(() => {
// const stillHere = declassify(interact.getChoice());
// });
// A.publish(stillHere)
// .timeout(relativeTime(COUNTDOWN), () => closeTo(Bob, informTimeout));
// each([A, Bob], () => {
// interact.showStillHere(stillHere);
// });
// //if Alice is still here, then I have to reset the counter
// if(stillHere && end_program2 - lastConsensusTime() != 0){
// end_program2 = lastConsensusTime() + COUNTDOWN; //the date in the future is prolonged
// continue;
// }
// if (end_program2 - lastConsensusTime() == 0 && stillHere){ //If timer reaches “zero” and Alice is “still here”.
// transfer(balance()).to(A); // transfer the balance of the contract to Alice
// commit();
// exit();
// }
// each([A, Bob], () => {
// interact.showTime(count);
// });
// count = end_program2 - lastConsensusTime();
// continue;
// }