Skip to content

Commit

Permalink
Merge pull request #2666 from onflow/sainati/copy-pure
Browse files Browse the repository at this point in the history
Make more `AuthAccount` functions `view`
  • Loading branch information
dsainati1 authored Jul 19, 2023
2 parents 9211ced + ad87189 commit d129d24
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 13 deletions.
26 changes: 13 additions & 13 deletions runtime/sema/authaccount.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ access(all) struct AuthAccount {
/// The given type must not necessarily be exactly the same as the type of the copied structure.
///
/// The path must be a storage path, i.e., only the domain `storage` is allowed.
access(all) fun copy<T: AnyStruct>(from: StoragePath): T?
access(all) view fun copy<T: AnyStruct>(from: StoragePath): T?

/// Returns a reference to an object in storage without removing it from storage.
///
Expand All @@ -95,15 +95,15 @@ access(all) struct AuthAccount {
/// The given type must not necessarily be exactly the same as the type of the borrowed object.
///
/// The path must be a storage path, i.e., only the domain `storage` is allowed
access(all) fun borrow<T: &Any>(from: StoragePath): T?
access(all) view fun borrow<T: &Any>(from: StoragePath): T?

/// Returns true if the object in account storage under the given path satisfies the given type,
/// i.e. could be borrowed using the given type.
///
/// The given type must not necessarily be exactly the same as the type of the borrowed object.
///
/// The path must be a storage path, i.e., only the domain `storage` is allowed.
access(all) fun check<T: Any>(from: StoragePath): Bool
access(all) view fun check<T: Any>(from: StoragePath): Bool

/// **DEPRECATED**: Instead, use `capabilities.storage.issue`, and `capabilities.publish` if the path is public.
///
Expand Down Expand Up @@ -137,13 +137,13 @@ access(all) struct AuthAccount {
/// **DEPRECATED**: Use `capabilities.get` instead.
///
/// Returns the capability at the given private or public path.
access(all) fun getCapability<T: &Any>(_ path: CapabilityPath): Capability<T>
access(all) view fun getCapability<T: &Any>(_ path: CapabilityPath): Capability<T>

/// **DEPRECATED**: Use `capabilities.storage.getController` and `StorageCapabilityController.target()`.
///
/// Returns the target path of the capability at the given public or private path,
/// or nil if there exists no capability at the given path.
access(all) fun getLinkTarget(_ path: CapabilityPath): Path?
access(all) view fun getLinkTarget(_ path: CapabilityPath): Path?

/// **DEPRECATED**: Use `capabilities.unpublish` instead if the path is public.
///
Expand Down Expand Up @@ -247,7 +247,7 @@ access(all) struct AuthAccount {
/// Returns the deployed contract for the contract/contract interface with the given name in the account, if any.
///
/// Returns nil if no contract/contract interface with the given name exists in the account.
access(all) fun get(name: String): DeployedContract?
access(all) view fun get(name: String): DeployedContract?

/// Removes the contract/contract interface from the account which has the given name, if any.
///
Expand All @@ -260,7 +260,7 @@ access(all) struct AuthAccount {
///
/// Returns nil if no contract with the given name exists in the account,
/// or if the contract does not conform to the given type.
access(all) fun borrow<T: &Any>(name: String): T?
access(all) view fun borrow<T: &Any>(name: String): T?
}

access(all) struct Keys {
Expand All @@ -277,7 +277,7 @@ access(all) struct AuthAccount {
/// Returns the key at the given index, if it exists, or nil otherwise.
///
/// Revoked keys are always returned, but they have `isRevoked` field set to true.
access(all) fun get(keyIndex: Int): AccountKey?
access(all) view fun get(keyIndex: Int): AccountKey?

/// Marks the key at the given index revoked, but does not delete it.
///
Expand Down Expand Up @@ -329,7 +329,7 @@ access(all) struct AuthAccount {
/// Returns the capability at the given public path.
/// Returns nil if the capability does not exist,
/// or if the given type is not a supertype of the capability's borrow type.
access(all) fun get<T: &Any>(_ path: PublicPath): Capability<T>?
access(all) view fun get<T: &Any>(_ path: PublicPath): Capability<T>?

/// Borrows the capability at the given public path.
/// Returns nil if the capability does not exist, or cannot be borrowed using the given type.
Expand Down Expand Up @@ -369,10 +369,10 @@ access(all) struct AuthAccount {
/// Get the storage capability controller for the capability with the specified ID.
///
/// Returns nil if the ID does not reference an existing storage capability.
access(all) fun getController(byCapabilityID: UInt64): &StorageCapabilityController?
access(all) view fun getController(byCapabilityID: UInt64): &StorageCapabilityController?

/// Get all storage capability controllers for capabilities that target this storage path
access(all) fun getControllers(forPath: StoragePath): [&StorageCapabilityController]
access(all) view fun getControllers(forPath: StoragePath): [&StorageCapabilityController]

/// Iterate over all storage capability controllers for capabilities that target this storage path,
/// passing a reference to each controller to the provided callback function.
Expand All @@ -394,10 +394,10 @@ access(all) struct AuthAccount {
/// Get capability controller for capability with the specified ID.
///
/// Returns nil if the ID does not reference an existing account capability.
access(all) fun getController(byCapabilityID: UInt64): &AccountCapabilityController?
access(all) view fun getController(byCapabilityID: UInt64): &AccountCapabilityController?

/// Get all capability controllers for all account capabilities.
access(all) fun getControllers(): [&AccountCapabilityController]
access(all) view fun getControllers(): [&AccountCapabilityController]

/// Iterate over all account capability controllers for all account capabilities,
/// passing a reference to each controller to the provided callback function.
Expand Down
13 changes: 13 additions & 0 deletions runtime/sema/authaccount.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions runtime/tests/checker/purity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,94 @@ func TestCheckPurityEnforcement(t *testing.T) {
})
})

t.Run("copy", func(t *testing.T) {
t.Parallel()
_, err := ParseAndCheckAccount(t, `
view fun foo() {
authAccount.copy<Int>(from: /storage/foo)
}
`)

require.NoError(t, err)
})

t.Run("borrow", func(t *testing.T) {
t.Parallel()
_, err := ParseAndCheckAccount(t, `
view fun foo() {
authAccount.borrow<&Int>(from: /storage/foo)
}
`)

require.NoError(t, err)
})

t.Run("check", func(t *testing.T) {
t.Parallel()
_, err := ParseAndCheckAccount(t, `
view fun foo() {
authAccount.check<Int>(from: /storage/foo)
}
`)

require.NoError(t, err)
})

t.Run("getlinktarget", func(t *testing.T) {
t.Parallel()
_, err := ParseAndCheckAccount(t, `
view fun foo() {
authAccount.getLinkTarget(/public/foo)
}
`)

require.NoError(t, err)
})

t.Run("getcap", func(t *testing.T) {
t.Parallel()
_, err := ParseAndCheckAccount(t, `
view fun foo() {
authAccount.getCapability<&Int>(/public/foo)
}
`)

require.NoError(t, err)
})

t.Run("get contract", func(t *testing.T) {
t.Parallel()
_, err := ParseAndCheckAccount(t, `
view fun foo() {
authAccount.contracts.get(name: "")
}
`)

require.NoError(t, err)
})

t.Run("borrow contract", func(t *testing.T) {
t.Parallel()
_, err := ParseAndCheckAccount(t, `
view fun foo() {
authAccount.contracts.borrow<&Int>(name: "")
}
`)

require.NoError(t, err)
})

t.Run("get keys", func(t *testing.T) {
t.Parallel()
_, err := ParseAndCheckAccount(t, `
view fun foo() {
authAccount.keys.get(keyIndex: 0)
}
`)

require.NoError(t, err)
})

t.Run("save", func(t *testing.T) {
t.Parallel()
_, err := ParseAndCheckAccount(t, `
Expand Down

0 comments on commit d129d24

Please sign in to comment.