guild.gno
8.27 Kb · 326 lines
1// Package guild implements the quest state machine used by the Guildhall
2// bounty board. It holds no coins and keeps no globals: a realm posts a
3// Quest, locks the reward itself, and drives the quest through these pure
4// transition methods.
5package guild
6
7import (
8 "errors"
9 "strings"
10)
11
12// Field limits, in bytes, and block-count bounds for leases and review
13// windows.
14const (
15 MaxTitle = 80
16 MaxSpec = 2000
17 MaxURL = 300
18 MaxNote = 500
19 MinBlocks = 100
20 MaxBlocks = 500000
21)
22
23var (
24 ErrInvalidPoster = errors.New("guild: invalid poster address")
25 ErrEmptyTitle = errors.New("guild: title is required")
26 ErrTitleTooLong = errors.New("guild: title exceeds 80 bytes")
27 ErrSpecTooLong = errors.New("guild: spec exceeds 2000 bytes")
28 ErrInvalidReward = errors.New("guild: reward must be positive")
29 ErrInvalidLease = errors.New("guild: lease blocks out of range [100, 500000]")
30 ErrInvalidReview = errors.New("guild: review blocks out of range [100, 500000]")
31 ErrInvalidDeadline = errors.New("guild: deadline must be later than height plus lease")
32 ErrNotOpen = errors.New("guild: quest is not open")
33 ErrNotClaimed = errors.New("guild: quest is not claimed")
34 ErrNotSubmitted = errors.New("guild: quest is not submitted")
35 ErrNotPoster = errors.New("guild: caller is not the poster")
36 ErrNotWorker = errors.New("guild: caller is not the worker")
37 ErrSelfClaim = errors.New("guild: poster cannot claim their own quest")
38 ErrPastDeadline = errors.New("guild: deadline has passed")
39 ErrLeaseExpired = errors.New("guild: lease has expired")
40 ErrReviewOpen = errors.New("guild: review window is still open")
41 ErrReviewClosed = errors.New("guild: review window has closed")
42 ErrBeforeDeadline = errors.New("guild: deadline has not passed")
43 ErrSubmittedPending = errors.New("guild: quest has a pending submission")
44 ErrProofURLEmpty = errors.New("guild: proof URL is required")
45 ErrProofURLTooLong = errors.New("guild: proof URL exceeds 300 bytes")
46 ErrNoteTooLong = errors.New("guild: note exceeds 500 bytes")
47 ErrReasonTooLong = errors.New("guild: reason exceeds 500 bytes")
48)
49
50// Status is the lifecycle state of a Quest.
51type Status int
52
53const (
54 Open Status = iota
55 Claimed
56 Submitted
57 Paid
58 Refunded
59 Cancelled
60)
61
62func (s Status) String() string {
63 switch s {
64 case Open:
65 return "open"
66 case Claimed:
67 return "claimed"
68 case Submitted:
69 return "submitted"
70 case Paid:
71 return "paid"
72 case Refunded:
73 return "refunded"
74 case Cancelled:
75 return "cancelled"
76 }
77 return "unknown"
78}
79
80// Event records one state change in a quest's history.
81type Event struct {
82 Height int64
83 Actor address
84 Action string
85 Detail string
86}
87
88// Quest is a single bounty: posted with an escrowed reward, claimed under a
89// lease, submitted with proof, then paid, rejected back to open, refunded or
90// cancelled.
91type Quest struct {
92 ID uint64
93 Poster address
94 Title string
95 Spec string
96 Reward int64
97 LeaseBlocks int64
98 ReviewBlocks int64
99 Deadline int64
100 Status Status
101 Worker address
102 LeaseEnd int64
103 ProofURL string
104 Note string
105 SubmittedAt int64
106 AutoPaid bool
107 History []Event
108}
109
110// New validates its inputs and returns an Open quest with a "posted" event
111// recorded at height h.
112func New(id uint64, poster address, title, spec string, reward, leaseBlocks, reviewBlocks, deadline, h int64) (*Quest, error) {
113 if !poster.IsValid() {
114 return nil, ErrInvalidPoster
115 }
116 title = strings.TrimSpace(title)
117 if title == "" {
118 return nil, ErrEmptyTitle
119 }
120 if len(title) > MaxTitle {
121 return nil, ErrTitleTooLong
122 }
123 if len(spec) > MaxSpec {
124 return nil, ErrSpecTooLong
125 }
126 if reward <= 0 {
127 return nil, ErrInvalidReward
128 }
129 if leaseBlocks < MinBlocks || leaseBlocks > MaxBlocks {
130 return nil, ErrInvalidLease
131 }
132 if reviewBlocks < MinBlocks || reviewBlocks > MaxBlocks {
133 return nil, ErrInvalidReview
134 }
135 if deadline <= h+leaseBlocks {
136 return nil, ErrInvalidDeadline
137 }
138 q := &Quest{
139 ID: id,
140 Poster: poster,
141 Title: title,
142 Spec: spec,
143 Reward: reward,
144 LeaseBlocks: leaseBlocks,
145 ReviewBlocks: reviewBlocks,
146 Deadline: deadline,
147 Status: Open,
148 }
149 q.appendEvent(h, poster, "posted", "")
150 return q, nil
151}
152
153// StatusAt reports the effective status at height h: a Claimed quest whose
154// lease has run out reads as Open again.
155func (q *Quest) StatusAt(h int64) Status {
156 if q.Status == Claimed && h > q.LeaseEnd {
157 return Open
158 }
159 return q.Status
160}
161
162// ReviewEnd is the last height at which the poster may reject a submission.
163func (q *Quest) ReviewEnd() int64 {
164 return q.SubmittedAt + q.ReviewBlocks
165}
166
167// Claim lets w take the quest while it is Open — including a stale claim
168// whose lease expired. The poster can never claim their own quest.
169func (q *Quest) Claim(w address, h int64) error {
170 if q.StatusAt(h) != Open {
171 return ErrNotOpen
172 }
173 if w == q.Poster {
174 return ErrSelfClaim
175 }
176 if h >= q.Deadline {
177 return ErrPastDeadline
178 }
179 if q.Status == Claimed {
180 q.appendEvent(h, q.Worker, "expired", q.Worker.String())
181 }
182 q.Worker = w
183 q.LeaseEnd = h + q.LeaseBlocks
184 q.Status = Claimed
185 q.appendEvent(h, w, "claimed", "")
186 return nil
187}
188
189// Submit turns in the worker's proof while the lease is live.
190func (q *Quest) Submit(w address, h int64, proofURL, note string) error {
191 if q.Status != Claimed {
192 return ErrNotClaimed
193 }
194 if w != q.Worker {
195 return ErrNotWorker
196 }
197 if h > q.LeaseEnd {
198 return ErrLeaseExpired
199 }
200 if strings.TrimSpace(proofURL) == "" {
201 return ErrProofURLEmpty
202 }
203 if len(proofURL) > MaxURL {
204 return ErrProofURLTooLong
205 }
206 if len(note) > MaxNote {
207 return ErrNoteTooLong
208 }
209 q.ProofURL = proofURL
210 q.Note = note
211 q.SubmittedAt = h
212 q.Status = Submitted
213 q.appendEvent(h, w, "submitted", proofURL)
214 return nil
215}
216
217// Accept pays the submission out: the poster approves inside or past the
218// review window, there is no time limit on acceptance.
219func (q *Quest) Accept(p address, h int64) error {
220 if q.Status != Submitted {
221 return ErrNotSubmitted
222 }
223 if p != q.Poster {
224 return ErrNotPoster
225 }
226 q.Status = Paid
227 q.appendEvent(h, p, "accepted", "")
228 return nil
229}
230
231// Reject sends the quest back to Open, inside the review window, and clears
232// the worker. The cleared fields are preserved in the event detail.
233func (q *Quest) Reject(p address, h int64, reason string) error {
234 if q.Status != Submitted {
235 return ErrNotSubmitted
236 }
237 if p != q.Poster {
238 return ErrNotPoster
239 }
240 if h > q.ReviewEnd() {
241 return ErrReviewClosed
242 }
243 if len(reason) > MaxNote {
244 return ErrReasonTooLong
245 }
246 detail := "worker=" + q.Worker.String()
247 if q.ProofURL != "" {
248 detail += " proof=" + q.ProofURL
249 }
250 if q.Note != "" {
251 detail += " note=" + q.Note
252 }
253 if reason != "" {
254 detail += " reason=" + reason
255 }
256 q.Status = Open
257 q.Worker = ""
258 q.LeaseEnd = 0
259 q.ProofURL = ""
260 q.Note = ""
261 q.SubmittedAt = 0
262 q.appendEvent(h, p, "rejected", detail)
263 return nil
264}
265
266// AutoAccept pays the worker when the poster let the review window lapse:
267// silence counts as acceptance.
268func (q *Quest) AutoAccept(w address, h int64) error {
269 if q.Status != Submitted {
270 return ErrNotSubmitted
271 }
272 if w != q.Worker {
273 return ErrNotWorker
274 }
275 if h <= q.ReviewEnd() {
276 return ErrReviewOpen
277 }
278 q.Status = Paid
279 q.AutoPaid = true
280 q.appendEvent(h, w, "auto-accepted", "")
281 return nil
282}
283
284// Refund returns the escrow to the poster once the deadline has passed, for
285// an Open quest or a claim whose lease went stale. A pending submission can
286// never be refunded.
287func (q *Quest) Refund(p address, h int64) error {
288 if q.Status == Submitted {
289 return ErrSubmittedPending
290 }
291 if q.StatusAt(h) != Open {
292 return ErrNotOpen
293 }
294 if p != q.Poster {
295 return ErrNotPoster
296 }
297 if h < q.Deadline {
298 return ErrBeforeDeadline
299 }
300 q.Status = Refunded
301 q.appendEvent(h, p, "refunded", "")
302 return nil
303}
304
305// Cancel withdraws an Open quest — or one whose claim went stale — and frees
306// the escrow back to the poster.
307func (q *Quest) Cancel(p address, h int64) error {
308 if q.StatusAt(h) != Open {
309 return ErrNotOpen
310 }
311 if p != q.Poster {
312 return ErrNotPoster
313 }
314 q.Status = Cancelled
315 q.appendEvent(h, p, "cancelled", "")
316 return nil
317}
318
319func (q *Quest) appendEvent(h int64, actor address, action, detail string) {
320 q.History = append(q.History, Event{
321 Height: h,
322 Actor: actor,
323 Action: action,
324 Detail: detail,
325 })
326}