core.gno
11.63 Kb · 268 lines
1// Package core is the platform's immutable ledger (design §2.5.2): every
2// collection, its official grc721 token and royalty, its provenance
3// (commit–reveal v2), the engine registry, fees and the admin timelock.
4//
5// What core guarantees, whoever holds the admin key:
6//
7// - I-1 admin cannot move, approve or burn a token, and holds no funds;
8// the op list is a closed set of parameters (admin.gno).
9// - I-2 pause only gates create, mint and trade. Transfer, burn,
10// finalize, distribute and every cancel stay open. A pause expires by
11// itself, cannot be renewed while it runs, and each scope has a budget
12// of MaxPauseBlocks per PauseWindowBlocks (SA-L2).
13// - I-5 supply only grows through the collection's bound minter, never
14// past maxSupply, and a token id is never minted twice.
15// - I-6 after Launch the engine, params, supply, payees, sale window,
16// commitments, randomness source and fee snapshots are frozen.
17// - I-9 MintedBy(addr) is exact while a collection is not Final:
18// grc721 balance minus the reserves sent to addr. It needs I-11 (no
19// token moves or burns before Final); if that lock is ever relaxed the
20// counter must be stored again (TestRegress_P2_*).
21// - I-10/11 a collection becomes Final at most once, only with both
22// reveals or after revealDeadline, and that deadline is bounded, so
23// the pre-reveal transfer lock always ends. With a randomness provider
24// Finalize may wait for the bound round, at most ProviderWaitBlocks
25// after the deadline or the binding (a Finalize after the deadline
26// binds), so anyone can end the lock by revealDeadline + 1 +
27// ProviderWaitBlocks + 1.
28// - I-12/I-16 engine code runs on a state-changing path only in the
29// engine user's own transaction: RegisterEngine (manifest, default
30// params, one sample render and its traits), CreateCollection
31// (ValidateParams, TraitTypes) and Launch (CheckLaunch). It never gets
32// core's cur. Every exported non-crossing function is read-only, so an
33// engine cannot call back into a mutator. An admin-approved randomness
34// provider (randomness.gno) runs only in RevealCreatorSecret,
35// RequestRandomness, FulfillRandomness and Finalize of collections
36// launched with it.
37// - I-13 every stored string passed safe.AssertText.
38// - I-14 a fingerprint reserved in source (config.gno) can only be
39// registered by its author; a commit-reveal reservation wins over any
40// later registration of the same art by another author; an unclaimed
41// record of another author blocks for at most UnclaimedHoldBlocks
42// (engines.gno).
43// - I-15 TokenURI is only ever core's placeholder, an allowlisted SVG, a
44// header-checked raster data URI of an allowlisted type, or an ipfs://
45// CIDv1 URI (tokens.gno, engine.CheckMedia).
46// - I-17 an open collection (LaunchSpec.Mode "open") never has a
47// reserve, a commitment or a beacon and is never a fair launch.
48// - I-18 the payee deny set only grows (admin.gno, payee.deny).
49// - I-20 a token of a non-transferable (soulbound) collection never
50// changes owner: core refuses every transfer of it (the lockExt hook,
51// so also through a RealmTeller) and every approval through core, the
52// market never lists it, and the flag is fixed at creation; its owner
53// may burn it once the collection is Final. Every token's first owner
54// (the address it was minted to) is recorded once and never changes
55// (tokens.gno, FirstOwner).
56// - I-19 a collection is created on an engine only by the engine's
57// manifest author, by anyone while the policy is open, or by a holder
58// of a licence with room while it is licensed — judged at creation
59// only, so a later policy or licence change never touches an existing
60// collection; a licence's used count never exceeds its maximum; a
61// non-empty (engine, params) pair is refused to anyone but the current
62// creator of the first collection with it; no admin op changes a
63// policy or a licence (access.gno).
64//
65// core imports no mutable realm: minters and engines are known only by
66// package path and interface value.
67package core
68
69import (
70 "chain"
71 "chain/runtime"
72 "chain/runtime/unsafe"
73 "strconv"
74
75 "gno.land/p/nt/avl/v0"
76 "gno.land/p/nt/grc721/royalty/v0"
77 "gno.land/p/nt/grc721/v0"
78
79 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/engine/v2"
80 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/safe/v2"
81 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/types/v2"
82)
83
84// Hard limits. They are constants on purpose: nobody, admin included, can
85// raise them after deploy (block counts assume ~3.54 s blocks, RA M-1).
86const (
87 ProtocolFeeCapBps = 500 // 5% of any sale
88 RoyaltyCapBps = 1000 // 10% (RA M-3)
89 CreateFeeCap = 1_000_000_000 // 1,000 GNOT in ugnot
90 MaxPayees = 4
91 MaxSupplyCap = 10_000
92 MaxMintPerTx = 10 // hypothesis: measure n=10 gas/deposit on Onyx
93 MaxReserveBps = 1000 // creator reserve <= 10% of supply
94 MaxSaleBlocks = 732_000 // ~30 days
95 RevealWindowMin = 1_017 // ~1 hour
96 RevealWindowMax = 170_000 // ~7 days
97 BeaconGraceBlocks = 254 // beacon may reveal first in the window's last ~15 min (< RevealWindowMin, so the creator always gets to go first)
98 TimelockBlocks = 48_800 // ~48 hours
99 MaxPauseBlocks = 195_000 // ~8 days: pause budget per scope and window
100 PauseWindowBlocks = 732_000 // ~30 days (SA-L2)
101 EngineFeeCapBps = engine.MaxFeeBps // 5%: hard cap of an engine author's fee (platform cap <= this)
102 ProviderWaitBlocks = 24_400 // ~24 h: Finalize waits this long after the deadline (or the round's binding) for a provider's round before falling back (CV-M1)
103 ListingDepositCap = 5_000_000 // 5 GNOT: hard cap of the market's listing deposit return (op listing.deposit, CV-L2)
104
105 MaxNameLen = 64
106 MaxDescriptionLen = 1024
107 MaxParamsLen = 1024
108 MinSecretLen = 32
109 MaxSecretLen = 256
110 MaxPageSize = 50
111
112 // Engine registry (ENG-M1, I-14).
113 ReservationTTLBlocks = 170_000 // ~7 days: a fingerprint reservation is usable this long
114 UnclaimedHoldBlocks = 24_400 // ~1 day: an unclaimed record stops blocking the same art after this
115 MinReserveSaltLen = 32
116 MaxReserveSaltLen = 256
117
118 // MaxDeniedPayees bounds the payee.deny entries (each payee check
119 // looks the address up once; the bound only caps admin-made growth).
120 MaxDeniedPayees = 64
121)
122
123// CoreVersion tells indexers and frontends which core they read (M-26).
124const CoreVersion = "sinopia-core/4"
125
126// Provenance badges (design §4.3).
127const (
128 BadgeBoth = "2/2"
129 BadgeCreatorOnly = "creator-only"
130 BadgeBeaconOnly = "beacon-only"
131 BadgeFallback = "fallback"
132 BadgeOpen = "open" // open mode: no secrets, never a fair launch (I-17)
133)
134
135type collection struct {
136 id, name, symbol, description string
137 creator, pendingCreator address
138 engineID, params string
139 traitTypes []string // the collection's trait list when it differs from the manifest's (nil = manifest)
140 mode string // types.ModeReveal or types.ModeOpen, set at Launch
141 soulbound bool // non-transferable, fixed at creation (I-20)
142 maxSupply int64
143 nextTID int64 // next token id; minted so far = nextTID-1
144 reserved int64
145 royaltyBps int64
146 primaryPayees, royaltyPayees []types.Payee
147 minter string
148 state types.State
149 startHeight, endHeight int64
150 revealWindow int64
151 closeHeight int64 // sell-out height, or endHeight once time ran out
152 useBeacon bool
153 beaconSource string // with useBeacon: types.BeaconCommitted or a randomness provider path
154 createdHeight int64
155 primaryBps int64 // protocol fee snapshot at Launch (SA-I5)
156 engineBps int64 // engine-author fee snapshot at Launch (0 when creator == author)
157 engineAuthor address // who receives engineBps
158 access string // AccessAuthor, AccessLicence or AccessOpen: how the creator could use the engine (I-19)
159 licenceFee int64 // engine fee of the licence it was created under, or LicenceFeeDefault (manifest fee)
160 burnedCommits []string // commitments whose secret went public; never accepted again here (SA-L1)
161 approvals avl.Tree // "o:"+owner+":"+operator -> int64 nonce (SA-L3; token nonces are packed in st)
162 prov *provenance
163 token *grc721.Token
164 ledger *grc721.PrivateLedger
165 roy *royalty.Royalty
166 st *tokenState
167 reservedTo avl.Tree // address -> int64 reserve tokens received (MintedBy = balance - this, I-9)
168}
169
170func (c *collection) minted() int64 { return c.nextTID - 1 }
171
172var (
173 // selfPath is this realm's package path, captured once in init so read
174 // functions can derive sub-addresses without a cur.
175 selfPath string
176
177 collections avl.Tree // safe.PadInt(cid) -> *collection
178 byCreator avl.Tree // creator + ":" + padded cid -> cid
179 collCount int64
180)
181
182func init(cur realm) {
183 selfPath = cur.PkgPath()
184 initDeniedPayees()
185}
186
187// Version is CoreVersion (M-26).
188func Version() string { return CoreVersion }
189
190const cidWidth = 10
191
192// collKey accepts a collection id as "<cid>" or as its token sub-path
193// "c/<cid>" (the form a wallet derives from Token.ID(), "…/core#c/<cid>";
194// K-1). Every function that takes a cid resolves it through
195// mustCollection and then uses c.id only.
196func collKey(cid string) string {
197 if len(cid) > 2 && cid[0] == 'c' && cid[1] == '/' {
198 cid = cid[2:]
199 }
200 return safe.PadInt(safe.ParseID("cid", cid), cidWidth)
201}
202
203func mustCollection(cid string) *collection {
204 v := collections.Get(collKey(cid))
205 if v == nil {
206 panic("unknown collection " + strconv.Quote(cid))
207 }
208 return v.(*collection)
209}
210
211// stateAt is the effective state at height h; the stored state lags until
212// the next write materializes it (refresh).
213func stateAt(c *collection, h int64) types.State {
214 switch c.state {
215 case types.StateScheduled:
216 if h > c.endHeight {
217 return types.StateClosed
218 }
219 if h >= c.startHeight {
220 return types.StateMinting
221 }
222 case types.StateMinting:
223 if h > c.endHeight {
224 return types.StateClosed
225 }
226 }
227 return c.state
228}
229
230func refresh(c *collection, h int64) {
231 s := stateAt(c, h)
232 if s == c.state {
233 return
234 }
235 if s == types.StateClosed && c.closeHeight == 0 {
236 c.closeHeight = c.endHeight
237 }
238 c.state = s
239 chain.Emit("CollectionState", "cid", c.id, "state", s.String())
240}
241
242// revealDeadline is when anyone may finalize without the missing secrets.
243// It is at most endHeight + RevealWindowMax, which bounds the transfer
244// lock (I-11).
245func revealDeadline(c *collection) int64 {
246 closed := c.closeHeight
247 if closed == 0 {
248 closed = c.endHeight
249 }
250 return closed + c.revealWindow
251}
252
253func caller(cur realm) address {
254 return cur.Previous().Address()
255}
256
257// assertNoDeposit refuses coins on functions that take none, so nothing can
258// get stuck in core (RA L-1). The origin-send envelope belongs to the first
259// realm of the call; it is only ours to judge when a wallet called us.
260func assertNoDeposit(cur realm) {
261 if cur.Previous().IsUserCall() && !unsafe.OriginSend().IsZero() {
262 panic("this function does not accept coins")
263 }
264}
265
266func itoa(n int64) string { return strconv.FormatInt(n, 10) }
267
268func height() int64 { return runtime.ChainHeight() }