proposal_test.gno

4.75 Kb ยท 171 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	"gno.land/r/gov/dao/v3/init" // so that the govdao initializer is executed
 13)
 14
 15func init() {
 16	c := std.OriginCaller()
 17	init.InitWithUsers(c)
 18}
 19
 20func TestValopers_ProposeNewValidator(t *testing.T) {
 21	const (
 22		registerMinFee int64 = 20 * 1_000_000 // minimum gnot must be paid to register.
 23		proposalMinFee int64 = 100 * 1_000_000
 24
 25		moniker     string = "moniker"
 26		description string = "description"
 27		pubKey             = "gpub1pggj7ard9eg82cjtv4u52epjx56nzwgjyg9zqwpdwpd0f9fvqla089ndw5g9hcsufad77fml2vlu73fk8q8sh8v72cza5p"
 28	)
 29
 30	test1Address := testutils.TestAddress("test1")
 31
 32	t.Run("remove an unexisting validator", func(t *testing.T) {
 33		// Send coins to be able to register a valoper
 34		testing.SetOriginSend(std.Coins{std.NewCoin("ugnot", registerMinFee)})
 35
 36		urequire.NotPanics(t, func() {
 37			valopers.Register(moniker, description, test1Address, pubKey)
 38			valopers.UpdateKeepRunning(test1Address, false)
 39		})
 40
 41		var valoper valopers.Valoper
 42
 43		urequire.NotPanics(t, func() {
 44			valoper = valopers.GetByAddr(test1Address)
 45		})
 46
 47		// Send coins to be able to make a proposal
 48		testing.SetOriginSend(std.Coins{std.NewCoin("ugnot", proposalMinFee)})
 49
 50		urequire.PanicsWithMessage(t, ErrValidatorMissing.Error(), func() {
 51			pr := NewValidatorProposalRequest(test1Address)
 52
 53			dao.MustCreateProposal(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(test1Address, true)
 63		})
 64
 65		var valoper valopers.Valoper
 66
 67		urequire.NotPanics(t, func() {
 68			valoper = valopers.GetByAddr(test1Address)
 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() {
 76			pr := NewValidatorProposalRequest(test1Address)
 77
 78			pid := dao.MustCreateProposal(pr)
 79		})
 80
 81		proposal, err := dao.GetProposal(pid) // index starts from 0
 82		urequire.NoError(t, err, "proposal not found")
 83
 84		description := ufmt.Sprintf("Valoper profile: [%s](/r/gnoland/valopers:%s)\n\n%s",
 85			valoper.Moniker,
 86			valoper.Address,
 87			valoper.Render(),
 88		)
 89
 90		// Check that the proposal is correct
 91		urequire.Equal(t, description, proposal.Description())
 92	})
 93
 94	t.Run("try to update a validator with the same values", func(t *testing.T) {
 95		// Send coins to be able to register a valoper
 96		testing.SetOriginSend(std.Coins{std.NewCoin("ugnot", registerMinFee)})
 97
 98		var valoper valopers.Valoper
 99
100		urequire.NotPanics(t, func() {
101			valoper = valopers.GetByAddr(test1Address)
102		})
103
104		urequire.NotPanics(t, func() {
105			// Vote the proposal created in the previous test
106			dao.MustVoteOnProposal(dao.VoteRequest{
107				Option:     dao.YesVote,
108				ProposalID: dao.ProposalID(0),
109			})
110
111			// Execute the proposal
112			dao.ExecuteProposal(dao.ProposalID(0))
113		})
114
115		// Send coins to be able to make a proposal
116		testing.SetOriginSend(std.Coins{std.NewCoin("ugnot", proposalMinFee)})
117
118		urequire.PanicsWithMessage(t, ErrSameValues.Error(), func() {
119			pr := NewValidatorProposalRequest(test1Address)
120
121			dao.MustCreateProposal(pr)
122		})
123	})
124}
125
126func TestValopers_ProposeNewInstructions(t *testing.T) {
127	const proposalMinFee int64 = 100 * 1_000_000
128
129	newInstructions := "new instructions"
130	description := ufmt.Sprintf("Update the instructions to: \n\n%s", newInstructions)
131
132	// Send coins to be able to make a proposal
133	testing.SetOriginSend(std.Coins{std.NewCoin("ugnot", proposalMinFee)})
134
135	var pid dao.ProposalID
136	urequire.NotPanics(t, func() {
137		pr := ProposeNewInstructionsProposalRequest(newInstructions)
138
139		pid = dao.MustCreateProposal(pr)
140	})
141
142	proposal, err := dao.GetProposal(pid) // index starts from 0
143	urequire.NoError(t, err, "proposal not found")
144	if proposal == nil {
145		panic("PROPOSAL NOT FOUND")
146	}
147
148	// Check that the proposal is correct
149	urequire.Equal(t, description, proposal.Description())
150}
151
152func TestValopers_ProposeNewMinFee(t *testing.T) {
153	const proposalMinFee int64 = 100 * 1_000_000
154	newMinFee := int64(10)
155	description := ufmt.Sprintf("Update the minimum register fee to: %d ugnot", newMinFee)
156
157	// Send coins to be able to make a proposal
158	testing.SetOriginSend(std.Coins{std.NewCoin("ugnot", proposalMinFee)})
159
160	var pid dao.ProposalID
161	urequire.NotPanics(t, func() {
162		pr := ProposeNewMinFeeProposalRequest(newMinFee)
163
164		pid = dao.MustCreateProposal(pr)
165	})
166
167	proposal, err := dao.GetProposal(pid) // index starts from 0
168	urequire.NoError(t, err, "proposal not found")
169	// Check that the proposal is correct
170	urequire.Equal(t, description, proposal.Description())
171}