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