Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve migration #3169

Merged
merged 7 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 59 additions & 31 deletions migrations/account_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,50 +39,72 @@ func NewAccountStorage(storage *runtime.Storage, address common.Address) Account
}
}

type StorageMapKeyMigrator func(
inter *interpreter.Interpreter,
storageKey interpreter.StorageKey,
storageMap *interpreter.StorageMap,
storageMapKey interpreter.StorageMapKey,
)
type StorageMapKeyMigrator interface {
Migrate(
inter *interpreter.Interpreter,
storageKey interpreter.StorageKey,
storageMap *interpreter.StorageMap,
storageMapKey interpreter.StorageMapKey,
)
Domains() map[string]struct{}
}

type ValueConverter func(
storageKey interpreter.StorageKey,
storageMapKey interpreter.StorageMapKey,
value interpreter.Value,
) interpreter.Value

func NewValueConverterPathMigrator(convertValue ValueConverter) StorageMapKeyMigrator {
return func(
inter *interpreter.Interpreter,
storageKey interpreter.StorageKey,
storageMap *interpreter.StorageMap,
storageMapKey interpreter.StorageMapKey,
) {
value := storageMap.ReadValue(nil, storageMapKey)

newValue := convertValue(storageKey, storageMapKey, value)
if newValue != nil {
// If the converter returns a new value,
// then replace the existing value with the new one.
storageMap.SetValue(
inter,
storageMapKey,
newValue,
)
}
type ValueConverterPathMigrator struct {
domains map[string]struct{}
ConvertValue ValueConverter
}

var _ StorageMapKeyMigrator = ValueConverterPathMigrator{}

func NewValueConverterPathMigrator(
domains map[string]struct{},
convertValue ValueConverter,
) StorageMapKeyMigrator {
return ValueConverterPathMigrator{
domains: domains,
ConvertValue: convertValue,
}
}

func (m ValueConverterPathMigrator) Migrate(
inter *interpreter.Interpreter,
storageKey interpreter.StorageKey,
storageMap *interpreter.StorageMap,
storageMapKey interpreter.StorageMapKey,
) {
value := storageMap.ReadValue(nil, storageMapKey)

newValue := m.ConvertValue(storageKey, storageMapKey, value)
if newValue != nil {
// If the converter returns a new value,
// then replace the existing value with the new one.
storageMap.SetValue(
inter,
storageMapKey,
newValue,
)
}
}

func (m ValueConverterPathMigrator) Domains() map[string]struct{} {
return m.domains
}

func (i *AccountStorage) MigrateStringKeys(
inter *interpreter.Interpreter,
key string,
migrate StorageMapKeyMigrator,
migrator StorageMapKeyMigrator,
) {
i.MigrateStorageMap(
inter,
key,
migrate,
migrator,
func(key atree.Value) interpreter.StorageMapKey {
return interpreter.StringStorageMapKey(key.(interpreter.StringAtreeValue))
},
Expand All @@ -92,12 +114,12 @@ func (i *AccountStorage) MigrateStringKeys(
func (i *AccountStorage) MigrateUint64Keys(
inter *interpreter.Interpreter,
key string,
migrate StorageMapKeyMigrator,
migrator StorageMapKeyMigrator,
) {
i.MigrateStorageMap(
inter,
key,
migrate,
migrator,
func(key atree.Value) interpreter.StorageMapKey {
return interpreter.Uint64StorageMapKey(key.(interpreter.Uint64AtreeValue))
},
Expand All @@ -107,11 +129,17 @@ func (i *AccountStorage) MigrateUint64Keys(
func (i *AccountStorage) MigrateStorageMap(
inter *interpreter.Interpreter,
domain string,
migrate StorageMapKeyMigrator,
migrator StorageMapKeyMigrator,
atreeKeyToStorageMapKey func(atree.Value) interpreter.StorageMapKey,
) {
address := i.address

if domains := migrator.Domains(); domains != nil {
if _, ok := domains[domain]; !ok {
return
}
}

storageMap := i.storage.GetStorageMap(address, domain, false)
if storageMap == nil || storageMap.Count() == 0 {
return
Expand All @@ -131,7 +159,7 @@ func (i *AccountStorage) MigrateStorageMap(

for _, storageMapKey := range keys {

migrate(
migrator.Migrate(
inter,
storageKey,
storageMap,
Expand Down
47 changes: 0 additions & 47 deletions migrations/address_iterator.go

This file was deleted.

4 changes: 4 additions & 0 deletions migrations/capcons/capabilitymigration.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func (*CapabilityValueMigration) Name() string {
return "CapabilityValueMigration"
}

func (*CapabilityValueMigration) Domains() map[string]struct{} {
return nil
}

var fullyEntitledAccountReferenceStaticType = interpreter.ConvertSemaReferenceTypeToStaticReferenceType(
nil,
sema.FullyEntitledAccountReferenceType,
Expand Down
9 changes: 9 additions & 0 deletions migrations/capcons/linkmigration.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ func (m *LinkValueMigration) CanSkip(valueType interpreter.StaticType) bool {
return CanSkipCapabilityValueMigration(valueType)
}

var linkValueMigrationDomains = map[string]struct{}{
common.PathDomainPublic.Identifier(): {},
common.PathDomainPrivate.Identifier(): {},
}

func (m *LinkValueMigration) Domains() map[string]struct{} {
return linkValueMigrationDomains
}

func (m *LinkValueMigration) Migrate(
storageKey interpreter.StorageKey,
storageMapKey interpreter.StorageMapKey,
Expand Down
56 changes: 14 additions & 42 deletions migrations/capcons/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,8 @@ func testPathCapabilityValueMigration(

reporter := &testMigrationReporter{}

migration.Migrate(
&migrations.AddressSliceIterator{
Addresses: []common.Address{
testAddress,
},
},
migration.MigrateAccount(
testAddress,
migration.NewValueMigrationsPathMigrator(
reporter,
&LinkValueMigration{
Expand All @@ -476,12 +472,8 @@ func testPathCapabilityValueMigration(
),
)

migration.Migrate(
&migrations.AddressSliceIterator{
Addresses: []common.Address{
testAddress,
},
},
migration.MigrateAccount(
testAddress,
migration.NewValueMigrationsPathMigrator(
reporter,
&CapabilityValueMigration{
Expand Down Expand Up @@ -1312,12 +1304,8 @@ func testLinkMigration(

reporter := &testMigrationReporter{}

migration.Migrate(
&migrations.AddressSliceIterator{
Addresses: []common.Address{
testAddress,
},
},
migration.MigrateAccount(
testAddress,
migration.NewValueMigrationsPathMigrator(
reporter,
&LinkValueMigration{
Expand Down Expand Up @@ -2025,12 +2013,8 @@ func TestPublishedPathCapabilityValueMigration(t *testing.T) {

reporter := &testMigrationReporter{}

migration.Migrate(
&migrations.AddressSliceIterator{
Addresses: []common.Address{
testAddress,
},
},
migration.MigrateAccount(
testAddress,
migration.NewValueMigrationsPathMigrator(
reporter,
&LinkValueMigration{
Expand All @@ -2041,12 +2025,8 @@ func TestPublishedPathCapabilityValueMigration(t *testing.T) {
),
)

migration.Migrate(
&migrations.AddressSliceIterator{
Addresses: []common.Address{
testAddress,
},
},
migration.MigrateAccount(
testAddress,
migration.NewValueMigrationsPathMigrator(
reporter,
&CapabilityValueMigration{
Expand Down Expand Up @@ -2274,12 +2254,8 @@ func TestUntypedPathCapabilityValueMigration(t *testing.T) {

reporter := &testMigrationReporter{}

migration.Migrate(
&migrations.AddressSliceIterator{
Addresses: []common.Address{
testAddress,
},
},
migration.MigrateAccount(
testAddress,
migration.NewValueMigrationsPathMigrator(
reporter,
&LinkValueMigration{
Expand All @@ -2290,12 +2266,8 @@ func TestUntypedPathCapabilityValueMigration(t *testing.T) {
),
)

migration.Migrate(
&migrations.AddressSliceIterator{
Addresses: []common.Address{
testAddress,
},
},
migration.MigrateAccount(
testAddress,
migration.NewValueMigrationsPathMigrator(
reporter,
&CapabilityValueMigration{
Expand Down
4 changes: 4 additions & 0 deletions migrations/entitlements/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (EntitlementsMigration) Name() string {
return "EntitlementsMigration"
}

func (EntitlementsMigration) Domains() map[string]struct{} {
return nil
}

// ConvertToEntitledType converts the given type to an entitled type according to the following rules:
// - ConvertToEntitledType(&T) --> auth(Entitlements(T)) &T
// - ConvertToEntitledType(Capability<T>) --> Capability<ConvertToEntitledType(T)>
Expand Down
Loading
Loading