advent.gno
5.89 Kb · 240 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/moul/kit/tally/v0"
13 "gno.land/p/moul/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 one immutable 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 if day < 1 || day > Days || doors(day) != nil {
76 panic("invalid or already open door")
77 }
78 if len(title) > 100 || strings.TrimSpace(ui.Inline(title)) == "" || len(clue) > 2500 || strings.TrimSpace(ui.Inline(clue)) == "" {
79 panic("invalid title or clue")
80 }
81 if !lowerHex(publicKey, 588) {
82 panic("expected a hex RSA-2048 public key")
83 }
84 now := time.Now().Unix()
85 if reviewFrom <= now || reviewFrom > now+26*60*60 {
86 panic("review must begin in the next 26 hours")
87 }
88 days[day-1] = &challenge{
89 title: title, clue: clue, publicKey: publicKey, reviewFrom: reviewFrom,
90 attempts: bptree.NewBPTreeN(128), latest: bptree.NewBPTreeN(128),
91 }
92}
93
94// Submit publicly records the commitment and its encrypted random salt.
95func Submit(cur realm, day int, commitment, sealedSalt string) int {
96 player := caller(cur).String()
97 d := requireDoor(day)
98 if !lowerHex(commitment, 64) || !lowerHex(sealedSalt, 512) {
99 panic("invalid sealed guess")
100 }
101 if status := d.status(player); status == pending || status == solved {
102 panic("you already have a pending guess or a solve")
103 }
104 d.submitted++
105 d.attempts.Set(strconv.Itoa(d.submitted), &attempt{player: player, commitment: commitment, sealedSalt: sealedSalt, status: pending})
106 d.latest.Set(player, d.submitted)
107 return d.submitted
108}
109
110// Review records one verdict per consecutive attempt, starting at the first
111// pending ID. A stale batch fails before it can apply verdicts to other guesses.
112func Review(cur realm, day, start int, verdicts string) {
113 if caller(cur) != oracle {
114 panic("oracle only")
115 }
116 d := requireDoor(day)
117 if time.Now().Unix() < d.reviewFrom {
118 panic("this case is still under seal")
119 }
120 if start != d.reviewed+1 || len(verdicts) == 0 || len(verdicts) > ReviewLimit || len(verdicts) > d.submitted-d.reviewed {
121 panic("invalid review range")
122 }
123 for i := 0; i < len(verdicts); i++ {
124 if verdicts[i] != '0' && verdicts[i] != '1' {
125 panic("verdict must be 0 or 1")
126 }
127 }
128 for i := 0; i < len(verdicts); i++ {
129 a := d.attempt(start + i)
130 if verdicts[i] == '1' {
131 a.status = solved
132 d.solves++
133 board.Add(a.player, 1)
134 } else {
135 a.status = missed
136 }
137 }
138 d.reviewed += len(verdicts)
139}
140
141func doors(day int) *challenge {
142 if day < 1 || day > Days {
143 return nil
144 }
145 return days[day-1]
146}
147
148func requireDoor(day int) *challenge {
149 d := doors(day)
150 if d == nil {
151 panic("this door is still locked")
152 }
153 return d
154}
155
156func (d *challenge) attempt(id int) *attempt {
157 return d.attempts.Get(strconv.Itoa(id)).(*attempt)
158}
159
160func (d *challenge) status(player string) int {
161 if id := d.latest.Get(player); id != nil {
162 return d.attempt(id.(int)).status
163 }
164 return 0
165}
166
167func lowerHex(s string, size int) bool {
168 if len(s) != size {
169 return false
170 }
171 for i := 0; i < len(s); i++ {
172 b := s[i]
173 if !(b >= '0' && b <= '9' || b >= 'a' && b <= 'f') {
174 return false
175 }
176 }
177 return true
178}
179
180// Calendar returns tab-separated rows for open doors, with title and clue in base64.
181func Calendar() string {
182 var out strings.Builder
183 for i, d := range days {
184 if d == nil {
185 continue
186 }
187 out.WriteString(strings.Join([]string{
188 strconv.Itoa(i + 1), strconv.Itoa(d.submitted), strconv.Itoa(d.reviewed), strconv.Itoa(d.solves),
189 strconv.FormatInt(d.reviewFrom, 10), d.publicKey,
190 base64.StdEncoding.EncodeToString([]byte(d.title)), base64.StdEncoding.EncodeToString([]byte(d.clue)),
191 }, "\t"))
192 out.WriteByte('\n')
193 }
194 return out.String()
195}
196
197// Attempts returns one bounded page of the public record, starting at a 1-based ID.
198func Attempts(day, start int) string {
199 d := requireDoor(day)
200 if start < 1 || start > d.submitted {
201 return ""
202 }
203 var out strings.Builder
204 for id := start; id <= d.submitted && id-start < PageLimit; id++ {
205 a := d.attempt(id)
206 out.WriteString(strings.Join([]string{strconv.Itoa(id), a.player, a.commitment, a.sealedSalt, strconv.Itoa(a.status)}, "\t"))
207 out.WriteByte('\n')
208 }
209 return out.String()
210}
211
212// Progress lists the latest status of an address across all 24 doors.
213func Progress(player string) string {
214 var out strings.Builder
215 for _, d := range days {
216 status := 0
217 if d != nil {
218 status = d.status(player)
219 }
220 out.WriteByte(byte('0' + status))
221 }
222 return out.String()
223}
224
225// Leaders returns the top 24 addresses with their confirmed solves.
226func Leaders() string {
227 var out strings.Builder
228 for _, e := range leaders() {
229 out.WriteString(e.Key + "\t" + strconv.FormatInt(e.Score, 10) + "\n")
230 }
231 return out.String()
232}
233
234func leaders() []tally.Entry {
235 entries := board.Entries()
236 if len(entries) > Days {
237 return entries[:Days]
238 }
239 return entries
240}