upgrade.gno
1.49 Kb ยท 54 lines
1package governance
2
3import (
4 "chain/runtime"
5
6 "gno.land/r/gnoswap/access"
7)
8
9func RegisterInitializer(cur realm, initializer func(governanceStore IGovernanceStore, stakerAccessor GovStakerAccessor) IGovernance) {
10 initializerFunc := func(domainStore any) any {
11 currentGovernanceStore, ok := domainStore.(IGovernanceStore)
12 if !ok {
13 panic("domainStore is not an IGovernanceStore")
14 }
15 return initializer(currentGovernanceStore, newGovStakerAccessor())
16 }
17
18 err := versionManager.RegisterInitializer(initializerFunc)
19 if err != nil {
20 panic(err)
21 }
22
23 err = updateImplementation()
24 if err != nil {
25 panic(err)
26 }
27}
28
29// UpgradeImpl switches the active governance implementation to a different version.
30// This function allows seamless upgrades from one version to another without
31// data migration or downtime.
32//
33// Security: Only admin or governance can perform upgrades.
34// The new implementation must have been previously registered via RegisterInitializer.
35func UpgradeImpl(cur realm, targetPackagePath string) {
36 // Ensure only admin or governance can perform upgrades
37 caller := runtime.PreviousRealm().Address()
38 access.AssertIsAdminOrGovernance(caller)
39
40 err := versionManager.ChangeImplementation(targetPackagePath)
41 if err != nil {
42 panic(err)
43 }
44
45 err = updateImplementation()
46 if err != nil {
47 panic(err)
48 }
49}
50
51// GetImplementationPackagePath returns the package path of the currently active implementation.
52func GetImplementationPackagePath() string {
53 return versionManager.GetCurrentPackagePath()
54}