upgrade.gno
1.67 Kb · 60 lines
1package common
2
3import (
4 "gno.land/r/gnoswap/access/v1"
5)
6
7// RegisterInitializer registers the versioned common implementation initializer
8// and activates the implementation returned by it.
9//
10// Parameters:
11// - cur: Current realm context; callers use cross(cur) when crossing into this realm.
12// - initializer: callback invoked with discriminator 0 and the propagated
13// implementation realm, returning the version's ICommon implementation
14func RegisterInitializer(cur realm, initializer func(_ int, rlm realm) ICommon) {
15 initializerFunc := func(_ int, rlm realm, _ any) any {
16 access.AssertIsRlmCurrent(0, rlm)
17
18 return initializer(0, rlm)
19 }
20
21 err := versionManager.RegisterInitializer(0, cur, initializerFunc)
22 if err != nil {
23 panic(err)
24 }
25
26 err = updateImplementation()
27 if err != nil {
28 panic(err)
29 }
30}
31
32// UpgradeImpl changes the active common implementation package and refreshes
33// the cached implementation.
34//
35// Parameters:
36// - cur: Current realm context; callers use cross(cur) when crossing into this realm.
37// - packagePath: fully qualified package path of the replacement implementation
38func UpgradeImpl(cur realm, packagePath string) {
39 prev := cur.Previous()
40 access.AssertIsAdminOrGovernance(prev.Address())
41
42 err := versionManager.ChangeImplementation(0, cur, packagePath)
43 if err != nil {
44 panic(err)
45 }
46
47 err = updateImplementation()
48 if err != nil {
49 panic(err)
50 }
51}
52
53// GetImplementationPackagePath returns the package path of the currently
54// selected common implementation.
55//
56// Returns:
57// - packagePath: fully qualified current implementation package path
58func GetImplementationPackagePath() string {
59 return versionManager.GetCurrentPackagePath()
60}