migration.gno
3.42 Kb · 138 lines
1package acr
2
3import (
4 "gno.land/p/akkadia/v0/accesscontrol"
5 "gno.land/r/akkadia/v0/admin"
6)
7
8var (
9 migrationMigratorPkgPath string
10 migrationExportCompleted bool
11 migrationStateCleaned bool
12)
13
14type MigrationConfig struct {
15 ListLimit int
16 Frozen bool
17 TotalSupply int64
18 KnownAccounts int
19 BalanceIndexCount int
20 MintIndexCount int
21 UserMintTotalCount int
22 ProcessedRequestCount int
23 HofCategoryCount int
24}
25
26func GetMigrationConfig() MigrationConfig {
27 assertMigrationStateAvailable()
28 return MigrationConfig{
29 ListLimit: listLimit,
30 Frozen: frozen,
31 TotalSupply: acrStore.TotalSupply(),
32 KnownAccounts: acrStore.Token().KnownAccounts(),
33 BalanceIndexCount: acrStore.BalanceIndexCount(),
34 MintIndexCount: acrStore.MintIndexCount(),
35 UserMintTotalCount: acrStore.UserMintTotalCount(),
36 ProcessedRequestCount: acrStore.ProcessedRequestCount(),
37 HofCategoryCount: acrStore.HofCategoryCount(),
38 }
39}
40
41func GetMigrationACRStore(cur realm) *ACRStore {
42 assertCanExposeMigrationStore(0, cur)
43 return acrStore
44}
45
46func SetMigrationMigrator(cur realm, pkgPath string) {
47 accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
48 assertMigrationStateAvailable()
49 if pkgPath == "" {
50 panic("migration migrator is required")
51 }
52 migrationMigratorPkgPath = pkgPath
53}
54
55func ClearMigrationMigrator(cur realm) {
56 accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
57 // Clearing is intentionally allowed after migration cleanup so the source realm
58 // can remove the temporary authorized migrator pointer as the final operation.
59 migrationMigratorPkgPath = ""
60}
61
62func SetMigrationExportCompleted(cur realm, completed bool) {
63 accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
64 assertMigrationStateAvailable()
65 migrationExportCompleted = completed
66}
67
68func IsMigrationExportCompleted() bool {
69 return migrationExportCompleted
70}
71
72func IsMigrationStateCleaned() bool {
73 return migrationStateCleaned
74}
75
76func CleanupMigrationState(cur realm) {
77 assertCanCleanupMigrationState(0, cur)
78
79 acrStore = nil
80
81 migrationStateCleaned = true
82}
83
84func assertCanExposeMigrationStore(_ int, rlm realm) {
85 accesscontrol.AssertCurrentRealm(0, rlm)
86 accesscontrol.AssertIsAdminOrigin(admin.IsAdmin)
87 assertMigrationMigratorConfigured()
88 assertMigrationMigratorCodeRealm(0, rlm)
89 assertMigrationMigratorRealm(0, rlm)
90 assertCanExposeMigrationACRStore()
91}
92
93func assertMigrationMigratorConfigured() {
94 if migrationMigratorPkgPath == "" {
95 panic("migration migrator not set")
96 }
97}
98
99func assertMigrationMigratorCodeRealm(_ int, rlm realm) {
100 if rlm.Previous().PkgPath() == "" {
101 panic("migration migrator must be code realm")
102 }
103}
104
105func assertMigrationMigratorRealm(_ int, rlm realm) {
106 if rlm.Previous().PkgPath() != migrationMigratorPkgPath {
107 panic("migration migrator access required")
108 }
109}
110
111func assertMigrationStateAvailable() {
112 if migrationStateCleaned {
113 panic("migration state cleaned")
114 }
115}
116
117func assertCanCleanupMigrationState(_ int, rlm realm) {
118 accesscontrol.AssertIsAdmin(0, rlm, admin.IsAdmin)
119 if !frozen {
120 panic("contract must be frozen")
121 }
122 if !migrationExportCompleted {
123 panic("migration not completed")
124 }
125 if migrationStateCleaned {
126 panic("migration state already cleaned")
127 }
128}
129
130func assertCanExposeMigrationACRStore() {
131 assertMigrationStateAvailable()
132 if !frozen {
133 panic("contract must be frozen")
134 }
135 if migrationExportCompleted {
136 panic("migration already completed")
137 }
138}