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

Implement FLIP 242 #3252

Merged
merged 6 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 5 additions & 5 deletions migrations/entitlements/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ func TestConvertToEntitledValue(t *testing.T) {
wrap: func(staticType interpreter.StaticType) interpreter.Value {
return interpreter.NewCapabilityValue(
nil,
0,
1,
interpreter.AddressValue{},
staticType,
)
Expand All @@ -1241,7 +1241,7 @@ func TestConvertToEntitledValue(t *testing.T) {
interpreter.AddressValue{},
interpreter.NewCapabilityValue(
nil,
0,
1,
interpreter.AddressValue{},
staticType,
),
Expand Down Expand Up @@ -1453,7 +1453,7 @@ func TestMigrateSimpleContract(t *testing.T) {

unentitledRCap := interpreter.NewCapabilityValue(
inter,
0,
1,
interpreter.NewAddressValue(inter, account),
unentitledRRefStaticType,
)
Expand All @@ -1475,7 +1475,7 @@ func TestMigrateSimpleContract(t *testing.T) {
entitledRRefStaticType := entitledRRef.StaticType(inter)
entitledRCap := interpreter.NewCapabilityValue(
inter,
0,
1,
interpreter.NewAddressValue(inter, account),
entitledRRefStaticType,
)
Expand All @@ -1492,7 +1492,7 @@ func TestMigrateSimpleContract(t *testing.T) {
storedValue: unentitledRCap.Clone(inter),
expectedValue: interpreter.NewCapabilityValue(
inter,
0,
1,
interpreter.NewAddressValue(inter, account),
entitledRRefStaticType,
),
Expand Down
34 changes: 22 additions & 12 deletions runtime/capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testR() {
let path = /public/r
let cap = self.account.capabilities.get<&R>(path)!
let cap = self.account.capabilities.get<&R>(path)

assert(self.account.capabilities.exists(path))

Expand Down Expand Up @@ -162,7 +162,7 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testRAsR2() {
let path = /public/rAsR2
let cap = self.account.capabilities.get<&R2>(path)!
let cap = self.account.capabilities.get<&R2>(path)

assert(self.account.capabilities.exists(path))

Expand All @@ -185,7 +185,7 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testRAsS() {
let path = /public/rAsS
let cap = self.account.capabilities.get<&S>(path)!
let cap = self.account.capabilities.get<&S>(path)

assert(self.account.capabilities.exists(path))

Expand All @@ -208,7 +208,7 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testNonExistentTarget() {
let path = /public/nonExistentTarget
let cap = self.account.capabilities.get<&R>(path)!
let cap = self.account.capabilities.get<&R>(path)

assert(self.account.capabilities.exists(path))

Expand All @@ -231,13 +231,18 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testNonExistent() {
let path = /public/nonExistent
assert(self.account.capabilities.get<&AnyResource>(path) == nil)
dsainati1 marked this conversation as resolved.
Show resolved Hide resolved
let cap = self.account.capabilities.get<&R>(path)
dsainati1 marked this conversation as resolved.
Show resolved Hide resolved
assert(cap.id == 0)
assert(cap as? Capability<&R> != nil)
assert(cap as? Capability<&AnyResource> != nil)
assert(cap.borrow() == nil)
assert(cap.check() == false)
dsainati1 marked this conversation as resolved.
Show resolved Hide resolved
assert(!self.account.capabilities.exists(path))
}

access(all)
fun testSwap(): Int {
let ref = self.account.capabilities.get<&R>(/public/r)!.borrow()!
let ref = self.account.capabilities.get<&R>(/public/r).borrow()!

let r <- self.account.storage.load<@R>(from: /storage/r)
destroy r
Expand Down Expand Up @@ -385,7 +390,7 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testS() {
let path = /public/s
let cap = self.account.capabilities.get<&S>(path)!
let cap = self.account.capabilities.get<&S>(path)

assert(self.account.capabilities.exists(path))

Expand Down Expand Up @@ -414,7 +419,7 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testSAsS2() {
let path = /public/sAsS2
let cap = self.account.capabilities.get<&S2>(path)!
let cap = self.account.capabilities.get<&S2>(path)

assert(self.account.capabilities.exists(path))

Expand All @@ -437,7 +442,7 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testSAsR() {
let path = /public/sAsR
let cap = self.account.capabilities.get<&R>(path)!
let cap = self.account.capabilities.get<&R>(path)

assert(self.account.capabilities.exists(path))

Expand All @@ -460,7 +465,7 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testNonExistentTarget() {
let path = /public/nonExistentTarget
let cap = self.account.capabilities.get<&S>(path)!
let cap = self.account.capabilities.get<&S>(path)

assert(self.account.capabilities.exists(path))

Expand All @@ -483,13 +488,18 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testNonExistent() {
let path = /public/nonExistent
assert(self.account.capabilities.get<&AnyStruct>(path) == nil)
let cap = self.account.capabilities.get<&S>(path)
dsainati1 marked this conversation as resolved.
Show resolved Hide resolved
assert(cap.id == 0)
dsainati1 marked this conversation as resolved.
Show resolved Hide resolved
assert(cap as? Capability<&S> != nil)
assert(cap as? Capability<&AnyStruct> != nil)
assert(cap.borrow() == nil)
assert(cap.check() == false)
assert(!self.account.capabilities.exists(path))
}

access(all)
fun testSwap(): Int {
let ref = self.account.capabilities.get<&S>(/public/s)!.borrow()!
let ref = self.account.capabilities.get<&S>(/public/s).borrow()!

self.account.storage.load<S>(from: /storage/s)

Expand Down
36 changes: 18 additions & 18 deletions runtime/capabilitycontrollers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,12 @@ func TestRuntimeCapabilityControllers(t *testing.T) {
let path = /public/x

// Act
let gotCap: Capability<&AnyStruct>? =
let gotCap: Capability<&AnyStruct> =
%[1]s.capabilities.get<&AnyStruct>(path)

// Assert
assert(!%[1]s.capabilities.exists(path))
assert(gotCap == nil)
assert(gotCap.id == 0)
dsainati1 marked this conversation as resolved.
Show resolved Hide resolved
}
}
`,
Expand Down Expand Up @@ -274,7 +274,7 @@ func TestRuntimeCapabilityControllers(t *testing.T) {

// Act
let gotCap: Capability<&Test.R> =
%[1]s.capabilities.get<&Test.R>(publicPath)!
%[1]s.capabilities.get<&Test.R>(publicPath)

// Assert
assert(%[1]s.capabilities.exists(publicPath))
Expand Down Expand Up @@ -308,7 +308,7 @@ func TestRuntimeCapabilityControllers(t *testing.T) {

// Act
let gotCap: Capability<&Account> =
%[1]s.capabilities.get<&Account>(publicPath)!
%[1]s.capabilities.get<&Account>(publicPath)

// Assert
assert(%[1]s.capabilities.exists(publicPath))
Expand Down Expand Up @@ -353,7 +353,7 @@ func TestRuntimeCapabilityControllers(t *testing.T) {

// Act
let gotCap: Capability<&Test.R> =
%[1]s.capabilities.get<&Test.R>(publicPath)!
%[1]s.capabilities.get<&Test.R>(publicPath)
let ref: &Test.R = gotCap.borrow()!

// Assert
Expand Down Expand Up @@ -390,7 +390,7 @@ func TestRuntimeCapabilityControllers(t *testing.T) {

// Act
let gotCap: Capability<&Account> =
%[1]s.capabilities.get<&Account>(publicPath)!
%[1]s.capabilities.get<&Account>(publicPath)
let ref: &Account = gotCap.borrow()!

// Assert
Expand Down Expand Up @@ -436,13 +436,13 @@ func TestRuntimeCapabilityControllers(t *testing.T) {
signer.capabilities.publish(issuedCap, at: publicPath)

// Act
let gotCap: Capability<auth(Test.X) &Test.R>? =
let gotCap: Capability<auth(Test.X) &Test.R> =
%[1]s.capabilities.get<auth(Test.X) &Test.R>(publicPath)

// Assert
assert(%[1]s.capabilities.exists(publicPath))
assert(issuedCap.id == expectedCapID)
assert(gotCap == nil)
assert(gotCap.id == 0)
}
}
`,
Expand Down Expand Up @@ -472,13 +472,13 @@ func TestRuntimeCapabilityControllers(t *testing.T) {
signer.capabilities.publish(issuedCap, at: publicPath)

// Act
let gotCap: Capability<&Test.R>? =
let gotCap: Capability<&Test.R> =
%[1]s.capabilities.get<&Test.R>(publicPath)

// Assert
assert(%[1]s.capabilities.exists(publicPath))
assert(issuedCap.id == expectedCapID)
assert(gotCap == nil)
assert(gotCap.id == 0)
}
}
`,
Expand Down Expand Up @@ -516,13 +516,13 @@ func TestRuntimeCapabilityControllers(t *testing.T) {
signer.capabilities.publish(issuedCap, at: publicPath)

// Act
let gotCap: Capability<&Test.S>? =
let gotCap: Capability<&Test.S> =
%[1]s.capabilities.get<&Test.S>(publicPath)

// Assert
assert(%[1]s.capabilities.exists(publicPath))
assert(issuedCap.id == expectedCapID)
assert(gotCap == nil)
assert(gotCap.id == 0)
}
}
`,
Expand Down Expand Up @@ -550,13 +550,13 @@ func TestRuntimeCapabilityControllers(t *testing.T) {
signer.capabilities.publish(issuedCap, at: publicPath)

// Act
let gotCap: Capability<&AnyResource>? =
let gotCap: Capability<&AnyResource> =
%[1]s.capabilities.get<&AnyResource>(publicPath)

// Assert
assert(%[1]s.capabilities.exists(publicPath))
assert(issuedCap.id == expectedCapID)
assert(gotCap == nil)
assert(gotCap.id == 0)
}
}
`,
Expand Down Expand Up @@ -594,14 +594,14 @@ func TestRuntimeCapabilityControllers(t *testing.T) {
let unpublishedcap = signer.capabilities.unpublish(publicPath)

// Act
let gotCap: Capability<&Test.R>? =
let gotCap: Capability<&Test.R> =
%[1]s.capabilities.get<&Test.R>(publicPath)

// Assert
assert(!%[1]s.capabilities.exists(publicPath))
assert(issuedCap.id == expectedCapID)
assert(unpublishedcap!.id == expectedCapID)
assert(gotCap == nil)
assert(gotCap.id == 0)
}
}
`,
Expand Down Expand Up @@ -629,14 +629,14 @@ func TestRuntimeCapabilityControllers(t *testing.T) {
let unpublishedcap = signer.capabilities.unpublish(publicPath)

// Act
let gotCap: Capability<&Account>? =
let gotCap: Capability<&Account> =
%[1]s.capabilities.get<&Account>(publicPath)

// Assert
assert(!%[1]s.capabilities.exists(publicPath))
assert(issuedCap.id == expectedCapID)
assert(unpublishedcap!.id == expectedCapID)
assert(gotCap == nil)
assert(gotCap.id == 0)
}
}
`,
Expand Down
18 changes: 9 additions & 9 deletions runtime/entitlements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ func TestRuntimeAccountEntitlementCapabilityCasting(t *testing.T) {
import Test from 0x1
transaction {
prepare(signer: &Account) {
let capX = signer.capabilities.get<auth(Test.X) &Test.R>(/public/foo)!
let capX = signer.capabilities.get<auth(Test.X) &Test.R>(/public/foo)
let upCap = capX as Capability<&Test.R>
let downCap = upCap as! Capability<auth(Test.X) &Test.R>
}
Expand Down Expand Up @@ -647,8 +647,8 @@ func TestRuntimeAccountEntitlementCapabilityDictionary(t *testing.T) {
import Test from 0x1
transaction {
prepare(signer: &Account) {
let capX = signer.capabilities.get<auth(Test.X) &Test.R>(/public/foo)!
let capY = signer.capabilities.get<auth(Test.Y) &Test.R>(/public/bar)!
let capX = signer.capabilities.get<auth(Test.X) &Test.R>(/public/foo)
let capY = signer.capabilities.get<auth(Test.Y) &Test.R>(/public/bar)

let dict: {Type: Capability<&Test.R>} = {}
dict[capX.getType()] = capX
Expand Down Expand Up @@ -762,8 +762,8 @@ func TestRuntimeAccountEntitlementGenericCapabilityDictionary(t *testing.T) {
import Test from 0x1
transaction {
prepare(signer: &Account) {
let capX = signer.capabilities.get<auth(Test.X) &Test.R>(/public/foo)!
let capY = signer.capabilities.get<auth(Test.Y) &Test.R>(/public/bar)!
let capX = signer.capabilities.get<auth(Test.X) &Test.R>(/public/foo)
let capY = signer.capabilities.get<auth(Test.Y) &Test.R>(/public/bar)

let dict: {Type: Capability} = {}
dict[capX.getType()] = capX
Expand Down Expand Up @@ -988,7 +988,7 @@ func TestRuntimeCapabilityEntitlements(t *testing.T) {
let issuedCap = account.capabilities.storage.issue<auth(X) &S>(/storage/foo)
account.capabilities.publish(issuedCap, at: /public/foo)

let cap: Capability<auth(X) &S> = account.capabilities.get<auth(X) &S>(/public/foo)!
let cap: Capability<auth(X) &S> = account.capabilities.get<auth(X) &S>(/public/foo)

let runtimeType = cap.getType()

Expand Down Expand Up @@ -1017,7 +1017,7 @@ func TestRuntimeCapabilityEntitlements(t *testing.T) {
let issuedCap = account.capabilities.storage.issue<&S>(/storage/foo)
account.capabilities.publish(issuedCap, at: /public/foo)

let cap: Capability<&S> = account.capabilities.get<&S>(/public/foo)!
let cap: Capability<&S> = account.capabilities.get<&S>(/public/foo)

let runtimeType = cap.getType()
let upcastCap = cap as Capability<&AnyStruct>
Expand Down Expand Up @@ -1050,7 +1050,7 @@ func TestRuntimeCapabilityEntitlements(t *testing.T) {
let issuedCap = account.capabilities.storage.issue<auth(X, Y) &R>(/storage/foo)
account.capabilities.publish(issuedCap, at: /public/foo)

let cap = account.capabilities.get<auth(X | Y) &R>(/public/foo)!
let cap = account.capabilities.get<auth(X | Y) &R>(/public/foo)
assert(cap.check())
}
`)
Expand Down Expand Up @@ -1109,7 +1109,7 @@ func TestRuntimeCapabilityEntitlements(t *testing.T) {
account.capabilities.publish(issuedCap, at: /public/foo)

let cap = account.capabilities.get<auth(X, Y) &R>(/public/foo)
assert(cap == nil)
assert(!cap.check())
dsainati1 marked this conversation as resolved.
Show resolved Hide resolved
}
`)
})
Expand Down
12 changes: 12 additions & 0 deletions runtime/interpreter/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -1107,3 +1107,15 @@ func (ResourceLossError) IsUserError() {}
func (e ResourceLossError) Error() string {
return "resource loss: attempting to assign to non-nil resource-typed value"
}

// InvalidCapabilityIDError

type InvalidCapabilityIDError struct{}

var _ errors.InternalError = InvalidCapabilityIDError{}

func (InvalidCapabilityIDError) IsInternalError() {}

func (e InvalidCapabilityIDError) Error() string {
return "capability created with invalid ID"
}
Loading
Loading