advent.gno
6.43 Kb · 262 lines
1// Package advent is Gnoël, an on-chain advent calendar with sealed guesses.
2package advent
3
4import (
5 "encoding/base64"
6 "strconv"
7 "strings"
8 "time"
9
10 "chain/runtime/unsafe"
11
12 "gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/tally/v0"
13 "gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/ui/v0"
14 "gno.land/p/nt/bptree/v0"
15)
16
17const (
18 Days = 24
19 ReviewLimit = 16
20 PageLimit = 32
21 organizer = address("g1manfred47kzduec920z88wfr64ylksmdcedlf5")
22)
23
24const (
25 pending = iota + 1
26 missed
27 solved
28)
29
30type attempt struct {
31 player string
32 commitment string
33 sealedSalt string
34 status int
35}
36
37type challenge struct {
38 title, clue, publicKey string
39 reviewFrom int64
40 submitted, reviewed int
41 solves int
42 attempts, latest *bptree.BPTree
43}
44
45var (
46 self = unsafe.CurrentRealm().PkgPath()
47 days [Days]*challenge
48 oracle = organizer
49 board = tally.NewBoard()
50)
51
52func caller(cur realm) address {
53 if !cur.IsCurrent() {
54 panic("spoofed realm")
55 }
56 return cur.Previous().Address()
57}
58
59func SetOracle(cur realm, signer address) {
60 if caller(cur) != organizer {
61 panic("organizer only")
62 }
63 if signer.String() == "" {
64 panic("missing oracle address")
65 }
66 oracle = signer
67}
68
69// Publish opens a case. The RSA-2048 SPKI public key is pinned for
70// that case so that rotating the key for a new door leaves old doors playable.
71func Publish(cur realm, day int, title, clue, publicKey string, reviewFrom int64) {
72 if caller(cur) != organizer {
73 panic("organizer only")
74 }
75 openDoor(day, title, clue, publicKey, reviewFrom)
76}
77
78func openDoor(day int, title, clue, publicKey string, reviewFrom int64) {
79 if day < 1 || day > Days || doors(day) != nil {
80 panic("invalid or already open door")
81 }
82 if len(title) > 100 || strings.TrimSpace(ui.Inline(title)) == "" {
83 panic("invalid title")
84 }
85 assertClue(clue)
86 if !lowerHex(publicKey, 588) {
87 panic("expected a hex RSA-2048 public key")
88 }
89 now := time.Now().Unix()
90 if reviewFrom <= now || reviewFrom > now+26*60*60 {
91 panic("review must begin in the next 26 hours")
92 }
93 days[day-1] = &challenge{
94 title: title, clue: clue, publicKey: publicKey, reviewFrom: reviewFrom,
95 attempts: bptree.NewBPTreeN(128), latest: bptree.NewBPTreeN(128),
96 }
97}
98
99// UpdateClue corrects the text of an open door while keeping its review
100// schedule, oracle key and submissions intact.
101func UpdateClue(cur realm, day int, clue string) {
102 if caller(cur) != organizer {
103 panic("organizer only")
104 }
105 d := requireDoor(day)
106 assertClue(clue)
107 d.clue = clue
108}
109
110func assertClue(clue string) {
111 if len(clue) > 2500 || strings.TrimSpace(ui.Inline(clue)) == "" {
112 panic("invalid clue")
113 }
114}
115
116// Submit publicly records the commitment and its encrypted random salt.
117func Submit(cur realm, day int, commitment, sealedSalt string) int {
118 player := caller(cur).String()
119 d := requireDoor(day)
120 if !lowerHex(commitment, 64) || !lowerHex(sealedSalt, 512) {
121 panic("invalid sealed guess")
122 }
123 if status := d.status(player); status == pending || status == solved {
124 panic("you already have a pending guess or a solve")
125 }
126 d.submitted++
127 d.attempts.Set(strconv.Itoa(d.submitted), &attempt{player: player, commitment: commitment, sealedSalt: sealedSalt, status: pending})
128 d.latest.Set(player, d.submitted)
129 return d.submitted
130}
131
132// Review records one verdict per consecutive attempt, starting at the first
133// pending ID. A stale batch fails before it can apply verdicts to other guesses.
134func Review(cur realm, day, start int, verdicts string) {
135 if caller(cur) != oracle {
136 panic("oracle only")
137 }
138 d := requireDoor(day)
139 if time.Now().Unix() < d.reviewFrom {
140 panic("this case is still under seal")
141 }
142 if start != d.reviewed+1 || len(verdicts) == 0 || len(verdicts) > ReviewLimit || len(verdicts) > d.submitted-d.reviewed {
143 panic("invalid review range")
144 }
145 for i := 0; i < len(verdicts); i++ {
146 if verdicts[i] != '0' && verdicts[i] != '1' {
147 panic("verdict must be 0 or 1")
148 }
149 }
150 for i := 0; i < len(verdicts); i++ {
151 a := d.attempt(start + i)
152 if verdicts[i] == '1' {
153 a.status = solved
154 d.solves++
155 board.Add(a.player, 1)
156 } else {
157 a.status = missed
158 }
159 }
160 d.reviewed += len(verdicts)
161}
162
163func doors(day int) *challenge {
164 if day < 1 || day > Days {
165 return nil
166 }
167 return days[day-1]
168}
169
170func requireDoor(day int) *challenge {
171 d := doors(day)
172 if d == nil {
173 panic("this door is still locked")
174 }
175 return d
176}
177
178func (d *challenge) attempt(id int) *attempt {
179 return d.attempts.Get(strconv.Itoa(id)).(*attempt)
180}
181
182func (d *challenge) status(player string) int {
183 if id := d.latest.Get(player); id != nil {
184 return d.attempt(id.(int)).status
185 }
186 return 0
187}
188
189func lowerHex(s string, size int) bool {
190 if len(s) != size {
191 return false
192 }
193 for i := 0; i < len(s); i++ {
194 b := s[i]
195 if !(b >= '0' && b <= '9' || b >= 'a' && b <= 'f') {
196 return false
197 }
198 }
199 return true
200}
201
202// Calendar returns tab-separated rows for open doors, with title and clue in base64.
203func Calendar() string {
204 var out strings.Builder
205 for i, d := range days {
206 if d == nil {
207 continue
208 }
209 out.WriteString(strings.Join([]string{
210 strconv.Itoa(i + 1), strconv.Itoa(d.submitted), strconv.Itoa(d.reviewed), strconv.Itoa(d.solves),
211 strconv.FormatInt(d.reviewFrom, 10), d.publicKey,
212 base64.StdEncoding.EncodeToString([]byte(d.title)), base64.StdEncoding.EncodeToString([]byte(d.clue)),
213 }, "\t"))
214 out.WriteByte('\n')
215 }
216 return out.String()
217}
218
219// Attempts returns one bounded page of the public record, starting at a 1-based ID.
220func Attempts(day, start int) string {
221 d := requireDoor(day)
222 if start < 1 || start > d.submitted {
223 return ""
224 }
225 var out strings.Builder
226 for id := start; id <= d.submitted && id-start < PageLimit; id++ {
227 a := d.attempt(id)
228 out.WriteString(strings.Join([]string{strconv.Itoa(id), a.player, a.commitment, a.sealedSalt, strconv.Itoa(a.status)}, "\t"))
229 out.WriteByte('\n')
230 }
231 return out.String()
232}
233
234// Progress lists the latest status of an address across all 24 doors.
235func Progress(player string) string {
236 var out strings.Builder
237 for _, d := range days {
238 status := 0
239 if d != nil {
240 status = d.status(player)
241 }
242 out.WriteByte(byte('0' + status))
243 }
244 return out.String()
245}
246
247// Leaders returns the top 24 addresses with their confirmed solves.
248func Leaders() string {
249 var out strings.Builder
250 for _, e := range leaders() {
251 out.WriteString(e.Key + "\t" + strconv.FormatInt(e.Score, 10) + "\n")
252 }
253 return out.String()
254}
255
256func leaders() []tally.Entry {
257 entries := board.Entries()
258 if len(entries) > Days {
259 return entries[:Days]
260 }
261 return entries
262}