Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

access.gno

10.95 Kb · 312 lines
  1package core
  2
  3import (
  4	"chain"
  5
  6	"gno.land/p/nt/avl/v0"
  7
  8	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/safe/v2"
  9)
 10
 11// Engine access policy (I-19, docs/mainnet/engine-access-policy.md).
 12//
 13// An engine's author decides who may open collections with it:
 14//
 15//   - closed   ("signature engine") only the author. Every registration
 16//     starts closed: nothing an author did not choose to share is usable
 17//     by others, and a squat copy stays useless to everyone else.
 18//   - licensed only the author and the creators the author licensed
 19//     (GrantLicence), each for a bounded number of collections and,
 20//     optionally, at a licence fee instead of the manifest's.
 21//   - open     ("tool engine") anyone.
 22//
 23// The policy is judged once, in CreateCollection, and never again: a
 24// policy change, a revoked or used-up licence leaves every collection
 25// already created (Drafts included) as it is. Only the claimed manifest
 26// author changes a policy or a licence; no admin op touches them (the
 27// admin's engine.deprecate still stops new collections for everyone).
 28//
 29// For every policy, a collection whose engine and params are byte-identical
 30// to an earlier collection's is refused unless its creator is that earlier
 31// collection's current creator (paramsTaken). Empty params (the engine's
 32// defaults) are exempt: an engine without params (genesis) would otherwise
 33// back one collection ever.
 34
 35// Policies.
 36const (
 37	PolicyClosed   = "closed"
 38	PolicyLicensed = "licensed"
 39	PolicyOpen     = "open"
 40)
 41
 42// How a collection's creator was allowed to use the engine (collection
 43// JSON "access", CollectionCreated.access).
 44const (
 45	AccessAuthor  = "author"
 46	AccessLicence = "licence"
 47	AccessOpen    = "open"
 48)
 49
 50const (
 51	MaxLicencesPerEngine  = 1000 // live licences of one engine (GrantLicence of a new holder)
 52	MaxLicenceCollections = 1000 // collections one licence allows
 53	LicenceFeeDefault     = -1   // GrantLicence feeBps: the manifest's FeeBps applies
 54)
 55
 56type licence struct {
 57	maxCollections int64
 58	used           int64 // collections created under this licence
 59	feeBps         int64 // LicenceFeeDefault or 0..EngineFeeCapBps, snapshotted into each collection
 60	grantedHeight  int64 // last GrantLicence of this holder
 61}
 62
 63var (
 64	licences    avl.Tree // engine id + ":" + holder -> *licence
 65	paramsTaken avl.Tree // paramsKey(engine id, params) -> cid of the first collection with them
 66)
 67
 68// SetEnginePolicy sets who may create collections with engine id: "closed",
 69// "licensed" or "open". The claimed manifest author only; refused for a
 70// deprecated or superseded engine (it backs no new collection anyway).
 71func SetEnginePolicy(cur realm, id, policy string) {
 72	assertNoDeposit(cur)
 73	rec := mustEngine(id)
 74	assertEngineAuthor(cur, rec, "set its policy")
 75	assertEngineNotRetired(rec)
 76	if policy != PolicyClosed && policy != PolicyLicensed && policy != PolicyOpen {
 77		panic("policy must be \"" + PolicyClosed + "\", \"" + PolicyLicensed + "\" or \"" + PolicyOpen + "\"")
 78	}
 79	rec.policy = policy
 80	chain.Emit("EnginePolicySet", "id", id, "policy", policy)
 81}
 82
 83// GrantLicence lets holder create up to maxCollections (1..
 84// MaxLicenceCollections) collections with engine id while its policy is
 85// "licensed". feeBps is LicenceFeeDefault (-1, the manifest fee) or
 86// 0..EngineFeeCapBps: the engine-author fee of the holder's collections,
 87// snapshotted at their creation (a sale still pays at most the platform
 88// cap). Granting again to a holder updates maxCollections (not below the
 89// used count) and feeBps and keeps the used count. Claimed author only;
 90// not on a deprecated or superseded engine. A grant does not change the
 91// policy.
 92func GrantLicence(cur realm, id string, holder address, maxCollections, feeBps int64) {
 93	assertNoDeposit(cur)
 94	rec := mustEngine(id)
 95	assertEngineAuthor(cur, rec, "grant a licence")
 96	assertEngineNotRetired(rec)
 97	if !holder.IsValid() {
 98		panic("invalid licence holder address")
 99	}
100	if holder == rec.manifest.Author {
101		panic("the engine's author needs no licence")
102	}
103	if maxCollections < 1 || maxCollections > MaxLicenceCollections {
104		panic("maxCollections must be 1.." + itoa(MaxLicenceCollections))
105	}
106	if feeBps != LicenceFeeDefault && (feeBps < 0 || feeBps > EngineFeeCapBps) {
107		panic("licence feeBps must be -1 (the manifest fee) or 0.." + itoa(EngineFeeCapBps))
108	}
109	key := licenceKey(id, holder)
110	l, _ := licences.Get(key).(*licence)
111	if l == nil {
112		if rec.licences >= MaxLicencesPerEngine {
113			panic("engine has " + itoa(MaxLicencesPerEngine) + " licences; revoke one first")
114		}
115		l = &licence{}
116		licences.Set(key, l)
117		rec.licences++
118	} else if maxCollections < l.used {
119		panic("maxCollections must be at least the " + itoa(l.used) +
120			" collections already created under this licence (revoke it to stop it)")
121	}
122	l.maxCollections, l.feeBps, l.grantedHeight = maxCollections, feeBps, height()
123	chain.Emit("LicenceGranted", "id", id, "holder", holder.String(),
124		"maxCollections", itoa(maxCollections), "feeBps", itoa(feeBps))
125}
126
127// RevokeLicence removes holder's licence on engine id. Collections already
128// created under it are not affected. Claimed author only (also on a
129// deprecated or superseded engine).
130func RevokeLicence(cur realm, id string, holder address) {
131	assertNoDeposit(cur)
132	rec := mustEngine(id)
133	assertEngineAuthor(cur, rec, "revoke a licence")
134	key := licenceKey(id, holder)
135	v := licences.Get(key)
136	if v == nil {
137		panic(holder.String() + " holds no licence on " + id)
138	}
139	licences.Remove(key)
140	rec.licences--
141	chain.Emit("LicenceRevoked", "id", id, "holder", holder.String(), "used", itoa(v.(*licence).used))
142}
143
144func assertEngineAuthor(cur realm, rec *engineRecord, what string) {
145	if caller(cur) != rec.manifest.Author {
146		panic("only the engine's author " + rec.manifest.Author.String() + " can " + what)
147	}
148	if !rec.claimed {
149		panic("engine is not claimed by its author yet")
150	}
151}
152
153func assertEngineNotRetired(rec *engineRecord) {
154	if rec.deprecated {
155		panic("engine is deprecated")
156	}
157	if rec.superseded {
158		panic("engine is superseded by " + rec.supersededBy)
159	}
160}
161
162func licenceKey(id string, holder address) string { return id + ":" + holder.String() }
163
164// paramsKey identifies an (engine, params) pair in bounded space.
165func paramsKey(id, params string) string {
166	return safe.Sha256Hex(safe.Domain + "|params|v1|" + id + "|" + params)
167}
168
169// createGrant is what CreateCollection learned from the access gate.
170type createGrant struct {
171	access string
172	lic    *licence // the licence used (AccessLicence), else nil
173	fee    int64    // engine fee override snapshotted into the collection, or LicenceFeeDefault
174	key    string   // paramsKey, "" for empty params
175	reuse  bool     // key already held by this creator
176}
177
178// createAccess is the engine side of CreateCollection's gate: engine
179// usable (claimed, live), creator allowed by the policy, params not held
180// by another creator. It returns the refusal text ("" = allowed) and
181// writes nothing; CreateCollection panics with that text and CreateRefusal
182// returns it.
183func createAccess(rec *engineRecord, creator address, params string) (createGrant, string) {
184	g := createGrant{fee: LicenceFeeDefault}
185	switch {
186	case !rec.claimed:
187		return g, "engine is not claimed by its author yet"
188	case rec.deprecated:
189		return g, "engine is deprecated"
190	case rec.superseded:
191		return g, "engine is superseded by " + rec.supersededBy
192	}
193	switch {
194	case creator == rec.manifest.Author:
195		g.access = AccessAuthor
196	case rec.policy == PolicyOpen:
197		g.access = AccessOpen
198	case rec.policy == PolicyLicensed:
199		l, _ := licences.Get(licenceKey(rec.id, creator)).(*licence)
200		if l == nil {
201			return g, "engine " + rec.id + " is licensed: " + creator.String() +
202				" holds no licence from its author " + rec.manifest.Author.String()
203		}
204		if l.used >= l.maxCollections {
205			return g, "the licence of " + creator.String() + " on " + rec.id + " is used up (" +
206				itoa(l.used) + "/" + itoa(l.maxCollections) + " collections)"
207		}
208		g.access, g.lic, g.fee = AccessLicence, l, l.feeBps
209	default:
210		return g, "engine " + rec.id + " is closed: only its author " + rec.manifest.Author.String() +
211			" creates collections with it"
212	}
213	if params == "" {
214		return g, ""
215	}
216	g.key = paramsKey(rec.id, params)
217	if v := paramsTaken.Get(g.key); v != nil {
218		first := mustCollection(v.(string))
219		if first.creator != creator {
220			return g, "engine " + rec.id + " with these params already backs collection " + first.id +
221				" of another creator; choose other params"
222		}
223		g.reuse = true
224	}
225	return g, ""
226}
227
228// --- reads ---------------------------------------------------------------------
229
230// EnginePolicy is engine id's policy: "closed", "licensed" or "open".
231func EnginePolicy(id string) string { return mustEngine(id).policy }
232
233// Licensed reports whether holder holds a licence on engine id with room
234// for another collection (whatever the current policy).
235func Licensed(id string, holder address) bool {
236	mustEngine(id)
237	l, _ := licences.Get(licenceKey(id, holder)).(*licence)
238	return l != nil && l.used < l.maxCollections
239}
240
241// Licence is holder's licence on engine id as JSON
242// {engine, holder, maxCollections, used, feeBps, grantedHeight}, or null.
243func Licence(id string, holder address) string {
244	mustEngine(id)
245	v := licences.Get(licenceKey(id, holder))
246	if v == nil {
247		return "null"
248	}
249	w := &safe.JSONWriter{}
250	writeLicence(w, id, holder.String(), v.(*licence))
251	return w.String()
252}
253
254// Licences pages engine id's licences in holder-address order.
255func Licences(id string, offset, limit int64) string {
256	mustEngine(id)
257	w := &safe.JSONWriter{}
258	w.BeginArray()
259	prefix := id + ":"
260	skipped, written, want := int64(0), int64(0), clampLimit(limit)
261	licences.Iterate(prefix, prefix+"\xff", func(k string, v any) bool {
262		if skipped < offset {
263			skipped++
264			return false
265		}
266		writeLicence(w, id, k[len(prefix):], v.(*licence))
267		written++
268		return written >= want
269	})
270	w.EndArray()
271	return w.String()
272}
273
274func writeLicence(w *safe.JSONWriter, id, holder string, l *licence) {
275	w.BeginObject()
276	w.StrField("engine", id)
277	w.StrField("holder", holder)
278	w.IntField("maxCollections", l.maxCollections)
279	w.IntField("used", l.used)
280	w.IntField("feeBps", l.feeBps)
281	w.IntField("grantedHeight", l.grantedHeight)
282	w.EndObject()
283}
284
285// ParamsTaken is the cid of the first collection created with engine id and
286// exactly these params, or "" (always "" for empty params).
287func ParamsTaken(id, params string) string {
288	if params == "" {
289		return ""
290	}
291	if v := paramsTaken.Get(paramsKey(id, params)); v != nil {
292		return v.(string)
293	}
294	return ""
295}
296
297// CreateRefusal is why CreateCollection would refuse creator on engine id
298// with params now, as core's own text, or "" when the engine gates pass:
299// unknown engine, create paused, unclaimed/deprecated/superseded, the
300// policy, a used-up licence, params of another creator. The engine's own
301// ValidateParams and the spec's other fields are not judged here.
302func CreateRefusal(id string, creator address, params string) string {
303	v := engines.Get(id)
304	if v == nil {
305		return "unknown engine " + id
306	}
307	if Paused("create") {
308		return "paused: create"
309	}
310	_, why := createAccess(v.(*engineRecord), creator, params)
311	return why
312}