core.gno
7.65 Kb · 204 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.
24// - I-12 engine code never runs on a state-changing path, except the
25// creator's own CreateCollection (ValidateParams) and the engine's own
26// RegisterEngine (one sample render). Every exported non-crossing
27// function is read-only, so an engine cannot call back into a mutator.
28// An admin-approved randomness provider (randomness.gno) runs only in
29// RequestRandomness/FulfillRandomness of collections launched with it.
30// - I-13 every stored string passed safe.AssertText.
31//
32// core imports no mutable realm: minters and engines are known only by
33// package path and interface value.
34package core
35
36import (
37 "chain"
38 "chain/runtime"
39 "chain/runtime/unsafe"
40 "strconv"
41
42 "gno.land/p/nt/avl/v0"
43 "gno.land/p/nt/grc721/royalty/v0"
44 "gno.land/p/nt/grc721/v0"
45
46 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia/engine/v1"
47 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia/safe/v1"
48 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia/types/v1"
49)
50
51// Hard limits. They are constants on purpose: nobody, admin included, can
52// raise them after deploy (block counts assume ~3.54 s blocks, RA M-1).
53const (
54 ProtocolFeeCapBps = 500 // 5% of any sale
55 RoyaltyCapBps = 1000 // 10% (RA M-3)
56 CreateFeeCap = 1_000_000_000 // 1,000 GNOT in ugnot
57 MaxPayees = 4
58 MaxSupplyCap = 10_000
59 MaxMintPerTx = 10 // hypothesis: measure n=10 gas/deposit on Onyx
60 MaxReserveBps = 1000 // creator reserve <= 10% of supply
61 MaxSaleBlocks = 732_000 // ~30 days
62 RevealWindowMin = 1_017 // ~1 hour
63 RevealWindowMax = 170_000 // ~7 days
64 BeaconGraceBlocks = 254 // beacon may reveal first in the window's last ~15 min (< RevealWindowMin, so the creator always gets to go first)
65 TimelockBlocks = 48_800 // ~48 hours
66 MaxPauseBlocks = 195_000 // ~8 days: pause budget per scope and window
67 PauseWindowBlocks = 732_000 // ~30 days (SA-L2)
68 EngineFeeCapBps = engine.MaxFeeBps // 5%: hard cap of an engine author's fee (platform cap <= this)
69 RequestMargin = 127 // a randomness request leaves >= this many blocks before revealDeadline
70
71 MaxNameLen = 64
72 MaxDescriptionLen = 1024
73 MaxParamsLen = 1024
74 MinSecretLen = 32
75 MaxSecretLen = 256
76 MaxPageSize = 50
77)
78
79// Provenance badges (design §4.3).
80const (
81 BadgeBoth = "2/2"
82 BadgeCreatorOnly = "creator-only"
83 BadgeBeaconOnly = "beacon-only"
84 BadgeFallback = "fallback"
85)
86
87type collection struct {
88 id, name, symbol, description string
89 creator, pendingCreator address
90 engineID, params string
91 maxSupply int64
92 nextTID int64 // next token id; minted so far = nextTID-1
93 reserved int64
94 royaltyBps int64
95 primaryPayees, royaltyPayees []types.Payee
96 minter string
97 state types.State
98 startHeight, endHeight int64
99 revealWindow int64
100 closeHeight int64 // sell-out height, or endHeight once time ran out
101 useBeacon bool
102 beaconSource string // with useBeacon: types.BeaconCommitted or a randomness provider path
103 createdHeight int64
104 primaryBps int64 // protocol fee snapshot at Launch (SA-I5)
105 engineBps int64 // engine-author fee snapshot at Launch (0 when creator == author)
106 engineAuthor address // who receives engineBps
107 burnedCommits []string // commitments whose secret went public; never accepted again here (SA-L1)
108 approvals avl.Tree // "o:"+owner+":"+operator -> int64 nonce (SA-L3; token nonces are packed in st)
109 prov *provenance
110 token *grc721.Token
111 ledger *grc721.PrivateLedger
112 roy *royalty.Royalty
113 st *tokenState
114 reservedTo avl.Tree // address -> int64 reserve tokens received (MintedBy = balance - this, I-9)
115}
116
117func (c *collection) minted() int64 { return c.nextTID - 1 }
118
119var (
120 // selfPath is this realm's package path, captured once in init so read
121 // functions can derive sub-addresses without a cur.
122 selfPath string
123
124 collections avl.Tree // safe.PadInt(cid) -> *collection
125 byCreator avl.Tree // creator + ":" + padded cid -> cid
126 collCount int64
127)
128
129func init(cur realm) {
130 selfPath = cur.PkgPath()
131}
132
133const cidWidth = 10
134
135func collKey(cid string) string {
136 return safe.PadInt(safe.ParseID("cid", cid), cidWidth)
137}
138
139func mustCollection(cid string) *collection {
140 v := collections.Get(collKey(cid))
141 if v == nil {
142 panic("unknown collection " + strconv.Quote(cid))
143 }
144 return v.(*collection)
145}
146
147// stateAt is the effective state at height h; the stored state lags until
148// the next write materializes it (refresh).
149func stateAt(c *collection, h int64) types.State {
150 switch c.state {
151 case types.StateScheduled:
152 if h > c.endHeight {
153 return types.StateClosed
154 }
155 if h >= c.startHeight {
156 return types.StateMinting
157 }
158 case types.StateMinting:
159 if h > c.endHeight {
160 return types.StateClosed
161 }
162 }
163 return c.state
164}
165
166func refresh(c *collection, h int64) {
167 s := stateAt(c, h)
168 if s == c.state {
169 return
170 }
171 if s == types.StateClosed && c.closeHeight == 0 {
172 c.closeHeight = c.endHeight
173 }
174 c.state = s
175 chain.Emit("CollectionState", "cid", c.id, "state", s.String())
176}
177
178// revealDeadline is when anyone may finalize without the missing secrets.
179// It is at most endHeight + RevealWindowMax, which bounds the transfer
180// lock (I-11).
181func revealDeadline(c *collection) int64 {
182 closed := c.closeHeight
183 if closed == 0 {
184 closed = c.endHeight
185 }
186 return closed + c.revealWindow
187}
188
189func caller(cur realm) address {
190 return cur.Previous().Address()
191}
192
193// assertNoDeposit refuses coins on functions that take none, so nothing can
194// get stuck in core (RA L-1). The origin-send envelope belongs to the first
195// realm of the call; it is only ours to judge when a wallet called us.
196func assertNoDeposit(cur realm) {
197 if cur.Previous().IsUserCall() && !unsafe.OriginSend().IsZero() {
198 panic("this function does not accept coins")
199 }
200}
201
202func itoa(n int64) string { return strconv.FormatInt(n, 10) }
203
204func height() int64 { return runtime.ChainHeight() }