Search Apps Documentation Source Content File Folder Download Copy Actions Download

upgrade.gno

2.09 Kb · 64 lines
 1package staker
 2
 3import "gno.land/r/gnoswap/access"
 4
 5// RegisterInitializer registers a new staker implementation version.
 6// This function is called by each version (v1, v2, etc.) during initialization
 7// to register their implementation with the proxy system.
 8//
 9// The initializer function creates a new instance of the implementation
10// using the provided stakerStore interface.
11//
12// Security: Only contracts within the domain path can register initializers.
13// Each package path can only register once to prevent duplicate registrations.
14func RegisterInitializer(cur realm, initializer func(_ int, rlm realm, stakerStore IStakerStore, poolAccessor PoolAccessor, emissionAccessor EmissionAccessor, nftAccessor NFTAccessor) IStaker) {
15	initializerFunc := func(_ int, rlm realm, domainStore any) any {
16		if !rlm.IsCurrent() {
17			panic(ErrSpoofedRealm)
18		}
19
20		currentStakerStore, ok := domainStore.(IStakerStore)
21		if !ok {
22			panic("domainStore is not an IStakerStore")
23		}
24
25		return initializer(0, rlm, currentStakerStore, newPoolAccessor(), newEmissionAccessor(), newNFTAccessor())
26	}
27
28	err := versionManager.RegisterInitializer(0, cur, initializerFunc)
29	if err != nil {
30		panic(err)
31	}
32
33	err = updateImplementation()
34	if err != nil {
35		panic(err)
36	}
37}
38
39// UpgradeImpl switches the active staker implementation to a different version.
40// This function allows seamless upgrades from one version to another without
41// data migration or downtime.
42//
43// Security: Only admin or governance can perform upgrades.
44// The new implementation must have been previously registered via RegisterInitializer.
45func UpgradeImpl(cur realm, packagePath string) {
46	// Ensure only admin or governance can perform upgrades
47	caller := cur.Previous().Address()
48	access.AssertIsAdminOrGovernance(caller)
49
50	err := versionManager.ChangeImplementation(0, cur, packagePath)
51	if err != nil {
52		panic(err)
53	}
54
55	err = updateImplementation()
56	if err != nil {
57		panic(err)
58	}
59}
60
61// GetImplementationPackagePath returns the package path of the currently active implementation.
62func GetImplementationPackagePath() string {
63	return versionManager.GetCurrentPackagePath()
64}