-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/ab/contract-entrypoint-dispatch'…
… into fc/avm-dispatch-changes
- Loading branch information
Showing
13 changed files
with
574 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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 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 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
152 changes: 152 additions & 0 deletions
152
noir-projects/aztec-nr/aztec/src/macros/dispatch/mod.nr
This file contains 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,152 @@ | ||
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 param_sizes = parameters.map(|param: (Quoted, Type)| { | ||
size_in_fields(param.1) | ||
}); | ||
|
||
let mut parameters_size = 0; | ||
for param_size in param_sizes { | ||
parameters_size += param_size; | ||
} | ||
|
||
let initial_read = if parameters.len() == 0 { | ||
quote {} | ||
} else { | ||
// The initial calldata_copy offset is 1 to skip the Field selector | ||
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 mut offset = 0; | ||
let reads = parameters.map(|param: (Quoted, Type)| { | ||
// let param_size = param_sizes[parameter_index]; | ||
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((($param_type)::deserialize)); | ||
let $param_name: $param_type = reader.read_struct(dep::aztec::protocol_types::traits::Deserialize::deserialize); | ||
// let ($param_name, _) = dep::aztec::protocol_types::traits::FromCalldata::from_calldata(input_calldata, $offset); | ||
}; | ||
// offset += param_size; | ||
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 } | ||
} 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 { else }); | ||
|
||
let body = quote { | ||
unconstrained pub fn public_dispatch(selector: Field) { | ||
$dispatch | ||
} | ||
}; | ||
|
||
// println(body); | ||
|
||
body | ||
} | ||
} | ||
|
||
comptime fn size_in_fields(typ: Type) -> u32 { | ||
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 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 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 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
5 changes: 3 additions & 2 deletions
5
noir-projects/noir-protocol-circuits/crates/types/src/address/aztec_address.nr
This file contains 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
3 changes: 2 additions & 1 deletion
3
noir-projects/noir-protocol-circuits/crates/types/src/address/eth_address.nr
This file contains 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
Oops, something went wrong.