-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaim.rsh
More file actions
179 lines (163 loc) · 7.73 KB
/
Copy pathclaim.rsh
File metadata and controls
179 lines (163 loc) · 7.73 KB
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
'reach 0.1';
const maxParticipants = 10;
const sizeBinaryInfo = 32;
const nbrOracles = 3;
// The minimal number of oracles that need to agree in order to take a decision
const minForDecisions = nbrOracles/2 + 1;
const Child = Maybe(Tuple(Address, Contract));
export const main = Reach.App(() => {
const A = Participant('Alice', {
addressesOracles: Array(Address, nbrOracles),
addressSkeptic: Maybe(Address),
interaction: Bytes(sizeBinaryInfo),
wagerUp: UInt, // Non-zero only if a claimer is defending against a skeptic's attack, will go to the skeptic's address if A is wrong
wagerDown: UInt, // Will go to the person who shows that this claim or this attack is wrong. It is 0 if this claim is at the bottom and should be formally verified.
deadline: UInt, // Unix timestamp for the deadline
isBottom: Bool,
});
const Oracle = API('Oracle', {
correctContract: Fun([], Null),
addParticipant: Fun([Address, Contract], Null),
announceVerification: Fun([Bool], Null), // Only called if it is at the bottom and should be formal verification
announceWinner: Fun([UInt], Null) // The UInt should be the index of the winner. The index is 0 if the original claim is correct.
});
const V = View({
author: Address,
addressesOracles: Array(Address, nbrOracles),
addressSkeptic: Maybe(Address),
interaction: Bytes(sizeBinaryInfo),
wagerUp: UInt,
wagerDown: UInt,
deadline: UInt,
isBottom: Bool,
isCorrect: Array(Bool, nbrOracles),
participants: Array(Child, maxParticipants),
});
const E = Events({
correctContract: [],
newParticipant: [Address, Contract],
announceWinner: [Address, Contract], // The address is the address of the winner, and the contract is the winning interaction
announceVerification: [Bool],
});
init();
A.only(() => {
const addressesOracles = declassify(interact.addressesOracles);
const addressSkeptic = declassify(interact.addressSkeptic);
const interaction = declassify(interact.interaction);
const wagerUp = declassify(interact.wagerUp);
const wagerDown = declassify(interact.wagerDown);
const deadline = declassify(interact.deadline);
const isBottom = declassify(interact.isBottom);
assume(implies(isBottom, wagerDown == 0));
});
A.publish(addressesOracles, addressSkeptic, interaction, wagerUp, wagerDown, deadline, isBottom)
.pay(wagerUp + wagerDown);
require(implies(isBottom, wagerDown == 0));
V.author.set(A);
V.addressesOracles.set(addressesOracles);
V.addressSkeptic.set(addressSkeptic);
V.interaction.set(interaction);
V.wagerUp.set(wagerUp);
V.wagerDown.set(wagerDown);
V.deadline.set(deadline);
V.isBottom.set(isBottom);
const initParticipants = () => Array.replicate(maxParticipants,
Child.None(null))
.set(0, Child.Some([A, getContract()])); // Will contain the address of the participants and the contracts of the child interactions
const [ isConcluded,
isCorrect,
numberParticipants,
participants,
scores,
hasDeclaredAWinner,
verifications
] =
parallelReduce([ false,
Array.replicate(nbrOracles, false),
1,
initParticipants(),
Array.replicate(maxParticipants, 0),
Array.replicate(nbrOracles, false),
Array.replicate(nbrOracles, Maybe(Bool).None(null)) ])
.define(() => {
V.participants.set(participants);
V.isCorrect.set(isCorrect);
const verificationConcluded = () => (verifications.count(x => x == Maybe(Bool).Some(true)) == minForDecisions)
|| (verifications.count(x => x == Maybe(Bool).Some(false)) == minForDecisions);
})
.invariant(numberParticipants <= maxParticipants)
.invariant(balance() == (isConcluded ? 0 : wagerUp + wagerDown))
.invariant(implies(isBottom, scores.all(x => x == 0) && hasDeclaredAWinner.all(x => x == false)))
.invariant(implies(isBottom && isConcluded, verificationConcluded()))
// Should be true, but is not able to verify:
// .invariant(implies(isBottom && verificationConcluded(), isConcluded))
.invariant(implies(!isBottom, isConcluded == scores.any(x => x == minForDecisions)))
.invariant(implies(!isBottom, scores.all(x => x <= minForDecisions)))
.while ( !isConcluded )
.api_(Oracle.correctContract, () => {
const index = addressesOracles.findIndex(x => x == this);
check(isSome(index), 'You are not an oracle');
return [ 0, (ret) => {
const newIsCorrect = isCorrect.set(fromSome(index, 0), true);
if ( newIsCorrect.count(x => x) == minForDecisions ){
E.correctContract();
}
ret(null);
return [ false, newIsCorrect, numberParticipants, participants, scores, hasDeclaredAWinner, verifications ];
} ];
})
.api_(Oracle.addParticipant, (newPart, newInter) => {
const index = addressesOracles.findIndex(x => x == this);
check(isSome(index), 'You are not an oracle');
check(numberParticipants < maxParticipants, 'Too many participants');
check(!isBottom, 'Cannot challenge a bottom claim');
const newChild = Child.Some([ newPart, newInter ]);
check(!participants.includes(newChild), "It is already a participant")
return [ 0, (ret) => {
const newListParticipants = participants.set(numberParticipants, newChild);
E.newParticipant(newPart, newInter);
ret(null);
return [ false, isCorrect, numberParticipants + 1, newListParticipants, scores, hasDeclaredAWinner, verifications ];
} ];
})
.api_(Oracle.announceWinner, (indexWinner) => {
const index = addressesOracles.findIndex(x => x == this);
check(isSome(index), 'You are not an oracle');
check(!hasDeclaredAWinner[fromSome(index, 0)], 'You already declared a winner');
check(indexWinner < numberParticipants, 'This participant does not exist.');
check(!isBottom, 'Can only announce verification.')
return [ 0, (ret) => {
const newHasDeclaredAWinner = hasDeclaredAWinner.set(fromSome(index,0), true);
const newScore = scores[indexWinner] + 1;
const newScores = scores.set(indexWinner, newScore);
const newIsConcluded = newScore == minForDecisions;
if ( newIsConcluded ){
const winner = fromSome(participants[indexWinner], [ A, getContract() ]);
E.announceWinner(winner[0], winner[1]);
transfer(wagerDown).to(winner[0]);
transfer(wagerUp).to(indexWinner == 0 ? A : fromSome(addressSkeptic, A));
}
ret(null);
return [ newIsConcluded, isCorrect, numberParticipants, participants, newScores, newHasDeclaredAWinner, verifications ];
} ];
})
.api_(Oracle.announceVerification, (wasRight) => {
const someIndex = addressesOracles.findIndex(x => x == this);
check(isSome(someIndex), 'You are not an oracle');
check(isBottom, 'Can only announce formal verification if it is bottom');
const index = fromSome(someIndex, 0);
check(isNone(verifications[index]), "You already announced a verification")
return [ 0, (ret) => {
const newVerifications = verifications.set(index, Maybe(Bool).Some(wasRight));
const newIsConcluded = (newVerifications.count(x => x == Maybe(Bool).Some(wasRight)) == minForDecisions);
if ( newIsConcluded ) {
E.announceVerification(wasRight);
transfer(wagerUp).to(wasRight ? A : fromSome(addressSkeptic, A));
}
ret(null);
return [ newIsConcluded, isCorrect, numberParticipants, participants, scores, hasDeclaredAWinner, newVerifications ];
} ];
});
commit();
exit();
});