optimistic.gno
10.90 Kb · 309 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/v3"
11 "gno.land/p/moul/txlink/v0"
12 "gno.land/p/nt/avl/v0"
13 "gno.land/r/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/treasury/v4"
14)
15
16type Config struct {
17 Lane string
18 Proposer address
19 Guardians [2]address
20 ChallengeBlocks int64
21}
22
23type proposal struct {
24 digest string
25 challengeEnd int64
26 vetoedBy address
27 vetoReason string
28 settled bool
29}
30
31type ProposalView struct {
32 Pruned bool
33 Evidence string
34 ChallengeEnd int64
35 VetoedBy address
36 VetoReason string
37 Settled bool
38 Reservation treasury.ReservationView
39}
40
41type laneConfig struct {
42 config Config
43 proposals *avl.Tree
44}
45
46var (
47 realmPath = unsafe.CurrentRealm().PkgPath()
48 lanes = avl.NewTree()
49)
50
51func Configure(cur realm, programID int64, lane string, proposer, guardian1, guardian2 address, challengeBlocks int64) {
52 caller := callerAddress(cur)
53 view := treasury.GetProgram(programID)
54 if view.Sponsor != caller {
55 panic("only the program sponsor can configure optimistic policy")
56 }
57 if challengeBlocks < 2 || challengeBlocks > view.MaxHoldBlocks-2 {
58 panic("challenge window requires at least 2 blocks and a 2-block settlement buffer")
59 }
60 config := Config{Lane: lane, Proposer: proposer, Guardians: [2]address{guardian1, guardian2}, ChallengeBlocks: challengeBlocks}
61 requireRoles(config)
62 treasury.RegisterPolicy(cross(cur), programID, lane)
63 lanes.Set(budget.PolicyLaneKey(programID, lane), &laneConfig{config: config, proposals: avl.NewTree()})
64 chain.Emit("OptimisticConfigured", "program", number(programID), "lane", lane,
65 "proposer", string(proposer), "guardian1", string(guardian1), "guardian2", string(guardian2),
66 "challenge_blocks", number(challengeBlocks))
67}
68
69func Propose(cur realm, programID int64, lane string, recipient address, amount, holdBlocks int64, proofDigest, memo string) (int64, int64) {
70 caller := callerAddress(cur)
71 p := mustLane(programID, lane)
72 if caller != p.config.Proposer {
73 panic("only the optimistic proposer can submit")
74 }
75 if recipient == caller {
76 panic("proposer cannot be the recipient")
77 }
78 if holdBlocks < p.config.ChallengeBlocks+2 {
79 panic("hold must leave at least 2 blocks after the challenge window")
80 }
81 budget.RequireDigest(proofDigest)
82 height := runtime.ChainHeight()
83 challengeEnd := budget.Add(height, p.config.ChallengeBlocks)
84 deadline := budget.Add(height, holdBlocks)
85 itemID, available := treasury.Reserve(cross(cur), programID, lane, recipient, amount, deadline, memo)
86 evidence := budget.EvidenceCommitment(programID, lane, itemID, proofDigest)
87 p.proposals.Set(number(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 int64, lane string, itemID int64, reason string) {
95 caller := callerAddress(cur)
96 p := mustLane(programID, lane)
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 treasury.Reject(cross(cur), programID, lane, itemID)
106 item.vetoedBy, item.vetoReason = caller, reason
107 chain.Emit("OptimisticVetoed", "program", number(programID), "lane", p.config.Lane,
108 "item", number(itemID), "guardian", string(caller), "reason", reason)
109}
110
111func Settle(cur realm, programID int64, lane string, itemID int64) {
112 caller := callerAddress(cur)
113 p := mustLane(programID, lane)
114 item := p.mustProposal(itemID)
115 if item.vetoedBy != "" {
116 panic("proposal was vetoed")
117 }
118 if item.settled {
119 panic("proposal already settled")
120 }
121 if runtime.ChainHeight() < item.challengeEnd {
122 panic("challenge window is still open")
123 }
124 treasury.Approve(cross(cur), programID, lane, itemID)
125 item.settled = true
126 chain.Emit("OptimisticSettled", "program", number(programID), "lane", p.config.Lane,
127 "item", number(itemID), "actor", string(caller))
128}
129
130func CancelProposal(cur realm, programID int64, lane string, itemID int64) {
131 caller := callerAddress(cur)
132 p := mustLane(programID, lane)
133 if caller != p.config.Proposer {
134 panic("only the optimistic proposer can cancel")
135 }
136 p.mustProposal(itemID)
137 treasury.Cancel(cross(cur), programID, lane, itemID)
138}
139
140func RotateGuardian(cur realm, programID int64, lane string, index int, newGuardian address) {
141 caller := callerAddress(cur)
142 if treasury.GetProgram(programID).Sponsor != caller {
143 panic("only the program sponsor can rotate guardians")
144 }
145 p := mustLane(programID, lane)
146 if index < 0 || index >= len(p.config.Guardians) {
147 panic("guardian index must be 0 or 1")
148 }
149 config := p.config
150 old := config.Guardians[index]
151 config.Guardians[index] = newGuardian
152 requireRoles(config)
153 p.config = config
154 chain.Emit("GuardianRotated", "program", number(programID), "lane", config.Lane,
155 "index", strconv.Itoa(index), "old_guardian", string(old), "guardian", string(newGuardian))
156}
157
158func RotateProposer(cur realm, programID int64, lane string, newProposer address) {
159 if callerAddress(cur) != treasury.GetProgram(programID).Sponsor {
160 panic("only the program sponsor can rotate the proposer")
161 }
162 p := mustLane(programID, lane)
163 config := p.config
164 config.Proposer = newProposer
165 requireRoles(config)
166 p.config = config
167 chain.Emit("ProposerRotated", "program", number(programID), "lane", lane, "proposer", string(newProposer))
168}
169
170func GetConfig(programID int64, lane string) Config {
171 return mustLane(programID, lane).config
172}
173
174func PruneClosed(cur realm, programID int64, lane string) int64 {
175 caller := callerAddress(cur)
176 program := treasury.GetProgram(programID)
177 if caller != program.Sponsor {
178 panic("only the program sponsor can prune optimistic")
179 }
180 p := mustLane(programID, lane)
181 if program.Status != treasury.Closed || program.Balance.Claimable != 0 ||
182 program.ActiveReservations != 0 || p.proposals == nil {
183 panic("pruning requires an unpruned closed lane with no unpaid claims or pending reservations")
184 }
185 count := int64(p.proposals.Size())
186 p.proposals = nil
187 chain.Emit("OptimisticPruned", "program", number(programID), "lane", lane, "items", number(count))
188 return count
189}
190
191func GetProposal(programID int64, lane string, itemID int64) ProposalView {
192 p := mustLane(programID, lane)
193 if p.proposals == nil || treasury.GetProgram(programID).Pruned {
194 return ProposalView{Pruned: true}
195 }
196 item := p.mustProposal(itemID)
197 return ProposalView{Evidence: item.digest, ChallengeEnd: item.challengeEnd,
198 VetoedBy: item.vetoedBy, VetoReason: item.vetoReason, Settled: item.settled,
199 Reservation: treasury.GetReservation(programID, lane, itemID)}
200}
201
202func Render(path string) string {
203 program, names, itemID := treasury.PolicyRenderRoute(path, realmPath)
204 if len(names) == 0 {
205 return "# Optimistic policy\n\nOpen a configured lane with `:programID/lane` or a proposal with `:programID/lane/itemID`."
206 }
207 if len(names) > 1 {
208 return "# Optimistic policy lanes\n\n" + budget.PolicyLaneLinks(realmPath, program.ID, names)
209 }
210 id, lane := program.ID, names[0]
211 p := mustLane(id, lane)
212 tx := txlink.Realm(realmPath)
213 args := []string{"programID", number(id), "lane", lane}
214 if itemID == 0 {
215 config := p.config
216 page := "# Optimistic policy · Program " + number(id) + "\n\n**Lane** " + budget.MarkdownText(config.Lane) +
217 " · **Challenge window** " + number(config.ChallengeBlocks) + " blocks\n\n" +
218 "| Role | Address |\n| --- | --- |\n| Proposer | " + string(config.Proposer) +
219 " |\n| Guardian 1 | " + string(config.Guardians[0]) + " |\n| Guardian 2 | " + string(config.Guardians[1]) +
220 " |\n\n[Rotate guardian](" + tx.Call("RotateGuardian", args...) + ") · [Rotate proposer](" +
221 tx.Call("RotateProposer", args...) + ")"
222 if program.Status == treasury.Active {
223 page += " · [Propose spending](" + tx.Call("Propose", args...) + ")"
224 }
225 if program.Status == treasury.Closed && program.Balance.Claimable == 0 && p.proposals != nil {
226 page += " · [Prune proposals](" + tx.Call("PruneClosed", args...) + ")"
227 }
228 return page + "\n"
229 }
230 if program.Pruned || p.proposals == nil {
231 return "# Optimistic policy\n\nProposal records were pruned."
232 }
233 if p.proposal(itemID) == nil {
234 return "# Optimistic policy\n\nProposal not found."
235 }
236 view := GetProposal(id, lane, itemID)
237 reservation := view.Reservation
238 page := "# Optimistic proposal " + number(itemID) + "\n\n**Status** " + reservation.Status +
239 " · **Amount** " + budget.MarkdownText(program.FormatAmount(reservation.Amount)) +
240 "\n\n**Challenge ends at block** " + number(view.ChallengeEnd) +
241 " · **Reservation deadline** " + number(reservation.Deadline) +
242 "\n\n**Memo** " + budget.MarkdownText(reservation.Memo) + "\n\n**Evidence** `" + view.Evidence + "`\n"
243 if view.VetoedBy != "" {
244 return page + "\n**Vetoed by** " + string(view.VetoedBy) + "\n\n**Reason** " + budget.MarkdownText(view.VetoReason) + "\n"
245 }
246 if reservation.ExpiredAtHeight {
247 return page + "\nThis hold has reached its deadline and can be expired in the treasury.\n"
248 }
249 if reservation.Status != treasury.Reserved {
250 return page
251 }
252 args = append(args, "itemID", number(itemID))
253 page += "\n[Cancel proposal](" + tx.Call("CancelProposal", args...) + ")\n"
254 if runtime.ChainHeight() < view.ChallengeEnd {
255 return page + "\nThe challenge window is open. [Veto proposal](" + tx.Call("Veto", args...) + ")\n"
256 }
257 return page + "\nAnyone can [settle this proposal](" + tx.Call("Settle", args...) + ") before its deadline.\n"
258}
259
260func requireRoles(config Config) {
261 budget.RequireAddress(config.Proposer)
262 for _, guardian := range config.Guardians {
263 budget.RequireAddress(guardian)
264 if guardian == config.Proposer {
265 panic("proposer cannot be a guardian")
266 }
267 }
268 if config.Guardians[0] == config.Guardians[1] {
269 panic("guardians must be distinct")
270 }
271}
272
273func mustLane(programID int64, lane string) *laneConfig {
274 p, _ := lanes.Get(budget.PolicyLaneKey(programID, lane)).(*laneConfig)
275 if p == nil {
276 panic("optimistic lane is not configured")
277 }
278 return p
279}
280
281func (p *laneConfig) proposal(itemID int64) *proposal {
282 if p.proposals == nil {
283 return nil
284 }
285 item, _ := p.proposals.Get(number(itemID)).(*proposal)
286 return item
287}
288
289func (p *laneConfig) mustProposal(itemID int64) *proposal {
290 item := p.proposal(itemID)
291 if item == nil {
292 panic("proposal not found")
293 }
294 return item
295}
296
297func callerAddress(cur realm) address {
298 if !cur.IsCurrent() || cur.PkgPath() != realmPath {
299 panic("invalid optimistic context")
300 }
301 if len(unsafe.OriginSend()) != 0 {
302 panic("unexpected attached coins")
303 }
304 return cur.Previous().Address()
305}
306
307func number(n int64) string {
308 return strconv.FormatInt(n, 10)
309}