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