Playground
challenge.gno
package livery
import (
"gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
"gno.land/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/hall"
)
var info = flag.Info{
Slug: "livery",
Title: "Borrowed Livery",
Difficulty: flag.Hard,
Category: "realm context",
Brief: "Only the steward may dress a guest in the house livery, and the wardrobe checks that the " +
"steward is the realm acting before it hands one over. The steward is a busy realm with a soft " +
"spot for toasts. Wear the livery and enter the private cellar.",
}
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/livery" gno = "0.9" [addpkg] creator = "g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf" height = 1457793 max_deposit = "100000000ugnot"
livery.gno
// Package livery is a Cellar challenge about the difference between the
// realm that is acting and the code that is running.
package livery
import (
"chain/runtime/unsafe"
"strings"
"gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
"gno.land/p/nt/avl/v0"
)
// StewardPath is the only realm allowed to hand out the livery.
const StewardPath = "gno.land/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/steward"
var dressed = avl.NewTree() // guest address -> true
// Dress puts guest in the house livery. The wardrobe only obeys the
// steward: it checks that the steward is the realm currently acting.
func Dress(guest address) {
if unsafe.CurrentRealm().PkgPath() != StewardPath {
panic("livery: only the steward hands out the livery")
}
dressed.Set(guest.String(), true)
}
// Enter lets a guest in livery into the private cellar, where the flag is.
func Enter(cur realm) {
player := flag.MustPlayer(cur.Previous())
if !dressed.Has(player.String()) {
panic("livery: guests in livery only")
}
captures.Record(player)
}
// InLivery reports whether addr wears the livery.
func InLivery(addr address) bool { return dressed.Has(addr.String()) }
// Render shows who wears the livery.
func Render(_ string) string {
var b strings.Builder
b.WriteString(flag.Card(info, captures))
b.WriteString("\n## In livery\n\n")
if dressed.Size() == 0 {
b.WriteString("*Nobody yet.*\n")
}
dressed.Iterate("", "", func(addr string, _ any) bool {
b.WriteString("- `" + addr + "`\n")
return false
})
b.WriteString("\n## Captured by\n\n" + flag.Captures(captures, 5))
return b.String()
}
livery_test.gno
package livery
import (
"testing"
"gno.land/p/nt/uassert/v0"
)
const ash = address("g1zkzjmyveh7euq83h5zhnhj6mg0d7kr35nphzd2")
func TestNoLiveryNoEntry(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(ash))
uassert.AbortsContains(t, cur, "guests in livery only", func() { Enter(cross(cur)) })
}
func TestWardrobeRefusesOthers(t *testing.T) {
uassert.PanicsContains(t, nil, "only the steward", func() { Dress(ash) })
uassert.False(t, InLivery(ash))
}