Playground
challenge.gno
package rack
import (
"gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
"gno.land/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/hall"
)
var info = flag.Info{
Slug: "rack",
Title: "The Open Rack",
Difficulty: flag.Medium,
Category: "storage authority",
Brief: "The reserve rack is posted on the cellar door for every realm to read. Gno makes other " +
"realms' objects read-only, so the keeper sleeps soundly. Nobody has ever reserved the " +
"1811 Château d'Yquem. Collect it in your name.",
}
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/rack" gno = "0.9" [addpkg] creator = "g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf" height = 1457763 max_deposit = "100000000ugnot"
rack.gno
// Package rack is a Cellar challenge about who may write an object, and
// why a read-only view of a /p/ type is not read-only.
package rack
import (
"strings"
"gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
"gno.land/p/nt/avl/v0"
)
// Prize is the bottle nobody has ever been promised.
const Prize = "1811 Château d'Yquem"
// Rack is the reserve rack: which bottle is set aside for which guest.
type Rack struct {
Keeper address
Reserved *avl.Tree // guest address -> bottle
}
// Ledger is posted on the door for every realm to read. Other realms can
// import it but cannot change it: Gno makes their view of it read-only.
var Ledger = &Rack{
Keeper: address("g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf"),
Reserved: avl.NewTree(),
}
// Reserve sets a bottle aside for guest. Only the keeper reserves, and the
// keeper never reserves the Prize.
func Reserve(cur realm, guest address, bottle string) {
if cur.Previous().Address() != Ledger.Keeper {
panic("rack: only the keeper reserves bottles")
}
if bottle == Prize {
panic("rack: the keeper would never")
}
Ledger.Reserved.Set(guest.String(), bottle)
}
// Collect hands over the bottle reserved in your name. If it is the Prize,
// the flag is yours.
func Collect(cur realm) {
player := flag.MustPlayer(cur.Previous())
v := Ledger.Reserved.Get(player.String())
if v == nil {
panic("rack: nothing is reserved in your name")
}
if v.(string) != Prize {
panic("rack: you collect a " + v.(string) + ". Pleasant, but not the flag")
}
captures.Record(player)
}
// Render shows the rack.
func Render(_ string) string {
var b strings.Builder
b.WriteString(flag.Card(info, captures))
b.WriteString("\n**Keeper:** `" + Ledger.Keeper.String() + "`\n\n## Reserved\n\n")
if Ledger.Reserved.Size() == 0 {
b.WriteString("*Nothing set aside.*\n")
}
Ledger.Reserved.Iterate("", "", func(addr string, v any) bool {
b.WriteString("- `" + addr + "`: " + v.(string) + "\n")
return false
})
b.WriteString("\n## Captured by\n\n" + flag.Captures(captures, 5))
return b.String()
}
rack_test.gno
package rack
import (
"testing"
"gno.land/p/nt/uassert/v0"
)
const ash = address("g1zkzjmyveh7euq83h5zhnhj6mg0d7kr35nphzd2")
func TestOnlyKeeperReserves(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(ash))
uassert.AbortsContains(t, cur, "only the keeper", func() { Reserve(cross(cur), ash, Prize) })
}
func TestKeeperNeverReservesThePrize(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(Ledger.Keeper))
uassert.AbortsContains(t, cur, "would never", func() { Reserve(cross(cur), ash, Prize) })
}
func TestOrdinaryBottleIsNotTheFlag(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(Ledger.Keeper))
Reserve(cross(cur), ash, "1990 Barolo")
testing.SetRealm(testing.NewUserRealm(ash))
uassert.AbortsContains(t, cur, "not the flag", func() { Collect(cross(cur)) })
uassert.False(t, captures.Captured(ash))
}