Search Apps Documentation Source Content File Folder Download Copy Actions Download

migration.gno

3.56 Kb · 152 lines
  1package blueprint
  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	CreationCost    int64
 17	ListLimit       int
 18	BatchLimit      int
 19	Frozen          bool
 20	NextBlueprintID uint32
 21	TotalBlueprints int
 22	TotalVerifiers  int
 23}
 24
 25func GetMigrationConfig() MigrationConfig {
 26	assertMigrationStateAvailable()
 27	return MigrationConfig{
 28		CreationCost:    creationCost,
 29		ListLimit:       listLimit,
 30		BatchLimit:      batchLimit,
 31		Frozen:          frozen,
 32		NextBlueprintID: blueprintStore.NextID(),
 33		TotalBlueprints: blueprintStore.Total(),
 34		TotalVerifiers:  verifierStore.Total(),
 35	}
 36}
 37
 38func GetMigrationBlueprintStore(cur realm) *BlueprintStore {
 39	assertCanExposeMigrationStore(0, cur)
 40	return blueprintStore
 41}
 42
 43func GetMigrationVerifierStore(cur realm) *VerifierStore {
 44	assertCanExposeMigrationStore(0, cur)
 45	return verifierStore
 46}
 47
 48func GetMigrationBlueprintCreateValidator(cur realm) *validate.Validator {
 49	assertCanExposeMigrationStore(0, cur)
 50	return blueprintCreateValidator
 51}
 52
 53func GetMigrationBlueprintUpdateValidator(cur realm) *validate.Validator {
 54	assertCanExposeMigrationStore(0, cur)
 55	return blueprintUpdateValidator
 56}
 57
 58func SetMigrationMigrator(cur realm, pkgPath string) {
 59	accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
 60	assertMigrationStateAvailable()
 61	if pkgPath == "" {
 62		panic("migration migrator is required")
 63	}
 64	migrationMigratorPkgPath = pkgPath
 65}
 66
 67func ClearMigrationMigrator(cur realm) {
 68	accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
 69	assertMigrationStateAvailable()
 70	migrationMigratorPkgPath = ""
 71}
 72
 73func SetMigrationExportCompleted(cur realm, completed bool) {
 74	accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
 75	assertMigrationStateAvailable()
 76	migrationExportCompleted = completed
 77}
 78
 79func IsMigrationExportCompleted() bool {
 80	return migrationExportCompleted
 81}
 82
 83func IsMigrationStateCleaned() bool {
 84	return migrationStateCleaned
 85}
 86
 87func CleanupMigrationState(cur realm) {
 88	assertCanCleanupMigrationState(0, cur)
 89
 90	blueprintStore = nil
 91	verifierStore = nil
 92	blueprintCreateValidator = nil
 93	blueprintUpdateValidator = nil
 94
 95	migrationStateCleaned = true
 96}
 97
 98func assertCanExposeMigrationStore(_ int, rlm realm) {
 99	accesscontrol.AssertCurrentRealm(0, rlm)
100	accesscontrol.AssertIsAdminOrigin(admin.IsAdmin)
101	assertMigrationMigratorConfigured()
102	assertMigrationMigratorCodeRealm(0, rlm)
103	assertMigrationMigratorRealm(0, rlm)
104	assertCanExposeMigrationStores()
105}
106
107func assertMigrationMigratorConfigured() {
108	if migrationMigratorPkgPath == "" {
109		panic("migration migrator not set")
110	}
111}
112
113func assertMigrationMigratorCodeRealm(_ int, rlm realm) {
114	if rlm.Previous().PkgPath() == "" {
115		panic("migration migrator must be code realm")
116	}
117}
118
119func assertMigrationMigratorRealm(_ int, rlm realm) {
120	if rlm.Previous().PkgPath() != migrationMigratorPkgPath {
121		panic("migration migrator access required")
122	}
123}
124
125func assertMigrationStateAvailable() {
126	if migrationStateCleaned {
127		panic("migration state cleaned")
128	}
129}
130
131func assertCanCleanupMigrationState(_ int, rlm realm) {
132	accesscontrol.AssertIsAdmin(0, rlm, admin.IsAdmin)
133	if !frozen {
134		panic("contract must be frozen")
135	}
136	if !migrationExportCompleted {
137		panic("migration not completed")
138	}
139	if migrationStateCleaned {
140		panic("migration state already cleaned")
141	}
142}
143
144func assertCanExposeMigrationStores() {
145	assertMigrationStateAvailable()
146	if !frozen {
147		panic("contract must be frozen")
148	}
149	if migrationExportCompleted {
150		panic("migration already completed")
151	}
152}