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