config.gno
3.29 Kb · 123 lines
1package governance
2
3import (
4 "chain"
5 "errors"
6 "time"
7
8 "gno.land/p/gnoswap/utils"
9
10 "gno.land/r/gnoswap/access"
11 en "gno.land/r/gnoswap/emission"
12 "gno.land/r/gnoswap/halt"
13
14 "gno.land/r/gnoswap/gov/governance"
15)
16
17// makeConfig creates a new governance configuration.
18func makeConfig(
19 votingStartDelay int64,
20 votingPeriod int64,
21 votingWeightSmoothingDuration int64,
22 quorum int64,
23 proposalCreationThreshold int64,
24 executionDelay int64,
25 executionWindow int64,
26) governance.Config {
27 // Create new configuration with provided parameters. Constructed via the
28 // domain constructor so the Config value is allocated inside the governance
29 // domain realm (satisfies the realm allocation check).
30 return governance.NewConfig(
31 votingStartDelay,
32 votingPeriod,
33 votingWeightSmoothingDuration,
34 quorum,
35 proposalCreationThreshold,
36 executionDelay,
37 executionWindow,
38 )
39}
40
41// Reconfigure updates governance configuration.
42// Only admin or governance contract can call this function.
43// Updates all governance parameters and emits a "Reconfigure" event.
44func (gv *governanceV1) Reconfigure(
45 _ int, rlm realm,
46 votingStartDelay int64,
47 votingPeriod int64,
48 votingWeightSmoothingDuration int64,
49 quorum int64,
50 proposalCreationThreshold int64,
51 executionDelay int64,
52 executionWindow int64,
53) int64 {
54 if !rlm.IsCurrent() {
55 panic(errors.New(errSpoofedRealm))
56 }
57
58 // Check if system is halted before proceeding
59 halt.AssertIsNotHaltedGovernance()
60
61 prev := rlm.Previous()
62 caller := prev.Address()
63 access.AssertIsAdminOrGovernance(caller)
64
65 assertIsValidSmoothingPeriod(votingWeightSmoothingDuration)
66
67 // Create and validate new configuration
68 newCfg := makeConfig(
69 votingStartDelay,
70 votingPeriod,
71 votingWeightSmoothingDuration,
72 quorum,
73 proposalCreationThreshold,
74 executionDelay,
75 executionWindow,
76 )
77
78 currentTime := time.Now().Unix()
79
80 if err := newCfg.IsValid(currentTime); err != nil {
81 panic(makeErrorWithDetails(errInvalidConfiguration, err.Error()))
82 }
83
84 // Mint and distribute GNS tokens as part of the process
85 en.MintAndDistributeGns(cross(rlm))
86
87 // Store previous version for event emission
88 previousVersion := gv.getCurrentConfigVersion()
89
90 // Apply the new configuration
91 nextVersion := gv.reconfigure(0, rlm, newCfg)
92
93 chain.Emit(
94 "Reconfigure",
95 "prevAddr", caller.String(),
96 "prevRealm", prev.PkgPath(),
97 "votingStartDelay", utils.FormatInt(newCfg.VotingStartDelay),
98 "votingPeriod", utils.FormatInt(newCfg.VotingPeriod),
99 "votingWeightSmoothingDuration", utils.FormatInt(newCfg.VotingWeightSmoothingDuration),
100 "quorum", utils.FormatInt(newCfg.Quorum),
101 "proposalCreationThreshold", utils.FormatInt(newCfg.ProposalCreationThreshold),
102 "executionDelay", utils.FormatInt(newCfg.ExecutionDelay),
103 "executionPeriod", utils.FormatInt(newCfg.ExecutionWindow),
104 "newConfigVersion", utils.FormatInt(nextVersion),
105 "prevConfigVersion", utils.FormatInt(previousVersion),
106 )
107
108 return nextVersion
109}
110
111// reconfigure stores the validated configuration with incremented version number.
112func (gv *governanceV1) reconfigure(_ int, rlm realm, cfg governance.Config) int64 {
113 // Generate next version number
114 nextVersion := gv.nextConfigVersion(0, rlm)
115
116 // Store the new configuration with version
117 err := gv.setConfig(0, rlm, nextVersion, cfg)
118 if err != nil {
119 panic("failed to set config: " + err.Error())
120 }
121
122 return nextVersion
123}