upgrade.gno
1.87 Kb · 63 lines
1package router
2
3import (
4 "gno.land/r/gnoswap/access/v1"
5)
6
7// RegisterInitializer registers a new router implementation version.
8// Each implementation package calls it during initialization before it can be selected.
9//
10// Parameters:
11// - cur: current realm context; callers use cross(cur) when crossing into this realm
12// - initializer: callback that receives the propagated realm context and router store, then constructs the IRouter implementation
13func RegisterInitializer(cur realm, initializer func(_ int, rlm realm, routerStore IRouterStore) IRouter) {
14 initializerFunc := func(_ int, rlm realm, domainStore any) any {
15 access.AssertIsRlmCurrent(0, rlm)
16
17 currentRouterStore, ok := domainStore.(IRouterStore)
18 if !ok {
19 panic("domainStore is not an IRouterStore")
20 }
21
22 return initializer(0, rlm, currentRouterStore)
23 }
24
25 err := versionManager.RegisterInitializer(0, cur, initializerFunc)
26 if err != nil {
27 panic(err)
28 }
29
30 err = updateImplementation()
31 if err != nil {
32 panic(err)
33 }
34}
35
36// UpgradeImpl switches the active router implementation to a different registered version.
37//
38// Parameters:
39// - cur: current realm context; callers use cross(cur) when crossing into this realm
40// - packagePath: package path of the previously registered implementation to activate
41func UpgradeImpl(cur realm, packagePath string) {
42 // Ensure only admin or governance can perform upgrades
43 caller := cur.Previous().Address()
44 access.AssertIsAdminOrGovernance(caller)
45
46 err := versionManager.ChangeImplementation(0, cur, packagePath)
47 if err != nil {
48 panic(err)
49 }
50
51 err = updateImplementation()
52 if err != nil {
53 panic(err)
54 }
55}
56
57// GetImplementationPackagePath returns the package path of the currently active implementation.
58//
59// Returns:
60// - packagePath: package path of the active implementation
61func GetImplementationPackagePath() string {
62 return versionManager.GetCurrentPackagePath()
63}