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