From f5609bea23f76c26dc00ca8e8ba14034c0972af1 Mon Sep 17 00:00:00 2001 From: Supun Setunga Date: Wed, 28 Feb 2024 08:50:20 +0530 Subject: [PATCH 1/2] Migrate static types of arrays and dictionaries --- .../account_type_migration_test.go | 66 +++++++++++++++++++ .../statictypes/statictype_migration.go | 47 ++++++++++++- 2 files changed, 112 insertions(+), 1 deletion(-) diff --git a/migrations/statictypes/account_type_migration_test.go b/migrations/statictypes/account_type_migration_test.go index f1d74bdb0f..a601398e49 100644 --- a/migrations/statictypes/account_type_migration_test.go +++ b/migrations/statictypes/account_type_migration_test.go @@ -880,6 +880,46 @@ func TestMigratingValuesWithAccountStaticType(t *testing.T) { require.NoError(t, err) testCases := map[string]testCase{ + "dictionary_value": { + storedValue: interpreter.NewDictionaryValue( + inter, + locationRange, + interpreter.NewDictionaryStaticType( + nil, + interpreter.PrimitiveStaticTypeString, + interpreter.PrimitiveStaticTypePublicAccount, //nolint:staticcheck + ), + ), + expectedValue: interpreter.NewDictionaryValue( + inter, + locationRange, + interpreter.NewDictionaryStaticType( + nil, + interpreter.PrimitiveStaticTypeString, + unauthorizedAccountReferenceType, + ), + ), + }, + "array_value": { + storedValue: interpreter.NewArrayValue( + inter, + locationRange, + interpreter.NewVariableSizedStaticType( + nil, + interpreter.PrimitiveStaticTypePublicAccount, //nolint:staticcheck + ), + common.Address{}, + ), + expectedValue: interpreter.NewArrayValue( + inter, + locationRange, + interpreter.NewVariableSizedStaticType( + nil, + unauthorizedAccountReferenceType, + ), + common.Address{}, + ), + }, "account_capability_value": { storedValue: interpreter.NewUnmeteredCapabilityValue( 123, @@ -955,6 +995,32 @@ func TestMigratingValuesWithAccountStaticType(t *testing.T) { BorrowType: unauthorizedAccountReferenceType, }, }, + "capability_dictionary": { + storedValue: interpreter.NewDictionaryValue( + inter, + locationRange, + interpreter.NewDictionaryStaticType( + nil, + interpreter.PrimitiveStaticTypeString, + interpreter.NewCapabilityStaticType( + nil, + interpreter.PrimitiveStaticTypePublicAccount, //nolint:staticcheck + ), + ), + ), + expectedValue: interpreter.NewDictionaryValue( + inter, + locationRange, + interpreter.NewDictionaryStaticType( + nil, + interpreter.PrimitiveStaticTypeString, + interpreter.NewCapabilityStaticType( + nil, + unauthorizedAccountReferenceType, + ), + ), + ), + }, } // Store values diff --git a/migrations/statictypes/statictype_migration.go b/migrations/statictypes/statictype_migration.go index 038e49a899..a0b7c7132d 100644 --- a/migrations/statictypes/statictype_migration.go +++ b/migrations/statictypes/statictype_migration.go @@ -62,7 +62,7 @@ func (m *StaticTypeMigration) Migrate( _ interpreter.StorageKey, _ interpreter.StorageMapKey, value interpreter.Value, - _ *interpreter.Interpreter, + inter *interpreter.Interpreter, ) (newValue interpreter.Value, err error) { switch value := value.(type) { case interpreter.TypeValue: @@ -129,6 +129,51 @@ func (m *StaticTypeMigration) Migrate( value.CapabilityID, value.TargetPath, ), nil + + case *interpreter.ArrayValue: + convertedElementType := m.maybeConvertStaticType(value.Type, nil) + if convertedElementType == nil { + return + } + + iterator := value.Iterator(inter, interpreter.EmptyLocationRange) + + return interpreter.NewArrayValueWithIterator( + inter, + convertedElementType.(interpreter.ArrayStaticType), + value.GetOwner(), + uint64(value.Count()), + func() interpreter.Value { + return iterator.Next(inter, interpreter.EmptyLocationRange) + }, + ), nil + + case *interpreter.DictionaryValue: + convertedElementType := m.maybeConvertStaticType(value.Type, nil) + if convertedElementType == nil { + return + } + + var keysAndValues []interpreter.Value + + iterator := value.Iterator() + for { + keyValue, value := iterator.Next(inter) + if keyValue == nil { + break + } + + keysAndValues = append(keysAndValues, keyValue) + keysAndValues = append(keysAndValues, value) + } + + return interpreter.NewDictionaryValueWithAddress( + inter, + interpreter.EmptyLocationRange, + convertedElementType.(*interpreter.DictionaryStaticType), + value.GetOwner(), + keysAndValues..., + ), nil } return From 621b6116976a3b5a8099ebd9ac355134fb48453c Mon Sep 17 00:00:00 2001 From: Supun Setunga Date: Wed, 28 Feb 2024 10:48:00 +0530 Subject: [PATCH 2/2] Add entries to the dictionary test --- .../statictypes/account_type_migration_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/migrations/statictypes/account_type_migration_test.go b/migrations/statictypes/account_type_migration_test.go index a601398e49..0dadb8bffc 100644 --- a/migrations/statictypes/account_type_migration_test.go +++ b/migrations/statictypes/account_type_migration_test.go @@ -1007,6 +1007,13 @@ func TestMigratingValuesWithAccountStaticType(t *testing.T) { interpreter.PrimitiveStaticTypePublicAccount, //nolint:staticcheck ), ), + interpreter.NewUnmeteredStringValue("key"), + interpreter.NewCapabilityValue( + nil, + interpreter.NewUnmeteredUInt64Value(1234), + interpreter.NewAddressValue(nil, common.Address{}), + interpreter.PrimitiveStaticTypePublicAccount, //nolint:staticcheck + ), ), expectedValue: interpreter.NewDictionaryValue( inter, @@ -1019,6 +1026,13 @@ func TestMigratingValuesWithAccountStaticType(t *testing.T) { unauthorizedAccountReferenceType, ), ), + interpreter.NewUnmeteredStringValue("key"), + interpreter.NewCapabilityValue( + nil, + interpreter.NewUnmeteredUInt64Value(1234), + interpreter.NewAddressValue(nil, common.Address{}), + unauthorizedAccountReferenceType, + ), ), }, }