-
Notifications
You must be signed in to change notification settings - Fork 611
feat(aztec-nr/public): dispatch function #8821
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8763204
done
fcarreiro 611f8ba
Don't use if-else in dispatch
asterite a5a8d28
include dispatch in report
fcarreiro 3665c84
address changes
fcarreiro 797353e
fix void early return
fcarreiro 0c6a711
mark dispatch as public
fcarreiro cc5b604
Revert "include dispatch in report"
fcarreiro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
noir-projects/aztec-nr/aztec/src/macros/dispatch/mod.nr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| use super::utils::compute_fn_selector; | ||
|
|
||
| /// Returns an `fn public_dispatch(...)` function for the given module that's assumed to be an Aztec contract. | ||
| pub comptime fn generate_public_dispatch(m: Module) -> Quoted { | ||
| let functions = m.functions(); | ||
| let functions = functions.filter(|function: FunctionDefinition| function.has_named_attribute("public")); | ||
|
|
||
| let unit = get_type::<()>(); | ||
|
|
||
| let ifs = functions.map( | ||
| |function: FunctionDefinition| { | ||
| let name = function.name(); | ||
| let parameters = function.parameters(); | ||
| let return_type = function.return_type(); | ||
|
|
||
| let selector: Field = compute_fn_selector(function); | ||
|
|
||
| let mut parameters_size = 0; | ||
| for param in parameters { | ||
| parameters_size += size_in_fields(param.1); | ||
| } | ||
|
|
||
| let initial_read = if parameters.len() == 0 { | ||
| quote {} | ||
| } else { | ||
| // The initial calldata_copy offset is 1 to skip the Field selector | ||
| // The expected calldata is the serialization of | ||
| // - FunctionSelector: the selector of the function intended to dispatch | ||
| // - Parameters: the parameters of the function intended to dispatch | ||
| // That is, exactly what is expected for a call to the target function, | ||
| // but with a selector added at the beginning. | ||
| quote { | ||
| let input_calldata: [Field; $parameters_size] = dep::aztec::context::public_context::calldata_copy(1, $parameters_size); | ||
| let mut reader = dep::aztec::protocol_types::utils::reader::Reader::new(input_calldata); | ||
| } | ||
| }; | ||
|
|
||
| let mut parameter_index = 0; | ||
| let reads = parameters.map(|param: (Quoted, Type)| { | ||
| let param_name = f"arg{parameter_index}".quoted_contents(); | ||
| let param_type = param.1; | ||
| let read = quote { | ||
| let $param_name: $param_type = reader.read_struct(dep::aztec::protocol_types::traits::Deserialize::deserialize); | ||
| }; | ||
| parameter_index += 1; | ||
| quote { $read } | ||
| }); | ||
| let read = reads.join(quote { }); | ||
|
|
||
| let mut args = &[]; | ||
| for parameter_index in 0..parameters.len() { | ||
| let param_name = f"arg{parameter_index}".quoted_contents(); | ||
| args = args.push_back(quote { $param_name }); | ||
| } | ||
|
|
||
| let args = args.join(quote { , }); | ||
| let call = quote { $name($args) }; | ||
|
|
||
| let return_code = if return_type == unit { | ||
| quote { | ||
| $call; | ||
| // Force early return. | ||
| dep::aztec::context::public_context::avm_return([]); | ||
| } | ||
| } else { | ||
| quote { | ||
| let return_value = dep::aztec::protocol_types::traits::Serialize::serialize($call); | ||
| dep::aztec::context::public_context::avm_return(return_value); | ||
| } | ||
| }; | ||
|
|
||
| let if_ = quote { | ||
| if selector == $selector { | ||
| $initial_read | ||
| $read | ||
| $return_code | ||
| } | ||
| }; | ||
| if_ | ||
| } | ||
| ); | ||
|
|
||
| if ifs.len() == 0 { | ||
| // No dispatch function if there are no public functions | ||
| quote {} | ||
| } else { | ||
| let ifs = ifs.push_back(quote { panic(f"Unknown selector") }); | ||
| let dispatch = ifs.join(quote { }); | ||
|
|
||
| let body = quote { | ||
| // We mark this as public because our whole system depends on public | ||
| // functions having this attribute. However, the public MACRO will | ||
| // handle the public_dispatch function specially and do nothing. | ||
| #[public] | ||
| unconstrained pub fn public_dispatch(selector: Field) { | ||
| $dispatch | ||
| } | ||
| }; | ||
|
|
||
| body | ||
| } | ||
| } | ||
|
|
||
| comptime fn size_in_fields(typ: Type) -> u32 { | ||
|
Collaborator
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. I'm stealing this util for other macros instead of relying on |
||
| if typ.as_slice().is_some() { | ||
| panic(f"Can't determine size in fields of Slice type") | ||
| } else { | ||
| let size = array_size_in_fields(typ); | ||
| let size = size.or_else(|| struct_size_in_fields(typ)); | ||
| let size = size.or_else(|| tuple_size_in_fields(typ)); | ||
| size.unwrap_or(1) | ||
| } | ||
| } | ||
|
|
||
| comptime fn array_size_in_fields(typ: Type) -> Option<u32> { | ||
| typ.as_array().and_then( | ||
| |typ: (Type, Type)| { | ||
| let (typ, element_size) = typ; | ||
| element_size.as_constant().map(|x: u32| { | ||
| x * size_in_fields(typ) | ||
| }) | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| comptime fn struct_size_in_fields(typ: Type) -> Option<u32> { | ||
| typ.as_struct().map( | ||
| |typ: (StructDefinition, [Type])| { | ||
| let struct_type = typ.0; | ||
| let mut size = 0; | ||
| for field in struct_type.fields() { | ||
| size += size_in_fields(field.1); | ||
| } | ||
| size | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| comptime fn tuple_size_in_fields(typ: Type) -> Option<u32> { | ||
| typ.as_tuple().map( | ||
| |types: [Type]| { | ||
| let mut size = 0; | ||
| for typ in types { | ||
| size += size_in_fields(typ); | ||
| } | ||
| size | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| comptime fn get_type<T>() -> Type { | ||
| let t: T = std::mem::zeroed(); | ||
| std::meta::type_of(t) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.