Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

upgrade.gno

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