-
Notifications
You must be signed in to change notification settings - Fork 599
refactor: cleaning up flow of calls generation #18265
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| use crate::macros::{ | ||
| calls_generation::internal_functions::generate_call_internal_struct, | ||
| calls_generation::{ | ||
| external_functions::{ | ||
| generate_external_function_calls, generate_external_function_self_calls_structs, | ||
| }, | ||
| internal_functions::generate_call_internal_struct, | ||
| }, | ||
| dispatch::generate_public_dispatch, | ||
| functions::{self_call_registry, stub_registry, utils::check_each_fn_macroified}, | ||
| functions::utils::check_each_fn_macroified, | ||
| internals_functions_generation::{create_fn_abi_exports, process_functions}, | ||
| notes::NOTES, | ||
| storage::STORAGE_LAYOUT_NAME, | ||
|
|
@@ -19,8 +24,9 @@ pub comptime fn aztec(m: Module) -> Quoted { | |
| // with `static_assert(false, ...)` to prevent them from being called directly from within the contract. | ||
| let functions = process_functions(m); | ||
|
|
||
| // We generate structs and their implementations necessary for convenient functions calls. | ||
| let interface = generate_contract_interface(m); | ||
| let self_call_structs = generate_self_call_structs(m); | ||
| let self_call_structs = generate_external_function_self_calls_structs(m); | ||
| let call_internal_struct = generate_call_internal_struct(m); | ||
|
|
||
| // We generate ABI exports for all the external functions in the contract. | ||
|
|
@@ -67,13 +73,9 @@ pub comptime fn aztec(m: Module) -> Quoted { | |
| } | ||
|
|
||
| comptime fn generate_contract_interface(m: Module) -> Quoted { | ||
| let calls = generate_external_function_calls(m); | ||
|
|
||
| let module_name = m.name(); | ||
| let contract_stubs = stub_registry::get(m); | ||
| let fn_stubs_quote = if contract_stubs.is_some() { | ||
| contract_stubs.unwrap().join(quote {}) | ||
| } else { | ||
| quote {} | ||
| }; | ||
|
|
||
| let has_storage_layout = module_has_storage(m) & STORAGE_LAYOUT_NAME.get(m).is_some(); | ||
| let storage_layout_getter = if has_storage_layout { | ||
|
|
@@ -102,7 +104,7 @@ comptime fn generate_contract_interface(m: Module) -> Quoted { | |
| } | ||
|
|
||
| impl $module_name { | ||
| $fn_stubs_quote | ||
| $calls | ||
|
|
||
| pub fn at( | ||
| addr: aztec::protocol_types::address::AztecAddress | ||
|
|
@@ -313,71 +315,3 @@ comptime fn generate_process_message() -> Quoted { | |
| } | ||
| } | ||
| } | ||
|
|
||
| /// Generates helper structs for convenient self-invocation of contract functions: | ||
| /// | ||
| /// - `CallSelf`: Call your own private or public functions, e.g.: | ||
| /// `self.call_self.some_private_function(args)` | ||
| /// - `CallSelfStatic`: Call your own view (static) functions, e.g.: | ||
| /// `self.call_self_static.some_view_function(args)` | ||
| /// - `EnqueueSelf`: Enqueue your own (non-view) public functions for deferred execution, e.g.: | ||
| /// `self.enqueue_self.some_public_function(args)` | ||
| /// - `EnqueueSelfStatic`: Enqueue your own view public functions, e.g.: | ||
| /// `self.enqueue_self_static.some_view_function(args)` | ||
| comptime fn generate_self_call_structs(m: Module) -> Quoted { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replaced with |
||
| let stubs = self_call_registry::get_all_self_call_stubs(m); | ||
|
|
||
| // We need to destructure the stubs here because accessing struct fields in a quote block is not supported. | ||
| let call_self_private_methods = stubs.call_self_private_methods; | ||
| let call_self_public_methods = stubs.call_self_public_methods; | ||
| let enqueue_self_methods = stubs.enqueue_self_methods; | ||
| let call_self_private_static_methods = stubs.call_self_private_static_methods; | ||
| let call_self_public_static_methods = stubs.call_self_public_static_methods; | ||
| let enqueue_self_static_methods = stubs.enqueue_self_static_methods; | ||
|
|
||
| quote { | ||
| pub struct CallSelf<Context> { | ||
| pub address: dep::aztec::protocol_types::address::AztecAddress, | ||
| pub context: Context, | ||
| } | ||
|
|
||
| impl CallSelf<&mut dep::aztec::context::private_context::PrivateContext> { | ||
| $call_self_private_methods | ||
| } | ||
|
|
||
| impl CallSelf<dep::aztec::context::public_context::PublicContext> { | ||
| $call_self_public_methods | ||
| } | ||
|
|
||
| pub struct CallSelfStatic<Context> { | ||
| pub address: dep::aztec::protocol_types::address::AztecAddress, | ||
| pub context: Context, | ||
| } | ||
|
|
||
| impl CallSelfStatic<&mut dep::aztec::context::private_context::PrivateContext> { | ||
| $call_self_private_static_methods | ||
| } | ||
|
|
||
| impl CallSelfStatic<dep::aztec::context::public_context::PublicContext> { | ||
| $call_self_public_static_methods | ||
| } | ||
|
|
||
| pub struct EnqueueSelf<Context> { | ||
| pub address: dep::aztec::protocol_types::address::AztecAddress, | ||
| pub context: Context, | ||
| } | ||
|
|
||
| impl EnqueueSelf<&mut dep::aztec::context::private_context::PrivateContext> { | ||
| $enqueue_self_methods | ||
| } | ||
|
|
||
| pub struct EnqueueSelfStatic<Context> { | ||
| pub address: dep::aztec::protocol_types::address::AztecAddress, | ||
| pub context: Context, | ||
| } | ||
|
|
||
| impl EnqueueSelfStatic<&mut dep::aztec::context::private_context::PrivateContext> { | ||
| $enqueue_self_static_methods | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| use crate::macros::{ | ||
| calls_generation::external_functions_stubs::{ | ||
| create_private_self_call_stub, create_private_static_stub, create_private_stub, | ||
| create_public_self_call_static_stub, create_public_self_call_stub, | ||
| create_public_self_enqueue_static_stub, create_public_self_enqueue_stub, | ||
| create_public_static_stub, create_public_stub, create_utility_stub, | ||
| }, | ||
| internals_functions_generation::external_functions_registry, | ||
| utils::is_fn_view, | ||
| }; | ||
|
|
||
| /// Generates external function calls that are to be injected into the contract interface. | ||
| /// | ||
| /// The contract interface enables you to form calls such as `Token::at(address).some_function(args)`. | ||
| /// This function generates the implementation of `some_function` within the interface. | ||
| pub(crate) comptime fn generate_external_function_calls(m: Module) -> Quoted { | ||
| let private_functions = external_functions_registry::get_private_functions(m); | ||
| let public_functions = external_functions_registry::get_public_functions(m); | ||
| let utility_functions = external_functions_registry::get_utility_functions(m); | ||
|
|
||
| let private_contract_methods = private_functions | ||
| .map(|function| { | ||
| if is_fn_view(function) { | ||
| create_private_static_stub(function) | ||
| } else { | ||
| create_private_stub(function) | ||
| } | ||
| }) | ||
| .join(quote {}); | ||
|
|
||
| let public_contract_methods = public_functions | ||
| .map(|function| { | ||
| if is_fn_view(function) { | ||
| create_public_static_stub(function) | ||
| } else { | ||
| create_public_stub(function) | ||
| } | ||
| }) | ||
| .join(quote {}); | ||
|
|
||
| let utility_contract_methods = | ||
| utility_functions.map(|function| create_utility_stub(function)).join(quote {}); | ||
|
|
||
| quote { | ||
| $private_contract_methods | ||
| $public_contract_methods | ||
| $utility_contract_methods | ||
| } | ||
| } | ||
|
|
||
| /// Generates helper structs for convenient self-invocation of contract functions: | ||
| /// | ||
| /// - `CallSelf`: Call your own private or public functions, e.g.: | ||
| /// `self.call_self.some_private_function(args)` | ||
| /// - `CallSelfStatic`: Call your own view (static) functions, e.g.: | ||
| /// `self.call_self_static.some_view_function(args)` | ||
| /// - `EnqueueSelf`: Enqueue your own (non-view) public functions for deferred execution, e.g.: | ||
| /// `self.enqueue_self.some_public_function(args)` | ||
| /// - `EnqueueSelfStatic`: Enqueue your own view public functions, e.g.: | ||
| /// `self.enqueue_self_static.some_view_function(args)` | ||
| pub(crate) comptime fn generate_external_function_self_calls_structs(m: Module) -> Quoted { | ||
| let private_functions = external_functions_registry::get_private_functions(m); | ||
| let public_functions = external_functions_registry::get_public_functions(m); | ||
|
|
||
| let call_self_private_methods = private_functions | ||
| .map(|function| { | ||
| if is_fn_view(function) { | ||
| quote {} | ||
| } else { | ||
| create_private_self_call_stub(function, false) | ||
| } | ||
| }) | ||
| .join(quote {}); | ||
|
|
||
| let call_self_private_static_methods = private_functions | ||
| .map(|function| { | ||
| if is_fn_view(function) { | ||
| create_private_self_call_stub(function, true) | ||
| } else { | ||
| quote {} | ||
| } | ||
| }) | ||
| .join(quote {}); | ||
|
|
||
| let call_self_public_methods = public_functions | ||
| .map(|function| { | ||
| if is_fn_view(function) { | ||
| quote {} | ||
| } else { | ||
| create_public_self_call_stub(function) | ||
| } | ||
| }) | ||
| .join(quote {}); | ||
|
|
||
| let call_self_public_static_methods = public_functions | ||
| .map(|function| { | ||
| if is_fn_view(function) { | ||
| create_public_self_call_static_stub(function) | ||
| } else { | ||
| quote {} | ||
| } | ||
| }) | ||
| .join(quote {}); | ||
|
|
||
| let enqueue_self_methods = public_functions | ||
| .map(|function| { | ||
| if is_fn_view(function) { | ||
| quote {} | ||
| } else { | ||
| create_public_self_enqueue_stub(function) | ||
| } | ||
| }) | ||
| .join(quote {}); | ||
|
|
||
| let enqueue_self_static_methods = public_functions | ||
| .map(|function| { | ||
| if is_fn_view(function) { | ||
| create_public_self_enqueue_static_stub(function) | ||
| } else { | ||
| quote {} | ||
| } | ||
| }) | ||
| .join(quote {}); | ||
|
|
||
| quote { | ||
| pub struct CallSelf<Context> { | ||
| pub address: dep::aztec::protocol_types::address::AztecAddress, | ||
| pub context: Context, | ||
| } | ||
|
|
||
| impl CallSelf<&mut dep::aztec::context::private_context::PrivateContext> { | ||
| $call_self_private_methods | ||
| } | ||
|
|
||
| impl CallSelf<dep::aztec::context::public_context::PublicContext> { | ||
| $call_self_public_methods | ||
| } | ||
|
|
||
| pub struct CallSelfStatic<Context> { | ||
| pub address: dep::aztec::protocol_types::address::AztecAddress, | ||
| pub context: Context, | ||
| } | ||
|
|
||
| impl CallSelfStatic<&mut dep::aztec::context::private_context::PrivateContext> { | ||
| $call_self_private_static_methods | ||
| } | ||
|
|
||
| impl CallSelfStatic<dep::aztec::context::public_context::PublicContext> { | ||
| $call_self_public_static_methods | ||
| } | ||
|
|
||
| pub struct EnqueueSelf<Context> { | ||
| pub address: dep::aztec::protocol_types::address::AztecAddress, | ||
| pub context: Context, | ||
| } | ||
|
|
||
| impl EnqueueSelf<&mut dep::aztec::context::private_context::PrivateContext> { | ||
| $enqueue_self_methods | ||
| } | ||
|
|
||
| pub struct EnqueueSelfStatic<Context> { | ||
| pub address: dep::aztec::protocol_types::address::AztecAddress, | ||
| pub context: Context, | ||
| } | ||
|
|
||
| impl EnqueueSelfStatic<&mut dep::aztec::context::private_context::PrivateContext> { | ||
| $enqueue_self_static_methods | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,10 +4,7 @@ | |
| //! | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved it from I want the |
||
| //! ExternalContract.at(address).some_method(arg1, arg2).enqueue() | ||
|
|
||
| use crate::macros::{ | ||
| functions::{self_call_registry, stub_registry as call_registry}, | ||
| utils::{AsStrQuote, compute_fn_selector, is_fn_view}, | ||
| }; | ||
| use crate::macros::utils::{AsStrQuote, compute_fn_selector}; | ||
| use protocol_types::meta::utils::derive_serialization_quotes; | ||
| use std::meta::unquote; | ||
|
|
||
|
|
@@ -21,47 +18,6 @@ comptime global FROM_FIELD: TypedExpr = { | |
| .as_typed_expr() | ||
| }; | ||
|
|
||
| // Call registry is used to register calls to the contract from other contracts. | ||
| // Self-call registry is used to register calls to the contract from itself. | ||
|
|
||
| pub comptime fn register_private_fn_stubs(f: FunctionDefinition) { | ||
| if is_fn_view(f) { | ||
| call_registry::register(f.module(), create_private_static_stub(f)); | ||
| self_call_registry::register_call_self_private_static( | ||
| f.module(), | ||
| create_private_self_call_stub(f, true), | ||
| ); | ||
| } else { | ||
| call_registry::register(f.module(), create_private_stub(f)); | ||
| self_call_registry::register_call_self_private( | ||
| f.module(), | ||
| create_private_self_call_stub(f, false), | ||
| ); | ||
| }; | ||
| } | ||
|
|
||
| pub comptime fn register_public_fn_stubs(f: FunctionDefinition) { | ||
| if is_fn_view(f) { | ||
| call_registry::register(f.module(), create_public_static_stub(f)); | ||
| self_call_registry::register_call_self_public_static( | ||
| f.module(), | ||
| create_public_self_call_static_stub(f), | ||
| ); | ||
| self_call_registry::register_enqueue_self_static( | ||
| f.module(), | ||
| create_public_self_enqueue_static_stub(f), | ||
| ); | ||
| } else { | ||
| call_registry::register(f.module(), create_public_stub(f)); | ||
| self_call_registry::register_call_self_public(f.module(), create_public_self_call_stub(f)); | ||
| self_call_registry::register_enqueue_self(f.module(), create_public_self_enqueue_stub(f)); | ||
| }; | ||
| } | ||
|
|
||
| pub comptime fn register_utility_fn_stub(f: FunctionDefinition) { | ||
| call_registry::register(f.module(), create_utility_stub(f)); | ||
| } | ||
|
|
||
| /// Utility function creating stubs used by all the stub functions in this file. | ||
| comptime fn create_stub_base( | ||
| f: FunctionDefinition, | ||
|
|
@@ -100,7 +56,7 @@ comptime fn create_stub_base( | |
| ) | ||
| } | ||
|
|
||
| comptime fn create_private_stub(f: FunctionDefinition) -> Quoted { | ||
| pub(crate) comptime fn create_private_stub(f: FunctionDefinition) -> Quoted { | ||
| let (fn_name, fn_parameters_list, serialized_args_array_construction, serialized_args_array_name, fn_name_str, fn_name_len, fn_selector) = | ||
| create_stub_base(f); | ||
| let fn_return_type = f.return_type(); | ||
|
|
@@ -120,7 +76,7 @@ comptime fn create_private_stub(f: FunctionDefinition) -> Quoted { | |
| } | ||
| } | ||
|
|
||
| comptime fn create_private_static_stub(f: FunctionDefinition) -> Quoted { | ||
| pub(crate) comptime fn create_private_static_stub(f: FunctionDefinition) -> Quoted { | ||
| let (fn_name, fn_parameters_list, serialized_args_array_construction, serialized_args_array_name, fn_name_str, fn_name_len, fn_selector) = | ||
| create_stub_base(f); | ||
| let fn_return_type = f.return_type(); | ||
|
|
@@ -139,7 +95,7 @@ comptime fn create_private_static_stub(f: FunctionDefinition) -> Quoted { | |
| } | ||
| } | ||
|
|
||
| comptime fn create_public_stub(f: FunctionDefinition) -> Quoted { | ||
| pub(crate) comptime fn create_public_stub(f: FunctionDefinition) -> Quoted { | ||
| let (fn_name, fn_parameters_list, serialized_args_array_construction, serialized_args_array_name, fn_name_str, fn_name_len, fn_selector) = | ||
| create_stub_base(f); | ||
| let fn_return_type = f.return_type(); | ||
|
|
@@ -159,7 +115,7 @@ comptime fn create_public_stub(f: FunctionDefinition) -> Quoted { | |
| } | ||
| } | ||
|
|
||
| comptime fn create_public_static_stub(f: FunctionDefinition) -> Quoted { | ||
| pub(crate) comptime fn create_public_static_stub(f: FunctionDefinition) -> Quoted { | ||
| let (fn_name, fn_parameters_list, serialized_args_array_construction, serialized_args_array_name, fn_name_str, fn_name_len, fn_selector) = | ||
| create_stub_base(f); | ||
| let fn_return_type = f.return_type(); | ||
|
|
@@ -178,7 +134,7 @@ comptime fn create_public_static_stub(f: FunctionDefinition) -> Quoted { | |
| } | ||
| } | ||
|
|
||
| comptime fn create_utility_stub(f: FunctionDefinition) -> Quoted { | ||
| pub(crate) comptime fn create_utility_stub(f: FunctionDefinition) -> Quoted { | ||
| let (fn_name, fn_parameters_list, serialized_args_array_construction, serialized_args_array_name, fn_name_str, fn_name_len, fn_selector) = | ||
| create_stub_base(f); | ||
| let fn_return_type = f.return_type(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somewhat unrelated but felt like the original function name was no longer descriptive enough.