From 9cf31dc060453cdabc247c70514d478faa8a8e3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Mon, 5 Feb 2024 16:20:23 -0800 Subject: [PATCH] add support for migrating path capability values in entitlements and static type migrations --- migrations/entitlements/migration.go | 15 +++++++++++++++ migrations/entitlements/migration_test.go | 13 +++++++++++++ .../statictypes/account_type_migration_test.go | 12 ++++++++++++ migrations/statictypes/statictype_migration.go | 11 +++++++++++ runtime/interpreter/value_pathcapability.go | 8 ++++++-- 5 files changed, 57 insertions(+), 2 deletions(-) diff --git a/migrations/entitlements/migration.go b/migrations/entitlements/migration.go index c1ed4f2968..8a74f52608 100644 --- a/migrations/entitlements/migration.go +++ b/migrations/entitlements/migration.go @@ -298,6 +298,21 @@ func ConvertValueToEntitlements( capabilityStaticType, ), nil + case *interpreter.PathCapabilityValue: //nolint:staticcheck + semaType := inter.MustConvertStaticToSemaType(staticType) + entitledType, converted := ConvertToEntitledType(semaType) + if !converted { + return nil, nil + } + + entitledCapabilityValue := entitledType.(*sema.CapabilityType) + capabilityStaticType := interpreter.ConvertSemaToStaticType(inter, entitledCapabilityValue.BorrowType) + return &interpreter.PathCapabilityValue{ //nolint:staticcheck + Path: v.Path, + Address: v.Address, + BorrowType: capabilityStaticType, + }, nil + case interpreter.TypeValue: if v.Type == nil { return nil, nil diff --git a/migrations/entitlements/migration_test.go b/migrations/entitlements/migration_test.go index 7bc433de15..a34fd5970a 100644 --- a/migrations/entitlements/migration_test.go +++ b/migrations/entitlements/migration_test.go @@ -1028,6 +1028,19 @@ func TestConvertToEntitledValue(t *testing.T) { ), Name: "Capability<&S>", }, + { + Input: &interpreter.PathCapabilityValue{ + Address: interpreter.NewAddressValue(inter, testAddress), + Path: testPathValue, + BorrowType: unentitledSRefStaticType, + }, + Output: &interpreter.PathCapabilityValue{ + Address: interpreter.NewAddressValue(inter, testAddress), + Path: testPathValue, + BorrowType: entitledSRefStaticType, + }, + Name: "PathCapability<&S>", + }, { Input: interpreter.NewCapabilityValue( inter, diff --git a/migrations/statictypes/account_type_migration_test.go b/migrations/statictypes/account_type_migration_test.go index 2c1df53fc2..ccbe46fe8d 100644 --- a/migrations/statictypes/account_type_migration_test.go +++ b/migrations/statictypes/account_type_migration_test.go @@ -835,6 +835,18 @@ func TestMigratingValuesWithAccountStaticType(t *testing.T) { storedValue: interpreter.AccountLinkValue{}, //nolint:staticcheck expectedValue: interpreter.AccountLinkValue{}, //nolint:staticcheck }, + "path_capability_value": { + storedValue: &interpreter.PathCapabilityValue{ + Address: interpreter.NewAddressValue(nil, common.Address{0x42}), + Path: interpreter.NewUnmeteredPathValue(common.PathDomainStorage, "v1"), + BorrowType: interpreter.PrimitiveStaticTypePublicAccount, //nolint:staticcheck + }, + expectedValue: &interpreter.PathCapabilityValue{ + Address: interpreter.NewAddressValue(nil, common.Address{0x42}), + Path: interpreter.NewUnmeteredPathValue(common.PathDomainStorage, "v1"), + BorrowType: unauthorizedAccountReferenceType, + }, + }, } // Store values diff --git a/migrations/statictypes/statictype_migration.go b/migrations/statictypes/statictype_migration.go index 0fc0ab51da..dfa021214e 100644 --- a/migrations/statictypes/statictype_migration.go +++ b/migrations/statictypes/statictype_migration.go @@ -77,6 +77,17 @@ func (m *StaticTypeMigration) Migrate( } return interpreter.NewUnmeteredCapabilityValue(value.ID, value.Address, convertedBorrowType), nil + case *interpreter.PathCapabilityValue: //nolint:staticcheck + convertedBorrowType := m.maybeConvertStaticType(value.BorrowType) + if convertedBorrowType == nil { + return + } + return &interpreter.PathCapabilityValue{ + BorrowType: convertedBorrowType, + Path: value.Path, + Address: value.Address, + }, nil + case interpreter.PathLinkValue: //nolint:staticcheck convertedBorrowType := m.maybeConvertStaticType(value.Type) if convertedBorrowType == nil { diff --git a/runtime/interpreter/value_pathcapability.go b/runtime/interpreter/value_pathcapability.go index eabc1dbd6a..5c509cd496 100644 --- a/runtime/interpreter/value_pathcapability.go +++ b/runtime/interpreter/value_pathcapability.go @@ -152,8 +152,12 @@ func (v *PathCapabilityValue) Transfer( return v } -func (v *PathCapabilityValue) Clone(_ *Interpreter) Value { - panic(errors.NewUnreachableError()) +func (v *PathCapabilityValue) Clone(interpreter *Interpreter) Value { + return &PathCapabilityValue{ + BorrowType: v.BorrowType, + Path: v.Path.Clone(interpreter).(PathValue), + Address: v.Address.Clone(interpreter).(AddressValue), + } } func (v *PathCapabilityValue) DeepRemove(interpreter *Interpreter) {