stair.gno
1.68 Kb · 59 lines
1// Package stair is a Cellar challenge about exported non-crossing
2// functions in a realm.
3package stair
4
5import (
6 "chain/runtime"
7 "strconv"
8 "strings"
9
10 "gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
11 "gno.land/p/nt/avl/v0"
12)
13
14var (
15 headSteward = address("g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf")
16 roster = avl.NewTree() // steward address -> height enrolled
17)
18
19// Appoint puts steward on the roster. Only the head steward appoints.
20func Appoint(cur realm, steward address) {
21 if cur.Previous().Address() != headSteward {
22 panic("stair: only the head steward appoints stewards")
23 }
24 Enroll(steward)
25}
26
27// Enroll writes steward on the roster. It is the helper Appoint uses. It
28// takes no realm, so no wallet can call it with MsgCall.
29func Enroll(steward address) {
30 roster.Set(steward.String(), runtime.ChainHeight())
31}
32
33// Descend takes a steward down to the old vintages, where the flag is.
34func Descend(cur realm) {
35 player := flag.MustPlayer(cur.Previous())
36 if !roster.Has(player.String()) {
37 panic("stair: the door steward does not know you")
38 }
39 captures.Record(player)
40}
41
42// IsSteward reports whether addr is on the roster.
43func IsSteward(addr address) bool { return roster.Has(addr.String()) }
44
45// Render shows the roster.
46func Render(_ string) string {
47 var b strings.Builder
48 b.WriteString(flag.Card(info, captures))
49 b.WriteString("\n## The roster\n\n")
50 if roster.Size() == 0 {
51 b.WriteString("*No stewards yet.*\n")
52 }
53 roster.Iterate("", "", func(addr string, v any) bool {
54 b.WriteString("- `" + addr + "` since block " + strconv.FormatInt(v.(int64), 10) + "\n")
55 return false
56 })
57 b.WriteString("\n## Captured by\n\n" + flag.Captures(captures, 5))
58 return b.String()
59}