state.gno
2.14 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
13const ErrSpoofedRealm = "rlm does not match the current crossing frame"
14
15var (
16 domainPath string
17
18 currentAddress address
19
20 // kvStore is the core storage instance for the pool domain.
21 // All pool implementations share this single storage instance,
22 // ensuring data consistency across version upgrades.
23 kvStore store.KVStore
24
25 // versionManager is the version manager for the pool domain.
26 // It manages the registration and switching of pool implementations.
27 versionManager version_manager.VersionManager
28
29 // implementation is the currently active pool implementation.
30 // This pointer is switched during upgrades to point to different versions (v1, v2, etc.).
31 // The proxy layer routes all calls to this implementation.
32 implementation IPool
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}