upgrade.gno
1.78 Kb · 68 lines
1package dca
2
3import (
4 pdca "gno.land/p/gnoswap/order/dca/v0"
5
6 "gno.land/r/gnoswap/access/v1"
7)
8
9// RegisterInitializer registers an implementation version.
10// Each version calls it from init with a factory that receives the shared store.
11// Only packages under this realm's path may register,
12// and the first registration becomes active.
13// A nil initializer is rejected here,
14// instead of panicking later when the version is activated.
15func RegisterInitializer(cur realm, initializer func(_ int, rlm realm, dcaStore pdca.IDCAOrderStore) pdca.IDCAOrder) {
16 if initializer == nil {
17 panic(ErrNilInitializer)
18 }
19
20 initializerFunc := func(_ int, rlm realm, domainStore any) any {
21 access.AssertIsRlmCurrent(0, rlm)
22
23 currentStore, ok := domainStore.(pdca.IDCAOrderStore)
24 if !ok {
25 panic(errInvalidDomainStore)
26 }
27
28 return initializer(0, rlm, currentStore)
29 }
30
31 err := versionManager.RegisterInitializer(0, cur, initializerFunc)
32 if err != nil {
33 panic(err.Error())
34 }
35
36 err = updateImplementation()
37 if err != nil {
38 panic(err.Error())
39 }
40}
41
42// UpgradeImpl activates a previously registered version by package path.
43// Admin or governance only.
44func UpgradeImpl(cur realm, packagePath string) {
45 caller := cur.Previous().Address()
46 access.AssertIsAdminOrGovernance(caller)
47
48 // The running version still writes the accumulator after its swap,
49 // so never switch during a round.
50 if NewDCAOrderStore(kvStore).IsExecuting() {
51 panic(ErrUpgradeWhileExecuting)
52 }
53
54 err := versionManager.ChangeImplementation(0, cur, packagePath)
55 if err != nil {
56 panic(err.Error())
57 }
58
59 err = updateImplementation()
60 if err != nil {
61 panic(err.Error())
62 }
63}
64
65// GetImplementationPackagePath returns the active implementation path.
66func GetImplementationPackagePath() string {
67 return versionManager.GetCurrentPackagePath()
68}