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.

requests.gno

3.85 Kb · 151 lines
  1package bouncer
  2
  3import (
  4	"chain"
  5	"chain/runtime"
  6	"strconv"
  7	"strings"
  8
  9	"gno.land/p/nt/seqid/v0"
 10)
 11
 12// Request is a scan request, as returned by reads.
 13type Request struct {
 14	ID        uint64
 15	PkgPath   string
 16	Requester address
 17	Height    int64
 18	// Status is "open", "attested", "cancelled" or "closed".
 19	Status string
 20	// Note explains a closed request.
 21	Note string
 22}
 23
 24type request struct {
 25	r Request
 26}
 27
 28func requestKey(id uint64) string { return seqid.ID(id).String() }
 29
 30// RequestScan asks the scanner bot to scan pkgpath. Each address may have
 31// one open request at a time.
 32func RequestScan(cur realm, pkgpath string) uint64 {
 33	if !cur.IsCurrent() {
 34		panic("invalid realm")
 35	}
 36	if !validPkgPath(pkgpath) {
 37		panic("invalid package path")
 38	}
 39	caller := cur.Previous().Address()
 40	if openByRequester.Has(string(caller)) {
 41		panic("you already have an open scan request")
 42	}
 43	id := uint64(requestID.Next())
 44	requests.Set(requestKey(id), &request{r: Request{
 45		ID:        id,
 46		PkgPath:   pkgpath,
 47		Requester: caller,
 48		Height:    runtime.ChainHeight(),
 49		Status:    "open",
 50	}})
 51	openByRequester.Set(string(caller), id)
 52	openRequests.Set(requestKey(id), true)
 53	chain.Emit("ScanRequested", "id", requestKey(id), "pkgpath", pkgpath, "requester", caller.String())
 54	return id
 55}
 56
 57// CancelRequest withdraws the caller's open request.
 58func CancelRequest(cur realm) {
 59	if !cur.IsCurrent() {
 60		panic("invalid realm")
 61	}
 62	caller := cur.Previous().Address()
 63	v := openByRequester.Get(string(caller))
 64	if v == nil {
 65		panic("no open scan request")
 66	}
 67	finishRequest(v.(uint64), "cancelled", "")
 68}
 69
 70// CloseRequest lets an attester close a request it cannot serve, such as a
 71// package that does not exist.
 72func CloseRequest(cur realm, id uint64, note string) {
 73	assertAttester(cur)
 74	if len(note) > maxNoteLen {
 75		panic("note too long")
 76	}
 77	v := requests.Get(requestKey(id))
 78	if v == nil || v.(*request).r.Status != "open" {
 79		panic("no open request with that id")
 80	}
 81	finishRequest(id, "closed", note)
 82}
 83
 84func finishRequest(id uint64, status, note string) {
 85	rq := requests.Get(requestKey(id)).(*request)
 86	rq.r.Status = status
 87	rq.r.Note = note
 88	openByRequester.Remove(string(rq.r.Requester))
 89	openRequests.Remove(requestKey(id))
 90	chain.Emit("ScanRequestFinished", "id", requestKey(id), "pkgpath", rq.r.PkgPath, "status", status)
 91}
 92
 93// closeRequestsFor marks every open request for pkgpath as attested.
 94func closeRequestsFor(pkgpath string) {
 95	var ids []uint64
 96	openRequests.Iterate("", "", func(k string, _ any) bool {
 97		r := requests.Get(k).(*request).r
 98		if r.PkgPath == pkgpath {
 99			ids = append(ids, r.ID)
100		}
101		return false
102	})
103	for _, id := range ids {
104		finishRequest(id, "attested", "")
105	}
106}
107
108// GetRequest returns a copy of request id.
109func GetRequest(id uint64) (Request, bool) {
110	v := requests.Get(requestKey(id))
111	if v == nil {
112		return Request{}, false
113	}
114	return v.(*request).r, true
115}
116
117// OpenRequests returns up to limit open requests, oldest first, for the
118// scanner bot.
119func OpenRequests(limit int) []Request {
120	out := []Request{}
121	if limit <= 0 {
122		return out
123	}
124	// seqid keys sort in ID order.
125	openRequests.Iterate("", "", func(k string, _ any) bool {
126		out = append(out, requests.Get(k).(*request).r)
127		return len(out) >= limit
128	})
129	return out
130}
131
132// OpenRequestsText is OpenRequests for the scanner bot, as plain text: one
133// "id pkgpath" line per open request, oldest first. Package paths never
134// contain spaces or newlines.
135func OpenRequestsText(limit int) string {
136	var b strings.Builder
137	for _, r := range OpenRequests(limit) {
138		b.WriteString(strconv.FormatUint(r.ID, 10) + " " + r.PkgPath + "\n")
139	}
140	return b.String()
141}
142
143// LatestHashes returns "<source hash> <rule-set hash>" of pkgpath's latest
144// attestation, or "" if there is none, so the bot can skip unchanged code.
145func LatestHashes(pkgpath string) string {
146	a, ok := Latest(pkgpath)
147	if !ok {
148		return ""
149	}
150	return a.SourceHash + " " + a.RulesetHash
151}