-
Notifications
You must be signed in to change notification settings - Fork 105
/
ColonyTask.sol
executable file
·346 lines (288 loc) · 11.1 KB
/
ColonyTask.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
/*
This file is part of The Colony Network.
The Colony Network is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The Colony Network is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Colony Network. If not, see <http://www.gnu.org/licenses/>.
*/
pragma solidity ^0.4.17;
pragma experimental "v0.5.0";
pragma experimental "ABIEncoderV2";
import "../lib/dappsys/math.sol";
import "./IColonyNetwork.sol";
import "./ColonyStorage.sol";
import "./IColony.sol";
import "./SafeMath.sol";
contract ColonyTask is ColonyStorage, DSMath {
uint8 constant MANAGER = 0;
uint8 constant EVALUATOR = 1;
uint8 constant WORKER = 2;
uint256 constant RATING_COMMIT_TIMEOUT = 432000;
uint256 constant RATING_REVEAL_TIMEOUT = 432000;
event TaskAdded(uint256 indexed id);
modifier confirmTaskRoleIdentity(uint256 _id, uint8 _role) {
Role storage role = tasks[_id].roles[_role];
require(msg.sender == role.user);
_;
}
modifier userCanRateRole(uint256 _id, uint8 _role) {
// Manager rated by worker
// Worker rated by evaluator
if (_role == MANAGER) {
require(tasks[_id].roles[WORKER].user == msg.sender);
} else if (_role == WORKER) {
require(tasks[_id].roles[EVALUATOR].user == msg.sender);
} else {
revert();
}
_;
}
modifier ratingSecretDoesNotExist(uint256 _id, uint8 _role) {
require(taskWorkRatings[_id].secret[_role] == "");
_;
}
modifier workNotSubmitted(uint256 _id) {
require(tasks[_id].deliverableTimestamp == 0);
_;
}
modifier beforeDueDate(uint256 _id) {
require(tasks[_id].dueDate >= now);
_;
}
modifier taskWorkRatingCommitOpen(uint256 _id) {
RatingSecrets storage ratingSecrets = taskWorkRatings[_id];
require(ratingSecrets.count < 2);
// Check we are either past the due date or work has already been submitted
uint taskCompletionTime = tasks[_id].deliverableTimestamp != 0 ? tasks[_id].deliverableTimestamp : tasks[_id].dueDate;
require(taskCompletionTime > 0 && taskCompletionTime <= now);
// Check we are within 5 days of the work submission time
require(sub(now, taskCompletionTime) <= RATING_COMMIT_TIMEOUT);
_;
}
modifier taskWorkRatingRevealOpen(uint256 _id) {
RatingSecrets storage ratingSecrets = taskWorkRatings[_id];
require(ratingSecrets.count <= 2);
// If both ratings have been received, start the reveal period from the time of the last rating commit
// Otherwise start the reveal period after the commit period has expired
// In both cases, keep reveal period open for 5 days
if (ratingSecrets.count == 2) {
require(sub(now, ratingSecrets.timestamp) <= RATING_REVEAL_TIMEOUT);
} else if (ratingSecrets.count < 2) {
uint taskCompletionTime = tasks[_id].deliverableTimestamp != 0 ? tasks[_id].deliverableTimestamp : tasks[_id].dueDate;
require(sub(now, taskCompletionTime) > RATING_COMMIT_TIMEOUT);
require(sub(now, taskCompletionTime) <= add(RATING_COMMIT_TIMEOUT, RATING_REVEAL_TIMEOUT));
}
_;
}
modifier taskWorkRatingsClosed(uint256 _id) {
uint taskCompletionTime = tasks[_id].deliverableTimestamp != 0 ? tasks[_id].deliverableTimestamp : tasks[_id].dueDate;
require(sub(now, taskCompletionTime) > add(RATING_COMMIT_TIMEOUT, RATING_REVEAL_TIMEOUT)); // More than 10 days from work submission have passed
_;
}
modifier taskWorkRatingsAssigned(uint256 _id) {
require(tasks[_id].roles[WORKER].rated);
require(tasks[_id].roles[MANAGER].rated);
_;
}
function makeTask(bytes32 _specificationHash, uint256 _domainId) public
auth
domainExists(_domainId)
{
taskCount += 1;
potCount += 1;
Task memory task;
task.specificationHash = _specificationHash;
task.potId = potCount;
task.domains = new uint256[](1);
task.skills = new uint256[](1);
tasks[taskCount] = task;
tasks[taskCount].roles[MANAGER] = Role({
user: msg.sender,
rated: false,
rating: 0
});
pots[potCount].taskId = taskCount;
setTaskDomain(taskCount, _domainId);
TaskAdded(taskCount);
}
function getTaskCount() public view returns (uint256) {
return taskCount;
}
function proposeTaskChange(bytes _data, uint256 _value, uint8 _role) public returns (uint256 transactionId) {
var (sig, taskId) = deconstructCall(_data);
Task storage task = tasks[taskId];
require(task.roles[_role].user == msg.sender);
require(!task.finalized);
uint8[2] storage _reviewers = reviewers[sig];
require(_reviewers[0] != 0 || _reviewers[1] != 0);
require(_reviewers[0] == _role || _reviewers[1] == _role);
transactionId = IColony(this).submitTransaction(_data, _value, _role);
}
function approveTaskChange(uint256 _transactionId, uint8 _role) public {
Transaction storage _transaction = transactions[_transactionId];
bytes memory _data = _transaction.data;
var (sig, taskId) = deconstructCall(_data);
Task storage task = tasks[taskId];
require(task.roles[_role].user == msg.sender);
require(!task.finalized);
uint8[2] storage _reviewers = reviewers[sig];
require(_reviewers[0] != 0 || _reviewers[1] != 0);
require(_reviewers[0] == _role || _reviewers[1] == _role);
IColony(this).confirmTransaction(_transactionId, _role);
}
function submitTaskWorkRating(uint256 _id, uint8 _role, bytes32 _ratingSecret) public
userCanRateRole(_id, _role)
ratingSecretDoesNotExist(_id, _role)
taskWorkRatingCommitOpen(_id)
{
RatingSecrets storage ratingSecrets = taskWorkRatings[_id];
ratingSecrets.count += 1;
ratingSecrets.timestamp = now;
ratingSecrets.secret[_role] = _ratingSecret;
}
function revealTaskWorkRating(uint256 _id, uint8 _role, uint8 _rating, bytes32 _salt) public
taskWorkRatingRevealOpen(_id)
{
bytes32 ratingSecret = generateSecret(_salt, _rating);
require(ratingSecret == taskWorkRatings[_id].secret[_role]);
Role storage role = tasks[_id].roles[_role];
role.rated = true;
role.rating = _rating;
}
// In the event of a user not committing or revealing within the 10 day rating window,
// their rating of their counterpart is assumed to be the highest possible
// and their own rating is decreased by 5 (e.g. 0.5 points)
function assignWorkRating(uint256 _id) public
taskWorkRatingsClosed(_id)
{
Role storage managerRole = tasks[_id].roles[MANAGER];
Role storage workerRole = tasks[_id].roles[WORKER];
if (!workerRole.rated) {
workerRole.rated = true;
workerRole.rating = 50;
}
if (!managerRole.rated) {
managerRole.rated = true;
managerRole.rating = 50;
workerRole.rating = (workerRole.rating > 5) ? (workerRole.rating - 5) : 0;
}
}
function generateSecret(bytes32 _salt, uint256 _value) public pure returns (bytes32) {
return keccak256(_salt, _value);
}
function getTaskWorkRatings(uint256 _id) public view returns (uint256, uint256) {
return (taskWorkRatings[_id].count, taskWorkRatings[_id].timestamp);
}
function getTaskWorkRatingSecret(uint256 _id, uint8 _role) public view returns (bytes32) {
return taskWorkRatings[_id].secret[_role];
}
// TODO: Restrict function visibility to whoever submits the approved Transaction from Client
// Note task assignment is agreed off-chain
function setTaskRoleUser(uint256 _id, uint8 _role, address _user) public
taskExists(_id)
taskNotFinalized(_id)
{
tasks[_id].roles[_role] = Role({
user: _user,
rated: false,
rating: 0
});
}
function setTaskDomain(uint256 _id, uint256 _domainId) public
taskExists(_id)
taskNotFinalized(_id)
domainExists(_domainId)
{
tasks[_id].domains[0] = _domainId;
}
// TODO: Restrict function visibility to whoever submits the approved Transaction from Client
// Maybe just the administrator is adequate for the skill?
function setTaskSkill(uint256 _id, uint256 _skillId) public
taskExists(_id)
taskNotFinalized(_id)
skillExists(_skillId)
globalSkill(_skillId)
{
tasks[_id].skills[0] = _skillId;
}
function setTaskBrief(uint256 _id, bytes32 _specificationHash) public
self()
taskExists(_id)
taskNotFinalized(_id)
{
tasks[_id].specificationHash = _specificationHash;
}
function setTaskDueDate(uint256 _id, uint256 _dueDate) public
self()
taskExists(_id)
taskNotFinalized(_id)
{
tasks[_id].dueDate = _dueDate;
}
function submitTaskDeliverable(uint256 _id, bytes32 _deliverableHash) public
taskExists(_id)
taskNotFinalized(_id)
beforeDueDate(_id)
workNotSubmitted(_id)
confirmTaskRoleIdentity(_id, WORKER)
{
tasks[_id].deliverableHash = _deliverableHash;
tasks[_id].deliverableTimestamp = now;
}
function finalizeTask(uint256 _id) public
auth
taskExists(_id)
taskWorkRatingsAssigned(_id)
taskNotFinalized(_id)
{
Task storage task = tasks[_id];
IColonyNetwork colonyNetworkContract = IColonyNetwork(colonyNetworkAddress);
for (uint8 roleId = 0; roleId <= 2; roleId++) {
uint payout = task.payouts[roleId][token];
Role storage role = task.roles[roleId];
uint8 rating = (roleId == EVALUATOR) ? 50 : role.rating;
int divider = (roleId == WORKER) ? 30 : 50;
int reputation = SafeMath.mulInt(int(payout), (int(rating)*2 - 50)) / divider;
if (roleId == WORKER) {
colonyNetworkContract.appendReputationUpdateLog(role.user, reputation, task.skills[0]);
}
colonyNetworkContract.appendReputationUpdateLog(role.user, reputation, task.domains[0]);
}
task.finalized = true;
}
function cancelTask(uint256 _id) public
auth
taskExists(_id)
taskNotFinalized(_id)
{
tasks[_id].cancelled = true;
}
function getTask(uint256 _id) public view returns (bytes32, bytes32, bool, bool, uint256, uint256, uint256, uint256) {
Task storage t = tasks[_id];
return (t.specificationHash, t.deliverableHash, t.finalized, t.cancelled, t.dueDate, t.payoutsWeCannotMake, t.potId, t.deliverableTimestamp);
}
function getTaskRole(uint256 _id, uint8 _idx) public view returns (address, bool, uint8) {
Role storage role = tasks[_id].roles[_idx];
return (role.user, role.rated, role.rating);
}
function getTaskSkill(uint256 _id, uint256 _idx) public view returns (uint256) {
return tasks[_id].skills[_idx];
}
function getTaskDomain(uint256 _id, uint256 _idx) public view returns (uint256) {
return tasks[_id].domains[_idx];
}
// Get the function signature and task id from the transaction bytes data
// Note: Relies on the encoded function's first parameter to be the uint256 taskId
function deconstructCall(bytes _data) internal pure returns (bytes4 sig, uint256 taskId) {
assembly {
sig := mload(add(_data, 0x20))
taskId := mload(add(_data, add(0x20, 4))) // same as calldataload(72)
}
}
}