-
Notifications
You must be signed in to change notification settings - Fork 51
/
task.go
185 lines (151 loc) · 3.41 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
package types
import (
"encoding/json"
"time"
"github.com/gogo/protobuf/proto"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewTask returns a new task.
func NewTask(
contract string,
function string,
beginBlock int64,
bounty sdk.Coins,
description string,
expiration time.Time,
creator sdk.AccAddress,
expireHeight int64,
waitingBlocks int64,
) Task {
return Task{
Contract: contract,
Function: function,
BeginBlock: beginBlock,
Bounty: bounty,
Description: description,
Expiration: expiration,
Creator: creator.String(),
ExpireHeight: expireHeight,
WaitingBlocks: waitingBlocks,
Status: TaskStatusPending,
}
}
// NewResponse returns a new response.
func NewResponse(score math.Int, operator sdk.AccAddress) Response {
return Response{
Operator: operator.String(),
Score: score,
}
}
type Responses []Response
// String implements the Stringer interface.
func (r Responses) String() string {
jsonBytes, err := json.Marshal(r)
if err != nil {
return "[]"
}
return string(jsonBytes)
}
func NewTxTask(
txHash []byte,
creator string,
bounty sdk.Coins,
validTime time.Time,
status TaskStatus,
) *TxTask {
return &TxTask{
AtxHash: txHash,
Creator: creator,
Bounty: bounty,
ValidTime: validTime,
Status: status,
}
}
type TaskI interface {
proto.Message
GetID() []byte
GetCreator() string
GetResponses() Responses
IsExpired(ctx sdk.Context) bool
GetValidTime() (int64, time.Time)
GetBounty() sdk.Coins
GetStatus() TaskStatus
GetScore() int64
AddResponse(response Response)
SetStatus(status TaskStatus)
SetScore(score int64)
ShouldAgg(ctx sdk.Context) bool
}
func (t *Task) GetID() []byte {
return append([]byte(t.Contract), []byte(t.Function)...)
}
func (t *Task) GetCreator() string {
return t.Creator
}
func (t *Task) GetResponses() Responses {
return t.Responses
}
func (t *Task) IsExpired(ctx sdk.Context) bool {
return t.Expiration.Before(ctx.BlockTime())
}
func (t *Task) GetValidTime() (int64, time.Time) {
return t.ExpireHeight, time.Time{}
}
func (t *Task) GetBounty() sdk.Coins {
return t.Bounty
}
func (t *Task) GetStatus() TaskStatus {
return t.Status
}
func (t *Task) GetScore() int64 {
return t.Result.Int64()
}
func (t *Task) AddResponse(response Response) {
t.Responses = append(t.Responses, response)
}
func (t *Task) SetStatus(status TaskStatus) {
t.Status = status
}
func (t *Task) SetScore(score int64) {
t.Result = sdk.NewInt(score)
}
func (t *Task) ShouldAgg(ctx sdk.Context) bool {
return t.ExpireHeight == ctx.BlockHeight()
}
func (t *TxTask) GetID() []byte {
return t.AtxHash
}
func (t *TxTask) GetCreator() string {
return t.Creator
}
func (t *TxTask) GetResponses() Responses {
return t.Responses
}
func (t *TxTask) IsExpired(ctx sdk.Context) bool {
return t.Expiration.Before(ctx.BlockTime())
}
func (t *TxTask) GetValidTime() (int64, time.Time) {
return -1, t.ValidTime
}
func (t *TxTask) GetBounty() sdk.Coins {
return t.Bounty
}
func (t *TxTask) GetStatus() TaskStatus {
return t.Status
}
func (t *TxTask) GetScore() int64 {
return t.Score
}
func (t *TxTask) AddResponse(response Response) {
t.Responses = append(t.Responses, response)
}
func (t *TxTask) SetStatus(status TaskStatus) {
t.Status = status
}
func (t *TxTask) SetScore(score int64) {
t.Score = score
}
func (t *TxTask) ShouldAgg(ctx sdk.Context) bool {
return !t.ValidTime.After(ctx.BlockTime())
}