state.gno
2.15 Kb · 83 lines
1package pool
2
3import (
4 "errors"
5
6 "gno.land/p/gnoswap/store"
7 "gno.land/p/gnoswap/version_manager"
8
9 // initialize rbac roles
10 _ "gno.land/r/gnoswap/rbac"
11)
12
13var (
14 domainPath string
15
16 currentAddress address
17
18 // kvStore is the core storage instance for the pool domain.
19 // All pool implementations share this single storage instance,
20 // ensuring data consistency across version upgrades.
21 kvStore store.KVStore
22
23 // versionManager is the version manager for the pool domain.
24 // It manages the registration and switching of pool implementations.
25 versionManager version_manager.VersionManager
26
27 // implementation is the currently active pool implementation.
28 // This pointer is switched during upgrades to point to different versions (v1, v2, etc.).
29 // The proxy layer routes all calls to this implementation.
30 implementation IPool
31
32 ErrSpoofedRealm = errors.New("rlm does not match the current crossing frame")
33)
34
35// init initializes the pool domain state.
36// This function is called when the pool domain contract is first deployed.
37func init(cur realm) {
38 domainPath = cur.PkgPath()
39 currentAddress = cur.Address()
40
41 // Create a new KV store instance for this domain
42 kvStore = store.NewKVStore(currentAddress)
43
44 // Initialize the initializers map to store implementation registration functions
45 versionManager = version_manager.NewVersionManager(
46 domainPath,
47 kvStore,
48 initializeDomainStore,
49 )
50
51 implementation = nil
52}
53
54func initializeDomainStore(_ int, rlm realm, kvStore store.KVStore) any {
55 return NewPoolStore(kvStore)
56}
57
58// getImplementation returns the currently active pool implementation.
59// This function is used by all proxy functions to route calls to the active implementation.
60// If no implementation is set, it panics to prevent invalid state.
61func getImplementation() IPool {
62 if implementation == nil {
63 panic("implementation is not initialized")
64 }
65
66 return implementation
67}
68
69func updateImplementation() error {
70 result := versionManager.GetCurrentImplementation()
71 if result == nil {
72 return errors.New("implementation is not initialized")
73 }
74
75 impl, ok := result.(IPool)
76 if !ok {
77 return errors.New("impl is not an IPool")
78 }
79
80 implementation = impl
81
82 return nil
83}