poc.gno

1.49 Kb ยท 66 lines
 1package validators
 2
 3import (
 4	"std"
 5
 6	"gno.land/p/gov/proposal"
 7	"gno.land/p/sys/validators"
 8)
 9
10const daoPkgPath = "gno.land/r/gov/dao"
11
12const (
13	errNoChangesProposed = "no set changes proposed"
14	errNotGovDAO         = "caller not govdao executor"
15)
16
17// NewPropExecutor creates a new executor that wraps a changes closure
18// proposal. This wrapper is required to ensure the GovDAO Realm actually
19// executed the callback.
20//
21// Concept adapted from:
22// https://github.com/gnolang/gno/pull/1945
23func NewPropExecutor(changesFn func() []validators.Validator) proposal.Executor {
24	if changesFn == nil {
25		panic(errNoChangesProposed)
26	}
27
28	callback := func() error {
29		// Make sure the GovDAO executor runs the valset changes
30		assertGovDAOCaller()
31
32		for _, change := range changesFn() {
33			if change.VotingPower == 0 {
34				// This change request is to remove the validator
35				removeValidator(change.Address)
36
37				continue
38			}
39
40			// This change request is to add the validator
41			addValidator(change)
42		}
43
44		return nil
45	}
46
47	return proposal.NewExecutor(callback)
48}
49
50// assertGovDAOCaller verifies the caller is the GovDAO executor
51func assertGovDAOCaller() {
52	if std.PrevRealm().PkgPath() != daoPkgPath {
53		panic(errNotGovDAO)
54	}
55}
56
57// IsValidator returns a flag indicating if the given bech32 address
58// is part of the validator set
59func IsValidator(addr std.Address) bool {
60	return vp.IsValidator(addr)
61}
62
63// GetValidators returns the typed validator set
64func GetValidators() []validators.Validator {
65	return vp.GetValidators()
66}