Search Apps Documentation Source Content File Folder Download Copy Actions Download

config.gno

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