Search Apps Documentation Source Content File Folder Download Copy Actions Download

poc.gno

2.33 Kb · 91 lines
 1package validators
 2
 3import (
 4	"strings"
 5
 6	"gno.land/p/nt/ufmt/v0"
 7	"gno.land/p/sys/validators"
 8	"gno.land/r/gov/dao"
 9)
10
11// NewPropRequest creates a new proposal request that wraps a changes closure
12// proposal. This wrapper is required to ensure the GovDAO Realm actually
13// executed the callback.
14func NewPropRequest(changesFn func() []validators.Validator, title, description string) dao.ProposalRequest {
15	if changesFn == nil {
16		panic("no set changes proposed")
17	}
18
19	title = strings.TrimSpace(title)
20	if title == "" {
21		panic("proposal title is empty")
22	}
23
24	// Get the list of validators now to make sure the list
25	// doesn't change during the lifetime of the proposal
26	changes := changesFn()
27
28	// Limit the number of validators to keep the description within a limit
29	// that makes sense because there is not pagination of validators
30	if len(changes) > 40 {
31		panic("max number of allowed validators per proposal is 40")
32	} else if len(changes) == 0 {
33		panic("proposal requires at least one validator")
34	}
35
36	// List the validator addresses and the action to be taken for each one
37	var desc strings.Builder
38	desc.WriteString(description)
39	if len(description) > 0 {
40		desc.WriteString("\n\n")
41	}
42
43	desc.WriteString("## Validator Updates\n")
44	for _, change := range changes {
45		if change.VotingPower == 0 {
46			desc.WriteString(ufmt.Sprintf("- %s: remove\n", change.Address))
47		} else {
48			desc.WriteString(ufmt.Sprintf("- %s: add\n", change.Address))
49		}
50	}
51
52	callback := func(cur realm) error {
53		for _, change := range changes {
54			if change.VotingPower == 0 {
55				// This change request is to remove the validator
56				removeValidator(change.Address)
57
58				continue
59			}
60
61			// This change request is to add the validator
62			addValidator(change)
63		}
64
65		return nil
66	}
67
68	e := dao.NewSimpleExecutor(callback, "")
69
70	return dao.NewProposalRequest(title, desc.String(), e)
71}
72
73// IsValidator returns a flag indicating if the given bech32 address
74// is part of the validator set
75func IsValidator(addr address) bool {
76	return vp.IsValidator(addr)
77}
78
79// GetValidator returns the typed validator
80func GetValidator(addr address) validators.Validator {
81	if validator, err := vp.GetValidator(addr); err == nil {
82		return validator
83	}
84
85	panic("validator not found")
86}
87
88// GetValidators returns the typed validator set
89func GetValidators() []validators.Validator {
90	return vp.GetValidators()
91}