upgrade.gno
2.52 Kb · 74 lines
1package protocol_fee
2
3import (
4 "gno.land/r/gnoswap/access"
5)
6
7// RegisterInitializer registers a new pool implementation version.
8// This function is called by each version (v1, v2, etc.) during initialization
9// to register their implementation with the proxy system.
10//
11// The initializer function creates a new instance of the implementation
12// using the provided protocolFeeStore interface. It receives a realm value
13// that resolves to the protocol_fee proxy realm — the only address with
14// write permission on the shared KV store — so any per-version store
15// bootstrapping performed inside the initializer passes the proxy's
16// authorization check.
17//
18// Security: Only contracts within the domain path can register initializers.
19// Each package path can only register once to prevent duplicate registrations.
20func RegisterInitializer(cur realm, initializer func(_ int, rlm realm, protocolFeeStore IProtocolFeeStore) IProtocolFee) {
21 // `cur` captured here is the protocol_fee crossing frame. The wrapping
22 // closure forwards it to the v1 initializer so any store writes the
23 // initializer performs run under the proxy's identity rather than the
24 // version package's, which would fail the kvStore ACL.
25 initializerFunc := func(_ int, rlm realm, domainStore any) any {
26 if !rlm.IsCurrent() {
27 return errSpoofedRealm
28 }
29
30 currentProtocolFeeStore, ok := domainStore.(IProtocolFeeStore)
31 if !ok {
32 panic("domainStore is not an IProtocolFeeStore")
33 }
34
35 return initializer(0, rlm, currentProtocolFeeStore)
36 }
37
38 err := versionManager.RegisterInitializer(0, cur, initializerFunc)
39 if err != nil {
40 panic(err)
41 }
42
43 err = updateImplementation()
44 if err != nil {
45 panic(err)
46 }
47}
48
49// UpgradeImpl switches the active protocol fee implementation to a different version.
50// This function allows seamless upgrades from one version to another without
51// data migration or downtime.
52//
53// Security: Only admin or governance can perform upgrades.
54// The new implementation must have been previously registered via RegisterInitializer.
55func UpgradeImpl(cur realm, packagePath string) {
56 // Ensure only admin or governance can perform upgrades
57 prev := cur.Previous()
58 access.AssertIsAdminOrGovernance(prev.Address())
59
60 err := versionManager.ChangeImplementation(0, cur, packagePath)
61 if err != nil {
62 panic(err)
63 }
64
65 err = updateImplementation()
66 if err != nil {
67 panic(err)
68 }
69}
70
71// GetImplementationPackagePath returns the package path of the currently active implementation.
72func GetImplementationPackagePath() string {
73 return versionManager.GetCurrentPackagePath()
74}