optimistic.gno
9.25 Kb · 272 lines
1// Package optimistic releases a proposal after its challenge window unless a guardian vetoes it.
2package optimistic
3
4import (
5 "chain"
6 "chain/runtime"
7 "chain/runtime/unsafe"
8 "strconv"
9
10 "gno.land/p/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/budget/v2"
11 "gno.land/p/moul/txlink/v0"
12 "gno.land/r/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/treasury/v3"
13)
14
15const packagePath = "gno.land/r/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/optimistic/v3"
16
17type Config struct {
18 Lane string
19 Proposer address
20 Guardians [2]address
21 ChallengeBlocks int64
22}
23
24type proposal struct {
25 digest string
26 challengeEnd int64
27 vetoedBy address
28 vetoReason string
29 settled bool
30}
31
32type ProposalView struct {
33 Evidence string
34 ChallengeEnd int64
35 VetoedBy address
36 VetoReason string
37 Settled bool
38 Reservation treasury.ReservationView
39}
40
41type program struct {
42 config Config
43 proposals map[int64]*proposal
44}
45
46var programs = make(map[int64]*program)
47
48func Configure(cur realm, programID int64, lane string, proposer, guardian1, guardian2 address, challengeBlocks int64) {
49 caller := userCaller(cur)
50 view := treasury.GetProgram(programID)
51 if view.Sponsor != caller {
52 panic("only the program sponsor can configure optimistic policy")
53 }
54 if view.Status != treasury.Draft || programs[programID] != nil {
55 panic("optimistic configuration requires an unconfigured draft")
56 }
57 if challengeBlocks <= 0 || challengeBlocks >= view.MaxHoldBlocks {
58 panic("challenge blocks must be positive and shorter than the maximum hold")
59 }
60 config := Config{Lane: lane, Proposer: proposer, Guardians: [2]address{guardian1, guardian2}, ChallengeBlocks: challengeBlocks}
61 requireRoles(config)
62 runtime.AssertOriginCall()
63 treasury.RegisterPolicy(cross(cur), programID, lane)
64 programs[programID] = &program{config: config, proposals: make(map[int64]*proposal)}
65 chain.Emit("OptimisticConfigured", "program", number(programID), "lane", lane,
66 "proposer", string(proposer), "guardian1", string(guardian1), "guardian2", string(guardian2),
67 "challenge_blocks", number(challengeBlocks))
68}
69
70func Propose(cur realm, programID int64, recipient address, amount, holdBlocks int64, proofDigest, memo string) (int64, int64) {
71 caller := userCaller(cur)
72 p := mustProgram(programID)
73 if caller != p.config.Proposer {
74 panic("only the optimistic proposer can submit")
75 }
76 if holdBlocks <= p.config.ChallengeBlocks {
77 panic("hold must be longer than the challenge window")
78 }
79 budget.RequireDigest(proofDigest)
80 height := runtime.ChainHeight()
81 challengeEnd := budget.Add(height, p.config.ChallengeBlocks)
82 deadline := budget.Add(height, holdBlocks)
83 runtime.AssertOriginCall()
84 itemID, available := treasury.Reserve(cross(cur), programID, recipient, amount, deadline, memo)
85 lane := p.config.Lane
86 evidence := budget.EvidenceCommitment(programID, lane, itemID, proofDigest)
87 p.proposals[itemID] = &proposal{digest: evidence, challengeEnd: challengeEnd}
88 chain.Emit("OptimisticProposed", "program", number(programID), "lane", lane, "item", number(itemID),
89 "actor", string(caller), "challenge_end", number(challengeEnd), "evidence", evidence)
90 return itemID, available
91}
92
93// Veto can be used by either guardian, including a guardian named as recipient.
94func Veto(cur realm, programID, itemID int64, reason string) {
95 caller := userCaller(cur)
96 p := mustProgram(programID)
97 if caller != p.config.Guardians[0] && caller != p.config.Guardians[1] {
98 panic("only an optimistic guardian can veto")
99 }
100 item := p.mustProposal(itemID)
101 if runtime.ChainHeight() >= item.challengeEnd {
102 panic("challenge window has ended")
103 }
104 budget.RequireMemo(reason)
105 runtime.AssertOriginCall()
106 treasury.Reject(cross(cur), programID, itemID)
107 item.vetoedBy, item.vetoReason = caller, reason
108 chain.Emit("OptimisticVetoed", "program", number(programID), "lane", p.config.Lane,
109 "item", number(itemID), "guardian", string(caller), "reason", reason)
110}
111
112func Settle(cur realm, programID, itemID int64) {
113 requireContext(cur)
114 p := mustProgram(programID)
115 item := p.mustProposal(itemID)
116 if item.vetoedBy != "" {
117 panic("proposal was vetoed")
118 }
119 if item.settled {
120 panic("proposal already settled")
121 }
122 if runtime.ChainHeight() < item.challengeEnd {
123 panic("challenge window is still open")
124 }
125 treasury.Approve(cross(cur), programID, itemID)
126 item.settled = true
127 chain.Emit("OptimisticSettled", "program", number(programID), "lane", p.config.Lane,
128 "item", number(itemID), "actor", string(cur.Previous().Address()))
129}
130
131func CancelProposal(cur realm, programID, itemID int64) {
132 caller := userCaller(cur)
133 p := mustProgram(programID)
134 if caller != p.config.Proposer {
135 panic("only the optimistic proposer can cancel")
136 }
137 p.mustProposal(itemID)
138 runtime.AssertOriginCall()
139 treasury.Cancel(cross(cur), programID, itemID)
140}
141
142func RotateGuardian(cur realm, programID int64, index int, newGuardian address) {
143 caller := userCaller(cur)
144 if treasury.GetProgram(programID).Sponsor != caller {
145 panic("only the program sponsor can rotate guardians")
146 }
147 p := mustProgram(programID)
148 if index < 0 || index >= len(p.config.Guardians) {
149 panic("guardian index must be 0 or 1")
150 }
151 config := p.config
152 old := config.Guardians[index]
153 config.Guardians[index] = newGuardian
154 requireRoles(config)
155 runtime.AssertOriginCall()
156 p.config = config
157 chain.Emit("GuardianRotated", "program", number(programID), "lane", config.Lane,
158 "index", strconv.Itoa(index), "old_guardian", string(old), "guardian", string(newGuardian))
159}
160
161func GetConfig(programID int64) Config {
162 return mustProgram(programID).config
163}
164
165func GetProposal(programID, itemID int64) ProposalView {
166 p := mustProgram(programID)
167 item := p.mustProposal(itemID)
168 return ProposalView{Evidence: item.digest, ChallengeEnd: item.challengeEnd,
169 VetoedBy: item.vetoedBy, VetoReason: item.vetoReason, Settled: item.settled,
170 Reservation: treasury.GetReservation(programID, p.config.Lane, itemID)}
171}
172
173func Render(path string) string {
174 id, itemID := budget.ParsePolicyViewPath(path)
175 p := programs[id]
176 if p == nil {
177 return "# Optimistic policy\n\nOpen a configured program with `:programID` or a proposal with `:programID/itemID`."
178 }
179 program := treasury.GetProgram(id)
180 tx := txlink.Realm(packagePath)
181 args := []string{"programID", number(id)}
182 if itemID == 0 {
183 config := p.config
184 page := "# Optimistic policy · Program " + number(id) + "\n\n**Lane** " + budget.MarkdownText(config.Lane) +
185 " · **Challenge window** " + number(config.ChallengeBlocks) + " blocks\n\n" +
186 "| Role | Address |\n| --- | --- |\n| Proposer | " + string(config.Proposer) +
187 " |\n| Guardian 1 | " + string(config.Guardians[0]) + " |\n| Guardian 2 | " + string(config.Guardians[1]) +
188 " |\n\n[Rotate guardian](" + tx.Call("RotateGuardian", args...) + ")"
189 if program.Status == treasury.Active {
190 page += " · [Propose spending](" + tx.Call("Propose", args...) + ")"
191 }
192 return page + "\n"
193 }
194 if program.Pruned {
195 return "# Optimistic policy\n\nThe treasury pruned this program's reservation records."
196 }
197 if p.proposals[itemID] == nil {
198 return "# Optimistic policy\n\nProposal not found."
199 }
200 view := GetProposal(id, itemID)
201 reservation := view.Reservation
202 page := "# Optimistic proposal " + number(itemID) + "\n\n**Status** " + reservation.Status +
203 " · **Amount** " + budget.FormatUGNOT(reservation.Amount) +
204 "\n\n**Challenge ends at block** " + number(view.ChallengeEnd) +
205 " · **Reservation deadline** " + number(reservation.Deadline) +
206 "\n\n**Memo** " + budget.MarkdownText(reservation.Memo) + "\n\n**Evidence** `" + view.Evidence + "`\n"
207 if view.VetoedBy != "" {
208 return page + "\n**Vetoed by** " + string(view.VetoedBy) + "\n\n**Reason** " + budget.MarkdownText(view.VetoReason) + "\n"
209 }
210 if reservation.ExpiredAtHeight {
211 return page + "\nThis hold has reached its deadline and can be expired in the treasury.\n"
212 }
213 if reservation.Status != treasury.Reserved {
214 return page
215 }
216 args = append(args, "itemID", number(itemID))
217 page += "\n[Cancel proposal](" + tx.Call("CancelProposal", args...) + ")\n"
218 if runtime.ChainHeight() < view.ChallengeEnd {
219 return page + "\nThe challenge window is open. [Veto proposal](" + tx.Call("Veto", args...) + ")\n"
220 }
221 return page + "\nAnyone can [settle this proposal](" + tx.Call("Settle", args...) + ") before its deadline.\n"
222}
223
224func requireRoles(config Config) {
225 budget.RequireAddress(config.Proposer)
226 for _, guardian := range config.Guardians {
227 budget.RequireAddress(guardian)
228 if guardian == config.Proposer {
229 panic("proposer cannot be a guardian")
230 }
231 }
232 if config.Guardians[0] == config.Guardians[1] {
233 panic("guardians must be distinct")
234 }
235}
236
237func mustProgram(id int64) *program {
238 p := programs[id]
239 if p == nil {
240 panic("optimistic policy is not configured for this program")
241 }
242 return p
243}
244
245func (p *program) mustProposal(itemID int64) *proposal {
246 item := p.proposals[itemID]
247 if item == nil {
248 panic("proposal not found")
249 }
250 return item
251}
252
253func requireContext(cur realm) {
254 if !cur.IsCurrent() || cur.PkgPath() != packagePath {
255 panic("invalid optimistic policy context")
256 }
257 if len(unsafe.OriginSend()) != 0 {
258 panic("unexpected attached coins")
259 }
260}
261
262func userCaller(cur realm) address {
263 requireContext(cur)
264 if !cur.Previous().IsUserCall() {
265 panic("direct optimistic policy call required")
266 }
267 return cur.Previous().Address()
268}
269
270func number(n int64) string {
271 return strconv.FormatInt(n, 10)
272}