state.gno
2.07 Kb · 81 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
33// init initializes the pool domain state.
34// This function is called when the pool domain contract is first deployed.
35func init(cur realm) {
36 domainPath = cur.PkgPath()
37 currentAddress = cur.Address()
38
39 // Create a new KV store instance for this domain
40 kvStore = store.NewKVStore(currentAddress)
41
42 // Initialize the initializers map to store implementation registration functions
43 versionManager = version_manager.NewVersionManager(
44 domainPath,
45 kvStore,
46 initializeDomainStore,
47 )
48
49 implementation = nil
50}
51
52func initializeDomainStore(_ int, rlm realm, kvStore store.KVStore) any {
53 return NewPoolStore(kvStore)
54}
55
56// getImplementation returns the currently active pool implementation.
57// This function is used by all proxy functions to route calls to the active implementation.
58// If no implementation is set, it panics to prevent invalid state.
59func getImplementation() IPool {
60 if implementation == nil {
61 panic("implementation is not initialized")
62 }
63
64 return implementation
65}
66
67func updateImplementation() error {
68 result := versionManager.GetCurrentImplementation()
69 if result == nil {
70 return errors.New("implementation is not initialized")
71 }
72
73 impl, ok := result.(IPool)
74 if !ok {
75 return errors.New("impl is not an IPool")
76 }
77
78 implementation = impl
79
80 return nil
81}