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

validators_test.gno

3.37 Kb · 97 lines
 1// Tests for stateless v0.
 2//
 3// State isolation: gno's test runner constructs a fresh testParams
 4// per top-level TestXxx, but t.Run subtests share the same map.
 5// Each subtest below seeds (or clears) the valset slots it cares
 6// about up front; do the same when adding cases.
 7package validators
 8
 9import (
10	"chain"
11	"testing"
12
13	"gno.land/p/nt/urequire/v0"
14)
15
16const (
17	// Three distinct deterministic test pubkeys (and the addresses
18	// they derive to) used across the cases below. Stable across
19	// runs.
20	pubKeyA = "gpub1pggj7ard9eg82cjtv4u52epjx56nzwgjyg9zq3ds6sdvc0shfkq02h6xx5g0jp04aadexfnpsmgjxu72xz9y30aqfrlpny"
21	pubKeyB = "gpub1pggj7ard9eg82cjtv4u52epjx56nzwgjyg9zqwpdwpd0f9fvqla089ndw5g9hcsufad77fml2vlu73fk8q8sh8v72cza5p"
22	pubKeyC = "gpub1pgfj7ard9eg82cjtv4u4xetrwqer2dntxyfzxz3pqddddqg2glc8x4fl7vxjlnr7p5a3czm5kcdp4239sg6yqdc4rc2r5cjrffs"
23
24	module    = "node"
25	submodule = "valset"
26	currKey   = "current"
27	propKey   = "proposed"
28	dirtyKey  = "dirty"
29)
30
31func resetValset(t *testing.T) {
32	t.Helper()
33	testing.SetSysParamStrings(module, submodule, currKey, []string{})
34	testing.SetSysParamStrings(module, submodule, propKey, []string{})
35	testing.SetSysParamBool(module, submodule, dirtyKey, false)
36}
37
38func mustAddr(t *testing.T, pk string) address {
39	t.Helper()
40	a, err := chain.PubKeyAddress(pk)
41	urequire.NoError(t, err, "valid pubkey")
42	return a
43}
44
45func TestEffectiveView_DirtyFalseReadsCurrent(cur realm, t *testing.T) {
46	resetValset(t)
47	testing.SetSysParamStrings(module, submodule, currKey, []string{pubKeyA + ":10"})
48
49	addrA := mustAddr(t, pubKeyA)
50	urequire.True(t, IsValidator(addrA), "A in current")
51	urequire.Equal(t, 1, len(GetValidators()))
52}
53
54func TestEffectiveView_DirtyTrueReadsProposed(cur realm, t *testing.T) {
55	resetValset(t)
56	testing.SetSysParamStrings(module, submodule, currKey, []string{pubKeyA + ":10"})
57	testing.SetSysParamStrings(module, submodule, propKey, []string{pubKeyA + ":10", pubKeyB + ":1"})
58	testing.SetSysParamBool(module, submodule, dirtyKey, true)
59
60	addrA := mustAddr(t, pubKeyA)
61	addrB := mustAddr(t, pubKeyB)
62	urequire.True(t, IsValidator(addrA), "A still visible")
63	urequire.True(t, IsValidator(addrB), "B visible via proposed")
64	urequire.Equal(t, 2, len(GetValidators()))
65}
66
67func TestGetValidator_ReturnsParsedEntry(cur realm, t *testing.T) {
68	resetValset(t)
69	testing.SetSysParamStrings(module, submodule, currKey, []string{pubKeyA + ":42"})
70
71	v := GetValidator(mustAddr(t, pubKeyA))
72	urequire.Equal(t, uint64(42), v.VotingPower)
73	urequire.Equal(t, pubKeyA, v.PubKey)
74}
75
76func TestRender_DefaultTestHeight(cur realm, t *testing.T) {
77	resetValset(t)
78	testing.SetSysParamStrings(module, submodule, currKey, []string{pubKeyA + ":10"})
79
80	out := Render("")
81	// gnovm/pkg/test/test.go sets DefaultHeight = 123.
82	urequire.Equal(t,
83		"## Valset at height 123\n\n- #0: "+mustAddr(t, pubKeyA).String()+" (10)\n",
84		out)
85}
86
87// The same-block accumulation, panic-on-remove-of-missing,
88// panic-on-add-of-existing, and pubkey-validation tests for the
89// signing-keyed NewProposalRequest/newValsetChangeExecutor were
90// removed alongside the function itself. Equivalent coverage for
91// the operator-keyed flow lives in proposal_test.gno
92// (TestNewValidatorProposalRequest_*) and cache_test.gno
93// (TestRotateValoperSigningKey_AccumulatesAcrossSameBlock).
94//
95// Empty-final-set rejection in the executor still applies; it's
96// covered by TestNewValidatorProposalRequest_RemoveOperator in
97// proposal_test.gno.