gauntlet.gno
7.25 Kb · 256 lines
1// Package gauntlet is a bounty board where the chain grades the answers.
2//
3// Post a coding challenge with a GNOT reward. Solvers commit a hash, deploy a
4// realm with a Solve function, and reveal it. This realm runs the tests itself
5// and pays the earliest commit that passes. No reviewer, no disputes.
6package gauntlet
7
8import (
9 "chain"
10 "chain/banker"
11 "chain/runtime/unsafe"
12 "strings"
13 "time"
14
15 "gno.land/p/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/gauntlet"
16 "gno.land/p/nt/avl/v0"
17)
18
19const denom = "ugnot"
20
21var (
22 board gauntlet.Board
23 profiles avl.Tree // address -> *Profile
24 judges avl.Tree // judge realm path -> *JudgeInfo
25
26 pkgPath = unsafe.CurrentRealm().PkgPath()
27
28 totalPaid int64 // ugnot paid to winners
29 paidOut []*PaidRecord
30)
31
32// Profile tracks what an address has done on the board.
33type Profile struct {
34 Addr address
35 Attempts int
36 Solved int
37 Earned int64
38 Posted int
39 Funded int64
40}
41
42// JudgeInfo is a registered judge: reusable, immutable test logic.
43type JudgeInfo struct {
44 Path string
45 Name string
46 About string
47 Judge *gauntlet.Judge
48 Uses int
49}
50
51// PaidRecord is one winner payout, for the activity feed.
52type PaidRecord struct {
53 ChallengeID uint64
54 To address
55 Amount int64
56 At time.Time
57}
58
59// Post creates a challenge with fixed test cases. Send the reward in ugnot
60// with the call. cases has one test per line: `input => expected output`.
61func Post(cur realm, title, spec, cases string, hours int, bond int64, windowMinutes int) uint64 {
62 poster := mustUser(cur)
63 reward := sentUgnot()
64 parsed, err := gauntlet.ParseCases(cases)
65 if err != nil {
66 panic(err)
67 }
68 return post(poster, gauntlet.PostParams{
69 Title: title, Spec: spec, Poster: poster, Reward: reward, Bond: bond,
70 Deadline: time.Now().Add(time.Duration(hours) * time.Hour),
71 Window: time.Duration(windowMinutes) * time.Minute,
72 Cases: parsed,
73 })
74}
75
76// PostJudged creates a challenge graded by a registered judge (see
77// RegisterJudge), plus optional fixed cases. Send the reward with the call.
78func PostJudged(cur realm, title, spec, judgePath, cases string, hours int, bond int64, windowMinutes int) uint64 {
79 poster := mustUser(cur)
80 reward := sentUgnot()
81 v := judges.Get(judgePath)
82 if v == nil {
83 panic("gauntlet: no judge registered at " + judgePath)
84 }
85 ji := v.(*JudgeInfo)
86 var parsed []gauntlet.Case
87 if strings.TrimSpace(cases) != "" {
88 var err error
89 parsed, err = gauntlet.ParseCases(cases)
90 if err != nil {
91 panic(err)
92 }
93 }
94 id := post(poster, gauntlet.PostParams{
95 Title: title, Spec: spec, Poster: poster, Reward: reward, Bond: bond,
96 Deadline: time.Now().Add(time.Duration(hours) * time.Hour),
97 Window: time.Duration(windowMinutes) * time.Minute,
98 Cases: parsed,
99 Judge: ji.Judge,
100 JudgePath: ji.Path,
101 })
102 ji.Uses++
103 return id
104}
105
106func post(poster address, p gauntlet.PostParams) uint64 {
107 c, err := board.Post(p, time.Now())
108 if err != nil {
109 panic(err)
110 }
111 pr := profile(poster)
112 pr.Posted++
113 pr.Funded += p.Reward
114 chain.Emit("Posted", "id", itoa(c.ID), "poster", poster.String(), "reward", i64toa(p.Reward))
115 return c.ID
116}
117
118// RegisterJudge is called by a judge realm, usually from its init. The judge
119// is stored under the caller's package path and can never change, so every
120// bounty that uses it is graded by exactly the code anyone can read.
121func RegisterJudge(cur realm, name, about string, inputs func(seed uint64) []string, check func(in, out string) bool) {
122 prev := cur.Previous()
123 if prev.IsUser() {
124 panic("gauntlet: judges must be registered by a deployed realm")
125 }
126 path := prev.PkgPath()
127 if judges.Has(path) {
128 panic("gauntlet: judge already registered for " + path)
129 }
130 if inputs == nil || check == nil {
131 panic("gauntlet: judge needs both inputs and check")
132 }
133 judges.Set(path, &JudgeInfo{
134 Path: path, Name: name, About: about,
135 Judge: &gauntlet.Judge{Inputs: inputs, Check: check},
136 })
137 chain.Emit("JudgeRegistered", "path", path)
138}
139
140// Commit reserves your place in line with sha256(solutionPath + ":" + salt).
141// Send the challenge's bond with the call. Use HashFor to compute the hash.
142func Commit(cur realm, id uint64, hash string) {
143 solver := mustUser(cur)
144 if _, err := board.Commit(id, solver, hash, sentUgnot(), time.Now()); err != nil {
145 panic(err)
146 }
147 profile(solver).Attempts++
148 chain.Emit("Committed", "id", itoa(id), "solver", solver.String())
149}
150
151// Reveal is called by your solution realm, never by a wallet directly:
152//
153// func Submit(cur realm, id uint64, salt string) {
154// gauntlet.Reveal(cross(cur), id, salt, Solve)
155// }
156//
157// The realm path of the caller is read from the runtime, so nobody can reveal
158// on behalf of a commit they do not own. The tests run here, on-chain.
159func Reveal(cur realm, id uint64, salt string, solve func(string) string) {
160 prev := cur.Previous()
161 if !prev.IsCode() || prev.IsUser() {
162 panic(gauntlet.ErrNotRealm)
163 }
164 if solve == nil {
165 panic("gauntlet: nil solver")
166 }
167 a, payouts, err := board.Reveal(id, prev.PkgPath(), salt, solve, time.Now())
168 if err != nil {
169 panic(err)
170 }
171 chain.Emit("Revealed", "id", itoa(id), "solver", a.Solver.String(), "state", a.State.String(),
172 "passed", itoa(uint64(a.Report.Passed)), "total", itoa(uint64(a.Report.Total)))
173 pay(cur, id, payouts)
174}
175
176// Settle expires late commits and pays out when the result is final.
177// Anyone can call it.
178func Settle(cur realm, id uint64) {
179 payouts, err := board.Settle(id, time.Now())
180 if err != nil {
181 panic(err)
182 }
183 pay(cur, id, payouts)
184}
185
186// Boost adds the ugnot sent with the call to an open challenge's reward.
187func Boost(cur realm, id uint64) {
188 from := mustUser(cur)
189 amt := sentUgnot()
190 if err := board.Boost(id, amt, time.Now()); err != nil {
191 panic(err)
192 }
193 profile(from).Funded += amt
194 chain.Emit("Boosted", "id", itoa(id), "from", from.String(), "amount", i64toa(amt))
195}
196
197// HashFor computes the commit hash for a solution realm path and a salt.
198// Keep the salt secret until you reveal.
199func HashFor(pkgPath, salt string) string {
200 return gauntlet.HashSolution(pkgPath, salt)
201}
202
203func pay(cur realm, id uint64, payouts []gauntlet.Payout) {
204 if len(payouts) == 0 {
205 return
206 }
207 bk := banker.NewBanker(banker.BankerTypeRealmSend, cur)
208 self := cur.Address()
209 c := board.Get(id)
210 for _, p := range payouts {
211 if p.Amount <= 0 {
212 continue
213 }
214 bk.SendCoins(self, p.To, chain.Coins{chain.NewCoin(denom, p.Amount)})
215 if p.Reason == "bounty won" {
216 totalPaid += p.Amount
217 pr := profile(p.To)
218 pr.Solved++
219 pr.Earned += p.Amount
220 paidOut = append(paidOut, &PaidRecord{ChallengeID: c.ID, To: p.To, Amount: p.Amount, At: time.Now()})
221 chain.Emit("Won", "id", itoa(c.ID), "winner", p.To.String(), "amount", i64toa(p.Amount))
222 }
223 }
224}
225
226func mustUser(cur realm) address {
227 prev := cur.Previous()
228 if !prev.IsUserCall() {
229 panic("gauntlet: call this directly from a wallet (maketx call)")
230 }
231 return prev.Address()
232}
233
234// sentUgnot returns the ugnot sent with a direct wallet call. Other denoms
235// are rejected so nothing gets stuck.
236func sentUgnot() int64 {
237 var amt int64
238 for _, c := range unsafe.OriginSend() {
239 if c.Denom != denom {
240 panic("gauntlet: only ugnot is accepted")
241 }
242 amt += c.Amount
243 }
244 return amt
245}
246
247func profile(a address) *Profile {
248 if v := profiles.Get(a.String()); v != nil {
249 return v.(*Profile)
250 }
251 p := &Profile{Addr: a}
252 profiles.Set(a.String(), p)
253 return p
254}
255
256func selfPkgPath() string { return pkgPath }