Skip to content

Commit 08a4868

Browse files
authored
Merge pull request #3141 from onflow/supun/fix-static-type-migration
Migrate static types of arrays and dictionaries
2 parents 2dd12d3 + 621b611 commit 08a4868

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

migrations/statictypes/account_type_migration_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,46 @@ func TestMigratingValuesWithAccountStaticType(t *testing.T) {
880880
require.NoError(t, err)
881881

882882
testCases := map[string]testCase{
883+
"dictionary_value": {
884+
storedValue: interpreter.NewDictionaryValue(
885+
inter,
886+
locationRange,
887+
interpreter.NewDictionaryStaticType(
888+
nil,
889+
interpreter.PrimitiveStaticTypeString,
890+
interpreter.PrimitiveStaticTypePublicAccount, //nolint:staticcheck
891+
),
892+
),
893+
expectedValue: interpreter.NewDictionaryValue(
894+
inter,
895+
locationRange,
896+
interpreter.NewDictionaryStaticType(
897+
nil,
898+
interpreter.PrimitiveStaticTypeString,
899+
unauthorizedAccountReferenceType,
900+
),
901+
),
902+
},
903+
"array_value": {
904+
storedValue: interpreter.NewArrayValue(
905+
inter,
906+
locationRange,
907+
interpreter.NewVariableSizedStaticType(
908+
nil,
909+
interpreter.PrimitiveStaticTypePublicAccount, //nolint:staticcheck
910+
),
911+
common.Address{},
912+
),
913+
expectedValue: interpreter.NewArrayValue(
914+
inter,
915+
locationRange,
916+
interpreter.NewVariableSizedStaticType(
917+
nil,
918+
unauthorizedAccountReferenceType,
919+
),
920+
common.Address{},
921+
),
922+
},
883923
"account_capability_value": {
884924
storedValue: interpreter.NewUnmeteredCapabilityValue(
885925
123,
@@ -955,6 +995,46 @@ func TestMigratingValuesWithAccountStaticType(t *testing.T) {
955995
BorrowType: unauthorizedAccountReferenceType,
956996
},
957997
},
998+
"capability_dictionary": {
999+
storedValue: interpreter.NewDictionaryValue(
1000+
inter,
1001+
locationRange,
1002+
interpreter.NewDictionaryStaticType(
1003+
nil,
1004+
interpreter.PrimitiveStaticTypeString,
1005+
interpreter.NewCapabilityStaticType(
1006+
nil,
1007+
interpreter.PrimitiveStaticTypePublicAccount, //nolint:staticcheck
1008+
),
1009+
),
1010+
interpreter.NewUnmeteredStringValue("key"),
1011+
interpreter.NewCapabilityValue(
1012+
nil,
1013+
interpreter.NewUnmeteredUInt64Value(1234),
1014+
interpreter.NewAddressValue(nil, common.Address{}),
1015+
interpreter.PrimitiveStaticTypePublicAccount, //nolint:staticcheck
1016+
),
1017+
),
1018+
expectedValue: interpreter.NewDictionaryValue(
1019+
inter,
1020+
locationRange,
1021+
interpreter.NewDictionaryStaticType(
1022+
nil,
1023+
interpreter.PrimitiveStaticTypeString,
1024+
interpreter.NewCapabilityStaticType(
1025+
nil,
1026+
unauthorizedAccountReferenceType,
1027+
),
1028+
),
1029+
interpreter.NewUnmeteredStringValue("key"),
1030+
interpreter.NewCapabilityValue(
1031+
nil,
1032+
interpreter.NewUnmeteredUInt64Value(1234),
1033+
interpreter.NewAddressValue(nil, common.Address{}),
1034+
unauthorizedAccountReferenceType,
1035+
),
1036+
),
1037+
},
9581038
}
9591039

9601040
// Store values

migrations/statictypes/statictype_migration.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (m *StaticTypeMigration) Migrate(
6262
_ interpreter.StorageKey,
6363
_ interpreter.StorageMapKey,
6464
value interpreter.Value,
65-
_ *interpreter.Interpreter,
65+
inter *interpreter.Interpreter,
6666
) (newValue interpreter.Value, err error) {
6767
switch value := value.(type) {
6868
case interpreter.TypeValue:
@@ -129,6 +129,51 @@ func (m *StaticTypeMigration) Migrate(
129129
value.CapabilityID,
130130
value.TargetPath,
131131
), nil
132+
133+
case *interpreter.ArrayValue:
134+
convertedElementType := m.maybeConvertStaticType(value.Type, nil)
135+
if convertedElementType == nil {
136+
return
137+
}
138+
139+
iterator := value.Iterator(inter, interpreter.EmptyLocationRange)
140+
141+
return interpreter.NewArrayValueWithIterator(
142+
inter,
143+
convertedElementType.(interpreter.ArrayStaticType),
144+
value.GetOwner(),
145+
uint64(value.Count()),
146+
func() interpreter.Value {
147+
return iterator.Next(inter, interpreter.EmptyLocationRange)
148+
},
149+
), nil
150+
151+
case *interpreter.DictionaryValue:
152+
convertedElementType := m.maybeConvertStaticType(value.Type, nil)
153+
if convertedElementType == nil {
154+
return
155+
}
156+
157+
var keysAndValues []interpreter.Value
158+
159+
iterator := value.Iterator()
160+
for {
161+
keyValue, value := iterator.Next(inter)
162+
if keyValue == nil {
163+
break
164+
}
165+
166+
keysAndValues = append(keysAndValues, keyValue)
167+
keysAndValues = append(keysAndValues, value)
168+
}
169+
170+
return interpreter.NewDictionaryValueWithAddress(
171+
inter,
172+
interpreter.EmptyLocationRange,
173+
convertedElementType.(*interpreter.DictionaryStaticType),
174+
value.GetOwner(),
175+
keysAndValues...,
176+
), nil
132177
}
133178

134179
return

0 commit comments

Comments
 (0)