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.

assets.gno

23.56 Kb · 774 lines
  1// Package assets is Sinopia's immutable asset store for uploaded
  2// collections (docs/uploaded-collections.md §4–§6, §0a). An artist creates a
  3// store, declares artworks (on-chain bytes or IPFS CIDs, each with its
  4// sha256, editions, a creator-assigned tier and trait values), uploads the
  5// on-chain bytes in chunks, and seals it. A sealed store never changes: the
  6// upload engine (r/…/engines/upload) binds one sealed store to one Draft
  7// collection and reads its art from here.
  8//
  9// Guarantees (design §14):
 10//   - only the store's owner writes, and only from a wallet (direct
 11//     MsgCall, IsUserCall); nothing here accepts or holds coins;
 12//   - a store is Draft until Seal, which is irreversible: records, titles,
 13//     dictionaries and bytes are immutable afterwards;
 14//   - an "on-chain" artwork is complete only when every byte is stored,
 15//     its sha256 equals the declared one and media.CheckImage passed
 16//     (magic + dimensions; SVG through safe.AssertSVG, 16 KiB cap);
 17//   - an "ipfs" artwork always says ipfs; a raw CID's digest is checked
 18//     against the declared sha256 on chain;
 19//   - every user string passed safe.AssertText; JSON goes through
 20//     safe.JSONWriter.
 21//
 22// Storage is paid by the owner who writes it, and only the owner can free
 23// Draft data (Truncate), so refunds go back to the payer (T8).
 24package assets
 25
 26import (
 27	"chain"
 28	"chain/runtime"
 29	"chain/runtime/unsafe"
 30	"crypto/sha256"
 31	"encoding/hex"
 32	"strconv"
 33	"strings"
 34
 35	"gno.land/p/nt/avl/v0"
 36
 37	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia/media/v1"
 38	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia/safe/v1"
 39)
 40
 41// Limits (design §4 as amended by §0a / D4, and by the UP-M1 review fix:
 42// every read of the largest store these limits allow must stay far below
 43// the 3B query gas cap; worst cases are locked by zz_worst_gas_test.gno in
 44// r/reader).
 45const (
 46	MaxArtworks          = 10_000 // = core MaxSupplyCap
 47	MaxSlots             = 10_000 // Bind requires slots == maxSupply
 48	MaxEditions          = 10_000
 49	MaxTraitTypes        = 8   // core-path manifest slots "Trait 1".."Trait 8" (was 16)
 50	MaxTiers             = 8
 51	MaxDictValues        = 100 // distinct values per trait type (was 255)
 52	MaxStoreValues       = 255 // distinct values over all trait types of a store (new; u8 id + 1 in a record)
 53	MaxNameLen           = 20  // trait type and tier names
 54	MaxValueLen          = 32  // trait values (was 40)
 55	MaxTitleLen          = 64  // artwork titles (= engine.MaxTraitLen)
 56	MaxLabelLen          = 64
 57	MaxStoreOnchainBytes = 4 << 20 // ~430 GNOT of deposit at 100 ugnot/B
 58	MaxChunk             = 262_144 // per AppendBytes call; a tx carries ~540 KB raw (WP0)
 59	SegmentBytes         = 32_768  // byte-log value size (~6% key overhead, measured)
 60	MaxBatch             = 100     // records per AddArtworks
 61	MaxPage              = 20      // Artworks page (was 50)
 62)
 63
 64const zero32 = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
 65
 66// reserved trait type names: the adapter adds these itself (case-insensitive).
 67var reservedTraitTypes = []string{"tier", "edition", "storage", "status", "artwork"}
 68
 69type store struct {
 70	id            int64
 71	owner         address
 72	label         string
 73	sealed        bool
 74	createdHeight int64
 75	sealedHeight  int64
 76	traitTypes    []string // order = slot order ("Trait k")
 77	tiers         []string // ordered low -> high
 78	n             int64    // artworks
 79	slots         int64    // sum of editions
 80	onchainN      int64
 81	ipfsN         int64
 82	onchainBytes  int64 // declared on-chain bytes (sum of chain sizes)
 83	logLen        int64 // bytes received in the byte log
 84	buckets       avl.Tree // safe.PadInt(idx/bucketSize, 3) -> *bucket
 85	log           avl.Tree // safe.PadInt(segment, 3) -> string (<= SegmentBytes)
 86	shas          *shaIndex
 87	// Set composition and names for reads that never loop over artworks and
 88	// never escape at read time (review UP-M1). Three slice objects per store.
 89	names     []string    // JSON of the trait types, then of the tiers (escaped once, at NewStore)
 90	dict      []dictEntry // every distinct trait value, in first-use order; a record stores id + 1
 91	cnt       []int64     // counters: [0..t] tier artworks, [t+1..2t+1] tier slots (index 0 = untiered),
 92	                      // then per dict entry i: [base+2i] artworks, [base+2i+1] slots (base = 2t+2)
 93	perType   [MaxTraitTypes]int
 94	chainIdx  string // indexes of the on-chain artworks, u32 big-endian each, in order
 95	chainDone int64  // how many of them are complete (bytes arrive in artwork order)
 96}
 97
 98// dictEntry is one distinct trait value of a store: its trait type, the
 99// value and its JSON (escaped once, when first used).
100type dictEntry struct {
101	k  int
102	v  string
103	js string
104}
105
106func (s *store) cntBase() int { return 2*len(s.tiers) + 2 }
107
108// count counts one record in (sign +1) or out (sign -1).
109func (s *store) count(r *record, sign int64) {
110	ed := r.editions * sign
111	t := len(s.tiers)
112	s.cnt[r.tier] += sign
113	s.cnt[t+1+r.tier] += ed
114	base := s.cntBase()
115	for k := range s.traitTypes {
116		if id := r.traits[k]; id > 0 {
117			s.cnt[base+2*(id-1)] += sign
118			s.cnt[base+2*(id-1)+1] += ed
119		}
120	}
121}
122
123// shaIndex is the sorted list of every artwork's raw sha256 (32 B each),
124// one object per store: duplicate detection without one avl key (~2 KB)
125// per artwork.
126type shaIndex struct {
127	s string
128}
129
130var (
131	stores     avl.Tree // safe.PadInt(id, 10) -> *store
132	storeCount int64
133)
134
135func storeKey(id int64) string { return safe.PadInt(id, 10) }
136
137func bucketKey(b int64) string { return safe.PadInt(b, 3) }
138
139func mustStore(storeID string) *store {
140	v := stores.Get(storeKey(safe.ParseID("store", storeID)))
141	if v == nil {
142		panic("unknown store " + strconv.Quote(storeID))
143	}
144	return v.(*store)
145}
146
147func (s *store) idStr() string { return strconv.FormatInt(s.id, 10) }
148
149// assertWallet: every write comes straight from a wallet's MsgCall and
150// carries no coins.
151func assertWallet(cur realm) {
152	if !cur.Previous().IsUserCall() {
153		panic("asset stores are written by their owner's wallet only (direct call)")
154	}
155	if !unsafe.OriginSend().IsZero() {
156		panic("this function does not accept coins")
157	}
158}
159
160func ownedDraft(cur realm, storeID string) *store {
161	assertWallet(cur)
162	s := mustStore(storeID)
163	if cur.Previous().Address() != s.owner {
164		panic("only the store owner " + s.owner.String() + " can change store " + storeID)
165	}
166	if s.sealed {
167		panic("store " + storeID + " is sealed")
168	}
169	return s
170}
171
172// --- writes ------------------------------------------------------------------
173
174// NewStore creates an empty Draft store owned by the calling wallet.
175// traitTypes and tiers are '|'-separated name lists ("" = none): at most 16
176// trait types and 8 tiers (tiers ordered low -> high), names 1..20 bytes,
177// no ':' and no leading/trailing space, unique ignoring case; "Tier",
178// "Edition", "Storage", "Status" and "Artwork" are reserved trait types.
179// Returns the store id. Event StoreCreated{store, owner}.
180func NewStore(cur realm, label, traitTypes, tiers string) string {
181	assertWallet(cur)
182	safe.AssertText("label", label, MaxLabelLen)
183	tt := parseNames("traitTypes", traitTypes, MaxTraitTypes, true)
184	ts := parseNames("tiers", tiers, MaxTiers, false)
185	storeCount++
186	s := &store{
187		id:            storeCount,
188		owner:         cur.Previous().Address(),
189		label:         label,
190		createdHeight: runtime.ChainHeight(),
191		traitTypes:    tt,
192		tiers:         ts,
193		shas:          &shaIndex{},
194		names:         append(jsonAll(tt), jsonAll(ts)...),
195		cnt:           make([]int64, 2*len(ts)+2),
196	}
197	stores.Set(storeKey(s.id), s)
198	chain.Emit("StoreCreated", "store", s.idStr(), "owner", s.owner.String())
199	return s.idStr()
200}
201
202func jsonAll(xs []string) []string {
203	out := make([]string, len(xs))
204	for i, x := range xs {
205		out[i] = jsonStr(x)
206	}
207	return out
208}
209
210// jsonStr is safe.JSONString (same bytes: '"' '\\' \n \r \t, '<' '>' '&',
211// U+2028/9 and other controls escaped) without its per-character hex
212// formatting, which made '<'-heavy text cost ~10x more (UP-M1).
213func jsonStr(x string) string {
214	plain := true
215	for i := 0; i < len(x); i++ {
216		c := x[i]
217		if c < 0x20 || c == '"' || c == '\\' || c == '<' || c == '>' || c == '&' || c == 0x7f || c == 0xe2 {
218			plain = false
219			break
220		}
221	}
222	if plain {
223		return `"` + x + `"`
224	}
225	const hexd = "0123456789abcdef"
226	var b strings.Builder
227	b.WriteByte('"')
228	for _, r := range x {
229		switch r {
230		case '"':
231			b.WriteString(`\"`)
232		case '\\':
233			b.WriteString(`\\`)
234		case '\n':
235			b.WriteString(`\n`)
236		case '\r':
237			b.WriteString(`\r`)
238		case '\t':
239			b.WriteString(`\t`)
240		case '<':
241			b.WriteString(`\u003c`)
242		case '>':
243			b.WriteString(`\u003e`)
244		case '&':
245			b.WriteString(`\u0026`)
246		case 0x2028:
247			b.WriteString(`\u2028`)
248		case 0x2029:
249			b.WriteString(`\u2029`)
250		default:
251			if r < 0x20 || r == 0x7f {
252				b.WriteString(`\u00`)
253				b.WriteByte(hexd[(r>>4)&0xf])
254				b.WriteByte(hexd[r&0xf])
255				continue
256			}
257			b.WriteRune(r)
258		}
259	}
260	b.WriteByte('"')
261	return b.String()
262}
263
264func parseNames(field, list string, max int, traits bool) []string {
265	if list == "" {
266		return []string{}
267	}
268	parts := strings.Split(list, "|")
269	if len(parts) > max {
270		panic(field + ": at most " + strconv.Itoa(max))
271	}
272	out := make([]string, 0, len(parts))
273	for _, p := range parts {
274		safe.AssertNonEmptyText(field, p, MaxNameLen)
275		if strings.Contains(p, ":") || strings.TrimSpace(p) != p {
276			panic(field + ": " + strconv.Quote(p) + " may not contain ':' or leading/trailing spaces")
277		}
278		low := strings.ToLower(p)
279		if traits {
280			for _, r := range reservedTraitTypes {
281				if low == r {
282					panic(field + ": " + strconv.Quote(p) + " is reserved")
283				}
284			}
285		}
286		for _, prev := range out {
287			if strings.ToLower(prev) == low {
288				panic(field + ": duplicate " + strconv.Quote(p))
289			}
290		}
291		out = append(out, p)
292	}
293	return out
294}
295
296// AddArtworks declares up to MaxBatch artworks, one per '\n'-separated line:
297//
298//	kind|mime|size|sha256|cid|editions|tier|title|v1|…|vN
299//
300// with exactly one value per trait type of the store (N = len(traitTypes)):
301//   - kind "chain" | "ipfs"; mime "svg" | "png" | "webp" | "jpeg";
302//   - size: decimal bytes, chain svg <= 16,384, chain raster <= 262,144,
303//     ipfs raw <= 1 MiB, ipfs dag-pb <= 50 MiB; the store's on-chain total
304//     stays <= 4 MiB;
305//   - sha256: 64 lower-case hex of the file bytes, unique in the store
306//     (use editions for copies);
307//   - cid: "" for chain; for ipfs a CIDv1 (media.ParseCIDv1): raw codec ⇒
308//     its digest must equal sha256 (verified binding), dag-pb ⇒ digest stored;
309//   - editions 1..10,000 (the store's slots stay <= 10,000);
310//   - tier "" or one of the store's tiers; title <= 64 bytes;
311//   - values "" (none) or <= 32 bytes, no leading/trailing space; at most
312//     100 distinct values per trait type and 255 over all types of a store.
313//
314// Returns "first-last" (0-based indexes). Event ArtworkAdded per line.
315func AddArtworks(cur realm, storeID, batch string) string {
316	s := ownedDraft(cur, storeID)
317	if batch == "" {
318		panic("batch: empty")
319	}
320	lines := split(batch, '\n')
321	if len(lines) > MaxBatch {
322		panic("batch: at most " + strconv.Itoa(MaxBatch) + " artworks per call")
323	}
324	if s.n+int64(len(lines)) > MaxArtworks {
325		panic("store: at most " + strconv.Itoa(MaxArtworks) + " artworks")
326	}
327	first := s.n
328	shas := make([]string, 0, len(lines))
329	// per-call (type, value) -> id map, built once from the dictionary: a
330	// line costs O(1) per trait instead of a scan of the values (UP-L1)
331	var look map[string]int
332	for i, line := range lines {
333		f := split(line, '|')
334		r, title := s.parseLine(i+1, f, &look)
335		shas = append(shas, r.sha)
336		s.appendRecord(&r, title)
337		// the line's sha256 and CID are already canonical (strict parsers):
338		// re-encoding them would cost ~1.5M gas per line for the same text
339		chain.Emit("ArtworkAdded", "store", s.idStr(), "idx", strconv.FormatInt(s.n-1, 10),
340			"kind", media.KindName(r.kind), "mime", media.MimeName(r.mime),
341			"size", strconv.FormatInt(r.size, 10), "sha256", f[3], "cid", f[4],
342			"editions", strconv.FormatInt(r.editions, 10), "tier", s.tierName(r.tier))
343	}
344	s.shas.insert(shas)
345	return strconv.FormatInt(first, 10) + "-" + strconv.FormatInt(s.n-1, 10)
346}
347
348// split is strings.Split for a one-byte separator in a single pass (half
349// the VM gas of strings.Split, which dominates a 100-line batch).
350func split(s string, sep byte) []string {
351	out := make([]string, 0, 16)
352	start := 0
353	for i := 0; i < len(s); i++ {
354		if s[i] == sep {
355			out = append(out, s[start:i])
356			start = i + 1
357		}
358	}
359	return append(out, s[start:])
360}
361
362func (s *store) parseLine(no int, f []string, look *map[string]int) (record, string) {
363	at := "line " + strconv.Itoa(no) + ": "
364	if want := 8 + len(s.traitTypes); len(f) != want {
365		panic(at + "expected " + strconv.Itoa(want) + " '|'-separated fields, got " + strconv.Itoa(len(f)))
366	}
367	var r record
368	switch f[0] {
369	case "chain":
370		r.kind = media.KindChain
371	case "ipfs":
372	default:
373		panic(at + "kind must be chain or ipfs")
374	}
375	r.mime = parseMime(at, f[1])
376	r.size = parseCount(at+"size", f[2], 1<<62)
377	shaRaw, ok := media.Raw32(f[3])
378	if !ok {
379		panic(at + "sha256 must be 64 lower-case hex characters")
380	}
381	r.sha = shaRaw
382	r.digest = zero32
383	if r.kind == media.KindChain {
384		if f[4] != "" {
385			panic(at + "an on-chain artwork has no cid")
386		}
387	} else {
388		codec, digest, err := media.ParseCIDv1(f[4])
389		if err != nil {
390			panic(at + err.Error())
391		}
392		if codec == media.CodecRaw {
393			if digest != f[3] {
394				panic(at + "a raw CID's digest must equal the file's sha256")
395			}
396			r.kind = media.KindIPFSRaw
397		} else {
398			r.kind = media.KindIPFSUnixFS
399			r.digest, _ = media.Raw32(digest)
400		}
401	}
402	if max := media.MaxBytes(r.kind, r.mime); r.size > max {
403		panic(at + "size " + f[2] + " exceeds " + strconv.FormatInt(max, 10) + " bytes for this kind and type")
404	}
405	r.editions = parseCount(at+"editions", f[5], MaxEditions)
406	if s.slots+r.editions > MaxSlots {
407		panic(at + "the store would exceed " + strconv.Itoa(MaxSlots) + " slots (sum of editions)")
408	}
409	r.tier = s.tierIndex(at, f[6])
410	title := f[7]
411	safe.AssertText(at+"title", title, MaxTitleLen)
412	for k := range s.traitTypes {
413		r.traits[k] = s.dictIndex(at, k, f[8+k], look)
414	}
415	if r.kind == media.KindChain {
416		if s.onchainBytes+r.size > MaxStoreOnchainBytes {
417			panic(at + "the store would exceed " + strconv.Itoa(MaxStoreOnchainBytes) + " on-chain bytes")
418		}
419		r.logOff = s.onchainBytes
420	} else {
421		r.have = r.size
422	}
423	r.slotEnd = s.slots + r.editions
424	return r, title
425}
426
427func parseMime(at, s string) (m int) {
428	switch s {
429	case "svg":
430		return media.MimeSVG
431	case "png":
432		return media.MimePNG
433	case "webp":
434		return media.MimeWebP
435	case "jpeg":
436		return media.MimeJPEG
437	}
438	panic(at + "mime must be svg, png, webp or jpeg")
439}
440
441// parseCount parses a canonical positive decimal (no sign, no leading zero) <= max.
442func parseCount(field, s string, max int64) int64 {
443	if s == "" || len(s) > 19 || s[0] == '0' {
444		panic(field + ": must be a positive decimal")
445	}
446	var n int64
447	for i := 0; i < len(s); i++ {
448		c := s[i]
449		if c < '0' || c > '9' {
450			panic(field + ": must be a positive decimal")
451		}
452		n = n*10 + int64(c-'0')
453		if n > max {
454			panic(field + ": at most " + strconv.FormatInt(max, 10))
455		}
456	}
457	return n
458}
459
460func (s *store) tierIndex(at, name string) int {
461	if name == "" {
462		return 0
463	}
464	for i, t := range s.tiers {
465		if t == name {
466			return i + 1
467		}
468	}
469	panic(at + "unknown tier " + strconv.Quote(name))
470}
471
472func (s *store) tierName(i int) string {
473	if i == 0 {
474		return ""
475	}
476	return s.tiers[i-1]
477}
478
479func (s *store) dictIndex(at string, k int, v string, look *map[string]int) int {
480	if v == "" {
481		return 0
482	}
483	if *look == nil {
484		m := make(map[string]int, len(s.dict))
485		for i, e := range s.dict {
486			m[strconv.Itoa(e.k)+"|"+e.v] = i + 1
487		}
488		*look = m
489	}
490	key := strconv.Itoa(k) + "|" + v
491	if id, ok := (*look)[key]; ok {
492		return id
493	}
494	// a new value: checked once, stored with its JSON and zero counters
495	safe.AssertText(at+s.traitTypes[k], v, MaxValueLen)
496	if strings.TrimSpace(v) != v {
497		panic(at + s.traitTypes[k] + ": no leading or trailing spaces")
498	}
499	if s.perType[k] >= MaxDictValues {
500		panic(at + s.traitTypes[k] + ": at most " + strconv.Itoa(MaxDictValues) + " distinct values")
501	}
502	if len(s.dict) >= MaxStoreValues {
503		panic(at + "a store holds at most " + strconv.Itoa(MaxStoreValues) + " distinct trait values")
504	}
505	s.dict = append(s.dict, dictEntry{k: k, v: v, js: jsonStr(v)})
506	s.cnt = append(s.cnt, 0, 0)
507	s.perType[k]++
508	(*look)[key] = len(s.dict)
509	return len(s.dict)
510}
511
512func (s *store) appendRecord(r *record, title string) {
513	k := bucketKey(s.n / bucketSize)
514	var b *bucket
515	if v := s.buckets.Get(k); v != nil {
516		b = v.(*bucket)
517	} else {
518		b = &bucket{}
519		s.buckets.Set(k, b)
520	}
521	b.rec += r.encode()
522	b.titles += string([]byte{byte(len(title))}) + title
523	if r.kind == media.KindChain {
524		s.onchainN++
525		s.onchainBytes += r.size
526		s.chainIdx += u32(s.n)
527	} else {
528		s.ipfsN++
529	}
530	s.n++
531	s.slots = r.slotEnd
532	s.count(r, 1)
533}
534
535func u32(v int64) string {
536	return string([]byte{byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)})
537}
538
539// insert merges sorted-unique new digests into the index; any duplicate
540// (inside the batch or with an earlier artwork) aborts the call.
541func (x *shaIndex) insert(add []string) {
542	for i := 1; i < len(add); i++ { // insertion sort, <= MaxBatch items
543		for j := i; j > 0 && add[j] < add[j-1]; j-- {
544			add[j], add[j-1] = add[j-1], add[j]
545		}
546	}
547	for i := 1; i < len(add); i++ {
548		if add[i] == add[i-1] {
549			panic("sha256 " + hex.EncodeToString([]byte(add[i])) + " appears twice; use editions for copies")
550		}
551	}
552	var b strings.Builder
553	pos := 0
554	for _, d := range add {
555		at := x.search(d)
556		if at < len(x.s) && x.s[at:at+32] == d {
557			panic("sha256 " + hex.EncodeToString([]byte(d)) + " is already in this store; use editions for copies")
558		}
559		b.WriteString(x.s[pos:at])
560		b.WriteString(d)
561		pos = at
562	}
563	b.WriteString(x.s[pos:])
564	x.s = b.String()
565}
566
567// remove drops the given digests (each present once).
568func (x *shaIndex) remove(drop []string) {
569	for _, d := range drop {
570		at := x.search(d)
571		if at < len(x.s) && x.s[at:at+32] == d {
572			x.s = x.s[:at] + x.s[at+32:]
573		}
574	}
575}
576
577// search is the byte offset of the first entry >= d.
578func (x *shaIndex) search(d string) int {
579	lo, hi := 0, len(x.s)/32
580	for lo < hi {
581		mid := (lo + hi) / 2
582		if x.s[mid*32:mid*32+32] < d {
583			lo = mid + 1
584		} else {
585			hi = mid
586		}
587	}
588	return lo * 32
589}
590
591// AppendBytes stores the next chunk of an on-chain artwork. Bytes arrive
592// in artwork order: idx must be the lowest incomplete on-chain artwork and
593// offset its Have (resume = read Have). chunk is a []byte argument (the
594// keeper decodes the MsgCall's base64 natively, WP0), 1..MaxChunk bytes.
595// When the artwork is complete its sha256 must equal the declared one and
596// media.CheckImage must pass, else the call panics and nothing is stored.
597// Event ArtworkComplete{store, idx}.
598func AppendBytes(cur realm, storeID string, idx, offset int64, chunk []byte) {
599	s := ownedDraft(cur, storeID)
600	if idx < 0 || idx >= s.n {
601		panic("no artwork " + strconv.FormatInt(idx, 10) + " in store " + storeID)
602	}
603	b, i := s.bucketOf(idx)
604	r := decodeRecord(b.raw(i))
605	if r.kind != media.KindChain {
606		panic("artwork " + strconv.FormatInt(idx, 10) + " is not stored on chain")
607	}
608	if r.complete() {
609		panic("artwork " + strconv.FormatInt(idx, 10) + " is already complete")
610	}
611	if offset != r.have {
612		panic("offset must equal have (" + strconv.FormatInt(r.have, 10) + ")")
613	}
614	if r.logOff+r.have != s.logLen {
615		panic("upload the lowest incomplete on-chain artwork first")
616	}
617	n := int64(len(chunk))
618	if n == 0 || n > MaxChunk {
619		panic("chunk must be 1.." + strconv.Itoa(MaxChunk) + " bytes")
620	}
621	if r.have+n > r.size {
622		panic("chunk exceeds the declared size (" + strconv.FormatInt(r.size-r.have, 10) + " bytes left)")
623	}
624	s.appendLog(string(chunk))
625	r.have += n
626	if r.complete() {
627		data := s.readLog(r.logOff, r.size)
628		sum := sha256.Sum256([]byte(data))
629		if string(sum[:]) != r.sha {
630			panic("sha256 mismatch: the uploaded bytes are not the declared file")
631		}
632		if _, _, err := media.CheckImage(r.mime, data); err != nil {
633			panic("artwork " + strconv.FormatInt(idx, 10) + " rejected: " + err.Error())
634		}
635		s.chainDone++ // in-order uploads: idx was the next on-chain artwork
636		chain.Emit("ArtworkComplete", "store", storeID, "idx", strconv.FormatInt(idx, 10))
637	}
638	b.setRaw(i, r.encode())
639}
640
641func (s *store) bucketOf(idx int64) (*bucket, int) {
642	v := s.buckets.Get(bucketKey(idx / bucketSize))
643	if v == nil {
644		panic("assets: missing record bucket")
645	}
646	return v.(*bucket), int(idx % bucketSize)
647}
648
649func (s *store) appendLog(chunk string) {
650	for len(chunk) > 0 {
651		k := bucketKey(s.logLen / SegmentBytes)
652		seg := ""
653		if v := s.log.Get(k); v != nil {
654			seg = v.(string)
655		}
656		n := SegmentBytes - len(seg)
657		if n > len(chunk) {
658			n = len(chunk)
659		}
660		s.log.Set(k, seg+chunk[:n])
661		s.logLen += int64(n)
662		chunk = chunk[n:]
663	}
664}
665
666func (s *store) readLog(off, n int64) string {
667	var b strings.Builder
668	for n > 0 {
669		seg := off / SegmentBytes
670		v := s.log.Get(bucketKey(seg)).(string)
671		lo := off - seg*SegmentBytes
672		hi := int64(len(v))
673		if hi-lo > n {
674			hi = lo + n
675		}
676		b.WriteString(v[lo:hi])
677		n -= hi - lo
678		off += hi - lo
679	}
680	return b.String()
681}
682
683// cutLog shortens the byte log to newLen bytes (frees whole segments).
684func (s *store) cutLog(newLen int64) {
685	for s.logLen > newLen {
686		seg := (s.logLen - 1) / SegmentBytes
687		start := seg * SegmentBytes
688		k := bucketKey(seg)
689		if start >= newLen {
690			s.log.Remove(k)
691			s.logLen = start
692			continue
693		}
694		v := s.log.Get(k).(string)
695		s.log.Set(k, v[:newLen-start])
696		s.logLen = newLen
697	}
698}
699
700// Truncate drops artworks keep..n-1 of a Draft store and cuts the byte log
701// back, freeing their storage (the chain refunds the signer, i.e. the owner
702// who paid it). Trait values first used by dropped artworks stay in the
703// dictionary (with zero counts; they still count towards MaxStoreValues).
704// Event StoreTruncated{store, keep}.
705func Truncate(cur realm, storeID string, keep int64) {
706	s := ownedDraft(cur, storeID)
707	if keep < 0 || keep >= s.n {
708		panic("keep must be 0.." + strconv.FormatInt(s.n-1, 10))
709	}
710	cut := int64(-1)
711	var dropped []string
712	for idx := keep; idx < s.n; idx++ {
713		b, i := s.bucketOf(idx)
714		r := decodeRecord(b.raw(i))
715		dropped = append(dropped, r.sha)
716		s.count(&r, -1)
717		if r.kind == media.KindChain {
718			s.onchainN--
719			if r.complete() {
720				s.chainDone--
721			}
722			if cut < 0 {
723				cut = r.logOff
724			}
725		} else {
726			s.ipfsN--
727		}
728	}
729	s.chainIdx = s.chainIdx[:s.onchainN*4]
730	if cut >= 0 {
731		s.onchainBytes = cut
732		if s.logLen > cut {
733			s.cutLog(cut)
734		}
735	}
736	s.slots = 0
737	if keep > 0 {
738		b, i := s.bucketOf(keep - 1)
739		s.slots = decodeRecord(b.raw(i)).slotEnd
740	}
741	last := (s.n - 1) / bucketSize
742	kb := keep / bucketSize
743	for bk := last; bk > kb; bk-- {
744		s.buckets.Remove(bucketKey(bk))
745	}
746	if keep%bucketSize == 0 {
747		s.buckets.Remove(bucketKey(kb))
748	} else {
749		v := s.buckets.Get(bucketKey(kb)).(*bucket)
750		v.keep(int(keep % bucketSize))
751	}
752	s.n = keep
753	s.shas.remove(dropped)
754	chain.Emit("StoreTruncated", "store", storeID, "keep", strconv.FormatInt(keep, 10))
755}
756
757// Seal freezes a store for good. It needs at least one artwork and every
758// on-chain artwork complete. Event StoreSealed{store, artworks, slots,
759// onchainN, ipfsN, onchainBytes}.
760func Seal(cur realm, storeID string) {
761	s := ownedDraft(cur, storeID)
762	if s.n == 0 {
763		panic("a store needs at least one artwork to be sealed")
764	}
765	if s.logLen != s.onchainBytes {
766		panic("on-chain artworks are incomplete: " + strconv.FormatInt(s.logLen, 10) + " of " +
767			strconv.FormatInt(s.onchainBytes, 10) + " bytes uploaded")
768	}
769	s.sealed = true
770	s.sealedHeight = runtime.ChainHeight()
771	chain.Emit("StoreSealed", "store", storeID, "artworks", strconv.FormatInt(s.n, 10),
772		"slots", strconv.FormatInt(s.slots, 10), "onchainN", strconv.FormatInt(s.onchainN, 10),
773		"ipfsN", strconv.FormatInt(s.ipfsN, 10), "onchainBytes", strconv.FormatInt(s.onchainBytes, 10))
774}