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