bouncer.gno
4.61 Kb · 175 lines
1// Package bouncer is Bouncer's on-chain registry: scan requests,
2// attestations, disputes and author replies, plus the reads other realms use
3// (Check, Score) and the Tavern pages in Render.
4//
5// One realm holds all state and every write, and every logic-data type is
6// declared here (security guide §3A). Writes are crossing functions that
7// check cur.IsCurrent() and take the caller from cur.Previous(). Reads are
8// plain functions that return values or copies, never pointers into state,
9// and never take callbacks.
10package bouncer
11
12import (
13 "chain"
14 "strings"
15
16 "gno.land/p/nt/avl/v0"
17 "gno.land/p/nt/seqid/v0"
18)
19
20var (
21 admin address
22 attesters = avl.NewTree() // address -> true
23 // selfPath is this realm's package path, for links in Render.
24 selfPath string
25
26 // Scan requests.
27 requests = avl.NewTree() // request id -> *request
28 openByRequester = avl.NewTree() // address -> request id (one open each)
29 openRequests = avl.NewTree() // request id -> true, open ones only
30 requestID seqid.ID
31
32 // Attestations.
33 records = avl.NewTree() // pkgpath -> *record
34 byAuthor = avl.NewTree() // author address -> *avl.Tree (pkgpath -> true)
35 attestationID seqid.ID
36 recent []string // pkgpaths, most recently attested first
37
38 // Disputes.
39 disputes = avl.NewTree() // dispute id -> *dispute
40 disputeID seqid.ID
41
42 // Totals over the latest attestation of each package.
43 totalFindings int
44 totalProven int
45)
46
47func init(cur realm) {
48 selfPath = cur.PkgPath()
49 prev := cur.Previous()
50 if !prev.IsUserCall() {
51 panic("bouncer must be deployed by a user")
52 }
53 // The deployer becomes admin and the first attester. Under gno test
54 // the deploying user has no address; then no admin is set, which
55 // locks every admin action rather than opening it.
56 if deployer := prev.Address(); deployer.IsValid() {
57 admin = deployer
58 attesters.Set(string(admin), true)
59 }
60}
61
62// ---- admin and attesters ----
63
64func assertAdmin(cur realm) {
65 if !cur.IsCurrent() {
66 panic("invalid realm")
67 }
68 if cur.Previous().Address() != admin {
69 panic("admin only")
70 }
71}
72
73func assertAttester(cur realm) address {
74 if !cur.IsCurrent() {
75 panic("invalid realm")
76 }
77 caller := cur.Previous().Address()
78 if !attesters.Has(string(caller)) {
79 panic("attester only")
80 }
81 return caller
82}
83
84// AddAttester allows addr to post attestations.
85func AddAttester(cur realm, addr address) {
86 assertAdmin(cur)
87 if !addr.IsValid() {
88 panic("invalid address")
89 }
90 attesters.Set(string(addr), true)
91 chain.Emit("AttesterAdded", "address", addr.String())
92}
93
94// RemoveAttester revokes addr. Its past attestations stay on record.
95func RemoveAttester(cur realm, addr address) {
96 assertAdmin(cur)
97 if _, removed := attesters.Remove(string(addr)); !removed {
98 panic("not an attester")
99 }
100 chain.Emit("AttesterRemoved", "address", addr.String())
101}
102
103// TransferAdmin hands the admin role to next.
104func TransferAdmin(cur realm, next address) {
105 assertAdmin(cur)
106 if !next.IsValid() {
107 panic("invalid address")
108 }
109 prev := admin
110 admin = next
111 chain.Emit("AdminTransferred", "from", prev.String(), "to", next.String())
112}
113
114// Admin returns the admin address.
115func Admin() address { return admin }
116
117// IsAttester reports whether addr may post attestations.
118func IsAttester(addr address) bool { return attesters.Has(string(addr)) }
119
120// Attesters returns the attester allowlist, sorted.
121func Attesters() []address {
122 out := make([]address, 0, attesters.Size())
123 attesters.Iterate("", "", func(k string, _ any) bool {
124 out = append(out, address(k))
125 return false
126 })
127 return out
128}
129
130// ---- package paths ----
131
132const maxPkgPathLen = 200
133
134// validPkgPath accepts gno.land/r/... and gno.land/p/... paths of at least
135// a namespace and a name, made of lowercase letters, digits and underscores.
136func validPkgPath(p string) bool {
137 if len(p) > maxPkgPathLen {
138 return false
139 }
140 rest, ok := strings.CutPrefix(p, "gno.land/")
141 if !ok {
142 return false
143 }
144 segs := strings.Split(rest, "/")
145 if len(segs) < 3 || (segs[0] != "r" && segs[0] != "p") {
146 return false
147 }
148 for _, s := range segs[1:] {
149 if s == "" {
150 return false
151 }
152 for _, c := range s {
153 if !(c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '_') {
154 return false
155 }
156 }
157 }
158 return true
159}
160
161// authorOf returns the address that owns an address-namespace path
162// (gno.land/r/<address>/... or gno.land/p/<address>/...). Only that address
163// can deploy there, so authorship is verified by the path alone. Other
164// namespaces have no verified author.
165func authorOf(pkgpath string) (address, bool) {
166 segs := strings.Split(pkgpath, "/")
167 if len(segs) < 4 {
168 return "", false
169 }
170 a := address(segs[2])
171 if !a.IsValid() {
172 return "", false
173 }
174 return a, true
175}