proposal_test.gno

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