assert.gno
2.34 Kb · 91 lines
1package governance
2
3import (
4 "errors"
5
6 ufmt "gno.land/p/nt/ufmt/v0"
7 "gno.land/r/gnoswap/common"
8 "gno.land/r/gnoswap/halt/v1"
9
10 "gno.land/r/gnoswap/gov/governance"
11)
12
13// assertIsNotHaltedGovernanceForProposalData panics if governance is halted,
14// unless the proposal only targets the halt realm.
15// This keeps a recovery path open so governance can unhalt itself.
16func assertIsNotHaltedGovernanceForProposalData(proposalData *governance.ProposalData) {
17 if isHaltRecoveryProposalData(proposalData) {
18 return
19 }
20
21 halt.AssertIsNotHaltedGovernance()
22}
23
24// assertIsNotHaltedGovernanceForProposal panics if governance is halted,
25// unless the proposal only targets the halt realm.
26func assertIsNotHaltedGovernanceForProposal(gv *governanceV1, proposalID int64) {
27 proposal, exists := gv.getProposal(proposalID)
28 if !exists {
29 halt.AssertIsNotHaltedGovernance()
30 return
31 }
32
33 assertIsNotHaltedGovernanceForProposalData(proposal.Data())
34}
35
36// isHaltRecoveryProposalData returns true if the proposal is a parameter change
37// whose every execution targets the halt realm.
38func isHaltRecoveryProposalData(proposalData *governance.ProposalData) bool {
39 if proposalData == nil || proposalData.ProposalType() != governance.ParameterChange {
40 return false
41 }
42
43 infos, err := NewProposalDataResolver(proposalData).ParameterChangesInfos()
44 if err != nil || len(infos) == 0 {
45 return false
46 }
47
48 for _, info := range infos {
49 if info.PkgPath() != HALT_PATH {
50 return false
51 }
52 }
53
54 return true
55}
56
57// assertCallerIsProposer panics if the caller is not the proposer of the given proposal.
58func assertCallerIsProposer(gv *governanceV1, proposalID int64, caller address) {
59 proposal, exists := gv.getProposal(proposalID)
60 if !exists {
61 panic(errors.New(errProposalNotFound))
62 }
63
64 if !proposal.IsProposer(caller) {
65 panic(errors.New(errNotProposer))
66 }
67}
68
69func assertIsValidSmoothingPeriod(smoothingPeriod int64) {
70 if smoothingPeriod < 0 {
71 panic(errors.New(errInvalidSmoothingPeriod))
72 }
73
74 if smoothingPeriod > maxSmoothingPeriod {
75 panic(errors.New(errInvalidSmoothingPeriod))
76 }
77}
78
79func assertIsValidToken(tokenPath string) {
80 if tokenPath == "" {
81 panic(makeErrorWithDetails(
82 errInvalidInput, "tokenPath is empty"))
83 }
84
85 if err := common.IsRegistered(tokenPath); err != nil {
86 panic(makeErrorWithDetails(
87 errInvalidInput,
88 ufmt.Sprintf("token(%s) is not registered", tokenPath),
89 ))
90 }
91}