feat: Blacklist attributes not to forward to ContractExt methods#952
feat: Blacklist attributes not to forward to ContractExt methods#952mooori wants to merge 4 commits intonear:masterfrom
Conversation
|
Another open PR that's about to use attributes with use darling::util::PathList;
use darling::FromMeta;
#[derive(Default, FromMeta, Clone, Debug)]
pub struct MacroConfig {
pub events: Option<EventsConfig>,
pub blacklist_ext_fn_attrs: Option<PathList>,
}
pub struct EventsConfig {
standard: String,
another_config_value: T, // darling supports many types
}Using #[near_bindgen(events(standard = "nepXXX", another_config_value = "..."))]
pub enum MyEvents { /* ... */ }
#[near_bindgen(blacklist_ext_fn_attrs(some_plugin_attr))]
impl Contract { /* ... */ } |
austinabell
left a comment
There was a problem hiding this comment.
Seems fine. A little unfortunate to add a dependency like this for something that doesn't affect the vast majority, but this seems reasonable if it enables more tooling.
None of the _Ext methods should require the attributes in general, possibly an oversight there. I'm not sure what developers would prefer in general.
I can't think of any attribute that a developer would want to keep, seems like there might be another option of just removing the attributes and if developers mention breakages we could do something like this? Just seems like a bit of an unintuitive pattern to have the #[near_bindgen(blacklist_ext_fn_attrs(attr1, attr2))] pattern. @itegulov wdyt?
Also, one other thought is that blocklist naming would be more clear, just as an alternative to consider.
Yes, also blacklisting is an extra step that has to be taken by contract developers that use plugin attributes. So not having to do this because attributes aren't forwarded to |
|
Closing this since it shouldn't be required anymore now that #959 is merged. If there'll ever be a need to allow contracts dev to whitelist some attributes to forward to |
Background
Smart contract plugins like
near-pluginsuse attribute macros to modify contract methods.For example,
access_control_anyin the following snippet injects code that panics if the caller is not a grantee ofRole::AorRole::B:Problem
#[near_bindgen]will create a structContractExtwith:Proposed solution
Make
#[near_bindgen]accept a blacklist of attributes which it should not forward to methods of the automatically generated*Exttype. This PR addsblacklist_ext_fn_attrsfor this purpose:Implementation overview
MacroConfigwhich allows configuring the behavior of macros.ContractExtmethods are created.Backwards compatibility
This should be backwards compatible since:
near_bindgenare ignored (ref. Remove unused tokens in compilation test #951).#[near_bindgen]without attributes will result in using the default value ofMacroConfigwhich does not alter behavior.Thoughts / Question
Maybe function attributes shouldn't be forwarded to
Extmethods by default? In that case, it should be possible to turn the blacklist feature proposed here into a whitelist feature. Though that would be a breaking change.