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.

maintenance.gno

6.05 Kb · 202 lines
  1// Package maintenance requires two independent reviewers to settle a milestone.
  2package maintenance
  3
  4import (
  5	"chain"
  6	"chain/runtime"
  7	"chain/runtime/unsafe"
  8	"strconv"
  9
 10	"gno.land/p/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/budget/v1"
 11	"gno.land/r/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/treasury/v1"
 12)
 13
 14const packagePath = "gno.land/r/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/maintenance/v1"
 15
 16type Roles struct {
 17	Coordinator address
 18	Reviewers   [3]address
 19}
 20
 21type milestone struct {
 22	digest string
 23	votes  [3]string
 24}
 25
 26type program struct {
 27	roles      Roles
 28	milestones map[int64]*milestone
 29}
 30
 31type MilestoneView struct {
 32	Evidence    string
 33	Votes       [3]string
 34	Reservation treasury.ReservationView
 35}
 36
 37var programs = make(map[int64]*program)
 38
 39func Configure(cur realm, programID int64, coordinator, reviewer1, reviewer2, reviewer3 address) {
 40	runtime.AssertOriginCall()
 41	caller := userCaller(cur)
 42	config := treasury.GetProgram(programID)
 43	if config.Sponsor != caller {
 44		panic("only the program sponsor can configure maintenance")
 45	}
 46	if config.Status != treasury.Draft || config.Maintenance.Path != packagePath || programs[programID] != nil {
 47		panic("maintenance configuration requires an unconfigured draft with this policy pinned")
 48	}
 49	budget.RequireAddress(coordinator)
 50	reviewers := [3]address{reviewer1, reviewer2, reviewer3}
 51	for _, reviewer := range reviewers {
 52		budget.RequireAddress(reviewer)
 53		if reviewer == coordinator {
 54			panic("coordinator cannot be a reviewer")
 55		}
 56	}
 57	if reviewer1 == reviewer2 || reviewer1 == reviewer3 || reviewer2 == reviewer3 {
 58		panic("reviewers must be distinct")
 59	}
 60
 61	programs[programID] = &program{
 62		roles:      Roles{Coordinator: coordinator, Reviewers: reviewers},
 63		milestones: make(map[int64]*milestone),
 64	}
 65	treasury.RegisterPolicy(cross(cur), programID)
 66	chain.Emit("MaintenanceConfigured", "program", number(programID), "coordinator", string(coordinator))
 67}
 68
 69// SubmitMilestone fixes the evidence and opens a reservation for holdBlocks.
 70func SubmitMilestone(cur realm, programID, itemID int64, recipient address, amount, holdBlocks int64, proofDigest string) int64 {
 71	runtime.AssertOriginCall()
 72	caller := userCaller(cur)
 73	p := mustProgram(programID)
 74	if caller != p.roles.Coordinator {
 75		panic("only the maintenance coordinator can submit")
 76	}
 77	evidence := budget.EvidenceCommitment(programID, budget.Maintenance, itemID, proofDigest)
 78	budget.RequirePositive(holdBlocks)
 79	deadline := budget.Add(runtime.ChainHeight(), holdBlocks)
 80	available := treasury.Reserve(cross(cur), programID, itemID, recipient, amount, deadline)
 81	p.milestones[itemID] = &milestone{digest: evidence}
 82	chain.Emit("MilestoneSubmitted", "program", number(programID), "item", number(itemID), "actor", string(caller), "evidence", evidence)
 83	return available
 84}
 85
 86func Vote(cur realm, programID, itemID int64, evidence string, approve bool) string {
 87	runtime.AssertOriginCall()
 88	caller := userCaller(cur)
 89	p := mustProgram(programID)
 90	index := p.reviewerIndex(caller)
 91	item := p.mustMilestone(itemID)
 92	reservation := treasury.GetReservation(programID, budget.Maintenance, itemID)
 93	if caller == reservation.Recipient {
 94		panic("recipient cannot review their own milestone")
 95	}
 96	if reservation.Status != treasury.Reserved || reservation.ExpiredAtHeight {
 97		panic("milestone is no longer open for review")
 98	}
 99	if item.digest != evidence {
100		panic("milestone evidence does not match")
101	}
102	if item.votes[index] != "" {
103		panic("reviewer already voted")
104	}
105
106	vote := treasury.Rejected
107	if approve {
108		vote = treasury.Approved
109	}
110	item.votes[index] = vote
111	chain.Emit("MilestoneVote", "program", number(programID), "item", number(itemID), "reviewer", string(caller), "decision", vote, "evidence", evidence)
112	matching := 0
113	for _, recorded := range item.votes {
114		if recorded == vote {
115			matching++
116		}
117	}
118	if matching < 2 {
119		return treasury.Reserved
120	}
121	if approve {
122		treasury.Approve(cross(cur), programID, itemID)
123	} else {
124		treasury.Reject(cross(cur), programID, itemID)
125	}
126	return vote
127}
128
129func GetRoles(programID int64) Roles {
130	return mustProgram(programID).roles
131}
132
133func GetMilestone(programID, itemID int64) MilestoneView {
134	item := mustProgram(programID).mustMilestone(itemID)
135	return MilestoneView{
136		Evidence:    item.digest,
137		Votes:       item.votes,
138		Reservation: treasury.GetReservation(programID, budget.Maintenance, itemID),
139	}
140}
141
142func Render(path string) string {
143	id, itemID := budget.ParsePolicyViewPath(path)
144	p := programs[id]
145	if p == nil {
146		return "# Shared Budget · Maintenance\n\nOpen a program with `:programID` or a milestone with `:programID/itemID`."
147	}
148	if itemID == 0 {
149		r := p.roles
150		return "# Maintenance · Program " + number(id) + "\n\n" +
151			"- Coordinator: " + string(r.Coordinator) + "\n" +
152			"- Quorum: two of three eligible reviewers\n" +
153			"- Reviewers: " + string(r.Reviewers[0]) + ", " + string(r.Reviewers[1]) + ", " + string(r.Reviewers[2]) + "\n"
154	}
155	if p.milestones[itemID] == nil {
156		return "# Maintenance\n\nMilestone not found."
157	}
158	m := GetMilestone(id, itemID)
159	return "# Maintenance · Milestone " + number(itemID) + "\n\n" +
160		"- Status: " + m.Reservation.Status + "\n" +
161		"- Evidence: `" + m.Evidence + "`\n" +
162		"- Votes in reviewer order: [" + m.Votes[0] + ", " + m.Votes[1] + ", " + m.Votes[2] + "]\n"
163}
164
165func mustProgram(id int64) *program {
166	p := programs[id]
167	if p == nil {
168		panic("maintenance is not configured for this program")
169	}
170	return p
171}
172
173func (p *program) mustMilestone(itemID int64) *milestone {
174	item := p.milestones[itemID]
175	if item == nil {
176		panic("milestone not found")
177	}
178	return item
179}
180
181func (p *program) reviewerIndex(caller address) int {
182	for i, reviewer := range p.roles.Reviewers {
183		if reviewer == caller {
184			return i
185		}
186	}
187	panic("only a registered maintenance reviewer can vote")
188}
189
190func userCaller(cur realm) address {
191	if !cur.IsCurrent() || cur.PkgPath() != packagePath || !cur.Previous().IsUserCall() {
192		panic("direct maintenance call required")
193	}
194	if len(unsafe.OriginSend()) != 0 {
195		panic("unexpected attached coins")
196	}
197	return cur.Previous().Address()
198}
199
200func number(n int64) string {
201	return strconv.FormatInt(n, 10)
202}