proposal_test.gno
7.46 Kb ยท 257 lines
1package proposal
2
3import (
4 "chain"
5 "testing"
6
7 "gno.land/p/nt/testutils"
8 "gno.land/p/nt/ufmt"
9 "gno.land/p/nt/urequire"
10 "gno.land/r/gnops/valopers"
11 "gno.land/r/gov/dao"
12 daoinit "gno.land/r/gov/dao/v3/init" // so that the govdao initializer is executed
13 susers "gno.land/r/sys/users"
14)
15
16var g1user = testutils.TestAddress("g1user")
17
18func init() {
19 daoinit.InitWithUsers(g1user)
20 registerTestUsers(g1user)
21}
22
23const gUsersV1Path = "gno.land/r/gnoland/users/v1"
24
25// Register a namespace for every addresses
26// Necessary to test GovDAO Vote
27func registerTestUsers(addrs ...address) {
28 // Set realm to users admin to register test user
29 testing.SetRealm(testing.NewCodeRealm(gUsersV1Path))
30 for _, addr := range addrs {
31 err := susers.RegisterUser(cross, addr.String()[1:], addr)
32 if err != nil {
33 panic(err.Error() + " : " + addr.String())
34 }
35 }
36}
37
38func TestValopers_ProposeNewValidator(t *testing.T) {
39 const (
40 registerMinFee int64 = 20 * 1_000_000 // minimum gnot must be paid to register.
41 proposalMinFee int64 = 100 * 1_000_000
42
43 moniker string = "moniker"
44 description string = "description"
45 serverType string = valopers.ServerTypeOnPrem
46 pubKey = "gpub1pggj7ard9eg82cjtv4u52epjx56nzwgjyg9zqwpdwpd0f9fvqla089ndw5g9hcsufad77fml2vlu73fk8q8sh8v72cza5p"
47 )
48
49 // Set origin caller
50 testing.SetRealm(testing.NewUserRealm(g1user))
51
52 t.Run("remove an unexisting validator", func(t *testing.T) {
53 // Send coins to be able to register a valoper
54 testing.SetOriginSend(chain.Coins{chain.NewCoin("ugnot", registerMinFee)})
55
56 urequire.NotPanics(t, func() {
57 valopers.Register(cross, moniker, description, serverType, g1user, pubKey)
58 valopers.UpdateKeepRunning(cross, g1user, false)
59 })
60
61 urequire.NotPanics(t, func() {
62 valopers.GetByAddr(g1user)
63 })
64
65 // Send coins to be able to make a proposal
66 testing.SetOriginSend(chain.Coins{chain.NewCoin("ugnot", proposalMinFee)})
67
68 urequire.AbortsWithMessage(t, ErrValidatorMissing.Error(), func(cur realm) {
69 pr := NewValidatorProposalRequest(cur, g1user)
70
71 dao.MustCreateProposal(cross, pr)
72 })
73 })
74
75 t.Run("proposal successfully created", func(t *testing.T) {
76 // Send coins to be able to register a valoper
77 testing.SetOriginSend(chain.Coins{chain.NewCoin("ugnot", registerMinFee)})
78
79 urequire.NotPanics(t, func() {
80 valopers.UpdateKeepRunning(cross, g1user, true)
81 })
82
83 var valoper valopers.Valoper
84
85 urequire.NotPanics(t, func() {
86 valoper = valopers.GetByAddr(g1user)
87 })
88
89 // Send coins to be able to make a proposal
90 testing.SetOriginSend(chain.Coins{chain.NewCoin("ugnot", proposalMinFee)})
91
92 var pid dao.ProposalID
93 urequire.NotPanics(t, func(cur realm) {
94 testing.SetRealm(testing.NewUserRealm(g1user))
95 pr := NewValidatorProposalRequest(cur, g1user)
96
97 pid = dao.MustCreateProposal(cross, pr)
98 })
99
100 proposal, err := dao.GetProposal(cross, pid) // index starts from 0
101 urequire.NoError(t, err, "proposal not found")
102
103 description := ufmt.Sprintf(
104 "Valoper profile: [%s](/r/gnops/valopers:%s)\n\n%s\n\n## Validator Updates\n- %s: add",
105 valoper.Moniker,
106 valoper.Address,
107 valoper.Render(),
108 valoper.Address,
109 )
110
111 // Check that the proposal is correct
112 urequire.Equal(t, description, proposal.Description())
113 })
114
115 t.Run("try to update a validator with the same values", func(t *testing.T) {
116 // Send coins to be able to register a valoper
117 testing.SetOriginSend(chain.Coins{chain.NewCoin("ugnot", registerMinFee)})
118
119 urequire.NotPanics(t, func() {
120 valopers.GetByAddr(g1user)
121 })
122
123 urequire.NotPanics(t, func() {
124 // Vote the proposal created in the previous test
125 dao.MustVoteOnProposal(cross, dao.VoteRequest{
126 Option: dao.YesVote,
127 ProposalID: dao.ProposalID(0),
128 })
129
130 // Execute the proposal
131 dao.ExecuteProposal(cross, dao.ProposalID(0))
132 })
133
134 // Send coins to be able to make a proposal
135 testing.SetOriginSend(chain.Coins{chain.NewCoin("ugnot", proposalMinFee)})
136
137 urequire.AbortsWithMessage(t, ErrSameValues.Error(), func() {
138 pr := NewValidatorProposalRequest(cross, g1user)
139 dao.MustCreateProposal(cross, pr)
140 })
141 })
142}
143
144func TestValopers_ProposeNewInstructions(t *testing.T) {
145 const proposalMinFee int64 = 100 * 1_000_000
146
147 newInstructions := "new instructions"
148 description := ufmt.Sprintf("Update the instructions to: \n\n%s", newInstructions)
149
150 // Set origin caller
151 testing.SetRealm(testing.NewUserRealm(g1user))
152
153 // Send coins to be able to make a proposal
154 testing.SetOriginSend(chain.Coins{chain.NewCoin("ugnot", proposalMinFee)})
155
156 var pid dao.ProposalID
157 urequire.NotPanics(t, func() {
158 pr := ProposeNewInstructionsProposalRequest(cross, newInstructions)
159
160 pid = dao.MustCreateProposal(cross, pr)
161 })
162
163 proposal, err := dao.GetProposal(cross, pid) // index starts from 0
164 urequire.NoError(t, err, "proposal not found")
165 if proposal == nil {
166 panic("PROPOSAL NOT FOUND")
167 }
168
169 // Check that the proposal is correct
170 urequire.Equal(t, description, proposal.Description())
171}
172
173func TestValopers_ProposeNewMinFee(t *testing.T) {
174 const proposalMinFee int64 = 100 * 1_000_000
175 newMinFee := int64(10)
176 description := ufmt.Sprintf("Update the minimum register fee to: %d ugnot", newMinFee)
177
178 // Set origin caller
179 testing.SetRealm(testing.NewUserRealm(g1user))
180
181 // Send coins to be able to make a proposal
182 testing.SetOriginSend(chain.Coins{chain.NewCoin("ugnot", proposalMinFee)})
183
184 var pid dao.ProposalID
185 urequire.NotPanics(t, func() {
186 pr := ProposeNewMinFeeProposalRequest(cross, newMinFee)
187
188 pid = dao.MustCreateProposal(cross, pr)
189 })
190
191 proposal, err := dao.GetProposal(cross, pid) // index starts from 0
192 urequire.NoError(t, err, "proposal not found")
193 // Check that the proposal is correct
194 urequire.Equal(t, description, proposal.Description())
195}
196
197/* TODO fix this @moul
198func TestValopers_ProposeNewValidator2(t *testing.T) {
199 const (
200 registerMinFee int64 = 20 * 1_000_000 // minimum gnot must be paid to register.
201 proposalMinFee int64 = 100 * 1_000_000
202
203 moniker string = "moniker"
204 description string = "description"
205 pubKey = "gpub1pggj7ard9eg82cjtv4u52epjx56nzwgjyg9zqwpdwpd0f9fvqla089ndw5g9hcsufad77fml2vlu73fk8q8sh8v72cza5p"
206 )
207
208 // Set origin caller
209 testing.SetRealm(std.NewUserRealm(g1user))
210
211 t.Run("create valid proposal", func(t *testing.T) {
212 // Validator exists, should not panic
213 urequire.NotPanics(t, func() {
214 _ = valopers.MustGetValoper(g1user)
215 })
216
217 // Create the proposal
218 urequire.NotPanics(t, func() {
219 cross(valopers.Register)(moniker, description, g1user, pubKey)
220 })
221
222 // Verify proposal details
223 urequire.NotPanics(t, func() {
224 valoper := valopers.MustGetValoper(g1user)
225 urequire.Equal(t, moniker, valoper.Moniker)
226 urequire.Equal(t, description, valoper.Description)
227 })
228 // Execute proposal with admin rights
229 urequire.NotPanics(t, func() {
230 std.TestSetOrigCaller(std.Admin)
231 cross(dao.ExecuteProposal)(dao.ProposalID(0))
232 })
233 // Check if valoper was updated
234 urequire.NotPanics(t, func() {
235 valoper := valopers.MustGetValoper(g1user)
236 urequire.Equal(t, moniker, valoper.Moniker)
237 urequire.Equal(t, description, valoper.Description)
238 })
239
240 // Expect ExecuteProposal to pass
241 urequire.NotPanics(t, func() {
242 cross(dao.ExecuteProposal)(dao.ProposalID(0))
243 })
244 // Check if valoper was updated
245 urequire.NotPanics(t, func() {
246 valoper := valopers.MustGetValoper(g1user)
247 urequire.Equal(t, moniker, valoper.Moniker)
248 urequire.Equal(t, description, valoper.Description)
249 })
250 // Execute proposal with admin rights
251 urequire.NotPanics(t, func() {
252 std.TestSetOrigCaller(std.Admin)
253 cross(dao.ExecuteProposal)(dao.ProposalID(0))
254 })
255 })
256}
257*/