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 Account type migration #2973

Merged
merged 3 commits into from
Dec 11, 2023
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
25 changes: 24 additions & 1 deletion migrations/account_type/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@
}
return interpreter.NewUnmeteredCapabilityValue(value.ID, value.Address, convertedBorrowType)

case *interpreter.AccountCapabilityControllerValue:
convertedBorrowType := maybeConvertAccountType(value.BorrowType)
if convertedBorrowType == nil {
return
}

Check warning on line 62 in migrations/account_type/migration.go

View check run for this annotation

Codecov / codecov/patch

migrations/account_type/migration.go#L61-L62

Added lines #L61 - L62 were not covered by tests
borrowType := convertedBorrowType.(*interpreter.ReferenceStaticType)
return interpreter.NewUnmeteredAccountCapabilityControllerValue(borrowType, value.CapabilityID)

case *interpreter.StorageCapabilityControllerValue:
// Note: A storage capability with Account type shouldn't be possible theoretically.
convertedBorrowType := maybeConvertAccountType(value.BorrowType)
if convertedBorrowType == nil {
return
}

Check warning on line 71 in migrations/account_type/migration.go

View check run for this annotation

Codecov / codecov/patch

migrations/account_type/migration.go#L70-L71

Added lines #L70 - L71 were not covered by tests
borrowType := convertedBorrowType.(*interpreter.ReferenceStaticType)
return interpreter.NewUnmeteredStorageCapabilityControllerValue(borrowType, value.CapabilityID, value.TargetPath)

default:
return nil
}
Expand Down Expand Up @@ -94,7 +111,13 @@
}

case *interpreter.IntersectionStaticType:
// Nothing to do. Inner types can only be interfaces.
// No need to convert `staticType.Types` as they can only be interfaces.
convertedLegacyType := maybeConvertAccountType(staticType.LegacyType)
if convertedLegacyType != nil {
intersectionType := interpreter.NewIntersectionStaticType(nil, staticType.Types)
intersectionType.LegacyType = convertedLegacyType
return intersectionType
}

case *interpreter.OptionalStaticType:
convertedInnerType := maybeConvertAccountType(staticType.Type)
Expand Down
57 changes: 53 additions & 4 deletions migrations/account_type/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/onflow/atree"
Expand Down Expand Up @@ -234,6 +235,16 @@ func TestTypeValueMigration(t *testing.T) {
[]*interpreter.InterfaceStaticType{},
),
},
"intersection_with_legacy_type": {
storedType: &interpreter.IntersectionStaticType{
Types: []*interpreter.InterfaceStaticType{},
LegacyType: publicAccountType,
},
expectedType: &interpreter.IntersectionStaticType{
Types: []*interpreter.InterfaceStaticType{},
LegacyType: unauthorizedAccountReferenceType,
},
},
"public_account_reference": {
storedType: interpreter.NewReferenceStaticType(
nil,
Expand Down Expand Up @@ -390,14 +401,22 @@ func TestTypeValueMigration(t *testing.T) {
testCase, ok := testCases[identifier]
require.True(t, ok)

var storageValue interpreter.Value
var expectedValue interpreter.Value
if testCase.expectedType != nil {
storageValue = interpreter.NewTypeValue(nil, testCase.expectedType)
expectedValue = interpreter.NewTypeValue(nil, testCase.expectedType)

// `IntersectionType.LegacyType` is not considered in the `IntersectionType.Equal` method.
// Therefore, check for the legacy type equality manually.
typeValue := value.(interpreter.TypeValue)
if actualIntersectionType, ok := typeValue.Type.(*interpreter.IntersectionStaticType); ok {
expectedIntersectionType := testCase.expectedType.(*interpreter.IntersectionStaticType)
assert.True(t, actualIntersectionType.LegacyType.Equal(expectedIntersectionType.LegacyType))
}
} else {
storageValue = interpreter.NewTypeValue(nil, testCase.storedType)
expectedValue = interpreter.NewTypeValue(nil, testCase.storedType)
}

utils.AssertValuesEqual(t, inter, storageValue, value)
utils.AssertValuesEqual(t, inter, expectedValue, value)
})
}
}
Expand Down Expand Up @@ -737,6 +756,36 @@ func TestValuesWithStaticTypeMigration(t *testing.T) {
interpreter.PrimitiveStaticTypeString,
),
},
"account_capability_controller": {
storedValue: interpreter.NewUnmeteredAccountCapabilityControllerValue(
interpreter.NewReferenceStaticType(
nil,
interpreter.UnauthorizedAccess,
interpreter.PrimitiveStaticTypeAuthAccount, //nolint:staticcheck,
),
1234,
),
expectedValue: interpreter.NewUnmeteredAccountCapabilityControllerValue(
authAccountReferenceType,
1234,
),
},
"storage_capability_controller": {
storedValue: interpreter.NewUnmeteredStorageCapabilityControllerValue(
interpreter.NewReferenceStaticType(
nil,
interpreter.UnauthorizedAccess,
interpreter.PrimitiveStaticTypePublicAccount, //nolint:staticcheck,
),
1234,
interpreter.NewUnmeteredPathValue(common.PathDomainStorage, "v1"),
),
expectedValue: interpreter.NewUnmeteredStorageCapabilityControllerValue(
unauthorizedAccountReferenceType,
1234,
interpreter.NewUnmeteredPathValue(common.PathDomainStorage, "v1"),
),
},
}

// Store values
Expand Down
Loading