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

Common validation functions #11

Merged
merged 3 commits into from
Mar 8, 2024
Merged

Common validation functions #11

merged 3 commits into from
Mar 8, 2024

Conversation

danenbm
Copy link
Contributor

@danenbm danenbm commented Mar 8, 2024

Summary

  • Move asset and collection validations to common util functions: validate_asset_permissions and validate_collection_permissions. Then within instructions can call with relevant function pointers:
 let (mut asset, _, _) = validate_asset_permissions(
    ctx.accounts.authority,
    ctx.accounts.asset,
    ctx.accounts.collection,
    Some(ctx.accounts.new_owner),
    Asset::check_transfer,
    Collection::check_transfer,
    PluginType::check_transfer,
    Asset::validate_transfer,
    Collection::validate_transfer,
    Plugin::validate_transfer,
)?;

Notes

  • So far just did it for transfer, update, burn to check out the idea. Still needs to be done for compress decompress for example.
  • I was able to prevent having to use a closure for the transfer check because I added Option for an unused new_owner to the other validations.

Benefits:

  • Reduces the copy and paste which will prevent errors when updating that code.

Drawbacks:

  • It may be possible that this is too rigid, if for example we needed to use TransferArgs or BurnArgs in the validations. Currently it looks like the specific args have mostly been removed. They are still left over on compress/decompress but I don't think they would be used there once updated.

Overall, we just need to consider if the validation function signatures/prototypes can look similar enough to enable the function pointer usage. I think they are now. Plus we could also change it back if too rigid.

Testing

JS tests pass locally.

Comment on lines -228 to -237
let mut approved = false;
match Asset::check_update() {
CheckResult::CanApprove => {
if asset.validate_update(ctx.accounts.authority)? == ValidationResult::Approved {
approved = true;
}
}
CheckResult::CanReject => return Err(MplCoreError::InvalidAuthority.into()),
CheckResult::None => (),
};
Copy link
Contributor Author

@danenbm danenbm Mar 8, 2024

Choose a reason for hiding this comment

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

Note this should have been a Collection check rather than Asset check. It is now fixed in the common function.

Comment on lines -60 to -70
// Check the collection plugins first.
ctx.accounts.collection.and_then(|collection_info| {
fetch_core_data::<Collection>(collection_info)
.map(|(_, _, registry)| {
registry.map(|r| {
r.check_registry(Key::Collection, PluginType::check_burn, &mut checks);
r
})
})
.ok()?
});
Copy link
Contributor Author

@danenbm danenbm Mar 8, 2024

Choose a reason for hiding this comment

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

Note this is a case of lost error propagation that was previously fixed in transfer, but still incorrect in burn. It is now fixed in the common function that both use.

@danenbm danenbm merged commit 5f21cd4 into main Mar 8, 2024
12 of 14 checks passed
@danenbm danenbm deleted the danenbm/common-validation-fns branch March 8, 2024 16:54
asset,
collection,
plugin_validate_fp,
)? || approved;
Copy link
Contributor

@kstepanovdev kstepanovdev Mar 11, 2024

Choose a reason for hiding this comment

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

Sorry for necroposting.
Wouldn't approve variable in the second place prevent short-circuit evaluation? I'm not quite sure tbh, though seems it could.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that's by design. Validations by plugins can both approve and deny an action (e.g. the Freeze plugin can reject a transfer because the token is frozen) so we always want to evaluate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think its a good callout, such that a comment would be helpful to someone looking at the code. So I added one that basically explains this reason with pretty much the response above:
f088080

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants