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

Add macros package and the with_components macro #1282

Open
wants to merge 29 commits into
base: main
Choose a base branch
from

Conversation

ericnordelo
Copy link
Member

@ericnordelo ericnordelo commented Jan 2, 2025

Quickstart #1258

NOTE: A summary of the features included in the macro is added below after the example:

Example:

Simplifies this:

#[starknet::contract]
pub mod MyToken {
    use openzeppelin_access::ownable::OwnableComponent;
    use openzeppelin_token::erc20::{ERC20Component, ERC20HooksEmptyImpl};
    use starknet::ContractAddress;

    component!(path: ERC20Component, storage: erc20, event: ERC20Event);
    component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);

    impl ERC20InternalImpl = ERC20Component::InternalImpl<ContractState>;
    impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;

    #[storage]
    pub struct Storage {
        #[substorage(v0)]
        erc20: ERC20Component::Storage,
        #[substorage(v0)]
        ownable: OwnableComponent::Storage,
    }

    #[event]
    #[derive(Drop, starknet::Event)]
    enum Event {
        #[flat]
        ERC20Event: ERC20Component::Event,
        #[flat]
        OwnableEvent: OwnableComponent::Event,
    }

    #[constructor]
    fn constructor(ref self: ContractState, owner: ContractAddress) {
        self.ownable.initializer(owner);
        self.erc20.initializer("MyToken", "MTK");
    }
}

Into this:

#[with_components(ERC20, Ownable)]
#[starknet::contract]
pub mod MyToken {
    use openzeppelin_token::erc20::ERC20HooksEmptyImpl;
    use starknet::ContractAddress;

    #[storage]
    pub struct Storage {}

    #[constructor]
    fn constructor(ref self: ContractState, owner: ContractAddress) {
        self.ownable.initializer(owner);
        self.erc20.initializer("MyToken", "MTK");
    }
}

Summary of features:

IT DOES:

  • Accepts multiple components from an internal whitelist (currently only ERC20 and Ownable for the POC).
  • Validates that the contract has the #[startknet::contract] attribute, shows an error in compilation otherwise.
  • Validates that the contract has the corresponding initializers in the constructor, shows a warning listing the missing initializers otherwise.
  • Adds the use clauses for each component (i.e: use openzeppelin_access::ownable::OwnableComponent;)
  • Adds the corresponding component! macros (i.e: component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);).
  • Adds the corresponding internal implementations (i.e: impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;)
  • Adds the storage entries to the Storage struct, using the lowercase version of the component name without the component keyword (i.e: ownable: OwnableComponent::Storage,)
  • Adds the event entries to the Event enum, and creates the enum if it is missing.

IT DOES NOT:

  • Adds the external implementations or hooks.
  • Create the constructor and/or initializers.

Things to be considered still:

  • openzeppelin_access vs openzeppelin::access in use clauses (configurable notation?)
  • Configurable storage and event entries per component.
  • Add default external embedded impls?
  • Add warnings to other things, like missing camelCase interface.

Some may be addressed in different PRs/issues.

NOTE: I have plans to include a with_component macro which will accept only a single component but more configuration, like replacing the storage entry. People then may use both macros together (with_components and with_component)

PR Checklist

  • Tests
  • Documentation
  • Added entry to CHANGELOG.md
  • Tried the feature on a public network

@ericnordelo ericnordelo linked an issue Jan 6, 2025 that may be closed by this pull request
@ericnordelo ericnordelo marked this pull request as ready for review January 22, 2025 14:10
Copy link
Collaborator

@andrew-fleming andrew-fleming left a comment

Choose a reason for hiding this comment

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

Fantastic work on this @ericnordelo! I left some comments

Also, since so much of the component boilerplate is abstracted away, I don't think it's super obvious what the storage entry is for a given component. It'll be documented, for sure, but would it make sense just to simplify it further and use the same capitalized name as the whitelisted component i.e.

#[with_components(ERC20, Ownable)]
#[starknet::contract]
pub mod MyToken {
    (...)

    #[constructor]
    fn constructor(ref self: ContractState, owner: ContractAddress) {
        self.Ownable.initializer(owner);
        self.ERC20.initializer("MyToken", "MTK");
    }
}

This gives a point of reference inside the contract as opposed to attempting, for example, reentrancy_guard or reentrancyguard

docs/modules/ROOT/pages/api/macros.adoc Outdated Show resolved Hide resolved
packages/macros/src/tests/test_with_components.rs Outdated Show resolved Hide resolved
packages/macros/src/tests/test_with_components.rs Outdated Show resolved Hide resolved
packages/macros/src/with_components.rs Outdated Show resolved Hide resolved
packages/macros/src/tests/test_with_components.rs Outdated Show resolved Hide resolved
packages/macros/src/with_components.rs Outdated Show resolved Hide resolved
"AccessControl" => Some(ComponentInfo {
name: "AccessControlComponent".to_string(),
path: "openzeppelin_access::accesscontrol::AccessControlComponent".to_string(),
storage: "accesscontrol".to_string(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here. I know the lib isn't entirely consistent (might be worth addressing in a separate issue), but I think it could get weird when we have timelock_controller vs accesscontrol and reentrancyguard

Copy link
Member Author

Choose a reason for hiding this comment

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

I will refactor them here (for macros). We may update presets and mocks in a separate issue later.

@ericnordelo
Copy link
Member Author

This gives a point of reference inside the contract as opposed to attempting, for example, reentrancy_guard or reentrancyguard

Since developers are already using snake case for storage members, I vote we stay with snake case, but split the words accordingly.

@andrew-fleming
Copy link
Collaborator

This gives a point of reference inside the contract as opposed to attempting, for example, reentrancy_guard or reentrancyguard

Since developers are already using snake case for storage members, I vote we stay with snake case, but split the words accordingly.

This is true...no strong pushback against keeping snake case, I just think reusing the name could be a bit more intuitive. If we wanted to make a change like this, it'd be better to do it now with the feature

Copy link
Collaborator

@immrsd immrsd left a comment

Choose a reason for hiding this comment

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

Well done, Eric, great job!

I left several minor suggestions of how we could improve the code structure

Comment on lines +58 to +61
if component_info.is_none() {
return ProcMacroResult::new(TokenStream::empty()).with_diagnostics(diagnostics);
}
components_info.push(component_info.unwrap());
Copy link
Collaborator

Choose a reason for hiding this comment

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

To avoid unsafe unwrapping when possible:

Suggested change
if component_info.is_none() {
return ProcMacroResult::new(TokenStream::empty()).with_diagnostics(diagnostics);
}
components_info.push(component_info.unwrap());
if let Some(info) = component_info {
components_info.push(info);
} else {
return ProcMacroResult::new(TokenStream::empty()).with_diagnostics(diagnostics);
}

Comment on lines +66 to +75
let parsed = db.parse_virtual(item_stream.to_string());
if parsed.is_err() {
let error_message = parsed.err().unwrap().format(&db);
let error = Diagnostic::error(error_message);
return ProcMacroResult::new(TokenStream::empty()).with_diagnostics(error.into());
}

// 3. Build the patch
let node = parsed.unwrap();
let (content, mut diagnostics) = build_patch(&db, node, components_info.clone());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
let parsed = db.parse_virtual(item_stream.to_string());
if parsed.is_err() {
let error_message = parsed.err().unwrap().format(&db);
let error = Diagnostic::error(error_message);
return ProcMacroResult::new(TokenStream::empty()).with_diagnostics(error.into());
}
// 3. Build the patch
let node = parsed.unwrap();
let (content, mut diagnostics) = build_patch(&db, node, components_info.clone());
let (content, mut diagnostics) = match db.parse_virtual(item_stream.to_string()) {
Ok(node) => build_patch(&db, node, components_info.clone()),
Err(err) => {
let error = Diagnostic::error(err.format(&db));
return ProcMacroResult::new(TokenStream::empty()).with_diagnostics(error.into());
}
}

}
}
}
}
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 add an else clause with an error?

if let RewriteNode::Copied(copied) = node {
    ...
} else {
    // panic with an error
}

Comment on lines +196 to +207
let components_with_initializer_missing_str =
components_with_initializer_missing.join(", ");
if !components_with_initializer_missing.is_empty() {
let warning = Diagnostic::warn(formatdoc! {"
It looks like the initializers for the following components are missing:

{components_with_initializer_missing_str}

This may lead to unexpected behavior. We recommend adding the corresponding initializer calls to the constructor.
"});
warnings.push(warning);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can move the line building the components_with_initializer_missing_str into the if clause

Suggested change
let components_with_initializer_missing_str =
components_with_initializer_missing.join(", ");
if !components_with_initializer_missing.is_empty() {
let warning = Diagnostic::warn(formatdoc! {"
It looks like the initializers for the following components are missing:
{components_with_initializer_missing_str}
This may lead to unexpected behavior. We recommend adding the corresponding initializer calls to the constructor.
"});
warnings.push(warning);
}
if !components_with_initializer_missing.is_empty() {
let components_with_initializer_missing_str = components_with_initializer_missing.join(", ");
let warning = Diagnostic::warn(formatdoc! {"
It looks like the initializers for the following components are missing:
{components_with_initializer_missing_str}
This may lead to unexpected behavior. We recommend adding the corresponding initializer calls to the constructor.
"});
warnings.push(warning);
}

Comment on lines +176 to +189
let mut constructor_code = String::new();
let constructor = body.items_vec(db).into_iter().find(|item| {
matches!(item, ast::ModuleItem::FreeFunction(function_ast) if function_ast.has_attr(db, CONSTRUCTOR_ATTRIBUTE))
});
if constructor.is_some() {
// Get the constructor code (maybe we can do this without the builder)
let constructor_ast = constructor.unwrap().as_syntax_node();
let typed = ast::ModuleItem::from_syntax_node(db, constructor_ast.clone());
let constructor_rnode = RewriteNode::from_ast(&typed);

let mut builder = PatchBuilder::new_ex(db, &constructor_ast);
builder.add_modified(constructor_rnode);
(constructor_code, _) = builder.build();
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

To avoid unsafe unwrapping and to make the constructor_code var immutable:

Suggested change
let mut constructor_code = String::new();
let constructor = body.items_vec(db).into_iter().find(|item| {
matches!(item, ast::ModuleItem::FreeFunction(function_ast) if function_ast.has_attr(db, CONSTRUCTOR_ATTRIBUTE))
});
if constructor.is_some() {
// Get the constructor code (maybe we can do this without the builder)
let constructor_ast = constructor.unwrap().as_syntax_node();
let typed = ast::ModuleItem::from_syntax_node(db, constructor_ast.clone());
let constructor_rnode = RewriteNode::from_ast(&typed);
let mut builder = PatchBuilder::new_ex(db, &constructor_ast);
builder.add_modified(constructor_rnode);
(constructor_code, _) = builder.build();
}
let constructor = body.items_vec(db).into_iter().find(|item| {
matches!(item, ast::ModuleItem::FreeFunction(function_ast) if function_ast.has_attr(db, CONSTRUCTOR_ATTRIBUTE))
});
let constructor_code = if let Some(constructor) = constructor {
// Get the constructor code (maybe we can do this without the builder)
let constructor_ast = constructor.as_syntax_node();
let typed = ast::ModuleItem::from_syntax_node(db, constructor_ast.clone());
let constructor_rnode = RewriteNode::from_ast(&typed);
let mut builder = PatchBuilder::new_ex(db, &constructor_ast);
builder.add_modified(constructor_rnode);
(constructor_code, _) = builder.build();
} else {
String::new()
};

let error = Diagnostic::error(formatdoc! {"
Contract module must have the `#[{CONTRACT_ATTRIBUTE}]` attribute.
"});
return (vec![error], vec![]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
return (vec![error], vec![]);
return (vec![error], warnings);

Comment on lines +501 to +502
/// Allowed components are:
/// `ERC20`, `Ownable`
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should update the list of the allowed components:

Suggested change
/// Allowed components are:
/// `ERC20`, `Ownable`
/// Allowed components are:
/// `ERC20`, `Ownable`

///
/// Allowed components are:
/// `ERC20`, `Ownable`
fn get_component_info(name: &str) -> (Option<ComponentInfo>, Diagnostics) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess it's better to change the function's output type to Result<ComponentInfo, Diagnostics>. It would make both the function itself and the code calling it more clear

Suggested change
fn get_component_info(name: &str) -> (Option<ComponentInfo>, Diagnostics) {
fn get_component_info(name: &str) -> Result<ComponentInfo, Diagnostics> {

Comment on lines +475 to +483
pub struct ComponentInfo {
pub name: String,
pub path: String,
pub storage: String,
pub event: String,
pub has_initializer: bool,
pub has_immutable_config: bool,
pub internal_impls: Vec<String>,
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like we don't need owned string in the code, so we could use &str type instead of String to make code cleaner and more performant. Especially the get_component_info will benefit from it if we can get rid of all the to_string() calls

Suggested change
pub struct ComponentInfo {
pub name: String,
pub path: String,
pub storage: String,
pub event: String,
pub has_initializer: bool,
pub has_immutable_config: bool,
pub internal_impls: Vec<String>,
}
pub struct ComponentInfo {
pub name: &str,
pub path: &str,
pub storage: &str,
pub event: &str,
pub has_initializer: bool,
pub has_immutable_config: bool,
pub internal_impls: Vec<&str>,
}

Comment on lines +701 to +715
internal_impls: vec!["InternalImpl".to_string()],
}),
_ => None,
};
if info.is_none() {
let allowed_components = ALLOWED_COMPONENTS.join(", ");
let error_message = formatdoc! {"
Invalid component: {name}

Allowed components are:
{allowed_components}
"};
let error = Diagnostic::error(error_message);
return (None, error.into());
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
internal_impls: vec!["InternalImpl".to_string()],
}),
_ => None,
};
if info.is_none() {
let allowed_components = ALLOWED_COMPONENTS.join(", ");
let error_message = formatdoc! {"
Invalid component: {name}
Allowed components are:
{allowed_components}
"};
let error = Diagnostic::error(error_message);
return (None, error.into());
}
internal_impls: vec!["InternalImpl".to_string()],
}),
_ => {
let allowed_components = ALLOWED_COMPONENTS.join(", ");
let error_message = formatdoc! {"
Invalid component: {name}
Allowed components are:
{allowed_components}
"};
let error = Diagnostic::error(error_message);
Err(error.into())
}
}

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.

Provide macros as part of the library
3 participants