params.gno
4.81 Kb · 137 lines
1// Package params provides functions for creating parameter executors that
2// interface with the Params Keeper.
3//
4// This package enables setting various parameter types (such as strings,
5// integers, booleans, and byte slices) through the GovDAO proposal mechanism.
6// Each function returns an executor that, when called, sets the specified
7// parameter in the Params Keeper.
8//
9// The executors are designed to be used within governance proposals to modify
10// parameters dynamically. The integration with the GovDAO allows for parameter
11// changes to be proposed and executed in a controlled manner, ensuring that
12// modifications are subject to governance processes.
13//
14// Example usage:
15//
16// // This executor can be used in a governance proposal to set the parameter.
17// pr := params.NewSysParamStringPropExecutor("bank", "p", "restricted_denoms")
18package params
19
20import (
21 "chain"
22 prms "sys/params"
23
24 "gno.land/r/gov/dao"
25)
26
27// this is only used for emitting events.
28func syskey(module, submodule, name string) string {
29 return module + ":" + submodule + ":" + name
30}
31
32// assertNotValsetKey rejects governance proposals that target the
33// node:valset:* key family. Those keys are reserved for the realm-side
34// gate in r/sys/params/valset.gno (SetValsetProposal) which checks
35// the immediate caller is gno.land/r/sys/validators/v3. Without this
36// guard, a generic NewSysParam*PropRequest("node","valset",...) would
37// let any GovDAO supermajority bypass the v3 authorization and write
38// validator-set state directly.
39func assertNotValsetKey(module, submodule string) {
40 if module == "node" && submodule == "valset" {
41 panic("node:valset:* is reserved for r/sys/validators/v3; use it instead of the generic factory")
42 }
43}
44
45func NewSysParamStringPropRequest(cur realm, module, submodule, name, value string) dao.ProposalRequest {
46 assertNotValsetKey(module, submodule)
47 return newPropRequest(cur,
48 syskey(module, submodule, name),
49 func() { prms.SetSysParamString(module, submodule, name, value) },
50 "",
51 )
52}
53
54func NewSysParamInt64PropRequest(cur realm, module, submodule, name string, value int64) dao.ProposalRequest {
55 assertNotValsetKey(module, submodule)
56 return newPropRequest(cur,
57 syskey(module, submodule, name),
58 func() { prms.SetSysParamInt64(module, submodule, name, value) },
59 "",
60 )
61}
62
63func NewSysParamUint64PropRequest(cur realm, module, submodule, name string, value uint64) dao.ProposalRequest {
64 assertNotValsetKey(module, submodule)
65 return newPropRequest(cur,
66 syskey(module, submodule, name),
67 func() { prms.SetSysParamUint64(module, submodule, name, value) },
68 "",
69 )
70}
71
72func NewSysParamBoolPropRequest(cur realm, module, submodule, name string, value bool) dao.ProposalRequest {
73 assertNotValsetKey(module, submodule)
74 return newPropRequest(cur,
75 syskey(module, submodule, name),
76 func() { prms.SetSysParamBool(module, submodule, name, value) },
77 "",
78 )
79}
80
81func NewSysParamBytesPropRequest(cur realm, module, submodule, name string, value []byte) dao.ProposalRequest {
82 assertNotValsetKey(module, submodule)
83 return newPropRequest(cur,
84 syskey(module, submodule, name),
85 func() { prms.SetSysParamBytes(module, submodule, name, value) },
86 "",
87 )
88}
89
90func NewSysParamStringsPropRequest(cur realm, module, submodule, name string, value []string) dao.ProposalRequest {
91 assertNotValsetKey(module, submodule)
92 return newPropRequest(cur,
93 syskey(module, submodule, name),
94 func() { prms.SetSysParamStrings(module, submodule, name, value) },
95 "",
96 )
97}
98
99func NewSysParamStringsPropRequestWithTitle(cur realm, module, submodule, name, title string, value []string) dao.ProposalRequest {
100 assertNotValsetKey(module, submodule)
101 return newPropRequest(cur,
102 syskey(module, submodule, name),
103 func() { prms.SetSysParamStrings(module, submodule, name, value) },
104 title,
105 )
106}
107func NewSysParamStringsPropRequestAddWithTitle(cur realm, module, submodule, name, title string, value []string) dao.ProposalRequest {
108 assertNotValsetKey(module, submodule)
109 return newPropRequest(cur,
110 syskey(module, submodule, name),
111 func() { prms.UpdateSysParamStrings(module, submodule, name, value, true) },
112 title,
113 )
114}
115func NewSysParamStringsPropRequestRemoveWithTitle(cur realm, module, submodule, name, title string, value []string) dao.ProposalRequest {
116 assertNotValsetKey(module, submodule)
117 return newPropRequest(cur,
118 syskey(module, submodule, name),
119 func() { prms.UpdateSysParamStrings(module, submodule, name, value, false) },
120 title,
121 )
122}
123func newPropRequest(cur realm, key string, fn func(), title string) dao.ProposalRequest {
124 callback := func(cur realm) error {
125 fn()
126 chain.Emit("set", "key", key) // TODO document, make const, make consistent. 'k'??
127 return nil
128 }
129
130 if title == "" {
131 title = "Set new sys/params key"
132 }
133
134 e := dao.NewSimpleExecutor(0, cur, callback, "")
135
136 return dao.NewProposalRequest(title, "This proposal wants to add a new key to sys/params: "+key, e)
137}