Search Apps Documentation Source Content File Folder Download Copy Actions Download

upgrade.gno

2.01 Kb ยท 64 lines
 1package staker
 2
 3import (
 4	"chain/runtime"
 5
 6	"gno.land/r/gnoswap/access"
 7)
 8
 9// RegisterInitializer registers a new staker implementation version.
10// This function is called by each version (v1, v2, etc.) during initialization
11// to register their implementation with the proxy system.
12//
13// The initializer function creates a new instance of the implementation
14// using the provided stakerStore interface.
15//
16// Security: Only contracts within the domain path can register initializers.
17// Each package path can only register once to prevent duplicate registrations.
18func RegisterInitializer(cur realm, initializer func(stakerStore IStakerStore, poolAccessor PoolAccessor, emissionAccessor EmissionAccessor, nftAccessor NFTAccessor) IStaker) {
19	initializerFunc := func(domainStore any) any {
20		currentStakerStore, ok := domainStore.(IStakerStore)
21		if !ok {
22			panic("domainStore is not an IStakerStore")
23		}
24
25		return initializer(currentStakerStore, newPoolAccessor(), newEmissionAccessor(), newNFTAccessor())
26	}
27
28	err := versionManager.RegisterInitializer(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 := runtime.PreviousRealm().Address()
48	access.AssertIsAdminOrGovernance(caller)
49
50	err := versionManager.ChangeImplementation(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}