Search Apps Documentation Source Content File Folder Download Copy Actions Download

migration.gno

4.74 Kb · 182 lines
  1package block
  2
  3import (
  4	"gno.land/p/akkadia/v0/accesscontrol"
  5	"gno.land/p/akkadia/v0/validate"
  6	"gno.land/r/akkadia/v0/admin"
  7)
  8
  9var (
 10	migrationMigratorPkgPath string
 11	migrationExportCompleted bool
 12	migrationStateCleaned    bool
 13)
 14
 15type MigrationConfig struct {
 16	NextBlockID           uint32
 17	SystemBlockLimit      uint32
 18	MaxSystemBlockID      uint32
 19	NextLogID             uint64
 20	CreatorBPS            int
 21	ListLimit             int
 22	Frozen                bool
 23	BlockCount            int
 24	BlockNameIndexCount   int
 25	SupplyCount           int
 26	UserInventoryCount    int
 27	MintAllowlistCount    int
 28	AcrRewardCount        int
 29	PersonalInstalledSize int
 30	SystemInstalledSize   int
 31	UseLogCount           int
 32	UserLogIndexCount     int
 33}
 34
 35func GetMigrationConfig() MigrationConfig {
 36	assertMigrationStateAvailable()
 37	return MigrationConfig{
 38		NextBlockID:           blockStore.NextID(),
 39		SystemBlockLimit:      blockStore.SystemBlockLimit(),
 40		MaxSystemBlockID:      blockStore.MaxSystemBlockID(),
 41		NextLogID:             installedBlockStore.NextLogID(),
 42		CreatorBPS:            creatorBPS,
 43		ListLimit:             listLimit,
 44		Frozen:                frozen,
 45		BlockCount:            blockStore.Total(),
 46		BlockNameIndexCount:   blockStore.NameIndexTotal(),
 47		SupplyCount:           mintedBlockStore.SupplyTotal(),
 48		UserInventoryCount:    mintedBlockStore.UserInventoryTotal(),
 49		MintAllowlistCount:    mintedBlockStore.MintAllowlistTotal(),
 50		AcrRewardCount:        blockStore.AcrRewardTotal(),
 51		PersonalInstalledSize: installedBlockStore.PersonalInstalledTotal(),
 52		SystemInstalledSize:   installedBlockStore.SystemInstalledTotal(),
 53		UseLogCount:           installedBlockStore.UseLogTotal(),
 54		UserLogIndexCount:     installedBlockStore.UserLogIndexTotal(),
 55	}
 56}
 57
 58func GetMigrationBlockStore(cur realm) *BlockStore {
 59	assertCanExposeMigrationStore(0, cur)
 60	return blockStore
 61}
 62
 63func GetMigrationInstalledBlockStore(cur realm) *InstalledBlockStore {
 64	assertCanExposeMigrationStore(0, cur)
 65	return installedBlockStore
 66}
 67
 68func GetMigrationMintedBlockStore(cur realm) *MintedBlockStore {
 69	assertCanExposeMigrationStore(0, cur)
 70	return mintedBlockStore
 71}
 72
 73func GetMigrationCreateValidator(cur realm) *validate.Validator {
 74	assertCanExposeMigrationStore(0, cur)
 75	return createValidator
 76}
 77
 78func GetMigrationUpdateValidator(cur realm) *validate.Validator {
 79	assertCanExposeMigrationStore(0, cur)
 80	return updateValidator
 81}
 82
 83func GetMigrationInstallValidator(cur realm) *validate.Validator {
 84	assertCanExposeMigrationStore(0, cur)
 85	return installValidator
 86}
 87
 88func SetMigrationMigrator(cur realm, pkgPath string) {
 89	accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
 90	if pkgPath == "" {
 91		panic("migration migrator is required")
 92	}
 93	migrationMigratorPkgPath = pkgPath
 94}
 95
 96func ClearMigrationMigrator(cur realm) {
 97	accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
 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	blockStore = nil
119	installedBlockStore = nil
120	mintedBlockStore = nil
121	createValidator = nil
122	updateValidator = nil
123	installValidator = nil
124
125	migrationStateCleaned = true
126}
127
128func assertCanExposeMigrationStore(_ int, rlm realm) {
129	accesscontrol.AssertCurrentRealm(0, rlm)
130	accesscontrol.AssertIsAdminOrigin(admin.IsAdmin)
131	assertMigrationMigratorConfigured()
132	assertMigrationMigratorCodeRealm(0, rlm)
133	assertMigrationMigratorRealm(0, rlm)
134	assertCanExposeMigrationStoreAvailable()
135}
136
137func assertMigrationMigratorConfigured() {
138	if migrationMigratorPkgPath == "" {
139		panic("migration migrator not set")
140	}
141}
142
143func assertMigrationMigratorCodeRealm(_ int, rlm realm) {
144	if rlm.Previous().PkgPath() == "" {
145		panic("migration migrator must be code realm")
146	}
147}
148
149func assertMigrationMigratorRealm(_ int, rlm realm) {
150	if rlm.Previous().PkgPath() != migrationMigratorPkgPath {
151		panic("migration migrator access required")
152	}
153}
154
155func assertMigrationStateAvailable() {
156	if migrationStateCleaned {
157		panic("migration state cleaned")
158	}
159}
160
161func assertCanCleanupMigrationState(_ int, rlm realm) {
162	accesscontrol.AssertIsAdmin(0, rlm, admin.IsAdmin)
163	if !frozen {
164		panic("contract must be frozen")
165	}
166	if !migrationExportCompleted {
167		panic("migration not completed")
168	}
169	if migrationStateCleaned {
170		panic("migration state already cleaned")
171	}
172}
173
174func assertCanExposeMigrationStoreAvailable() {
175	assertMigrationStateAvailable()
176	if !frozen {
177		panic("contract must be frozen")
178	}
179	if migrationExportCompleted {
180		panic("migration already completed")
181	}
182}