migration.gno
4.36 Kb · 181 lines
1package chunk
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 ListLimit int
18 BatchLimit int
19 Frozen bool
20 TokenCount int64
21 WorldCount int
22 MetadataWorlds int
23 OwnerWorlds int
24 VerifierWorlds int
25 PermissionCount int
26 RoleCount int
27 GrantableCount int
28 AuthzGrantCount int
29 WorldMasterCount int
30}
31
32func GetMigrationConfig() MigrationConfig {
33 assertMigrationStateAvailable()
34 return MigrationConfig{
35 ListLimit: listLimit,
36 BatchLimit: batchLimit,
37 Frozen: frozen,
38 TokenCount: nftStore.TokenCount(),
39 WorldCount: worldStore.Total(),
40 MetadataWorlds: nftStore.MetadataWorldSize(),
41 OwnerWorlds: nftStore.OwnerWorldSize(),
42 VerifierWorlds: verifierStore.Verifiers().Size(),
43 PermissionCount: chunkAuthzRBAC.PermissionSize(),
44 RoleCount: chunkAuthzRBAC.RoleSize(),
45 GrantableCount: chunkAuthzRBAC.GrantSize(),
46 AuthzGrantCount: chunkAuthzRBAC.AssignmentSize(),
47 WorldMasterCount: worldAuthzRBAC.AssignmentSize(),
48 }
49}
50
51func GetMigrationWorldStore(cur realm) *WorldStore {
52 assertCanExposeMigrationStore(0, cur)
53 return worldStore
54}
55
56func GetMigrationWorldCreateValidator(cur realm) *validate.Validator {
57 assertCanExposeMigrationStore(0, cur)
58 return worldCreateValidator
59}
60
61func GetMigrationWorldUpdateValidator(cur realm) *validate.Validator {
62 assertCanExposeMigrationStore(0, cur)
63 return worldUpdateValidator
64}
65
66func GetMigrationNFTStore(cur realm) *NFTStore {
67 assertCanExposeMigrationStore(0, cur)
68 return nftStore
69}
70
71func GetMigrationVerifierStore(cur realm) *VerifierStore {
72 assertCanExposeMigrationStore(0, cur)
73 return verifierStore
74}
75
76func GetMigrationChunkAuthzRBAC(cur realm) *rbac.RBAC {
77 assertCanExposeMigrationStore(0, cur)
78 return chunkAuthzRBAC
79}
80
81func GetMigrationWorldAuthzRBAC(cur realm) *rbac.RBAC {
82 assertCanExposeMigrationStore(0, cur)
83 return worldAuthzRBAC
84}
85
86func SetMigrationMigrator(cur realm, pkgPath string) {
87 accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
88 assertMigrationStateAvailable()
89 if pkgPath == "" {
90 panic("migration migrator is required")
91 }
92 migrationMigratorPkgPath = pkgPath
93}
94
95func ClearMigrationMigrator(cur realm) {
96 accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
97 assertMigrationStateAvailable()
98 migrationMigratorPkgPath = ""
99}
100
101func SetMigrationExportCompleted(cur realm, completed bool) {
102 accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
103 assertMigrationStateAvailable()
104 migrationExportCompleted = completed
105}
106
107func IsMigrationExportCompleted() bool {
108 return migrationExportCompleted
109}
110
111func IsMigrationStateCleaned() bool {
112 return migrationStateCleaned
113}
114
115func CleanupMigrationState(cur realm) {
116 assertCanCleanupMigrationState(0, cur)
117
118 worldStore = nil
119 nftStore = nil
120 verifierStore = nil
121 chunkAuthzRBAC = nil
122 worldAuthzRBAC = nil
123
124 migrationStateCleaned = true
125}
126
127func assertCanExposeMigrationStore(_ int, rlm realm) {
128 accesscontrol.AssertCurrentRealm(0, rlm)
129 accesscontrol.AssertIsAdminOrigin(admin.IsAdmin)
130 assertMigrationMigratorConfigured()
131 assertMigrationMigratorCodeRealm(0, rlm)
132 assertMigrationMigratorRealm(0, rlm)
133 assertCanExposeMigrationChunkStore()
134}
135
136func assertMigrationMigratorConfigured() {
137 if migrationMigratorPkgPath == "" {
138 panic("migration migrator not set")
139 }
140}
141
142func assertMigrationMigratorCodeRealm(_ int, rlm realm) {
143 if rlm.Previous().PkgPath() == "" {
144 panic("migration migrator must be code realm")
145 }
146}
147
148func assertMigrationMigratorRealm(_ int, rlm realm) {
149 if rlm.Previous().PkgPath() != migrationMigratorPkgPath {
150 panic("migration migrator access required")
151 }
152}
153
154func assertMigrationStateAvailable() {
155 if migrationStateCleaned {
156 panic("migration state cleaned")
157 }
158}
159
160func assertCanCleanupMigrationState(_ int, rlm realm) {
161 accesscontrol.AssertIsAdmin(0, rlm, admin.IsAdmin)
162 if !frozen {
163 panic("contract must be frozen")
164 }
165 if !migrationExportCompleted {
166 panic("migration not completed")
167 }
168 if migrationStateCleaned {
169 panic("migration state already cleaned")
170 }
171}
172
173func assertCanExposeMigrationChunkStore() {
174 assertMigrationStateAvailable()
175 if !frozen {
176 panic("contract must be frozen")
177 }
178 if migrationExportCompleted {
179 panic("migration already completed")
180 }
181}