upgrade.gno
2.27 Kb · 70 lines
1package staker
2
3import (
4 "gno.land/r/gnoswap/access/v1"
5)
6
7// RegisterInitializer registers an implementation initializer for this realm.
8// Each implementation version calls it during initialization. The callback
9// receives the current realm context and shared gov/staker store so it can
10// construct the versioned implementation.
11//
12// Parameters:
13// - cur: current realm context; callers use cross(cur) when crossing into this realm
14// - initializer: callback invoked with discriminator 0, validated propagated realm context, and IGovStakerStore, returning the implementation
15func RegisterInitializer(cur realm, initializer func(_ int, rlm realm, govStakerStore IGovStakerStore) IGovStaker) {
16 // `cur` captured here is the gov/staker crossing frame. The wrapping
17 // closure forwards it to the v1 initializer so any store writes the
18 // initializer performs run under the proxy's identity rather than the
19 // version package's, which would fail the kvStore ACL.
20 initializerFunc := func(_ int, rlm realm, domainStore any) any {
21 access.AssertIsRlmCurrent(0, rlm)
22
23 currentGovStakerStore, ok := domainStore.(IGovStakerStore)
24 if !ok {
25 panic("domainStore is not an IGovStakerStore")
26 }
27
28 return initializer(0, rlm, currentGovStakerStore)
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 upgrades the active implementation to packagePath.
43// Only admin or governance can call this function.
44//
45// Parameters:
46// - cur: current realm context; callers use cross(cur) when crossing into this realm
47// - packagePath: package path of the implementation version to activate
48func UpgradeImpl(cur realm, packagePath string) {
49 // Check admin or governance permission
50 prev := cur.Previous()
51 access.AssertIsAdminOrGovernance(prev.Address())
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.
65//
66// Returns:
67// - packagePath: package path of the active implementation
68func GetImplementationPackagePath() string {
69 return versionManager.GetCurrentPackagePath()
70}