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.

disputes.gno

4.23 Kb · 153 lines
  1package bouncer
  2
  3import (
  4	"chain"
  5	"chain/runtime"
  6
  7	"gno.land/p/nt/seqid/v0"
  8)
  9
 10// DisputeRecord is a challenge to an attestation, backed by a different report.
 11type DisputeRecord struct {
 12	ID            uint64
 13	PkgPath       string
 14	AttestationID uint64 // the attestation disputed (the latest at filing time)
 15	ReportHash    string // the disputer's report
 16	Note          string
 17	Filer         address
 18	Height        int64
 19	Resolved      bool
 20	Resolution    string
 21}
 22
 23type dispute struct {
 24	d DisputeRecord
 25}
 26
 27func disputeKey(id uint64) string { return seqid.ID(id).String() }
 28
 29// Dispute files a challenge against the latest attestation of pkgpath,
 30// backed by a report with a different hash. One open dispute per filer
 31// per package.
 32func Dispute(cur realm, pkgpath, reportHash, note string) uint64 {
 33	if !cur.IsCurrent() {
 34		panic("invalid realm")
 35	}
 36	filer := cur.Previous().Address()
 37	rec := getRecord(pkgpath)
 38	if rec == nil {
 39		panic("no attestation to dispute")
 40	}
 41	latest := rec.history[len(rec.history)-1]
 42	if !isHex64(reportHash) {
 43		panic("report hash must be 64 lowercase hex characters")
 44	}
 45	if reportHash == latest.ReportHash {
 46		panic("a dispute needs a report that differs from the attested one")
 47	}
 48	if note == "" || len(note) > maxNoteLen {
 49		panic("note must be 1 to 280 bytes")
 50	}
 51	if hasOpenDispute(pkgpath, filer) {
 52		panic("you already have an open dispute on this package")
 53	}
 54	id := uint64(disputeID.Next())
 55	disputes.Set(disputeKey(id), &dispute{d: DisputeRecord{
 56		ID:            id,
 57		PkgPath:       pkgpath,
 58		AttestationID: latest.ID,
 59		ReportHash:    reportHash,
 60		Note:          note,
 61		Filer:         filer,
 62		Height:        runtime.ChainHeight(),
 63	}})
 64	chain.Emit("Disputed", "id", disputeKey(id), "pkgpath", pkgpath, "filer", filer.String(), "report_hash", reportHash)
 65	return id
 66}
 67
 68// ResolveDispute closes a dispute with a resolution note. Admin or an
 69// attester.
 70func ResolveDispute(cur realm, id uint64, resolution string) {
 71	if !cur.IsCurrent() {
 72		panic("invalid realm")
 73	}
 74	caller := cur.Previous().Address()
 75	if caller != admin && !attesters.Has(string(caller)) {
 76		panic("admin or attester only")
 77	}
 78	if resolution == "" || len(resolution) > maxNoteLen {
 79		panic("resolution must be 1 to 280 bytes")
 80	}
 81	v := disputes.Get(disputeKey(id))
 82	if v == nil {
 83		panic("no such dispute")
 84	}
 85	d := v.(*dispute)
 86	if d.d.Resolved {
 87		panic("dispute already resolved")
 88	}
 89	d.d.Resolved = true
 90	d.d.Resolution = resolution
 91	chain.Emit("DisputeResolved", "id", disputeKey(id), "pkgpath", d.d.PkgPath)
 92}
 93
 94func hasOpenDispute(pkgpath string, filer address) bool {
 95	found := false
 96	disputes.Iterate("", "", func(_ string, v any) bool {
 97		d := v.(*dispute).d
 98		if d.PkgPath == pkgpath && !d.Resolved && (filer == "" || d.Filer == filer) {
 99			found = true
100		}
101		return found
102	})
103	return found
104}
105
106// DisputesFor returns copies of every dispute on pkgpath, oldest first.
107func DisputesFor(pkgpath string) []DisputeRecord {
108	out := []DisputeRecord{}
109	disputes.Iterate("", "", func(_ string, v any) bool {
110		if d := v.(*dispute).d; d.PkgPath == pkgpath {
111			out = append(out, d)
112		}
113		return false
114	})
115	return out
116}
117
118// Reply lets the author of an address-namespace package answer its
119// Passport and, optionally, name the fixed version (successor) in the same
120// namespace.
121func Reply(cur realm, pkgpath, note, successor string) {
122	if !cur.IsCurrent() {
123		panic("invalid realm")
124	}
125	caller := cur.Previous().Address()
126	author, ok := authorOf(pkgpath)
127	if !ok {
128		panic("only address-namespace packages have a verified author")
129	}
130	if caller != author {
131		panic("author only")
132	}
133	rec := getRecord(pkgpath)
134	if rec == nil {
135		panic("no attestation to reply to")
136	}
137	if note == "" || len(note) > maxNoteLen {
138		panic("note must be 1 to 280 bytes")
139	}
140	if successor != "" {
141		if !validPkgPath(successor) || successor == pkgpath {
142			panic("invalid successor path")
143		}
144		if sa, ok := authorOf(successor); !ok || sa != author {
145			panic("the successor must be in the same address namespace")
146		}
147	}
148	if len(rec.replies) >= maxReplies {
149		panic("reply limit reached for this package")
150	}
151	rec.replies = append(rec.replies, AuthorReply{Author: caller, Note: note, Successor: successor, Height: runtime.ChainHeight()})
152	chain.Emit("Replied", "pkgpath", pkgpath, "author", caller.String(), "successor", successor)
153}