Skip to content

Commit

Permalink
feat!: moving compute_selector to FunctionSelector (#3806)
Browse files Browse the repository at this point in the history
Fixes #3681
  • Loading branch information
benesjan authored and sirasistant committed Jan 4, 2024
1 parent ce02b01 commit 50925ea
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
14 changes: 9 additions & 5 deletions aztec_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,12 @@ const SIGNATURE_PLACEHOLDER: &str = "SIGNATURE_PLACEHOLDER";

/// Generates the impl for an event selector
///
/// TODO(https://github.com/AztecProtocol/aztec-packages/issues/3590): Make this point to aztec-nr once the issue is fixed.
/// Inserts the following code:
/// ```noir
/// impl SomeStruct {
/// fn selector() -> FunctionSelector {
/// aztec::selector::compute_selector("SIGNATURE_PLACEHOLDER")
/// protocol_types::abis::function_selector::FunctionSelector::from_signature("SIGNATURE_PLACEHOLDER")
/// }
/// }
/// ```
Expand All @@ -498,15 +499,18 @@ const SIGNATURE_PLACEHOLDER: &str = "SIGNATURE_PLACEHOLDER";
fn generate_selector_impl(structure: &NoirStruct) -> TypeImpl {
let struct_type = make_type(UnresolvedTypeData::Named(path(structure.name.clone()), vec![]));

// TODO(https://github.com/AztecProtocol/aztec-packages/issues/3590): Make this point to aztec-nr once the issue is fixed.
let selector_path = chained_path!("protocol_types", "abis", "function_selector", "FunctionSelector");
let mut from_signature_path = selector_path.clone();
from_signature_path.segments.push(ident("from_signature"));

let selector_fun_body = BlockExpression(vec![make_statement(StatementKind::Expression(call(
variable_path(chained_path!("aztec", "selector", "compute_selector")),
variable_path(from_signature_path),
vec![expression(ExpressionKind::Literal(Literal::Str(SIGNATURE_PLACEHOLDER.to_string())))],
)))]);

// Define `FunctionSelector` return type
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/3590): Make this point to aztec-nr once the issue is fixed.
let return_type_path = chained_path!("protocol_types", "abis", "function_selector", "FunctionSelector");
let return_type = FunctionReturnType::Ty(make_type(UnresolvedTypeData::Named(return_type_path, vec![])));
let return_type = FunctionReturnType::Ty(make_type(UnresolvedTypeData::Named(selector_path, vec![])));

let mut selector_fn_def = FunctionDefinition::normal(
&ident("selector"),
Expand Down
5 changes: 3 additions & 2 deletions tooling/nargo_fmt/tests/expected/contract.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
// Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
contract Benchmarking {
use dep::protocol_types::abis::function_selector::FunctionSelector;

use dep::value_note::{
utils::{increment, decrement},
value_note::{VALUE_NOTE_LEN, ValueNote, ValueNoteMethods},
Expand All @@ -11,7 +13,6 @@ contract Benchmarking {
use dep::aztec::{
context::{Context},
note::{utils as note_utils, note_getter_options::NoteGetterOptions, note_header::NoteHeader},
selector::compute_selector,
log::emit_unencrypted_log,
state_vars::{map::Map, public_state::PublicState, set::Set},
types::type_serialization::field_serialization::{FieldSerializationMethods, FIELD_SERIALIZED_LEN},
Expand Down Expand Up @@ -59,7 +60,7 @@ contract Benchmarking {
storage.balances.at(owner).write(current + value);
let _callStackItem1 = context.call_public_function(
context.this_address(),
compute_selector("broadcast(Field)"),
FunctionSelector::from_signature("broadcast(Field)"),
[owner]
);
}
Expand Down
5 changes: 3 additions & 2 deletions tooling/nargo_fmt/tests/input/contract.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
// Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
contract Benchmarking {
use dep::protocol_types::abis::function_selector::FunctionSelector;

use dep::value_note::{
utils::{increment, decrement},
value_note::{VALUE_NOTE_LEN, ValueNote, ValueNoteMethods},
Expand All @@ -11,7 +13,6 @@ contract Benchmarking {
use dep::aztec::{
context::{Context},
note::{utils as note_utils, note_getter_options::NoteGetterOptions, note_header::NoteHeader},
selector::compute_selector,
log::emit_unencrypted_log,
state_vars::{map::Map, public_state::PublicState, set::Set},
types::type_serialization::field_serialization::{FieldSerializationMethods, FIELD_SERIALIZED_LEN},
Expand Down Expand Up @@ -57,7 +58,7 @@ contract Benchmarking {
fn increment_balance(owner: Field, value: Field) {
let current = storage.balances.at(owner).read();
storage.balances.at(owner).write(current + value);
let _callStackItem1 = context.call_public_function(context.this_address(), compute_selector("broadcast(Field)"), [owner]);
let _callStackItem1 = context.call_public_function(context.this_address(), FunctionSelector::from_signature("broadcast(Field)"), [owner]);
}

// Est ultricies integer quis auctor elit sed. In nibh mauris cursus mattis molestie a iaculis.
Expand Down

0 comments on commit 50925ea

Please sign in to comment.