-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColabXStorage.sol
420 lines (370 loc) · 13.4 KB
/
ColabXStorage.sol
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.6;
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "./SharedStructs.sol";
import "./ControllerOwnable.sol";
contract ColabXStorage is ControllerOwnable, Initializable {
// the address that is allowed to be the msg.sender for the payment functions
address private controllerAddress;
// used to cut off upgrades for the remote contract
bool private canChangeControllerAddress = true;
// a map of deal id -> deal
mapping(string => SharedStructs.Deal) private deals;
// a map of party -> dealid[]
mapping(address => string[]) private dealsForParty;
// a map of deal id -> agreement
mapping(string => SharedStructs.Agreement) private agreements;
// a map of deal id -> result
mapping(string => SharedStructs.Result) private results;
// a map of deal id -> result
mapping(string => SharedStructs.Result) private mediations;
event DealStateChange(string dealId, SharedStructs.AgreementState state);
/**
* Init
*/
// https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable
function initialize() public initializer {}
/**
* Deals
*/
function getDeal(
string memory dealId
) public view returns (SharedStructs.Deal memory) {
return deals[dealId];
}
function getDealsForParty(
address party
) public view returns (string[] memory) {
return dealsForParty[party];
}
function checkDealMembers(
SharedStructs.DealMembers memory members
) private pure {
require(members.resourceProvider != address(0), "RP missing");
require(members.jobCreator != address(0), "JC missing");
require(members.solver != address(0), "Solver missing");
require(members.mediators.length > 0, "Mediators <= 0"); //TODO: make it necessary only if JC!=RP
// if (members.resourceProvider != members.jobCreator){
// //TODO: ColabXController.sol, ColabXMediationRandom.sol changes required to support 0 mediator cases.
// require(members.mediators.length > 0, "Mediators <= 0");
// }
// require(members.resourceProvider != members.jobCreator, "RP / JC same"); #Fix #119
}
function checkTimeouts(
SharedStructs.DealTimeouts memory timeouts
) private pure {
// the cost of the agree timeout cannot be > 0 because the whole point is
// one party has not paid anything into the contract is what has timed out
require(timeouts.agree.collateral == 0, "Agree deposit must be 0");
// the same is true of the mediation timeout - it's cost cannot be zero
require(
timeouts.mediateResults.collateral == 0,
"Mediate deposit must be 0"
);
}
function compareDealMembers(
SharedStructs.DealMembers memory members1,
SharedStructs.DealMembers memory members2
) private pure {
require(members1.resourceProvider == members2.resourceProvider, "RP");
require(members1.jobCreator == members2.jobCreator, "JC");
require(members1.solver == members2.solver, "Solver");
require(
members1.mediators.length == members2.mediators.length,
"Mediators"
);
for (uint256 i = 0; i < members1.mediators.length; i++) {
require(members1.mediators[i] == members2.mediators[i], "Mediator");
}
}
function compareDealTimeout(
SharedStructs.DealTimeout memory timeout1,
SharedStructs.DealTimeout memory timeout2
) private pure {
require(timeout1.timeout == timeout2.timeout, "Timeout");
require(timeout1.collateral == timeout2.collateral, "Collateral");
}
function compareDealTimeouts(
SharedStructs.DealTimeouts memory timeouts1,
SharedStructs.DealTimeouts memory timeouts2
) private pure {
compareDealTimeout(timeouts1.agree, timeouts2.agree);
compareDealTimeout(timeouts1.submitResults, timeouts2.submitResults);
compareDealTimeout(timeouts1.judgeResults, timeouts2.judgeResults);
compareDealTimeout(timeouts1.mediateResults, timeouts2.mediateResults);
}
function compareDealPricing(
SharedStructs.DealPricing memory pricing1,
SharedStructs.DealPricing memory pricing2
) private pure {
require(
pricing1.instructionPrice == pricing2.instructionPrice,
"Price"
);
require(
pricing1.paymentCollateral == pricing2.paymentCollateral,
"Payment"
);
require(
pricing1.resultsCollateralMultiple ==
pricing2.resultsCollateralMultiple,
"Results"
);
require(pricing1.mediationFee == pricing2.mediationFee, "Mediation");
}
function ensureDeal(
string memory dealId,
SharedStructs.DealMembers memory members,
SharedStructs.DealTimeouts memory timeouts,
SharedStructs.DealPricing memory pricing
) public onlyController returns (SharedStructs.Deal memory) {
require(
isState(dealId, SharedStructs.AgreementState.DealNegotiating),
"DealNegotiating"
);
checkDealMembers(members);
checkTimeouts(timeouts);
if (hasDeal(dealId)) {
SharedStructs.Deal memory existingDeal = getDeal(dealId);
compareDealMembers(existingDeal.members, members);
compareDealTimeouts(existingDeal.timeouts, timeouts);
compareDealPricing(existingDeal.pricing, pricing);
} else {
deals[dealId] = SharedStructs.Deal(
dealId,
members,
timeouts,
pricing
);
dealsForParty[members.resourceProvider].push(dealId);
dealsForParty[members.jobCreator].push(dealId);
}
return deals[dealId];
}
/**
* Agreements
*/
function getAgreement(
string memory dealId
) public view returns (SharedStructs.Agreement memory) {
return agreements[dealId];
}
function agreeResourceProvider(
string memory dealId
) public onlyController returns (SharedStructs.Agreement memory) {
require(hasDeal(dealId), "Deal does not exist");
require(
agreements[dealId].resourceProviderAgreedAt == 0,
"RP has already agreed"
);
agreements[dealId].resourceProviderAgreedAt = block.timestamp;
_maybeAgreeDeal(dealId);
return agreements[dealId];
}
function agreeJobCreator(
string memory dealId
) public onlyController returns (SharedStructs.Agreement memory) {
require(hasDeal(dealId), "Deal does not exist");
require(
agreements[dealId].jobCreatorAgreedAt == 0,
"JC has already agreed"
);
agreements[dealId].jobCreatorAgreedAt = block.timestamp;
_maybeAgreeDeal(dealId);
return agreements[dealId];
}
/**
* Post Results
*/
function getResult(
string memory dealId
) public view returns (SharedStructs.Result memory) {
return results[dealId];
}
function addResult(
string memory dealId,
string memory resultsId,
string memory dataId,
uint256 instructionCount
) public onlyController returns (SharedStructs.Result memory) {
require(
isState(dealId, SharedStructs.AgreementState.DealAgreed),
"DealAgreed"
);
agreements[dealId].resultsSubmittedAt = block.timestamp;
_changeAgreementState(
dealId,
SharedStructs.AgreementState.ResultsSubmitted
);
results[dealId] = SharedStructs.Result(
dealId,
resultsId,
dataId,
instructionCount
);
return results[dealId];
}
/**
* Judge Results
*/
function acceptResult(string memory dealId) public onlyController {
require(
isState(dealId, SharedStructs.AgreementState.ResultsSubmitted),
"ResultsSubmitted"
);
agreements[dealId].resultsAcceptedAt = block.timestamp;
_changeAgreementState(
dealId,
SharedStructs.AgreementState.ResultsAccepted
);
}
function checkResult(string memory dealId) public onlyController {
require(
isState(dealId, SharedStructs.AgreementState.ResultsSubmitted),
"ResultsSubmitted"
);
agreements[dealId].resultsCheckedAt = block.timestamp;
_changeAgreementState(
dealId,
SharedStructs.AgreementState.ResultsChecked
);
}
/**
* Mediati:
*/
function mediationAcceptResult(string memory dealId) public onlyController {
require(
isState(dealId, SharedStructs.AgreementState.ResultsChecked),
"ResultsChecked"
);
agreements[dealId].mediationAcceptedAt = block.timestamp;
_changeAgreementState(
dealId,
SharedStructs.AgreementState.MediationAccepted
);
}
function mediationRejectResult(string memory dealId) public onlyController {
require(
isState(dealId, SharedStructs.AgreementState.ResultsChecked),
"ResultsChecked"
);
agreements[dealId].mediationRejectedAt = block.timestamp;
_changeAgreementState(
dealId,
SharedStructs.AgreementState.MediationRejected
);
}
/**
* Timeouts
*/
// called because one party submitted a deal and the other party
// did not agree in time
function timeoutAgree(string memory dealId) public onlyController {
require(
isState(dealId, SharedStructs.AgreementState.DealNegotiating),
"DealNegotiating"
);
agreements[dealId].timeoutAgreeAt = block.timestamp;
_changeAgreementState(
dealId,
SharedStructs.AgreementState.TimeoutAgree
);
}
// called because the JC waited too long for a result to be submitted
// and wants it's money back
function timeoutSubmitResult(string memory dealId) public onlyController {
require(
isState(dealId, SharedStructs.AgreementState.DealAgreed),
"DealAgreed"
);
agreements[dealId].timeoutSubmitResultsAt = block.timestamp;
_changeAgreementState(
dealId,
SharedStructs.AgreementState.TimeoutSubmitResults
);
}
// called because the RP waited too long for a judgement of it's results
// and wants it's money back
function timeoutJudgeResult(string memory dealId) public onlyController {
require(
isState(dealId, SharedStructs.AgreementState.ResultsSubmitted),
"ResultsSubmitted"
);
agreements[dealId].timeoutJudgeResultsAt = block.timestamp;
_changeAgreementState(
dealId,
SharedStructs.AgreementState.TimeoutJudgeResults
);
}
// called because the RP or JC waited too long for a mediation of it's results
// and both want their money back
function timeoutMediateResult(string memory dealId) public onlyController {
require(
isState(dealId, SharedStructs.AgreementState.ResultsChecked),
"ResultsChecked"
);
agreements[dealId].timeoutMediateResultsAt = block.timestamp;
_changeAgreementState(
dealId,
SharedStructs.AgreementState.TimeoutMediateResults
);
}
/**
* Costings
*/
function getJobCost(string memory dealId) public view returns (uint256) {
return
deals[dealId].pricing.instructionPrice *
results[dealId].instructionCount;
}
function getResultsCollateral(
string memory dealId
) public view returns (uint256) {
return
deals[dealId].pricing.resultsCollateralMultiple *
getJobCost(dealId);
}
/**
* Checkers
*/
function hasDeal(string memory dealId) public view returns (bool) {
return bytes(getDeal(dealId).dealId).length > 0;
}
function isState(
string memory dealId,
SharedStructs.AgreementState state
) public view returns (bool) {
// if we don't have a deal, we should check against DealNegotiating
// as this is the default state - otherwise it's impossible to check
// for isState('DealNegotiating')
if (!hasDeal(dealId)) {
return state == SharedStructs.AgreementState.DealNegotiating;
}
return agreements[dealId].state == state;
}
/**
* Utils
*/
function _maybeAgreeDeal(string memory dealId) private {
if (
agreements[dealId].resourceProviderAgreedAt != 0 &&
agreements[dealId].jobCreatorAgreedAt != 0
) {
agreements[dealId].dealAgreedAt = block.timestamp;
_changeAgreementState(
dealId,
SharedStructs.AgreementState.DealAgreed
);
} else {
// this is used so we can know if a party can call an agree timeout trigger
agreements[dealId].dealCreatedAt = block.timestamp;
}
}
function _changeAgreementState(
string memory dealId,
SharedStructs.AgreementState state
) private {
agreements[dealId].state = state;
emit DealStateChange(dealId, state);
}
}