migration.gno
4.28 Kb · 180 lines
1package personal_world
2
3import (
4 "gno.land/p/akkadia/v0/accesscontrol"
5 "gno.land/p/akkadia/v0/rbac"
6 "gno.land/p/akkadia/v0/validate"
7 "gno.land/r/akkadia/v0/admin"
8)
9
10var (
11 migrationMigratorPkgPath string
12 migrationExportCompleted bool
13 migrationStateCleaned bool
14)
15
16type MigrationConfig struct {
17 FeeCollectorBPS int
18 ListLimit int
19 BatchLimit int
20 Frozen bool
21 NextWorldID uint32
22 TotalWorlds int
23 TotalBiomes int
24 TotalSizes int
25}
26
27func GetMigrationConfig() MigrationConfig {
28 assertMigrationStateAvailable()
29 worlds := worldStore
30 return MigrationConfig{
31 FeeCollectorBPS: feeCollectorBPS,
32 ListLimit: listLimit,
33 BatchLimit: batchLimit,
34 Frozen: frozen,
35 NextWorldID: worlds.NextID(),
36 TotalWorlds: worlds.Total(),
37 TotalBiomes: worldConfigStore.TotalBiomes(),
38 TotalSizes: worldConfigStore.TotalSizes(),
39 }
40}
41
42func GetMigrationWorldStore(cur realm) *WorldStore {
43 assertCanExposeMigrationStore(0, cur)
44 return worldStore
45}
46
47func GetMigrationWorldConfigStore(cur realm) *WorldConfigStore {
48 assertCanExposeMigrationStore(0, cur)
49 return worldConfigStore
50}
51
52func GetMigrationVerifierStore(cur realm) *VerifierStore {
53 assertCanExposeMigrationStore(0, cur)
54 return verifierStore
55}
56
57func GetMigrationPersonalWorldAuthzRBAC(cur realm) *rbac.RBAC {
58 assertCanExposeMigrationStore(0, cur)
59 return personalWorldAuthzRBAC
60}
61
62func GetMigrationWorldCreateValidator(cur realm) *validate.Validator {
63 assertCanExposeMigrationStore(0, cur)
64 return worldCreateValidator
65}
66
67func GetMigrationWorldUpdateValidator(cur realm) *validate.Validator {
68 assertCanExposeMigrationStore(0, cur)
69 return worldUpdateValidator
70}
71
72func GetMigrationSizeInfoValidator(cur realm) *validate.Validator {
73 assertCanExposeMigrationStore(0, cur)
74 return sizeInfoValidator
75}
76
77func GetMigrationBiomeInfoValidator(cur realm) *validate.Validator {
78 assertCanExposeMigrationStore(0, cur)
79 return biomeInfoValidator
80}
81
82func SetMigrationMigrator(cur realm, pkgPath string) {
83 accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
84 assertMigrationStateAvailable()
85 if pkgPath == "" {
86 panic("migration migrator is required")
87 }
88 migrationMigratorPkgPath = pkgPath
89}
90
91func ClearMigrationMigrator(cur realm) {
92 accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
93 assertMigrationStateAvailable()
94 migrationMigratorPkgPath = ""
95}
96
97func SetMigrationExportCompleted(cur realm, completed bool) {
98 accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
99 assertMigrationStateAvailable()
100 migrationExportCompleted = completed
101}
102
103func IsMigrationExportCompleted() bool {
104 return migrationExportCompleted
105}
106
107func IsMigrationStateCleaned() bool {
108 return migrationStateCleaned
109}
110
111func CleanupMigrationState(cur realm) {
112 assertCanCleanupMigrationState(0, cur)
113
114 personalWorldAuthzRBAC = nil
115 worldStore = nil
116 worldConfigStore = nil
117 verifierStore = nil
118 worldCreateValidator = nil
119 worldUpdateValidator = nil
120 sizeInfoValidator = nil
121 biomeInfoValidator = nil
122
123 migrationStateCleaned = true
124}
125
126func assertCanExposeMigrationStore(_ int, rlm realm) {
127 accesscontrol.AssertCurrentRealm(0, rlm)
128 accesscontrol.AssertIsAdminOrigin(admin.IsAdmin)
129 assertMigrationMigratorConfigured()
130 assertMigrationMigratorCodeRealm(0, rlm)
131 assertMigrationMigratorRealm(0, rlm)
132 assertCanExposeMigrationStores()
133}
134
135func assertMigrationMigratorConfigured() {
136 if migrationMigratorPkgPath == "" {
137 panic("migration migrator not set")
138 }
139}
140
141func assertMigrationMigratorCodeRealm(_ int, rlm realm) {
142 if rlm.Previous().PkgPath() == "" {
143 panic("migration migrator must be code realm")
144 }
145}
146
147func assertMigrationMigratorRealm(_ int, rlm realm) {
148 if rlm.Previous().PkgPath() != migrationMigratorPkgPath {
149 panic("migration migrator access required")
150 }
151}
152
153func assertMigrationStateAvailable() {
154 if migrationStateCleaned {
155 panic("migration state cleaned")
156 }
157}
158
159func assertCanCleanupMigrationState(_ int, rlm realm) {
160 accesscontrol.AssertIsAdmin(0, rlm, admin.IsAdmin)
161 if !frozen {
162 panic("contract must be frozen")
163 }
164 if !migrationExportCompleted {
165 panic("migration not completed")
166 }
167 if migrationStateCleaned {
168 panic("migration state already cleaned")
169 }
170}
171
172func assertCanExposeMigrationStores() {
173 assertMigrationStateAvailable()
174 if !frozen {
175 panic("contract must be frozen")
176 }
177 if migrationExportCompleted {
178 panic("migration already completed")
179 }
180}