Search Apps Documentation Source Content File Folder Download Copy Actions Download

upgrade.gno

1.95 Kb · 62 lines
 1package staker
 2
 3import (
 4	"gno.land/r/gnoswap/access"
 5)
 6
 7// RegisterInitializer registers an implementation.
 8// This function is called by each implementation version during init.
 9//
10// The initializer function creates a new instance of the implementation
11// using the provided IGovStakerStore. It receives a realm value that
12// resolves to the gov/staker proxy realm — the only address with write
13// permission on the shared KV store — so any per-version store
14// bootstrapping performed inside the initializer passes the proxy's
15// authorization check.
16func RegisterInitializer(cur realm, initializer func(_ int, rlm realm, govStakerStore IGovStakerStore) IGovStaker) {
17	// `cur` captured here is the gov/staker crossing frame. The wrapping
18	// closure forwards it to the v1 initializer so any store writes the
19	// initializer performs run under the proxy's identity rather than the
20	// version package's, which would fail the kvStore ACL.
21	initializerFunc := func(_ int, rlm realm, domainStore any) any {
22		currentGovStakerStore, ok := domainStore.(IGovStakerStore)
23		if !ok {
24			panic("domainStore is not an IGovStakerStore")
25		}
26
27		return initializer(0, rlm, currentGovStakerStore)
28	}
29
30	err := versionManager.RegisterInitializer(0, cur, initializerFunc)
31	if err != nil {
32		panic(err)
33	}
34
35	err = updateImplementation()
36	if err != nil {
37		panic(err)
38	}
39}
40
41// UpgradeImpl upgrades the implementation to a new version
42// Only admin or governance can call this function
43func UpgradeImpl(cur realm, packagePath string) {
44	// Check admin or governance permission
45	prev := cur.Previous()
46	access.AssertIsAdminOrGovernance(prev.Address())
47
48	err := versionManager.ChangeImplementation(0, cur, packagePath)
49	if err != nil {
50		panic(err)
51	}
52
53	err = updateImplementation()
54	if err != nil {
55		panic(err)
56	}
57}
58
59// GetImplementationPackagePath returns the package path of the currently active implementation.
60func GetImplementationPackagePath() string {
61	return versionManager.GetCurrentPackagePath()
62}