Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

entry.gno

2.92 Kb · 132 lines
  1package hall
  2
  3import (
  4	"chain"
  5	"chain/runtime"
  6	"strings"
  7
  8	"gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
  9)
 10
 11// Status is where a challenge stands in the hall.
 12type Status int
 13
 14// Challenge statuses.
 15const (
 16	Pending Status = iota // enlisted by a community realm, awaiting review
 17	Listed                // open for claims
 18	Retired               // closed for claims; past solves still score
 19)
 20
 21// String returns the human name of s.
 22func (s Status) String() string {
 23	switch s {
 24	case Pending:
 25		return "pending"
 26	case Listed:
 27		return "listed"
 28	case Retired:
 29		return "retired"
 30	}
 31	return "unknown"
 32}
 33
 34// entry is a challenge as the hall knows it.
 35type entry struct {
 36	path       string
 37	ch         flag.Challenge
 38	info       flag.Info
 39	official   bool
 40	status     Status
 41	enlistedAt int64
 42	solvers    []address // claim order; solvers[0] drew first blood
 43}
 44
 45// value is what the challenge is worth to each of its solvers right now.
 46func (e *entry) value() int { return flag.Value(e.info.Difficulty, len(e.solvers)) }
 47
 48// Enlist registers the challenge ch for the realm that calls it. Call it
 49// from your challenge realm's init:
 50//
 51//	func init(cur realm) { hall.Enlist(cross(cur), challenge{}) }
 52//
 53// Official challenges are listed at once; community ones wait for review.
 54func Enlist(cur realm, ch flag.Challenge) {
 55	caller := cur.Previous()
 56	path := caller.PkgPath()
 57	if path == "" || caller.IsEphemeral() {
 58		panic(ErrNotRealm)
 59	}
 60	if ch == nil {
 61		panic(ErrNilChallenge)
 62	}
 63	info := ch.Info()
 64	if err := info.Validate(); err != nil {
 65		panic(err)
 66	}
 67	if paths.Has(path) {
 68		panic(ErrAlreadyEnlisted)
 69	}
 70	if entries.Has(info.Slug) {
 71		panic(ErrSlugTaken)
 72	}
 73
 74	e := &entry{
 75		path:       path,
 76		ch:         ch,
 77		info:       info,
 78		status:     Pending,
 79		enlistedAt: runtime.ChainHeight(),
 80	}
 81	if strings.HasPrefix(path, officialPrefix) {
 82		e.official = true
 83		e.status = Listed
 84	}
 85	entries.Set(info.Slug, e)
 86	paths.Set(path, info.Slug)
 87	order = append(order, info.Slug)
 88	chain.Emit("Enlisted", "slug", info.Slug, "path", path, "status", e.status.String())
 89}
 90
 91// Approve opens a pending community challenge for claims.
 92func Approve(cur realm, slug string) {
 93	assertCurator(cur)
 94	e := mustEntry(slug)
 95	if e.status != Pending {
 96		panic(ErrNotPending)
 97	}
 98	e.status = Listed
 99	chain.Emit("Approved", "slug", slug)
100}
101
102// Retire closes a challenge for new claims. Existing solves keep scoring.
103func Retire(cur realm, slug string) {
104	assertCurator(cur)
105	e := mustEntry(slug)
106	e.status = Retired
107	chain.Emit("Retired", "slug", slug)
108}
109
110func getEntry(slug string) (*entry, bool) {
111	v := entries.Get(slug)
112	if v == nil {
113		return nil, false
114	}
115	return v.(*entry), true
116}
117
118func mustEntry(slug string) *entry {
119	e, ok := getEntry(slug)
120	if !ok {
121		panic(ErrUnknownChallenge)
122	}
123	return e
124}
125
126// eachEntry calls fn for every challenge in enlistment order.
127func eachEntry(fn func(slug string, e *entry)) {
128	for _, slug := range order {
129		e, _ := getEntry(slug)
130		fn(slug, e)
131	}
132}