Search Apps Documentation Source Content File Folder Download Copy

validators_test.gno

2.41 Kb ยท 102 lines
  1package validators
  2
  3import (
  4	"testing"
  5
  6	"std"
  7
  8	"gno.land/p/demo/avl"
  9	"gno.land/p/demo/testutils"
 10	"gno.land/p/demo/uassert"
 11	"gno.land/p/demo/ufmt"
 12	"gno.land/p/sys/validators"
 13)
 14
 15// generateTestValidators generates a dummy validator set
 16func generateTestValidators(count int) []validators.Validator {
 17	vals := make([]validators.Validator, 0, count)
 18
 19	for i := 0; i < count; i++ {
 20		val := validators.Validator{
 21			Address:     testutils.TestAddress(ufmt.Sprintf("%d", i)),
 22			PubKey:      "public-key",
 23			VotingPower: 10,
 24		}
 25
 26		vals = append(vals, val)
 27	}
 28
 29	return vals
 30}
 31
 32func TestValidators_AddRemove(t *testing.T) {
 33	// Clear any changes
 34	changes = avl.NewTree()
 35
 36	var (
 37		vals          = generateTestValidators(100)
 38		initialHeight = int64(123)
 39	)
 40
 41	// Add in the validators
 42	for _, val := range vals {
 43		addValidator(val)
 44
 45		// Make sure the validator is added
 46		uassert.True(t, vp.IsValidator(val.Address))
 47
 48		std.TestSkipHeights(1)
 49	}
 50
 51	for i := initialHeight; i < initialHeight+int64(len(vals)); i++ {
 52		// Make sure the changes are saved
 53		chs := GetChanges(i)
 54
 55		// We use the funky index calculation to make sure
 56		// changes are properly handled for each block span
 57		uassert.Equal(t, initialHeight+int64(len(vals))-i, int64(len(chs)))
 58
 59		for index, val := range vals[i-initialHeight:] {
 60			// Make sure the changes are equal to the additions
 61			ch := chs[index]
 62
 63			uassert.Equal(t, val.Address, ch.Address)
 64			uassert.Equal(t, val.PubKey, ch.PubKey)
 65			uassert.Equal(t, val.VotingPower, ch.VotingPower)
 66		}
 67	}
 68
 69	// Save the beginning height for the removal
 70	initialRemoveHeight := std.GetHeight()
 71
 72	// Clear any changes
 73	changes = avl.NewTree()
 74
 75	// Remove the validators
 76	for _, val := range vals {
 77		removeValidator(val.Address)
 78
 79		// Make sure the validator is removed
 80		uassert.False(t, vp.IsValidator(val.Address))
 81
 82		std.TestSkipHeights(1)
 83	}
 84
 85	for i := initialRemoveHeight; i < initialRemoveHeight+int64(len(vals)); i++ {
 86		// Make sure the changes are saved
 87		chs := GetChanges(i)
 88
 89		// We use the funky index calculation to make sure
 90		// changes are properly handled for each block span
 91		uassert.Equal(t, initialRemoveHeight+int64(len(vals))-i, int64(len(chs)))
 92
 93		for index, val := range vals[i-initialRemoveHeight:] {
 94			// Make sure the changes are equal to the additions
 95			ch := chs[index]
 96
 97			uassert.Equal(t, val.Address, ch.Address)
 98			uassert.Equal(t, val.PubKey, ch.PubKey)
 99			uassert.Equal(t, uint64(0), ch.VotingPower)
100		}
101	}
102}