state.gno
9.56 Kb · 352 lines
1// Package hearthbounty implements the HearthBounty v1 micro-bounty escrow,
2// per-contributor top-up custody, deterministic review/dispute timeouts,
3// verifiable proof registry, reputation leaderboard, and gnoweb UI on Gno.land.
4package hearthbounty
5
6import (
7 "strings"
8
9 "gno.land/p/g1x582angdzrqadgv4382j6y7ju6fj5wll6yuuq4/hearthcore/v0"
10 "gno.land/p/nt/avl/v0"
11 "gno.land/p/nt/mux/v0"
12 "gno.land/p/nt/seqid/v0"
13)
14
15const (
16 // EscrowTokenKey identifies the official 1:1 ugnot-backed wrapped GNOT
17 // token registered in gno.land/r/nt/grc20reg/v0.
18 EscrowTokenKey = "gno.land/r/gnoland/wugnot.wugnot"
19
20 // MinTopUpUgnot matches the 0.10 GNOT floor so dust spam cannot grief donor ledgers.
21 MinTopUpUgnot int64 = hearthcore.MinRewardUgnot
22
23 // MaxEscrowUgnot caps a single bounty's escrow (1,000,000 GNOT) to prevent int64 overflow.
24 MaxEscrowUgnot int64 = 1000000000000
25
26 // MaxExternalDonors bounds deterministic donor iteration during cancel/dispute refunds.
27 MaxExternalDonors = 20
28
29 // BoardPageSize caps rendered rows per gnoweb page well below the 1 MiB ceiling.
30 BoardPageSize = 20
31)
32
33type bountyRecord struct {
34 ID uint64
35 Title string
36 Spec string
37 Category hearthcore.Category
38 Status hearthcore.Status
39 Sponsor address
40 Claimant address
41 ClaimNote string
42 EscrowUgnot int64
43 InitialUgnot int64
44 SponsorFundedUgnot int64
45 ExternalTopUpUgnot int64
46 DonorShares *avl.Tree // non-sponsor address string -> int64 ugnot
47 PayoutUgnot int64
48 RefundUgnot int64
49 TopUpCount uint64
50 DeadlineBlocks int64
51 CreatedHeight int64
52 ClaimedHeight int64
53 DeadlineHeight int64
54 SubmittedHeight int64
55 ReviewDeadlineHeight int64
56 DisputedHeight int64
57 DisputeDeadlineHeight int64
58 SettledHeight int64
59 ProofURL string
60 ProofDigest string
61 ProofSummary string
62 SubmissionCount uint64
63 RevisionFeedback string
64 Rating uint8
65 ReviewNote string
66 DisputeReason string
67 DisputeOpenedBy address
68}
69
70type profileRecord struct {
71 Address address
72 Handle string
73 Bio string
74 Skills string
75 AgentEndpoint string
76 IsAgent bool
77 PostedCount uint64
78 ClaimedCount uint64
79 SettledCount uint64
80 SponsoredSettled uint64
81 EarnedUgnot int64
82 SponsoredUgnot int64
83 RatingSum uint64
84 RatingCount uint64
85 RegisteredHeight int64
86}
87
88// BountyView is an immutable value-copy projection of a bounty safe for external callers.
89type BountyView struct {
90 ID uint64
91 Title string
92 Spec string
93 Category hearthcore.Category
94 Status hearthcore.Status
95 Sponsor address
96 Claimant address
97 ClaimNote string
98 EscrowUgnot int64
99 InitialUgnot int64
100 SponsorFundedUgnot int64
101 ExternalTopUpUgnot int64
102 ExternalDonorCount int
103 PayoutUgnot int64
104 RefundUgnot int64
105 TopUpCount uint64
106 DeadlineBlocks int64
107 CreatedHeight int64
108 ClaimedHeight int64
109 DeadlineHeight int64
110 SubmittedHeight int64
111 ReviewDeadlineHeight int64
112 DisputedHeight int64
113 DisputeDeadlineHeight int64
114 SettledHeight int64
115 ProofURL string
116 ProofDigest string
117 ProofSummary string
118 SubmissionCount uint64
119 RevisionFeedback string
120 Rating uint8
121 ReviewNote string
122 DisputeReason string
123 DisputeOpenedBy address
124}
125
126// ProfileView is an immutable value-copy projection of a builder or AI agent profile.
127type ProfileView struct {
128 Address address
129 Handle string
130 Bio string
131 Skills string
132 AgentEndpoint string
133 IsAgent bool
134 Tier hearthcore.Tier
135 PostedCount uint64
136 ClaimedCount uint64
137 SettledCount uint64
138 SponsoredSettled uint64
139 EarnedUgnot int64
140 SponsoredUgnot int64
141 RatingSum uint64
142 RatingCount uint64
143 RegisteredHeight int64
144}
145
146// BoardStats summarizes global protocol activity and escrow invariants.
147type BoardStats struct {
148 TotalBounties int
149 OpenCount int
150 ClaimedCount int
151 SubmittedCount int
152 DisputedCount int
153 SettledCount int
154 CancelledCount int
155 ProfileCount int
156 AgentCount int
157 TotalEscrowUgnot int64
158 TotalSettledUgnot int64
159 TotalRefundUgnot int64
160}
161
162// All state variables are unexported so no external package can mutate realm state directly.
163var (
164 arbiter address
165 realmAddress address
166 realmPkgPath string
167 realmWebBase string
168 bountySeq seqid.ID
169 bounties *avl.Tree // seqid string -> *bountyRecord
170 profiles *avl.Tree // address string -> *profileRecord
171 openCount int
172 claimedCount int
173 submittedCount int
174 disputedCount int
175 settledCount int
176 cancelledCount int
177 agentCount int
178 totalEscrowUgnot int64
179 totalSettledUgnot int64
180 totalRefundUgnot int64
181 router *mux.Router
182)
183
184func init(cur realm) {
185 realmAddress = cur.Address()
186 realmPkgPath = cur.PkgPath()
187 realmWebBase = deriveWebBase(realmPkgPath)
188 if deployer := cur.Previous().Address(); deployer.IsValid() {
189 arbiter = deployer
190 }
191 bounties = avl.NewTree()
192 profiles = avl.NewTree()
193 initRouter()
194}
195
196func deriveWebBase(pkgPath string) string {
197 idx := strings.Index(pkgPath, "/")
198 if idx == -1 {
199 return "/r/g1x582angdzrqadgv4382j6y7ju6fj5wll6yuuq4/hearthbounty/v1"
200 }
201 return pkgPath[idx:]
202}
203
204func bountyKey(id uint64) string {
205 return seqid.ID(id).String()
206}
207
208func getBountyRecord(id uint64) (*bountyRecord, bool) {
209 if id == 0 {
210 return nil, false
211 }
212 raw := bounties.Get(bountyKey(id))
213 if raw == nil {
214 return nil, false
215 }
216 return raw.(*bountyRecord), true
217}
218
219func getOrCreateProfile(addr address, height int64) *profileRecord {
220 key := string(addr)
221 if raw := profiles.Get(key); raw != nil {
222 return raw.(*profileRecord)
223 }
224 p := &profileRecord{
225 Address: addr,
226 Handle: hearthcore.ShortAddr(addr),
227 RegisteredHeight: height,
228 }
229 profiles.Set(key, p)
230 return p
231}
232
233func toBountyView(b *bountyRecord) BountyView {
234 donorCount := 0
235 if b.DonorShares != nil {
236 donorCount = b.DonorShares.Size()
237 }
238 return BountyView{
239 ID: b.ID,
240 Title: b.Title,
241 Spec: b.Spec,
242 Category: b.Category,
243 Status: b.Status,
244 Sponsor: b.Sponsor,
245 Claimant: b.Claimant,
246 ClaimNote: b.ClaimNote,
247 EscrowUgnot: b.EscrowUgnot,
248 InitialUgnot: b.InitialUgnot,
249 SponsorFundedUgnot: b.SponsorFundedUgnot,
250 ExternalTopUpUgnot: b.ExternalTopUpUgnot,
251 ExternalDonorCount: donorCount,
252 PayoutUgnot: b.PayoutUgnot,
253 RefundUgnot: b.RefundUgnot,
254 TopUpCount: b.TopUpCount,
255 DeadlineBlocks: b.DeadlineBlocks,
256 CreatedHeight: b.CreatedHeight,
257 ClaimedHeight: b.ClaimedHeight,
258 DeadlineHeight: b.DeadlineHeight,
259 SubmittedHeight: b.SubmittedHeight,
260 ReviewDeadlineHeight: b.ReviewDeadlineHeight,
261 DisputedHeight: b.DisputedHeight,
262 DisputeDeadlineHeight: b.DisputeDeadlineHeight,
263 SettledHeight: b.SettledHeight,
264 ProofURL: b.ProofURL,
265 ProofDigest: b.ProofDigest,
266 ProofSummary: b.ProofSummary,
267 SubmissionCount: b.SubmissionCount,
268 RevisionFeedback: b.RevisionFeedback,
269 Rating: b.Rating,
270 ReviewNote: b.ReviewNote,
271 DisputeReason: b.DisputeReason,
272 DisputeOpenedBy: b.DisputeOpenedBy,
273 }
274}
275
276func toProfileView(p *profileRecord) ProfileView {
277 return ProfileView{
278 Address: p.Address,
279 Handle: p.Handle,
280 Bio: p.Bio,
281 Skills: p.Skills,
282 AgentEndpoint: p.AgentEndpoint,
283 IsAgent: p.IsAgent,
284 Tier: hearthcore.ComputeTier(p.SettledCount, p.EarnedUgnot, p.RatingSum, p.RatingCount),
285 PostedCount: p.PostedCount,
286 ClaimedCount: p.ClaimedCount,
287 SettledCount: p.SettledCount,
288 SponsoredSettled: p.SponsoredSettled,
289 EarnedUgnot: p.EarnedUgnot,
290 SponsoredUgnot: p.SponsoredUgnot,
291 RatingSum: p.RatingSum,
292 RatingCount: p.RatingCount,
293 RegisteredHeight: p.RegisteredHeight,
294 }
295}
296
297func decrementStatusCount(st hearthcore.Status) {
298 switch st {
299 case hearthcore.StatusOpen:
300 openCount--
301 case hearthcore.StatusClaimed:
302 claimedCount--
303 case hearthcore.StatusSubmitted:
304 submittedCount--
305 case hearthcore.StatusDisputed:
306 disputedCount--
307 case hearthcore.StatusSettled:
308 settledCount--
309 case hearthcore.StatusCancelled:
310 cancelledCount--
311 }
312}
313
314func incrementStatusCount(st hearthcore.Status) {
315 switch st {
316 case hearthcore.StatusOpen:
317 openCount++
318 case hearthcore.StatusClaimed:
319 claimedCount++
320 case hearthcore.StatusSubmitted:
321 submittedCount++
322 case hearthcore.StatusDisputed:
323 disputedCount++
324 case hearthcore.StatusSettled:
325 settledCount++
326 case hearthcore.StatusCancelled:
327 cancelledCount++
328 }
329}
330
331func transitionStatus(b *bountyRecord, next hearthcore.Status) {
332 if b.Status == next {
333 return
334 }
335 decrementStatusCount(b.Status)
336 b.Status = next
337 incrementStatusCount(next)
338}
339
340func decrementSponsoredCredit(addr address, amount int64) {
341 if amount <= 0 {
342 return
343 }
344 if raw := profiles.Get(string(addr)); raw != nil {
345 p := raw.(*profileRecord)
346 if p.SponsoredUgnot >= amount {
347 p.SponsoredUgnot -= amount
348 } else {
349 p.SponsoredUgnot = 0
350 }
351 }
352}