maintenance.gno
9.81 Kb · 306 lines
1// Package maintenance requires two independent reviewers to resolve a milestone.
2package maintenance
3
4import (
5 "chain"
6 "chain/runtime"
7 "chain/runtime/unsafe"
8 "strconv"
9 "strings"
10
11 "gno.land/p/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/budget/v2"
12 "gno.land/p/moul/txlink/v0"
13 "gno.land/r/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/treasury/v3"
14)
15
16const packagePath = "gno.land/r/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/maintenance/v3"
17
18type Roles struct {
19 Lane string
20 Coordinator address
21 Reviewers [3]address
22}
23
24type milestone struct {
25 digest string
26 votes [3]string
27 voters [3]address
28}
29
30type program struct {
31 roles Roles
32 milestones map[int64]*milestone
33}
34
35type MilestoneView struct {
36 Evidence string
37 Votes [3]string
38 Voters [3]address
39 Reservation treasury.ReservationView
40}
41
42var programs = make(map[int64]*program)
43
44func Configure(cur realm, programID int64, lane string, coordinator, r1, r2, r3 address) {
45 caller := userCaller(cur)
46 config := treasury.GetProgram(programID)
47 if config.Sponsor != caller {
48 panic("only the program sponsor can configure maintenance")
49 }
50 if config.Status != treasury.Draft || programs[programID] != nil {
51 panic("maintenance configuration requires an unconfigured draft")
52 }
53 roles := Roles{Lane: lane, Coordinator: coordinator, Reviewers: [3]address{r1, r2, r3}}
54 requireRoles(roles)
55 runtime.AssertOriginCall()
56 treasury.RegisterPolicy(cross(cur), programID, lane)
57 programs[programID] = &program{roles: roles, milestones: make(map[int64]*milestone)}
58 chain.Emit("MaintenanceConfigured", "program", number(programID), "lane", lane, "coordinator", string(coordinator))
59}
60
61func SubmitMilestone(cur realm, programID int64, recipient address, amount, holdBlocks int64, proofDigest, memo string) (int64, int64) {
62 caller := userCaller(cur)
63 p := mustProgram(programID)
64 if caller != p.roles.Coordinator {
65 panic("only the maintenance coordinator can submit")
66 }
67 budget.RequireDigest(proofDigest)
68 budget.RequirePositive(holdBlocks)
69 deadline := budget.Add(runtime.ChainHeight(), holdBlocks)
70 runtime.AssertOriginCall()
71 itemID, available := treasury.Reserve(cross(cur), programID, recipient, amount, deadline, memo)
72 lane := p.roles.Lane
73 evidence := budget.EvidenceCommitment(programID, lane, itemID, proofDigest)
74 p.milestones[itemID] = &milestone{digest: evidence}
75 chain.Emit("MilestoneSubmitted", "program", number(programID), "lane", lane, "item", number(itemID),
76 "actor", string(caller), "evidence", evidence)
77 return itemID, available
78}
79
80func Vote(cur realm, programID, itemID int64, evidence string, approve bool) string {
81 caller := userCaller(cur)
82 p := mustProgram(programID)
83 index := p.reviewerIndex(caller)
84 item := p.mustMilestone(itemID)
85 lane := p.roles.Lane
86 reservation := treasury.GetReservation(programID, lane, itemID)
87 if caller == reservation.Recipient {
88 panic("recipient cannot review their own milestone")
89 }
90 if reservation.Status != treasury.Reserved || reservation.ExpiredAtHeight {
91 panic("milestone is no longer open for review")
92 }
93 if item.digest != evidence {
94 panic("milestone evidence does not match")
95 }
96 if item.voted(caller) {
97 panic("reviewer already voted")
98 }
99 if item.votes[index] != "" {
100 panic("reviewer seat already voted on this milestone")
101 }
102 runtime.AssertOriginCall()
103 vote := treasury.Rejected
104 if approve {
105 vote = treasury.Approved
106 }
107 item.votes[index], item.voters[index] = vote, caller
108 chain.Emit("MilestoneVote", "program", number(programID), "lane", lane, "item", number(itemID),
109 "reviewer", string(caller), "decision", vote, "evidence", evidence)
110 approvals, rejections, remaining := item.tally(p.roles.Reviewers, reservation.Recipient)
111 switch {
112 case approvals >= 2:
113 treasury.Approve(cross(cur), programID, itemID)
114 return treasury.Approved
115 case rejections >= 2:
116 treasury.Reject(cross(cur), programID, itemID)
117 return treasury.Rejected
118 case approvals+remaining < 2 && rejections+remaining < 2:
119 treasury.Reject(cross(cur), programID, itemID)
120 chain.Emit("MilestoneDeadlocked", "program", number(programID), "lane", lane, "item", number(itemID))
121 return treasury.Rejected
122 }
123 return treasury.Reserved
124}
125
126func CancelMilestone(cur realm, programID, itemID int64) {
127 caller := userCaller(cur)
128 p := mustProgram(programID)
129 if caller != p.roles.Coordinator {
130 panic("only the maintenance coordinator can cancel")
131 }
132 if p.mustMilestone(itemID).votes != [3]string{} {
133 panic("milestone can only be canceled before a vote")
134 }
135 runtime.AssertOriginCall()
136 treasury.Cancel(cross(cur), programID, itemID)
137}
138
139func RotateReviewer(cur realm, programID int64, index int, newReviewer address) {
140 caller := userCaller(cur)
141 if treasury.GetProgram(programID).Sponsor != caller {
142 panic("only the program sponsor can rotate reviewers")
143 }
144 p := mustProgram(programID)
145 if index < 0 || index >= len(p.roles.Reviewers) {
146 panic("reviewer index must be 0 to 2")
147 }
148 roles := p.roles
149 old := roles.Reviewers[index]
150 roles.Reviewers[index] = newReviewer
151 requireRoles(roles)
152 runtime.AssertOriginCall()
153 p.roles = roles
154 chain.Emit("ReviewerRotated", "program", number(programID), "lane", roles.Lane,
155 "index", strconv.Itoa(index), "old_reviewer", string(old), "reviewer", string(newReviewer))
156}
157
158func GetRoles(programID int64) Roles {
159 return mustProgram(programID).roles
160}
161
162func GetMilestone(programID, itemID int64) MilestoneView {
163 p := mustProgram(programID)
164 item := p.mustMilestone(itemID)
165 return MilestoneView{Evidence: item.digest, Votes: item.votes, Voters: item.voters,
166 Reservation: treasury.GetReservation(programID, p.roles.Lane, itemID)}
167}
168
169func Render(path string) string {
170 id, itemID := budget.ParsePolicyViewPath(path)
171 p := programs[id]
172 if p == nil {
173 return "# Maintenance\n\nOpen a configured program with `:programID` or a milestone with `:programID/itemID`."
174 }
175 program := treasury.GetProgram(id)
176 tx := txlink.Realm(packagePath)
177 args := []string{"programID", number(id)}
178 if itemID == 0 {
179 var page strings.Builder
180 page.WriteString("# Maintenance · Program " + number(id) + "\n\n**Lane** " + budget.MarkdownText(p.roles.Lane) +
181 " · **Quorum** two eligible reviewers\n\n| Role | Address |\n| --- | --- |\n| Coordinator | " + string(p.roles.Coordinator) + " |\n")
182 for i, reviewer := range p.roles.Reviewers {
183 page.WriteString("| Reviewer " + strconv.Itoa(i+1) + " | " + string(reviewer) + " |\n")
184 }
185 page.WriteString("\n[Rotate a reviewer](" + tx.Call("RotateReviewer", args...) + ")")
186 if program.Status == treasury.Active {
187 page.WriteString(" · [Submit a milestone](" + tx.Call("SubmitMilestone", args...) + ")")
188 }
189 return page.String() + "\n"
190 }
191 if program.Pruned {
192 return "# Maintenance\n\nThe treasury pruned this program's reservation records."
193 }
194 item := p.milestones[itemID]
195 if item == nil {
196 return "# Maintenance\n\nMilestone not found."
197 }
198 m := GetMilestone(id, itemID)
199 var page strings.Builder
200 page.WriteString("# Milestone " + number(itemID) + "\n\n**Status** " + m.Reservation.Status +
201 " · **Amount** " + budget.FormatUGNOT(m.Reservation.Amount) +
202 "\n\n**Memo** " + budget.MarkdownText(m.Reservation.Memo) +
203 "\n\n**Evidence** `" + m.Evidence + "`\n\n| Reviewer seat | Current address | Vote | Signed by |\n| --- | --- | --- | --- |\n")
204 for i, reviewer := range p.roles.Reviewers {
205 vote, signer := m.Votes[i], m.Voters[i]
206 if vote == "" {
207 vote = "—"
208 if reviewer == m.Reservation.Recipient {
209 vote = "Recipient · ineligible"
210 } else if item.voted(reviewer) {
211 vote = "Already voted"
212 }
213 }
214 page.WriteString("| " + strconv.Itoa(i+1) + " | " + string(reviewer) + " | " + vote + " | " + string(signer) + " |\n")
215 }
216 if m.Reservation.Status == treasury.Reserved && !m.Reservation.ExpiredAtHeight {
217 args = append(args, "itemID", number(itemID))
218 if m.Votes == [3]string{} {
219 page.WriteString("\n[Cancel milestone](" + tx.Call("CancelMilestone", args...) + ")\n")
220 }
221 args = append(args, "evidence", m.Evidence)
222 page.WriteString("\n[Vote to approve](" + tx.Call("Vote", append(args, "approve", "true")...) +
223 ") · [Vote to reject](" + tx.Call("Vote", append(args, "approve", "false")...) + ")\n")
224 }
225 return page.String()
226}
227
228func requireRoles(roles Roles) {
229 budget.RequireAddress(roles.Coordinator)
230 for i, reviewer := range roles.Reviewers {
231 budget.RequireAddress(reviewer)
232 if reviewer == roles.Coordinator {
233 panic("coordinator cannot be a reviewer")
234 }
235 for _, previous := range roles.Reviewers[:i] {
236 if reviewer == previous {
237 panic("reviewers must be distinct")
238 }
239 }
240 }
241}
242
243func (item *milestone) voted(caller address) bool {
244 for _, voter := range item.voters {
245 if voter == caller {
246 return true
247 }
248 }
249 return false
250}
251
252// Votes belong to their signer and occupy a seat even if its reviewer changes.
253func (item *milestone) tally(reviewers [3]address, recipient address) (approvals, rejections, remaining int) {
254 for i, vote := range item.votes {
255 switch vote {
256 case treasury.Approved:
257 approvals++
258 case treasury.Rejected:
259 rejections++
260 default:
261 if reviewers[i] != recipient && !item.voted(reviewers[i]) {
262 remaining++
263 }
264 }
265 }
266 return
267}
268
269func mustProgram(id int64) *program {
270 p := programs[id]
271 if p == nil {
272 panic("maintenance is not configured for this program")
273 }
274 return p
275}
276
277func (p *program) mustMilestone(itemID int64) *milestone {
278 item := p.milestones[itemID]
279 if item == nil {
280 panic("milestone not found")
281 }
282 return item
283}
284
285func (p *program) reviewerIndex(caller address) int {
286 for i, reviewer := range p.roles.Reviewers {
287 if reviewer == caller {
288 return i
289 }
290 }
291 panic("only a registered maintenance reviewer can vote")
292}
293
294func userCaller(cur realm) address {
295 if !cur.IsCurrent() || cur.PkgPath() != packagePath || !cur.Previous().IsUserCall() {
296 panic("direct maintenance call required")
297 }
298 if len(unsafe.OriginSend()) != 0 {
299 panic("unexpected attached coins")
300 }
301 return cur.Previous().Address()
302}
303
304func number(n int64) string {
305 return strconv.FormatInt(n, 10)
306}