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