Playground
challenge.gno
package stair
import (
"gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
"gno.land/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/hall"
)
var info = flag.Info{
Slug: "stair",
Title: "The Back Stair",
Difficulty: flag.Medium,
Category: "crossing functions",
Brief: "The way down to the old vintages is guarded: only stewards on the roster may descend, " +
"and only the head steward appoints them, through the front door. The staff use a back stair " +
"that no wallet can reach. Get your name on the roster and descend.",
}
var captures = flag.NewLedger()
type challenge struct{}
func (challenge) Info() flag.Info { return info }
func (challenge) Captured(player address) bool { return captures.Captured(player) }
// Challenge describes this realm to a hall.
func Challenge() flag.Challenge { return challenge{} }
func init(cur realm) { hall.Enlist(cross(cur), challenge{}) }
gnomod.toml
module = "gno.land/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/stair" gno = "0.9" [addpkg] creator = "g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf" height = 1457753 max_deposit = "100000000ugnot"
stair.gno
// Package stair is a Cellar challenge about exported non-crossing
// functions in a realm.
package stair
import (
"chain/runtime"
"strconv"
"strings"
"gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
"gno.land/p/nt/avl/v0"
)
var (
headSteward = address("g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf")
roster = avl.NewTree() // steward address -> height enrolled
)
// Appoint puts steward on the roster. Only the head steward appoints.
func Appoint(cur realm, steward address) {
if cur.Previous().Address() != headSteward {
panic("stair: only the head steward appoints stewards")
}
Enroll(steward)
}
// Enroll writes steward on the roster. It is the helper Appoint uses. It
// takes no realm, so no wallet can call it with MsgCall.
func Enroll(steward address) {
roster.Set(steward.String(), runtime.ChainHeight())
}
// Descend takes a steward down to the old vintages, where the flag is.
func Descend(cur realm) {
player := flag.MustPlayer(cur.Previous())
if !roster.Has(player.String()) {
panic("stair: the door steward does not know you")
}
captures.Record(player)
}
// IsSteward reports whether addr is on the roster.
func IsSteward(addr address) bool { return roster.Has(addr.String()) }
// Render shows the roster.
func Render(_ string) string {
var b strings.Builder
b.WriteString(flag.Card(info, captures))
b.WriteString("\n## The roster\n\n")
if roster.Size() == 0 {
b.WriteString("*No stewards yet.*\n")
}
roster.Iterate("", "", func(addr string, v any) bool {
b.WriteString("- `" + addr + "` since block " + strconv.FormatInt(v.(int64), 10) + "\n")
return false
})
b.WriteString("\n## Captured by\n\n" + flag.Captures(captures, 5))
return b.String()
}
stair_test.gno
package stair
import (
"testing"
"gno.land/p/nt/uassert/v0"
)
const ash = address("g1zkzjmyveh7euq83h5zhnhj6mg0d7kr35nphzd2")
func TestOnlyHeadStewardAppoints(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(ash))
uassert.AbortsContains(t, cur, "only the head steward", func() { Appoint(cross(cur), ash) })
uassert.False(t, IsSteward(ash))
}
func TestStrangersCannotDescend(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(ash))
uassert.AbortsContains(t, cur, "does not know you", func() { Descend(cross(cur)) })
}
func TestAppointedStewardDescends(cur realm, t *testing.T) {
friend := address("g1rh0vd3xzwjpcqehrf0tpjlqeq35079wf4zklar")
testing.SetRealm(testing.NewUserRealm(headSteward))
Appoint(cross(cur), friend)
testing.SetRealm(testing.NewUserRealm(friend))
Descend(cross(cur))
uassert.True(t, captures.Captured(friend))
}