upgrade.gno
1.95 Kb · 66 lines
1package router
2
3import (
4 "gno.land/r/gnoswap/access"
5)
6
7// RegisterInitializer registers a new router 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 routerStore interface.
13//
14// Security: Only contracts within the domain path can register initializers.
15// Each package path can only register once to prevent duplicate registrations.
16func RegisterInitializer(cur realm, initializer func(_ int, rlm realm, routerStore IRouterStore) IRouter) {
17 initializerFunc := func(_ int, rlm realm, domainStore any) any {
18 if !rlm.IsCurrent() {
19 return errSpoofedRealm
20 }
21
22 currentRouterStore, ok := domainStore.(IRouterStore)
23 if !ok {
24 panic("domainStore is not an IRouterStore")
25 }
26
27 return initializer(0, rlm, currentRouterStore)
28 }
29
30 err := versionManager.RegisterInitializer(0, cur, initializerFunc)
31 if err != nil {
32 panic(err)
33 }
34
35 err = updateImplementation()
36 if err != nil {
37 panic(err)
38 }
39}
40
41// UpgradeImpl switches the active router implementation to a different version.
42// This function allows seamless upgrades from one version to another without
43// data migration or downtime.
44//
45// Security: Only admin or governance can perform upgrades.
46// The new implementation must have been previously registered via RegisterInitializer.
47func UpgradeImpl(cur realm, packagePath string) {
48 // Ensure only admin or governance can perform upgrades
49 caller := cur.Previous().Address()
50 access.AssertIsAdminOrGovernance(caller)
51
52 err := versionManager.ChangeImplementation(0, cur, packagePath)
53 if err != nil {
54 panic(err)
55 }
56
57 err = updateImplementation()
58 if err != nil {
59 panic(err)
60 }
61}
62
63// GetImplementationPackagePath returns the package path of the currently active implementation.
64func GetImplementationPackagePath() string {
65 return versionManager.GetCurrentPackagePath()
66}