upgrade.gno
2.01 Kb · 68 lines
1package pool
2
3import (
4 "gno.land/r/gnoswap/access"
5)
6
7// RegisterInitializer registers a new pool implementation version.
8// This function is called by each version (v1, v2, etc.) during initialization
9// to register their implementation with the proxy system.
10//
11// The initializer function creates a new instance of the implementation
12// using the provided poolStore interface.
13//
14// The stateInitializer function creates the initial state for this version.
15//
16// Security: Only contracts within the domain path can register initializers.
17// Each package path can only register once to prevent duplicate registrations.
18func RegisterInitializer(cur realm, initializer func(_ int, rlm realm, poolStore IPoolStore) IPool) {
19 initializerFunc := func(_ int, rlm realm, domainStore any) any {
20 if !rlm.IsCurrent() {
21 panic(ErrSpoofedRealm)
22 }
23
24 currentPoolStore, ok := domainStore.(IPoolStore)
25 if !ok {
26 panic("domainStore is not an IPoolStore")
27 }
28
29 return initializer(0, rlm, currentPoolStore)
30 }
31
32 err := versionManager.RegisterInitializer(0, cur, initializerFunc)
33 if err != nil {
34 panic(err)
35 }
36
37 err = updateImplementation()
38 if err != nil {
39 panic(err)
40 }
41}
42
43// UpgradeImpl switches the active pool implementation to a different version.
44// This function allows seamless upgrades from one version to another without
45// data migration or downtime.
46//
47// Security: Only admin or governance can perform upgrades.
48// The new implementation must have been previously registered via RegisterInitializer.
49func UpgradeImpl(cur realm, packagePath string) {
50 // Ensure only admin or governance can perform upgrades
51 caller := cur.Previous().Address()
52 access.AssertIsAdminOrGovernance(caller)
53
54 err := versionManager.ChangeImplementation(0, cur, packagePath)
55 if err != nil {
56 panic(err)
57 }
58
59 err = updateImplementation()
60 if err != nil {
61 panic(err)
62 }
63}
64
65// GetImplementationPackagePath returns the package path of the currently active implementation.
66func GetImplementationPackagePath() string {
67 return versionManager.GetCurrentPackagePath()
68}