render.gno
13.40 Kb · 342 lines
1package gauntlet
2
3import (
4 "strconv"
5 "strings"
6 "time"
7
8 "gno.land/p/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/gauntlet"
9 txlink "gno.land/p/moul/txlink/v0"
10 "gno.land/p/nt/markdown/sanitize/v0"
11)
12
13// Render serves the board in gnoweb.
14//
15// "" open bounties and recent winners
16// "c/<id>" one challenge: spec, tests, queue, result
17// "u/<address>" a solver's or poster's profile
18// "leaderboard" top earners
19// "judges" registered judges
20// "solve" how to submit a solution
21func Render(path string) string {
22 now := time.Now()
23 switch {
24 case path == "":
25 return renderHome(now)
26 case strings.HasPrefix(path, "c/"):
27 id, err := strconv.ParseUint(path[2:], 10, 64)
28 if err != nil {
29 return "# Not found\n\n[Back to the board](" + home() + ")"
30 }
31 return renderChallenge(id, now)
32 case strings.HasPrefix(path, "u/"):
33 a := address(path[2:])
34 if !a.IsValid() {
35 return "# Not found\n\n[Back to the board](" + home() + ")"
36 }
37 return renderProfile(a)
38 case path == "leaderboard":
39 return renderLeaderboard()
40 case path == "judges":
41 return renderJudges()
42 case path == "solve":
43 return renderSolveGuide()
44 }
45 return "# Not found\n\n[Back to the board](" + home() + ")"
46}
47
48func self() string { return webPath(selfPkgPath()) }
49func home() string { return self() }
50
51// tx builds a transaction link to this realm, whatever path it is deployed at.
52func tx(fn string) *txlink.TxBuilder { return txlink.Realm(selfPkgPath()).NewLink(fn) }
53
54func nav() string {
55 return "[Board](" + home() + ") · [Leaderboard](" + self() + ":leaderboard) · [Judges](" + self() +
56 ":judges) · [How to solve](" + self() + ":solve)\n\n"
57}
58
59func renderHome(now time.Time) string {
60 var b strings.Builder
61 b.WriteString("# Gauntlet\n\n")
62 b.WriteString("**Coding bounties graded by the chain, not by people.** Post a challenge with a GNOT reward. ")
63 b.WriteString("Solvers deploy a function, this realm runs the tests on-chain, and the earliest passing commit is paid instantly. ")
64 b.WriteString("No reviewer can refuse to pay, and copying someone's deployed code never wins.\n\n")
65 b.WriteString(nav())
66
67 open, escrow, solvers := 0, int64(0), profiles.Size()
68 board.Iterate(func(c *gauntlet.Challenge) bool {
69 if c.Status == gauntlet.Open {
70 open++
71 escrow += c.Pot()
72 }
73 return false
74 })
75 b.WriteString("| Open bounties | In escrow | Paid to solvers | Players |\n|---|---|---|---|\n")
76 b.WriteString("| " + strconv.Itoa(open) + " | " + gnot(escrow) + " | " + gnot(totalPaid) + " | " + strconv.Itoa(solvers) + " |\n\n")
77
78 b.WriteString("## Open bounties\n\n")
79 if open == 0 {
80 b.WriteString("_No open bounties right now. [Post one](" + tx("Post").URL() + ")._\n\n")
81 } else {
82 b.WriteString("| # | Challenge | Reward | Graded by | Attempts | Time |\n|---|---|---|---|---|---|\n")
83 board.Iterate(func(c *gauntlet.Challenge) bool {
84 if c.Status == gauntlet.Open {
85 b.WriteString("| " + itoa(c.ID) + " | [" + sanitize.TableCell(c.Title) + "](" + self() + ":c/" + itoa(c.ID) + ") | " +
86 gnot(c.Pot()) + " | " + gradedBy(c) + " | " + strconv.Itoa(len(c.Attempts)) + " | " + left(now, c.Deadline) + " |\n")
87 }
88 return false
89 })
90 b.WriteString("\n")
91 }
92
93 b.WriteString("## Recent winners\n\n")
94 if len(paidOut) == 0 {
95 b.WriteString("_Nobody has won yet. Be first._\n\n")
96 } else {
97 for i := len(paidOut) - 1; i >= 0 && i >= len(paidOut)-10; i-- {
98 p := paidOut[i]
99 c := board.Get(p.ChallengeID)
100 b.WriteString("- [" + short(p.To) + "](" + self() + ":u/" + p.To.String() + ") won **" + gnot(p.Amount) +
101 "** on [" + sanitize.InlineText(c.Title) + "](" + self() + ":c/" + itoa(c.ID) + ")\n")
102 }
103 b.WriteString("\n")
104 }
105
106 closed := 0
107 b.WriteString("## Finished\n\n")
108 board.Iterate(func(c *gauntlet.Challenge) bool {
109 if c.Status != gauntlet.Open {
110 closed++
111 b.WriteString("- [#" + itoa(c.ID) + " " + sanitize.InlineText(c.Title) + "](" + self() + ":c/" + itoa(c.ID) + ") · " + c.Status.String() + "\n")
112 }
113 return closed >= 20
114 })
115 if closed == 0 {
116 b.WriteString("_None yet._\n")
117 }
118 b.WriteString("\n---\n\n[Post a bounty with test cases](" + tx("Post").URL() + ") · " +
119 "[Post a judged bounty](" + tx("PostJudged").URL() + ")\n")
120 return b.String()
121}
122
123func gradedBy(c *gauntlet.Challenge) string {
124 if c.Judge != nil {
125 return "judge"
126 }
127 return strconv.Itoa(len(c.Cases)) + " tests"
128}
129
130func renderChallenge(id uint64, now time.Time) string {
131 c := board.Get(id)
132 if c == nil {
133 return "# Not found\n\n[Back to the board](" + home() + ")"
134 }
135 var b strings.Builder
136 b.WriteString("# #" + itoa(c.ID) + " " + sanitize.InlineText(c.Title) + "\n\n")
137 b.WriteString(nav())
138
139 switch c.Status {
140 case gauntlet.Open:
141 b.WriteString("> **Open** · reward **" + gnot(c.Pot()) + "** · commits close " + when(c.Deadline) + " (" + left(now, c.Deadline) + ")\n\n")
142 case gauntlet.Won:
143 w := c.Winner
144 b.WriteString("> **Won** by [" + short(w.Solver) + "](" + self() + ":u/" + w.Solver.String() + ") for **" + gnot(c.Pot()) +
145 "**. Winning code: [" + webPath(w.PkgPath) + "](" + webPath(w.PkgPath) + "$source)\n\n")
146 case gauntlet.Closed:
147 b.WriteString("> **Closed.** Nobody passed before the deadline; the reward went back to the poster.\n\n")
148 }
149
150 b.WriteString("| Reward | Bond per commit | Reveal window | Posted by | Posted |\n|---|---|---|---|---|\n")
151 b.WriteString("| " + gnot(c.Pot()) + " | " + gnot(c.Bond) + " | " + c.Window.String() + " | [" + short(c.Poster) + "](" +
152 self() + ":u/" + c.Poster.String() + ") | " + when(c.Created) + " |\n\n")
153 if c.Boosts > 0 || c.Forfeited > 0 {
154 b.WriteString("_Includes " + gnot(c.Boosts) + " in boosts and " + gnot(c.Forfeited) + " in forfeited bonds._\n\n")
155 }
156
157 b.WriteString("## The challenge\n\n")
158 if strings.TrimSpace(c.Spec) == "" {
159 b.WriteString("_No description._\n\n")
160 } else {
161 b.WriteString(sanitize.Block(c.Spec) + "\n\n")
162 }
163 b.WriteString("Write `func Solve(in string) string`.\n\n")
164
165 b.WriteString("## How it is graded\n\n")
166 if c.Judge != nil {
167 b.WriteString("A **judge** generates fresh inputs at the moment you reveal and checks each answer, so a lookup table cannot pass. ")
168 b.WriteString("Read the judge's code: [" + webPath(c.JudgePath) + "](" + webPath(c.JudgePath) + "$source).\n\n")
169 }
170 if len(c.Cases) > 0 {
171 b.WriteString("Every one of these must match exactly:\n\n")
172 for i, tc := range c.Cases {
173 if i >= 8 {
174 b.WriteString("_…and " + strconv.Itoa(len(c.Cases)-8) + " more. See them all in the realm source data or the web app._\n\n")
175 break
176 }
177 b.WriteString(sanitize.CodeBlock("Solve("+strconv.Quote(tc.In)+")\n// want "+strconv.Quote(tc.Want)) + "\n")
178 }
179 }
180
181 if c.Status == gauntlet.Open {
182 b.WriteString("## Enter\n\n")
183 b.WriteString("1. Pick a realm path for your solution, e.g. `gno.land/r/<your-address>/solve" + itoa(c.ID) + "`, and a secret salt.\n")
184 b.WriteString("2. Get your hash: `HashFor(\"<your realm path>\", \"<salt>\")` and [commit it](" +
185 tx("Commit").AddArgs("id", itoa(c.ID)).SetSend(i64toa(c.Bond)+"ugnot").URL() + ") with the " + gnot(c.Bond) + " bond.\n")
186 b.WriteString("3. Deploy your realm (template below), then call its `Submit(" + itoa(c.ID) + ", \"<salt>\")` within " + c.Window.String() + ".\n\n")
187 b.WriteString(sanitize.LanguageCodeBlock("go", solverTemplate(c.ID)) + "\n\n")
188 b.WriteString("[Boost the reward](" + tx("Boost").AddArgs("id", itoa(c.ID)).URL() + ") · " +
189 "[Settle](" + tx("Settle").AddArgs("id", itoa(c.ID)).URL() + ")\n\n")
190 }
191
192 b.WriteString("## Queue\n\n")
193 if len(c.Attempts) == 0 {
194 b.WriteString("_No commits yet._\n")
195 return b.String()
196 }
197 b.WriteString("Earlier commits always rank first. A later commit cannot win while an earlier one can still reveal.\n\n")
198 b.WriteString("| Place | Solver | Committed | Result | Solution |\n|---|---|---|---|---|\n")
199 for i, a := range c.Attempts {
200 result := a.State.String()
201 if a.State == gauntlet.Passed || a.State == gauntlet.Failed {
202 result += " (" + strconv.Itoa(a.Report.Passed) + "/" + strconv.Itoa(a.Report.Total) + ")"
203 }
204 if c.Winner == a {
205 result = "**winner** " + result
206 }
207 sol := "—"
208 if a.PkgPath != "" {
209 sol = "[source](" + webPath(a.PkgPath) + "$source)"
210 }
211 b.WriteString("| " + strconv.Itoa(i+1) + " | [" + short(a.Solver) + "](" + self() + ":u/" + a.Solver.String() + ") | " +
212 when(a.CommittedAt) + " | " + result + " | " + sol + " |\n")
213 }
214 for _, a := range c.Attempts {
215 if a.State == gauntlet.Failed && a.Report.FailIn != "" {
216 b.WriteString("\n**Failure** (" + short(a.Solver) + "): " + sanitize.InlineCode("Solve("+strconv.Quote(a.Report.FailIn)+")") +
217 " returned " + sanitize.InlineCode(strconv.Quote(a.Report.FailGot)))
218 if a.Report.FailWant != "" {
219 b.WriteString(", wanted " + sanitize.InlineCode(strconv.Quote(a.Report.FailWant)))
220 }
221 b.WriteString("\n")
222 }
223 }
224 return b.String()
225}
226
227func solverTemplate(id uint64) string {
228 return "package solve" + itoa(id) + "\n\n" +
229 "import \"" + selfPkgPath() + "\"\n\n" +
230 "func Solve(in string) string {\n\t// your code\n\treturn in\n}\n\n" +
231 "func Submit(cur realm, id uint64, salt string) {\n\tgauntlet.Reveal(cross(cur), id, salt, Solve)\n}"
232}
233
234func renderProfile(a address) string {
235 var b strings.Builder
236 b.WriteString("# " + short(a) + "\n\n" + nav())
237 v := profiles.Get(a.String())
238 if v == nil {
239 b.WriteString("_No activity yet._\n")
240 return b.String()
241 }
242 p := v.(*Profile)
243 b.WriteString("`" + a.String() + "`\n\n")
244 b.WriteString("| Solved | Earned | Attempts | Posted | Funded |\n|---|---|---|---|---|\n")
245 b.WriteString("| " + strconv.Itoa(p.Solved) + " | " + gnot(p.Earned) + " | " + strconv.Itoa(p.Attempts) + " | " +
246 strconv.Itoa(p.Posted) + " | " + gnot(p.Funded) + " |\n\n")
247 b.WriteString("## Wins\n\n")
248 wins := 0
249 for _, r := range paidOut {
250 if r.To == a {
251 c := board.Get(r.ChallengeID)
252 b.WriteString("- [" + sanitize.InlineText(c.Title) + "](" + self() + ":c/" + itoa(c.ID) + ") · " + gnot(r.Amount) + "\n")
253 wins++
254 }
255 }
256 if wins == 0 {
257 b.WriteString("_None yet._\n")
258 }
259 return b.String()
260}
261
262func renderLeaderboard() string {
263 var b strings.Builder
264 b.WriteString("# Leaderboard\n\n" + nav())
265 top := topSolvers(25)
266 if len(top) == 0 {
267 b.WriteString("_Nobody has solved a bounty yet._\n")
268 return b.String()
269 }
270 b.WriteString("| Rank | Solver | Solved | Earned |\n|---|---|---|---|\n")
271 for i, p := range top {
272 b.WriteString("| " + strconv.Itoa(i+1) + " | [" + short(p.Addr) + "](" + self() + ":u/" + p.Addr.String() + ") | " +
273 strconv.Itoa(p.Solved) + " | " + gnot(p.Earned) + " |\n")
274 }
275 return b.String()
276}
277
278// topSolvers returns profiles with at least one win, by earnings.
279func topSolvers(n int) []*Profile {
280 var out []*Profile
281 profiles.Iterate("", "", func(_ string, v any) bool {
282 p := v.(*Profile)
283 if p.Solved > 0 {
284 out = append(out, p)
285 }
286 return false
287 })
288 // insertion sort: small n, no allocations beyond the slice
289 for i := 1; i < len(out); i++ {
290 for j := i; j > 0 && better(out[j], out[j-1]); j-- {
291 out[j], out[j-1] = out[j-1], out[j]
292 }
293 }
294 if len(out) > n {
295 out = out[:n]
296 }
297 return out
298}
299
300func better(a, b *Profile) bool {
301 if a.Earned != b.Earned {
302 return a.Earned > b.Earned
303 }
304 return a.Solved > b.Solved
305}
306
307func renderJudges() string {
308 var b strings.Builder
309 b.WriteString("# Judges\n\n" + nav())
310 b.WriteString("A judge is a realm with two functions: one generates inputs from a seed, one checks an answer. ")
311 b.WriteString("It registers itself once and can never change, so anyone can post a bounty graded by it and trust the rules will not move.\n\n")
312 if judges.Size() == 0 {
313 b.WriteString("_No judges registered yet._\n")
314 return b.String()
315 }
316 b.WriteString("| Judge | About | Used by | Code |\n|---|---|---|---|\n")
317 judges.Iterate("", "", func(_ string, v any) bool {
318 j := v.(*JudgeInfo)
319 b.WriteString("| " + sanitize.TableCell(j.Name) + " | " + sanitize.TableCell(j.About) + " | " + strconv.Itoa(j.Uses) + " bounties | [" +
320 webPath(j.Path) + "](" + webPath(j.Path) + "$source) |\n")
321 return false
322 })
323 b.WriteString("\n[Post a judged bounty](" + tx("PostJudged").URL() + ")\n")
324 return b.String()
325}
326
327func renderSolveGuide() string {
328 var b strings.Builder
329 b.WriteString("# How to solve a bounty\n\n" + nav())
330 b.WriteString("Gauntlet uses **commit, deploy, reveal** so nobody can copy your answer.\n\n")
331 b.WriteString("1. **Commit.** Choose your solution's realm path and a secret salt. Compute `HashFor(path, salt)` and call `Commit(id, hash)` with the bond. Your place in line is now fixed.\n")
332 b.WriteString("2. **Deploy.** Publish your realm with `Solve` and `Submit` (template below). Anyone can now read it, but anyone who copies it commits after you.\n")
333 b.WriteString("3. **Reveal.** Call your realm's `Submit(id, salt)` before your reveal window ends. This realm reads your package path from the runtime, checks the hash, runs the tests and records the result. Your bond comes back either way.\n\n")
334 b.WriteString("The earliest commit that passes wins the whole pot. If you skip the reveal, your bond is added to the reward.\n\n")
335 b.WriteString(sanitize.LanguageCodeBlock("go", solverTemplate(1)) + "\n\n")
336 b.WriteString("## Rules the code enforces\n\n")
337 b.WriteString("- Solutions must live in a deployed `/r/` realm, so the winning code stays public on-chain.\n")
338 b.WriteString("- One pending commit per address per bounty. Failed? Commit again with a new realm.\n")
339 b.WriteString("- Your `Solve` cannot touch this realm's funds or state: Gno runs it under your realm's authority, and it never receives this realm's `cur`.\n")
340 b.WriteString("- The poster cannot cancel or change the tests. The reward is locked until someone wins or the deadline passes.\n")
341 return b.String()
342}