admin.gno
9.43 Kb · 331 lines
1package core
2
3import (
4 "chain"
5 "chain/runtime"
6 "strconv"
7 "strings"
8
9 "gno.land/p/nt/avl/v0"
10
11 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia/safe/v1"
12 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia/types/v1"
13)
14
15// Admin (design §2.5.2, §7.5). The admin can only change the parameters
16// listed in validateOp, each inside a hard cap, after a public timelock;
17// it can pause new activity for a bounded time. It can never move, approve
18// or burn a token, touch a collection's configuration, or receive funds
19// (I-1). With no collection yet there is nothing to protect, so ops execute
20// without delay — that is how the platform is configured after deploy.
21//
22// TODO(mainnet): the initial admin and treasury must be the 2/3 multisig.
23// SA-I6: CreateFee starts at 0 here so tests and bootstrap stay simple;
24// scripts/deploy-onyx.sh sets a non-zero fee.create during bootstrap.
25var (
26 admin = address("g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06")
27 pendingAdmin address
28 fees = types.Fees{
29 PrimaryBps: 250, SecondaryBps: 250, EngineCapBps: EngineFeeCapBps,
30 Treasury: address("g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06"),
31 }
32 beacon address
33 minters avl.Tree // minter pkgpath -> *minterRecord
34 ops avl.Tree // safe.PadInt(op id) -> *pendingOp
35 opCount int64
36 pauses avl.Tree // scope -> *pauseRecord
37)
38
39// pauseRecord is one scope's pause and its budget (SA-L2): within a window
40// of PauseWindowBlocks a scope can be paused for at most MaxPauseBlocks in
41// total; a pause cannot be renewed while it runs. Worst case two budgets
42// meet at a window boundary (<= 2 x MaxPauseBlocks in a row), never more.
43type pauseRecord struct {
44 windowStart int64
45 used int64 // blocks of budget charged in this window
46 until int64 // exclusive
47}
48
49type minterRecord struct {
50 deprecated bool
51}
52
53type pendingOp struct {
54 id int64
55 op, arg string
56 proposed int64
57}
58
59// Ops and their argument formats.
60const (
61 OpFeeCreate = "fee.create" // ugnot, <= CreateFeeCap
62 OpFeePrimary = "fee.primary" // bps, <= ProtocolFeeCapBps
63 OpFeeSecondary = "fee.secondary" // bps, <= ProtocolFeeCapBps
64 OpFeeEngine = "fee.engine" // bps, <= EngineFeeCapBps: platform cap on engine-author fees
65 OpTreasury = "treasury" // address
66 OpBeacon = "beacon" // address, or "" to disable new beacon launches
67 OpMinterAdd = "minter.add" // realm pkgpath
68 OpMinterDeprecate = "minter.deprecate" // realm pkgpath: no new collections
69 OpEngineReview = "engine.review" // engine id: reviewed badge (UI only)
70 OpAdminTransfer = "admin.transfer" // address; the new admin must AcceptAdmin
71 OpRandomnessAdd = "randomness.add" // registered provider path: creators may launch with it
72 OpRandomnessDepr = "randomness.deprecate" // provider path: no new launches; running ones finish
73)
74
75var pauseScopes = []string{"create", "mint", "trade"}
76
77func ProposeOp(cur realm, op, arg string) string {
78 assertNoDeposit(cur)
79 assertAdmin(cur)
80 validateOp(op, arg)
81 opCount++
82 p := &pendingOp{id: opCount, op: op, arg: arg, proposed: height()}
83 ops.Set(safe.PadInt(p.id, cidWidth), p)
84 id := itoa(p.id)
85 chain.Emit("OpProposed", "id", id, "op", op, "arg", arg, "earliest", itoa(earliestExecution(p)))
86 return id
87}
88
89// ExecuteOp applies a proposed op once its timelock has passed. The delay is
90// judged at execution, so an op proposed during bootstrap still waits the
91// full timelock if a collection appeared in the meantime.
92func ExecuteOp(cur realm, opID string) {
93 assertNoDeposit(cur)
94 assertAdmin(cur)
95 p := mustOp(opID)
96 if height() < earliestExecution(p) {
97 panic("timelocked until height " + itoa(earliestExecution(p)))
98 }
99 validateOp(p.op, p.arg)
100 applyOp(p.op, p.arg)
101 ops.Remove(safe.PadInt(p.id, cidWidth))
102 chain.Emit("OpExecuted", "id", opID, "op", p.op, "arg", p.arg)
103}
104
105func CancelOp(cur realm, opID string) {
106 assertNoDeposit(cur)
107 assertAdmin(cur)
108 p := mustOp(opID)
109 ops.Remove(safe.PadInt(p.id, cidWidth))
110 chain.Emit("OpCancelled", "id", opID)
111}
112
113func AcceptAdmin(cur realm) {
114 assertNoDeposit(cur)
115 who := caller(cur)
116 if pendingAdmin == "" || who != pendingAdmin {
117 panic("no pending admin transfer to this address")
118 }
119 chain.Emit("AdminTransferred", "from", admin.String(), "to", who.String())
120 admin = who
121 pendingAdmin = ""
122}
123
124// Pause stops new activity in one scope for the rest of its budget (at
125// most MaxPauseBlocks per PauseWindowBlocks). It never blocks exits (I-2)
126// and cannot be renewed while it runs (SA-L2).
127func Pause(cur realm, scope string) {
128 assertNoDeposit(cur)
129 assertAdmin(cur)
130 assertScope(scope)
131 h := height()
132 r := pauseOf(scope)
133 if h < r.until {
134 panic("already paused until height " + itoa(r.until) + "; a pause cannot be renewed")
135 }
136 if h >= r.windowStart+PauseWindowBlocks {
137 r.windowStart, r.used = h, 0
138 }
139 left := MaxPauseBlocks - r.used
140 if left <= 0 {
141 panic("pause budget of " + scope + " is spent until height " + itoa(r.windowStart+PauseWindowBlocks))
142 }
143 r.used += left
144 r.until = h + left
145 pauses.Set(scope, r)
146 chain.Emit("Paused", "scope", scope, "until", itoa(r.until))
147}
148
149// Unpause ends a pause early and gives the unused blocks back to the budget.
150func Unpause(cur realm, scope string) {
151 assertNoDeposit(cur)
152 assertAdmin(cur)
153 assertScope(scope)
154 h := height()
155 r := pauseOf(scope)
156 if h < r.until {
157 r.used -= r.until - h
158 }
159 r.until = 0
160 chain.Emit("Unpaused", "scope", scope)
161}
162
163func pauseOf(scope string) *pauseRecord {
164 if v := pauses.Get(scope); v != nil {
165 return v.(*pauseRecord)
166 }
167 return &pauseRecord{windowStart: -PauseWindowBlocks}
168}
169
170// Paused reports whether scope ("create", "mint", "trade") is paused now.
171func Paused(scope string) bool {
172 v := pauses.Get(scope)
173 return v != nil && height() < v.(*pauseRecord).until
174}
175
176// PauseBudget is the scope's pause record as JSON: {until, used,
177// windowStart, windowEnd, left}.
178func PauseBudget(scope string) string {
179 assertScope(scope)
180 r := pauseOf(scope)
181 used := r.used
182 if height() >= r.windowStart+PauseWindowBlocks && height() >= r.until {
183 used = 0
184 }
185 w := &safe.JSONWriter{}
186 w.BeginObject()
187 w.IntField("until", r.until)
188 w.IntField("used", used)
189 w.IntField("windowStart", r.windowStart)
190 w.IntField("windowEnd", r.windowStart+PauseWindowBlocks)
191 w.IntField("left", MaxPauseBlocks-used)
192 w.EndObject()
193 return w.String()
194}
195
196func assertNotPaused(scope string) {
197 if Paused(scope) {
198 panic("paused: " + scope)
199 }
200}
201
202func assertScope(scope string) {
203 for _, s := range pauseScopes {
204 if s == scope {
205 return
206 }
207 }
208 panic("pause scope must be one of " + strings.Join(pauseScopes, ", "))
209}
210
211func assertAdmin(cur realm) {
212 if caller(cur) != admin {
213 panic("admin only")
214 }
215}
216
217func bootstrapping() bool { return collCount == 0 }
218
219func earliestExecution(p *pendingOp) int64 {
220 if bootstrapping() {
221 return p.proposed
222 }
223 return p.proposed + TimelockBlocks
224}
225
226func mustOp(opID string) *pendingOp {
227 v := ops.Get(safe.PadInt(safe.ParseID("opID", opID), cidWidth))
228 if v == nil {
229 panic("unknown op " + opID)
230 }
231 return v.(*pendingOp)
232}
233
234// validateOp is the closed list of admin powers (I-1).
235func validateOp(op, arg string) {
236 switch op {
237 case OpFeeCreate:
238 n := parseAmount(arg)
239 if n > CreateFeeCap {
240 panic("create fee above cap " + itoa(CreateFeeCap))
241 }
242 case OpFeePrimary, OpFeeSecondary:
243 safe.AssertBps(op, parseAmount(arg), ProtocolFeeCapBps)
244 case OpFeeEngine:
245 safe.AssertBps(op, parseAmount(arg), EngineFeeCapBps)
246 case OpTreasury, OpAdminTransfer:
247 if !address(arg).IsValid() {
248 panic(op + ": invalid address")
249 }
250 if op == OpTreasury {
251 assertSpendable(op, address(arg), "", "") // SA-L5
252 }
253 case OpBeacon:
254 if arg != "" && !address(arg).IsValid() {
255 panic(op + ": invalid address")
256 }
257 case OpMinterAdd:
258 safe.AssertText("minter", arg, 256)
259 if !strings.HasPrefix(arg, runtime.ChainDomain()+"/r/") || strings.Contains(arg, "#") {
260 panic("minter must be a realm path")
261 }
262 if minters.Has(arg) {
263 panic("minter already added")
264 }
265 case OpMinterDeprecate:
266 if !minters.Has(arg) {
267 panic("unknown minter")
268 }
269 case OpEngineReview:
270 mustEngine(arg)
271 case OpRandomnessAdd:
272 if mustProvider(arg).active {
273 panic("randomness provider already approved")
274 }
275 case OpRandomnessDepr:
276 if !mustProvider(arg).active {
277 panic("randomness provider is not approved")
278 }
279 default:
280 panic("unknown op " + strconv.Quote(op))
281 }
282}
283
284func applyOp(op, arg string) {
285 switch op {
286 case OpFeeCreate:
287 fees.CreateFee = parseAmount(arg)
288 case OpFeePrimary:
289 fees.PrimaryBps = parseAmount(arg)
290 case OpFeeSecondary:
291 fees.SecondaryBps = parseAmount(arg)
292 case OpFeeEngine:
293 fees.EngineCapBps = parseAmount(arg)
294 case OpTreasury:
295 fees.Treasury = address(arg)
296 case OpBeacon:
297 beacon = address(arg)
298 case OpMinterAdd:
299 minters.Set(arg, &minterRecord{})
300 case OpMinterDeprecate:
301 minters.Get(arg).(*minterRecord).deprecated = true
302 case OpEngineReview:
303 mustEngine(arg).reviewed = true
304 case OpAdminTransfer:
305 pendingAdmin = address(arg)
306 case OpRandomnessAdd:
307 mustProvider(arg).active = true
308 activeProviders++
309 case OpRandomnessDepr:
310 mustProvider(arg).active = false
311 activeProviders--
312 }
313}
314
315func assertActiveMinter(path string) {
316 v := minters.Get(path)
317 if v == nil {
318 panic("caller " + strconv.Quote(path) + " is not an approved minter")
319 }
320 if v.(*minterRecord).deprecated {
321 panic("minter " + path + " is deprecated")
322 }
323}
324
325func parseAmount(s string) int64 {
326 n, err := strconv.ParseInt(s, 10, 64)
327 if err != nil || n < 0 {
328 panic("expected a non-negative integer, got " + strconv.Quote(s))
329 }
330 return n
331}