Search Apps Documentation Source Content File Folder Download Copy Actions Download

state.gno

1.53 Kb · 65 lines
 1package governance
 2
 3import (
 4	"errors"
 5
 6	_ "gno.land/r/gnoswap/rbac" // initialize readable contract role(s)
 7
 8	"gno.land/p/gnoswap/store"
 9	"gno.land/p/gnoswap/version_manager"
10)
11
12var (
13	// currentAddress and domainPath describe the governance realm itself.
14	// They are captured from the `cur realm` parameter in init(cur realm),
15	// which resolves to this governance domain realm.
16	currentAddress address
17	domainPath     string
18
19	kvStore store.KVStore
20
21	versionManager version_manager.VersionManager
22	implementation IGovernance
23
24	// ErrSpoofedRealm is returned/panicked when a captured realm token no
25	// longer matches the live crossing frame (rlm.IsCurrent() == false).
26	ErrSpoofedRealm = errors.New("rlm does not match the current crossing frame")
27)
28
29func init(cur realm) {
30	currentAddress = cur.Address()
31	domainPath = cur.PkgPath()
32
33	kvStore = store.NewKVStore(currentAddress)
34	versionManager = version_manager.NewVersionManager(domainPath, kvStore, initializeDomainStore)
35
36	implementation = nil
37}
38
39func initializeDomainStore(_ int, rlm realm, kvStore store.KVStore) any {
40	return NewGovernanceStore(kvStore)
41}
42
43func getImplementation() IGovernance {
44	if implementation == nil {
45		panic("implementation is not initialized")
46	}
47
48	return implementation
49}
50
51func updateImplementation() error {
52	result := versionManager.GetCurrentImplementation()
53	if result == nil {
54		return errors.New("implementation is not initialized")
55	}
56
57	impl, ok := result.(IGovernance)
58	if !ok {
59		return errors.New("impl is not an IGovernance")
60	}
61
62	implementation = impl
63
64	return nil
65}