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

2.54 Kb · 75 lines
 1package launchpad
 2
 3import (
 4	"gno.land/r/gnoswap/access/v1"
 5)
 6
 7// RegisterInitializer registers a new launchpad 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 launchpadStore 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.
16//
17// Parameters:
18//   - cur: current realm context; callers use cross(cur) when crossing into this realm.
19//   - initializer: callback that receives the implementation realm context and launchpad store, then returns the version's ILaunchpad implementation; its leading integer discriminator is invoked as 0.
20func RegisterInitializer(cur realm, initializer func(_ int, rlm realm, launchpadStore ILaunchpadStore) ILaunchpad) {
21	initializerFunc := func(_ int, rlm realm, domainStore any) any {
22		access.AssertIsRlmCurrent(0, rlm)
23
24		currentLaunchpadStore, ok := domainStore.(ILaunchpadStore)
25		if !ok {
26			panic("domainStore is not an ILaunchpadStore")
27		}
28
29		return initializer(0, rlm, currentLaunchpadStore)
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 launchpad 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.
49//
50// Parameters:
51//   - cur: current realm context; callers use cross(cur) when crossing into this realm.
52//   - packagePath: registered package path of the launchpad implementation to activate.
53func UpgradeImpl(cur realm, packagePath string) {
54	// Ensure only admin or governance can perform upgrades
55	caller := cur.Previous().Address()
56	access.AssertIsAdminOrGovernance(caller)
57
58	err := versionManager.ChangeImplementation(0, cur, packagePath)
59	if err != nil {
60		panic(err)
61	}
62
63	err = updateImplementation()
64	if err != nil {
65		panic(err)
66	}
67}
68
69// GetImplementationPackagePath returns the package path of the currently active implementation.
70//
71// Returns:
72//   - packagePath: package path of the active implementation
73func GetImplementationPackagePath() string {
74	return versionManager.GetCurrentPackagePath()
75}