// Package optimistic releases a proposal after its challenge window unless a guardian vetoes it. package optimistic import ( "chain" "chain/runtime" "chain/runtime/unsafe" "strconv" "gno.land/p/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/budget/v2" "gno.land/p/moul/txlink/v0" "gno.land/r/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/treasury/v3" ) const packagePath = "gno.land/r/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/optimistic/v3" type Config struct { Lane string Proposer address Guardians [2]address ChallengeBlocks int64 } type proposal struct { digest string challengeEnd int64 vetoedBy address vetoReason string settled bool } type ProposalView struct { Evidence string ChallengeEnd int64 VetoedBy address VetoReason string Settled bool Reservation treasury.ReservationView } type program struct { config Config proposals map[int64]*proposal } var programs = make(map[int64]*program) func Configure(cur realm, programID int64, lane string, proposer, guardian1, guardian2 address, challengeBlocks int64) { caller := userCaller(cur) view := treasury.GetProgram(programID) if view.Sponsor != caller { panic("only the program sponsor can configure optimistic policy") } if view.Status != treasury.Draft || programs[programID] != nil { panic("optimistic configuration requires an unconfigured draft") } if challengeBlocks <= 0 || challengeBlocks >= view.MaxHoldBlocks { panic("challenge blocks must be positive and shorter than the maximum hold") } config := Config{Lane: lane, Proposer: proposer, Guardians: [2]address{guardian1, guardian2}, ChallengeBlocks: challengeBlocks} requireRoles(config) runtime.AssertOriginCall() treasury.RegisterPolicy(cross(cur), programID, lane) programs[programID] = &program{config: config, proposals: make(map[int64]*proposal)} chain.Emit("OptimisticConfigured", "program", number(programID), "lane", lane, "proposer", string(proposer), "guardian1", string(guardian1), "guardian2", string(guardian2), "challenge_blocks", number(challengeBlocks)) } func Propose(cur realm, programID int64, recipient address, amount, holdBlocks int64, proofDigest, memo string) (int64, int64) { caller := userCaller(cur) p := mustProgram(programID) if caller != p.config.Proposer { panic("only the optimistic proposer can submit") } if holdBlocks <= p.config.ChallengeBlocks { panic("hold must be longer than the challenge window") } budget.RequireDigest(proofDigest) height := runtime.ChainHeight() challengeEnd := budget.Add(height, p.config.ChallengeBlocks) deadline := budget.Add(height, holdBlocks) runtime.AssertOriginCall() itemID, available := treasury.Reserve(cross(cur), programID, recipient, amount, deadline, memo) lane := p.config.Lane evidence := budget.EvidenceCommitment(programID, lane, itemID, proofDigest) p.proposals[itemID] = &proposal{digest: evidence, challengeEnd: challengeEnd} chain.Emit("OptimisticProposed", "program", number(programID), "lane", lane, "item", number(itemID), "actor", string(caller), "challenge_end", number(challengeEnd), "evidence", evidence) return itemID, available } // Veto can be used by either guardian, including a guardian named as recipient. func Veto(cur realm, programID, itemID int64, reason string) { caller := userCaller(cur) p := mustProgram(programID) if caller != p.config.Guardians[0] && caller != p.config.Guardians[1] { panic("only an optimistic guardian can veto") } item := p.mustProposal(itemID) if runtime.ChainHeight() >= item.challengeEnd { panic("challenge window has ended") } budget.RequireMemo(reason) runtime.AssertOriginCall() treasury.Reject(cross(cur), programID, itemID) item.vetoedBy, item.vetoReason = caller, reason chain.Emit("OptimisticVetoed", "program", number(programID), "lane", p.config.Lane, "item", number(itemID), "guardian", string(caller), "reason", reason) } func Settle(cur realm, programID, itemID int64) { requireContext(cur) p := mustProgram(programID) item := p.mustProposal(itemID) if item.vetoedBy != "" { panic("proposal was vetoed") } if item.settled { panic("proposal already settled") } if runtime.ChainHeight() < item.challengeEnd { panic("challenge window is still open") } treasury.Approve(cross(cur), programID, itemID) item.settled = true chain.Emit("OptimisticSettled", "program", number(programID), "lane", p.config.Lane, "item", number(itemID), "actor", string(cur.Previous().Address())) } func CancelProposal(cur realm, programID, itemID int64) { caller := userCaller(cur) p := mustProgram(programID) if caller != p.config.Proposer { panic("only the optimistic proposer can cancel") } p.mustProposal(itemID) runtime.AssertOriginCall() treasury.Cancel(cross(cur), programID, itemID) } func RotateGuardian(cur realm, programID int64, index int, newGuardian address) { caller := userCaller(cur) if treasury.GetProgram(programID).Sponsor != caller { panic("only the program sponsor can rotate guardians") } p := mustProgram(programID) if index < 0 || index >= len(p.config.Guardians) { panic("guardian index must be 0 or 1") } config := p.config old := config.Guardians[index] config.Guardians[index] = newGuardian requireRoles(config) runtime.AssertOriginCall() p.config = config chain.Emit("GuardianRotated", "program", number(programID), "lane", config.Lane, "index", strconv.Itoa(index), "old_guardian", string(old), "guardian", string(newGuardian)) } func GetConfig(programID int64) Config { return mustProgram(programID).config } func GetProposal(programID, itemID int64) ProposalView { p := mustProgram(programID) item := p.mustProposal(itemID) return ProposalView{Evidence: item.digest, ChallengeEnd: item.challengeEnd, VetoedBy: item.vetoedBy, VetoReason: item.vetoReason, Settled: item.settled, Reservation: treasury.GetReservation(programID, p.config.Lane, itemID)} } func Render(path string) string { id, itemID := budget.ParsePolicyViewPath(path) p := programs[id] if p == nil { return "# Optimistic policy\n\nOpen a configured program with `:programID` or a proposal with `:programID/itemID`." } program := treasury.GetProgram(id) tx := txlink.Realm(packagePath) args := []string{"programID", number(id)} if itemID == 0 { config := p.config page := "# Optimistic policy · Program " + number(id) + "\n\n**Lane** " + budget.MarkdownText(config.Lane) + " · **Challenge window** " + number(config.ChallengeBlocks) + " blocks\n\n" + "| Role | Address |\n| --- | --- |\n| Proposer | " + string(config.Proposer) + " |\n| Guardian 1 | " + string(config.Guardians[0]) + " |\n| Guardian 2 | " + string(config.Guardians[1]) + " |\n\n[Rotate guardian](" + tx.Call("RotateGuardian", args...) + ")" if program.Status == treasury.Active { page += " · [Propose spending](" + tx.Call("Propose", args...) + ")" } return page + "\n" } if program.Pruned { return "# Optimistic policy\n\nThe treasury pruned this program's reservation records." } if p.proposals[itemID] == nil { return "# Optimistic policy\n\nProposal not found." } view := GetProposal(id, itemID) reservation := view.Reservation page := "# Optimistic proposal " + number(itemID) + "\n\n**Status** " + reservation.Status + " · **Amount** " + budget.FormatUGNOT(reservation.Amount) + "\n\n**Challenge ends at block** " + number(view.ChallengeEnd) + " · **Reservation deadline** " + number(reservation.Deadline) + "\n\n**Memo** " + budget.MarkdownText(reservation.Memo) + "\n\n**Evidence** `" + view.Evidence + "`\n" if view.VetoedBy != "" { return page + "\n**Vetoed by** " + string(view.VetoedBy) + "\n\n**Reason** " + budget.MarkdownText(view.VetoReason) + "\n" } if reservation.ExpiredAtHeight { return page + "\nThis hold has reached its deadline and can be expired in the treasury.\n" } if reservation.Status != treasury.Reserved { return page } args = append(args, "itemID", number(itemID)) page += "\n[Cancel proposal](" + tx.Call("CancelProposal", args...) + ")\n" if runtime.ChainHeight() < view.ChallengeEnd { return page + "\nThe challenge window is open. [Veto proposal](" + tx.Call("Veto", args...) + ")\n" } return page + "\nAnyone can [settle this proposal](" + tx.Call("Settle", args...) + ") before its deadline.\n" } func requireRoles(config Config) { budget.RequireAddress(config.Proposer) for _, guardian := range config.Guardians { budget.RequireAddress(guardian) if guardian == config.Proposer { panic("proposer cannot be a guardian") } } if config.Guardians[0] == config.Guardians[1] { panic("guardians must be distinct") } } func mustProgram(id int64) *program { p := programs[id] if p == nil { panic("optimistic policy is not configured for this program") } return p } func (p *program) mustProposal(itemID int64) *proposal { item := p.proposals[itemID] if item == nil { panic("proposal not found") } return item } func requireContext(cur realm) { if !cur.IsCurrent() || cur.PkgPath() != packagePath { panic("invalid optimistic policy context") } if len(unsafe.OriginSend()) != 0 { panic("unexpected attached coins") } } func userCaller(cur realm) address { requireContext(cur) if !cur.Previous().IsUserCall() { panic("direct optimistic policy call required") } return cur.Previous().Address() } func number(n int64) string { return strconv.FormatInt(n, 10) }