Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

poa_test.gno

5.19 Kb · 237 lines
  1package poa
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/nt/testutils/v0"
  7	"gno.land/p/nt/uassert/v0"
  8	"gno.land/p/nt/urequire/v0"
  9	"gno.land/p/sys/validators/v0"
 10
 11	"gno.land/p/nt/ufmt/v0"
 12)
 13
 14// generateTestValidators generates a dummy validator set
 15func generateTestValidators(count int) []validators.Validator {
 16	vals := make([]validators.Validator, 0, count)
 17
 18	for i := 0; i < count; i++ {
 19		val := validators.Validator{
 20			Address:     testutils.TestAddress(ufmt.Sprintf("%d", i)),
 21			PubKey:      "public-key",
 22			VotingPower: 1,
 23		}
 24
 25		vals = append(vals, val)
 26	}
 27
 28	return vals
 29}
 30
 31func TestPoA_AddValidator_Invalid(t *testing.T) {
 32	t.Parallel()
 33
 34	t.Run("validator already in set", func(t *testing.T) {
 35		t.Parallel()
 36
 37		var (
 38			proposalAddress = testutils.TestAddress("caller")
 39			proposalKey     = "public-key"
 40
 41			initialSet = generateTestValidators(1)
 42		)
 43
 44		initialSet[0].Address = proposalAddress
 45		initialSet[0].PubKey = proposalKey
 46
 47		// Create the protocol with an initial set
 48		p := NewPoA(WithInitialSet(initialSet))
 49
 50		// Attempt to add the validator
 51		_, err := p.AddValidator(proposalAddress, proposalKey, 1)
 52		uassert.ErrorIs(t, err, validators.ErrValidatorExists)
 53	})
 54
 55	t.Run("invalid voting power", func(t *testing.T) {
 56		t.Parallel()
 57
 58		var (
 59			proposalAddress = testutils.TestAddress("caller")
 60			proposalKey     = "public-key"
 61		)
 62
 63		// Create the protocol with no initial set
 64		p := NewPoA()
 65
 66		// Attempt to add the validator
 67		_, err := p.AddValidator(proposalAddress, proposalKey, 0)
 68		uassert.ErrorIs(t, err, ErrInvalidVotingPower)
 69	})
 70}
 71
 72func TestPoA_AddValidator(t *testing.T) {
 73	t.Parallel()
 74
 75	var (
 76		proposalAddress = testutils.TestAddress("caller")
 77		proposalKey     = "public-key"
 78	)
 79
 80	// Create the protocol with no initial set
 81	p := NewPoA()
 82
 83	// Attempt to add the validator
 84	_, err := p.AddValidator(proposalAddress, proposalKey, 1)
 85	uassert.NoError(t, err)
 86
 87	// Make sure the validator is added
 88	if !p.IsValidator(proposalAddress) || p.validators.Size() != 1 {
 89		t.Fatal("address is not validator")
 90	}
 91}
 92
 93func TestPoA_RemoveValidator_Invalid(t *testing.T) {
 94	t.Parallel()
 95
 96	t.Run("proposed removal not in set", func(t *testing.T) {
 97		t.Parallel()
 98
 99		var (
100			proposalAddress = testutils.TestAddress("caller")
101			initialSet      = generateTestValidators(1)
102		)
103
104		initialSet[0].Address = proposalAddress
105
106		// Create the protocol with an initial set
107		p := NewPoA(WithInitialSet(initialSet))
108
109		// Attempt to remove the validator
110		_, err := p.RemoveValidator(testutils.TestAddress("totally random"))
111		uassert.ErrorIs(t, err, validators.ErrValidatorMissing)
112	})
113}
114
115func TestPoA_RemoveValidator(t *testing.T) {
116	t.Parallel()
117
118	var (
119		proposalAddress = testutils.TestAddress("caller")
120		initialSet      = generateTestValidators(1)
121	)
122
123	initialSet[0].Address = proposalAddress
124
125	// Create the protocol with an initial set
126	p := NewPoA(WithInitialSet(initialSet))
127
128	// Attempt to remove the validator
129	_, err := p.RemoveValidator(proposalAddress)
130	urequire.NoError(t, err)
131
132	// Make sure the validator is removed
133	if p.IsValidator(proposalAddress) || p.validators.Size() != 0 {
134		t.Fatal("address is validator")
135	}
136}
137
138func TestPoA_GetValidator(t *testing.T) {
139	t.Parallel()
140
141	t.Run("validator not in set", func(t *testing.T) {
142		t.Parallel()
143
144		// Create the protocol with no initial set
145		p := NewPoA()
146
147		// Attempt to get the voting power
148		_, err := p.GetValidator(testutils.TestAddress("caller"))
149		uassert.ErrorIs(t, err, validators.ErrValidatorMissing)
150	})
151
152	t.Run("validator fetched", func(t *testing.T) {
153		t.Parallel()
154
155		var (
156			address_XXX = testutils.TestAddress("caller")
157			pubKey      = "public-key"
158			votingPower = uint64(10)
159
160			initialSet = generateTestValidators(1)
161		)
162
163		initialSet[0].Address = address_XXX
164		initialSet[0].PubKey = pubKey
165		initialSet[0].VotingPower = votingPower
166
167		// Create the protocol with an initial set
168		p := NewPoA(WithInitialSet(initialSet))
169
170		// Get the validator
171		val, err := p.GetValidator(address_XXX)
172		urequire.NoError(t, err)
173
174		// Validate the address
175		if val.Address != address_XXX {
176			t.Fatal("invalid address")
177		}
178
179		// Validate the voting power
180		if val.VotingPower != votingPower {
181			t.Fatal("invalid voting power")
182		}
183
184		// Validate the public key
185		if val.PubKey != pubKey {
186			t.Fatal("invalid public key")
187		}
188	})
189}
190
191func TestPoA_GetValidators(t *testing.T) {
192	t.Parallel()
193
194	t.Run("empty set", func(t *testing.T) {
195		t.Parallel()
196
197		// Create the protocol with no initial set
198		p := NewPoA()
199
200		// Attempt to get the voting power
201		vals := p.GetValidators()
202
203		if len(vals) != 0 {
204			t.Fatal("validator set is not empty")
205		}
206	})
207
208	t.Run("validator set fetched", func(t *testing.T) {
209		t.Parallel()
210
211		initialSet := generateTestValidators(10)
212
213		// Create the protocol with an initial set
214		p := NewPoA(WithInitialSet(initialSet))
215
216		// Get the validator set
217		vals := p.GetValidators()
218
219		if len(vals) != len(initialSet) {
220			t.Fatal("returned validator set mismatch")
221		}
222
223		for _, val := range vals {
224			for _, initialVal := range initialSet {
225				if val.Address != initialVal.Address {
226					continue
227				}
228
229				// Validate the voting power
230				uassert.Equal(t, val.VotingPower, initialVal.VotingPower)
231
232				// Validate the public key
233				uassert.Equal(t, val.PubKey, initialVal.PubKey)
234			}
235		}
236	})
237}