-
Notifications
You must be signed in to change notification settings - Fork 49
/
task.go
382 lines (345 loc) · 11.5 KB
/
task.go
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
package keeper
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/certikfoundation/shentu/v2/x/oracle/types"
)
var (
amplifier = sdk.NewInt(1000000)
)
// SetTask sets a task in KVStore.
func (k Keeper) SetTask(ctx sdk.Context, task types.Task) {
store := ctx.KVStore(k.storeKey)
store.Set(types.TaskStoreKey(task.Contract, task.Function), k.cdc.MustMarshalLengthPrefixed(&task))
}
// DeleteTask deletes a task from KVStore.
func (k Keeper) DeleteTask(ctx sdk.Context, task types.Task) error {
store := ctx.KVStore(k.storeKey)
store.Delete(types.TaskStoreKey(task.Contract, task.Function))
return nil
}
// UpdateAndSetTask updates a task and set it in KVStore.
func (k Keeper) UpdateAndSetTask(ctx sdk.Context, task types.Task) {
task.ClosingBlock = ctx.BlockHeight() + task.WaitingBlocks
k.SetTask(ctx, task)
if task.WaitingBlocks > 0 {
k.SetClosingBlockStore(ctx, task)
}
}
// GetTask returns a task given contract and function.
func (k Keeper) GetTask(ctx sdk.Context, contract, function string) (types.Task, error) {
TaskData := ctx.KVStore(k.storeKey).Get(types.TaskStoreKey(contract, function))
if TaskData == nil {
return types.Task{}, types.ErrTaskNotExists
}
var task types.Task
k.cdc.MustUnmarshalLengthPrefixed(TaskData, &task)
return task, nil
}
// SetClosingBlockStore sets the store of the aggregation block for a task.
func (k Keeper) SetClosingBlockStore(ctx sdk.Context, task types.Task) {
store := ctx.KVStore(k.storeKey)
newTaskID := types.TaskID{Contract: task.Contract, Function: task.Function}
taskIDs := append(k.GetClosingTaskIDs(ctx, task.ClosingBlock), newTaskID)
bz := k.cdc.MustMarshalLengthPrefixed(&types.TaskIDs{TaskIds: taskIDs})
store.Set(types.ClosingTaskIDsStoreKey(task.ClosingBlock), bz)
}
// GetClosingTaskIDs returns a list of task IDs by the closing block.
func (k Keeper) GetClosingTaskIDs(ctx sdk.Context, closingBlock int64) []types.TaskID {
closingTaskIDsData := ctx.KVStore(k.storeKey).Get(types.ClosingTaskIDsStoreKey(closingBlock))
var taskIDsProto types.TaskIDs
if closingTaskIDsData != nil {
k.cdc.MustUnmarshalLengthPrefixed(closingTaskIDsData, &taskIDsProto)
}
return taskIDsProto.TaskIds
}
// DeleteClosingTaskIDs deletes stores for task IDs closed at given block.
func (k Keeper) DeleteClosingTaskIDs(ctx sdk.Context, closingBlock int64) {
ctx.KVStore(k.storeKey).Delete(types.ClosingTaskIDsStoreKey(closingBlock))
}
// CreateTask creates a new task.
func (k Keeper) CreateTask(ctx sdk.Context, contract string, function string, bounty sdk.Coins,
description string, expiration time.Time, creator sdk.AccAddress, waitingBlocks int64) error {
task, err := k.GetTask(ctx, contract, function)
if err == nil {
if task.ClosingBlock > ctx.BlockHeight() {
return types.ErrTaskNotClosed
}
if err := k.DeleteTask(ctx, task); err != nil {
return err
}
}
closingBlock := ctx.BlockHeight() + waitingBlocks
task = types.NewTask(contract, function, ctx.BlockHeight(), bounty, description, expiration, creator, closingBlock, waitingBlocks)
k.SetTask(ctx, task)
k.SetClosingBlockStore(ctx, task)
if err := k.CollectBounty(ctx, bounty, creator); err != nil {
return err
}
return nil
}
// RemoveTask removes a task from kvstore if it is closed, expired and requested by its creator.
func (k Keeper) RemoveTask(ctx sdk.Context, contract, function string, force bool, creator sdk.AccAddress) error {
task, err := k.GetTask(ctx, contract, function)
if err != nil {
return err
}
if !force && !task.Expiration.Before(ctx.BlockTime()) {
return types.ErrNotExpired
}
if ctx.BlockHeight() <= task.ClosingBlock {
return types.ErrNotFinished
}
// TODO: only creator can delete the task for now
creatorAddr, err := sdk.AccAddressFromBech32(task.Creator)
if err != nil {
panic(err)
}
if !creatorAddr.Equals(creator) {
return types.ErrNotCreator
}
err = k.DeleteTask(ctx, task)
if err != nil {
return err
}
return nil
}
// IteratorAllTasks iterates over all the stored tasks and performs a callback function.
func (k Keeper) IteratorAllTasks(ctx sdk.Context, callback func(task types.Task) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.TaskStoreKeyPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var task types.Task
k.cdc.MustUnmarshalLengthPrefixed(iterator.Value(), &task)
if callback(task) {
break
}
}
}
// GetAllTasks gets all tasks.
func (k Keeper) GetAllTasks(ctx sdk.Context) (tasks []types.Task) {
k.IteratorAllTasks(ctx, func(task types.Task) bool {
tasks = append(tasks, task)
return false
})
return
}
// UpdateAndGetAllTasks updates all tasks and returns them.
func (k Keeper) UpdateAndGetAllTasks(ctx sdk.Context) (tasks []types.Task) {
k.IteratorAllTasks(ctx, func(task types.Task) bool {
task.WaitingBlocks = task.ClosingBlock - ctx.BlockHeight()
tasks = append(tasks, task)
return false
})
return
}
// IsValidResponse returns error if a response is not valid.
func (k Keeper) IsValidResponse(ctx sdk.Context, task types.Task, response types.Response) error {
if ctx.BlockHeight() > task.ClosingBlock {
return types.ErrTaskClosed
}
for _, r := range task.Responses {
if r.Operator == response.Operator {
return types.ErrDuplicateResponse
}
}
if response.Score.LT(types.MinScore) || response.Score.GT(types.MaxScore) {
return types.ErrInvalidScore
}
return nil
}
// RespondToTask records the response from an operator for a task.
func (k Keeper) RespondToTask(ctx sdk.Context, contract string, function string, score int64, operatorAddress sdk.AccAddress) error {
if !k.IsOperator(ctx, operatorAddress) {
return types.ErrUnqualifiedOperator
}
task, err := k.GetTask(ctx, contract, function)
if err != nil {
return err
}
response := types.NewResponse(sdk.NewInt(score), operatorAddress)
err = k.IsValidResponse(ctx, task, response)
if err != nil {
return err
}
task.Responses = append(task.Responses, response)
k.SetTask(ctx, task)
return nil
}
// Aggregate does an aggregation of responses for a task and updated task result.
func (k Keeper) Aggregate(ctx sdk.Context, contract, function string) error {
taskParams := k.GetTaskParams(ctx)
task, err := k.GetTask(ctx, contract, function)
if err != nil {
return err
}
if task.Status != types.TaskStatusPending {
return types.ErrTaskClosed
}
result := taskParams.AggregationResult
totalCollateral := sdk.NewInt(0)
minScoreCollateral := sdk.NewInt(0)
for i, response := range task.Responses {
operatorAddr, err := sdk.AccAddressFromBech32(response.Operator)
if err != nil {
panic(err)
}
amount, err := k.GetCollateralAmount(ctx, operatorAddr)
if err != nil {
continue
}
result = result.Add(response.Score.Mul(amount))
task.Responses[i].Weight = amount
totalCollateral = totalCollateral.Add(amount)
if response.Score.Equal(types.MinScore) {
minScoreCollateral = minScoreCollateral.Add(amount)
}
}
if totalCollateral.IsPositive() {
if minScoreCollateral.MulRaw(3).GTE(totalCollateral) {
result = minScoreCollateral
for i, response := range task.Responses {
if !response.Score.Equal(types.MinScore) {
task.Responses[i].Weight = sdk.NewInt(0)
}
}
} else {
result = result.Quo(totalCollateral)
}
task.Status = types.TaskStatusSucceeded
} else {
task.Status = types.TaskStatusFailed
}
task.Result = result
k.SetTask(ctx, task)
return nil
}
// TotalValidTaskCollateral calculates the total amount of valid collateral of a task.
func (k Keeper) TotalValidTaskCollateral(ctx sdk.Context, task types.Task) sdk.Int {
taskParams := k.GetTaskParams(ctx)
totalValidTaskCollateral := sdk.NewInt(0)
if task.Result.Equal(types.MinScore) {
for _, response := range task.Responses {
if response.Score.Equal(types.MinScore) {
operatorAddr, err := sdk.AccAddressFromBech32(response.Operator)
if err != nil {
panic(err)
}
collateral, err := k.GetCollateralAmount(ctx, operatorAddr)
if err != nil {
continue
}
totalValidTaskCollateral = totalValidTaskCollateral.Add(collateral)
}
}
} else if task.Result.LT(taskParams.ThresholdScore) {
for _, response := range task.Responses {
if response.Score.LT(taskParams.ThresholdScore) {
operatorAddr, err := sdk.AccAddressFromBech32(response.Operator)
if err != nil {
panic(err)
}
collateral, err := k.GetCollateralAmount(ctx, operatorAddr)
if err != nil {
continue
}
totalValidTaskCollateral = totalValidTaskCollateral.Add(
amplifier.Mul(collateral).Quo(response.Score.Add(taskParams.Epsilon1)),
)
}
}
} else {
for _, response := range task.Responses {
if response.Score.GTE(taskParams.ThresholdScore) {
operatorAddr, err := sdk.AccAddressFromBech32(response.Operator)
if err != nil {
panic(err)
}
collateral, err := k.GetCollateralAmount(ctx, operatorAddr)
if err != nil {
continue
}
totalValidTaskCollateral = totalValidTaskCollateral.Add(
amplifier.Mul(collateral).Quo(types.MaxScore.Sub(response.Score).Add(taskParams.Epsilon2)),
)
}
}
}
return totalValidTaskCollateral
}
// TODO: this is a simplified version (without confidence calculation)
// DistributeBounty distributes bounty to operators based on responses and the aggregation result.
func (k Keeper) DistributeBounty(ctx sdk.Context, task types.Task) error {
taskParams := k.GetTaskParams(ctx)
totalValidTaskCollateral := k.TotalValidTaskCollateral(ctx, task)
if totalValidTaskCollateral.IsZero() {
return types.ErrTaskFailed
}
for _, bounty := range task.Bounty {
if task.Result.Equal(types.MinScore) {
for i, response := range task.Responses {
if response.Score.Equal(types.MinScore) {
operatorAddr, err := sdk.AccAddressFromBech32(response.Operator)
if err != nil {
panic(err)
}
collateral, err := k.GetCollateralAmount(ctx, operatorAddr)
if err != nil {
continue
}
amount := bounty.Amount.Mul(collateral).Quo(totalValidTaskCollateral)
reward := sdk.NewCoins(sdk.NewCoin(bounty.Denom, amount))
if err := k.AddReward(ctx, operatorAddr, reward); err != nil {
continue
}
task.Responses[i].Reward = reward
}
}
} else if task.Result.LT(taskParams.ThresholdScore) {
for i, response := range task.Responses {
if response.Score.LT(taskParams.ThresholdScore) {
operatorAddr, err := sdk.AccAddressFromBech32(response.Operator)
if err != nil {
panic(err)
}
collateral, err := k.GetCollateralAmount(ctx, operatorAddr)
if err != nil {
continue
}
amount := bounty.Amount.Mul(
amplifier.Mul(collateral).Quo(response.Score.Add(taskParams.Epsilon1)),
).Quo(totalValidTaskCollateral)
reward := sdk.NewCoins(sdk.NewCoin(task.Bounty[0].Denom, amount))
if err := k.AddReward(ctx, operatorAddr, reward); err != nil {
continue
}
task.Responses[i].Reward = reward
}
}
} else {
for i, response := range task.Responses {
if response.Score.GTE(taskParams.ThresholdScore) {
operatorAddr, err := sdk.AccAddressFromBech32(response.Operator)
if err != nil {
panic(err)
}
collateral, err := k.GetCollateralAmount(ctx, operatorAddr)
if err != nil {
continue
}
amount := bounty.Amount.Mul(
amplifier.Mul(collateral).Quo(types.MaxScore.Sub(response.Score).Add(taskParams.Epsilon2)),
).Quo(totalValidTaskCollateral)
reward := sdk.NewCoins(sdk.NewCoin(bounty.Denom, amount))
if err := k.AddReward(ctx, operatorAddr, reward); err != nil {
continue
}
task.Responses[i].Reward = reward
}
}
}
}
k.SetTask(ctx, task)
return nil
}