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

filter remove all #145

Merged
merged 2 commits into from
Sep 13, 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
16 changes: 16 additions & 0 deletions contracts/CapabilityFilter.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ pub contract CapabilityFilter {
}
}

/// Removes all types from the mapping of denied types
///
pub fun removeAllTypes() {
for type in self.deniedTypes.keys {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we instead make deniedTypes a var and just set it to an empty array?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will that be backward compatible change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also the events might be nice to have?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can probably have an event that says types cleared to make it more efficient. and lemme check if it's a backward compatible change, my gut says no

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will that be backward compatible change

I believe changing let to var is an an upgradable change yes

also the events might be nice to have?

👍 Sure, totally fine to add events. That might require some discussion so if you need some way to remove types right now for testing purposes, I'd split them up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth it to do the let/var change if we want to iterate through every key to emit each event?

Copy link
Collaborator

@sisyphusSmiling sisyphusSmiling Aug 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it be helpful for each removed type to be included in its own event or could we emit an array of removed types? If the latter, I think we could use the same event for both removal operations and just emit the dict keys

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Emitting an array is what I was thinking, yeah. It depends on what kinds of events you're thinking about @rrrkren. Is there any high-level thought on that yet that we can use to think through this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is what we are doing right now to reset filters:

        let filterDetails = filter.getDetails() as! {String:AnyStruct}
        for allowedType in filterDetails["allowedTypes"]! as! [Type]{
            filter.removeType(allowedType)
        }

I'd prefer to keep this PR as is if possible since it's just a sugared iterate remove all and doesn't require downstream event aggregations to change. Happy to discuss further on the single filter reset event as a follow up.

self.removeType(type)
}
}

/// Determines if a requested capability is allowed by this `Filter`
///
/// @param cap: The capability to check
Expand Down Expand Up @@ -112,6 +120,14 @@ pub contract CapabilityFilter {
emit FilterUpdated(id: self.uuid, filterType: self.getType(), type: type, active: false)
}
}

/// Removes all types from the mapping of denied types
///
pub fun removeAllTypes() {
for type in self.allowedTypes.keys {
self.removeType(type)
}
}

/// Determines if a requested capability is allowed by this `Filter`
///
Expand Down
67 changes: 67 additions & 0 deletions test/HybridCustody_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,57 @@ pub fun testGetCapability_ManagerFilterAllowed() {
scriptExecutor("hybrid-custody/get_nft_provider_capability.cdc", [parent.address, child.address])
}

pub fun testAllowlistFilterRemoveAllTypes() {
let child = blockchain.createAccount()
let parent = blockchain.createAccount()

setupChildAndParent_FilterKindAll(child: child, parent: parent)

setupNFTCollection(child)

scriptExecutor("hybrid-custody/get_nft_provider_capability.cdc", [parent.address, child.address])

let filter = getTestAccount(FilterKindAllowList)
setupFilter(filter, FilterKindAllowList)

let nftIdentifier = buildTypeIdentifier(getTestAccount(exampleNFT), exampleNFT, "Collection")
setManagerFilterOnChild(child: child, parent: parent, filterAddress: filter.address)

addTypeToFilter(filter, FilterKindAllowList, nftIdentifier)

scriptExecutor("hybrid-custody/get_nft_provider_capability.cdc", [parent.address, child.address])

removeAllFilterTypes(filter, FilterKindAllowList)

let error = expectScriptFailure("hybrid-custody/get_nft_provider_capability.cdc", [parent.address, child.address])
assert(contains(error, "Capability is not allowed by this account's Parent"), message: "failed to find expected error message")
}

pub fun testDenyListFilterRemoveAllTypes() {
let child = blockchain.createAccount()
let parent = blockchain.createAccount()

setupChildAndParent_FilterKindAll(child: child, parent: parent)

setupNFTCollection(child)

scriptExecutor("hybrid-custody/get_nft_provider_capability.cdc", [parent.address, child.address])

let filter = getTestAccount(FilterKindDenyList)
setupFilter(filter, FilterKindDenyList)

let nftIdentifier = buildTypeIdentifier(getTestAccount(exampleNFT), exampleNFT, "Collection")
addTypeToFilter(filter, FilterKindDenyList, nftIdentifier)
setManagerFilterOnChild(child: child, parent: parent, filterAddress: filter.address)

let error = expectScriptFailure("hybrid-custody/get_nft_provider_capability.cdc", [parent.address, child.address])
assert(contains(error, "Capability is not allowed by this account's Parent"), message: "failed to find expected error message")


removeAllFilterTypes(filter, FilterKindDenyList)
scriptExecutor("hybrid-custody/get_nft_provider_capability.cdc", [parent.address, child.address])
}

pub fun testGetCapability_ManagerFilterNotAllowed() {
let child = blockchain.createAccount()
let parent = blockchain.createAccount()
Expand Down Expand Up @@ -914,6 +965,22 @@ pub fun addTypeToFilter(_ acct: Test.Account, _ kind: String, _ identifier: Stri
txExecutor(filePath, [acct], [identifier], nil, nil)
}

pub fun removeAllFilterTypes(_ acct: Test.Account, _ kind: String) {
var filePath = ""
switch kind {
case FilterKindAllowList:
filePath = "filter/allow/remove_all_types.cdc"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the file needs to be renamed - remoe_all_types.cdc -> remove_all_types.cdc. Small typo is causing this test case to fail

break
case FilterKindDenyList:
filePath = "filter/deny/remove_all_types.cdc"
break
default:
assert(false, message: "unknown filter kind given")
}

txExecutor(filePath, [acct], [], nil, nil)
}

pub fun addNFTCollectionToDelegator(child: Test.Account, parent: Test.Account, isPublic: Bool) {
txExecutor("hybrid-custody/add_example_nft_collection_to_delegator.cdc", [child], [parent.address, isPublic], nil, nil)
}
Expand Down
10 changes: 10 additions & 0 deletions transactions/filter/allow/remove_all_types.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import "CapabilityFilter"

transaction() {
prepare(acct: AuthAccount) {
let filter = acct.borrow<&CapabilityFilter.AllowlistFilter>(from: CapabilityFilter.StoragePath)
?? panic("filter does not exist")

filter.removeAllTypes()
}
}
10 changes: 10 additions & 0 deletions transactions/filter/deny/remove_all_types.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import "CapabilityFilter"

transaction() {
prepare(acct: AuthAccount) {
let filter = acct.borrow<&CapabilityFilter.DenylistFilter>(from: CapabilityFilter.StoragePath)
?? panic("filter does not exist")

filter.removeAllTypes()
}
}