Skip to content

Commit

Permalink
use a function to infer the authorization of the inferred borrow type
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Aug 12, 2024
1 parent d4c68bc commit 1caa451
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
44 changes: 39 additions & 5 deletions migrations/capcons/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ import (
)

type testCapConHandler struct {
ids map[common.Address]uint64
events []cadence.Event
ids map[common.Address]uint64
events []cadence.Event
dropEvents bool
}

var _ stdlib.CapabilityControllerIssueHandler = &testCapConHandler{}
Expand All @@ -57,6 +58,10 @@ func (g *testCapConHandler) EmitEvent(
eventType *sema.CompositeType,
values []interpreter.Value,
) {
if g.dropEvents {
return
}

runtime.EmitEventFields(
inter,
locationRange,
Expand Down Expand Up @@ -585,6 +590,9 @@ func testPathCapabilityValueMigration(
handler,
typedStorageCapabilityMapping,
untypedStorageCapabilityMapping,
func(_ interpreter.StaticType) interpreter.Authorization {
return interpreter.UnauthorizedAccess
},
)
}

Expand Down Expand Up @@ -3050,6 +3058,9 @@ func TestStorageCapMigration(t *testing.T) {
handler,
typedCapabilityMapping,
untypedCapabilityMapping,
func(_ interpreter.StaticType) interpreter.Authorization {
return interpreter.UnauthorizedAccess
},
)

migration.Migrate(
Expand Down Expand Up @@ -3287,7 +3298,10 @@ func TestUntypedStorageCapMigration(t *testing.T) {
reporter := &testMigrationReporter{}
typedStorageCapabilityMapping := &PathTypeCapabilityMapping{}
untypedStorageCapabilityMapping := &PathCapabilityMapping{}
handler := &testCapConHandler{}
handler := &testCapConHandler{
// Avoid loading contract code for made-up entitlement
dropEvents: true,
}
storageDomainCapabilities := &AccountsCapabilities{}

migration.Migrate(
Expand All @@ -3302,6 +3316,20 @@ func TestUntypedStorageCapMigration(t *testing.T) {
storageCapabilities := storageDomainCapabilities.Get(testAddress)
require.NotNil(t, storageCapabilities)

inferredAuth := interpreter.NewEntitlementSetAuthorization(
nil,
func() []common.TypeID {
return []common.TypeID{
common.AddressLocation{
Address: testAddress,
Name: "Foo",
}.TypeID(nil, "Bar"),
}
},
1,
sema.Conjunction,
)

IssueAccountCapabilities(
inter,
storage,
Expand All @@ -3311,6 +3339,9 @@ func TestUntypedStorageCapMigration(t *testing.T) {
handler,
typedStorageCapabilityMapping,
untypedStorageCapabilityMapping,
func(_ interpreter.StaticType) interpreter.Authorization {
return inferredAuth
},
)

migration.Migrate(
Expand Down Expand Up @@ -3357,7 +3388,7 @@ func TestUntypedStorageCapMigration(t *testing.T) {
addressPath: testAddressPath,
borrowType: interpreter.NewReferenceStaticType(
nil,
interpreter.UnauthorizedAccess,
inferredAuth,
interpreter.PrimitiveStaticTypeString,
),
capabilityID: 1,
Expand All @@ -3374,7 +3405,7 @@ func TestUntypedStorageCapMigration(t *testing.T) {
addressPath: testAddressPath,
borrowType: interpreter.NewReferenceStaticType(
nil,
interpreter.UnauthorizedAccess,
inferredAuth,
interpreter.PrimitiveStaticTypeString,
),
},
Expand Down Expand Up @@ -3539,6 +3570,9 @@ func TestUntypedStorageCapWithMissingTargetMigration(t *testing.T) {
handler,
typedStorageCapabilityMapping,
untypedStorageCapabilityMapping,
func(_ interpreter.StaticType) interpreter.Authorization {
return interpreter.UnauthorizedAccess
},
)

migration.Migrate(
Expand Down
7 changes: 4 additions & 3 deletions migrations/capcons/storagecapmigration.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func IssueAccountCapabilities(
handler stdlib.CapabilityControllerIssueHandler,
typedCapabilityMapping *PathTypeCapabilityMapping,
untypedCapabilityMapping *PathCapabilityMapping,
inferAuth func(valueType interpreter.StaticType) interpreter.Authorization,
) {

storageMap := storage.GetStorageMap(
Expand Down Expand Up @@ -141,12 +142,12 @@ func IssueAccountCapabilities(
continue
}

staticType := value.StaticType(inter)
valueType := value.StaticType(inter)

borrowType = interpreter.NewReferenceStaticType(
nil,
interpreter.UnauthorizedAccess,
staticType,
inferAuth(valueType),
valueType,
)

reporter.InferredMissingBorrowType(address, addressPath, borrowType)
Expand Down

0 comments on commit 1caa451

Please sign in to comment.