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

fix: remove need for duplicate attributes on each function #9244

Merged
merged 16 commits into from
Oct 18, 2024
4 changes: 1 addition & 3 deletions boxes/boxes/react/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ contract BoxReact {
}

#[private]
#['private]
#['initializer]
#[initializer]
fn constructor(
number: Field,
owner: AztecAddress,
Expand All @@ -31,7 +30,6 @@ contract BoxReact {
}

#[private]
#['private]
fn setNumber(
number: Field,
owner: AztecAddress,
Expand Down
4 changes: 1 addition & 3 deletions boxes/boxes/vanilla/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ contract Vanilla {
}

#[private]
#['private]
#['initializer]
#[initializer]
fn constructor(
number: Field,
owner: AztecAddress,
Expand All @@ -31,7 +30,6 @@ contract Vanilla {
}

#[private]
#['private]
fn setNumber(
number: Field,
owner: AztecAddress,
Expand Down
3 changes: 1 addition & 2 deletions boxes/init/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use dep::aztec::macros::aztec;
#[aztec]
contract Main {
#[private]
#['private]
#['initializer]
#[initializer]
fn constructor() { }
}
1 change: 0 additions & 1 deletion noir-projects/aztec-nr/aztec/src/macros/dispatch/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ pub comptime fn generate_public_dispatch(m: Module) -> Quoted {
// functions having this attribute. However, the public MACRO will
// handle the public_dispatch function specially and do nothing.
#[public]
#['public]
pub unconstrained fn public_dispatch(selector: Field) {
$dispatch
}
Expand Down
20 changes: 12 additions & 8 deletions noir-projects/aztec-nr/aztec/src/macros/functions/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use interfaces::{create_fn_abi_export, register_stub, stub_fn};
// Therefore, the execution order of `#[internal]` and `#[private]` is irrelevant.

/// Internal functions can only be called by the contract itself, typically from private into public.
pub comptime fn internal(_f: FunctionDefinition) {
// Marker attribute
pub comptime fn internal(f: FunctionDefinition) {
f.add_attribute("internal")
}
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved

comptime fn create_internal_check(f: FunctionDefinition) -> Quoted {
Expand All @@ -33,8 +33,8 @@ comptime fn create_internal_check(f: FunctionDefinition) -> Quoted {
}

/// View functions can only be called in a static execution context.
pub comptime fn view(_f: FunctionDefinition) {
// Marker attribute
pub comptime fn view(f: FunctionDefinition) {
f.add_attribute("view")
}

comptime fn create_view_check(f: FunctionDefinition) -> Quoted {
Expand All @@ -53,13 +53,13 @@ comptime fn create_view_check(f: FunctionDefinition) -> Quoted {
/// - it can only be called once
/// - if there are multiple initializer functions, only one of them can be called
/// - no non-initializer functions can be called until an initializer has ben called (except `noinitcheck` functions)
pub comptime fn initializer(_f: FunctionDefinition) {
// Marker attribute
pub comptime fn initializer(f: FunctionDefinition) {
f.add_attribute("initializer")
}

/// Functions with noinitcheck can be called before contract initialization.
pub comptime fn noinitcheck(_f: FunctionDefinition) {
// Marker attribute
pub comptime fn noinitcheck(f: FunctionDefinition) {
f.add_attribute("noinitcheck")
}

comptime fn create_assert_correct_initializer_args(f: FunctionDefinition) -> Quoted {
Expand All @@ -79,6 +79,8 @@ comptime fn create_init_check(f: FunctionDefinition) -> Quoted {

/// Private functions are executed client-side and preserve privacy.
pub comptime fn private(f: FunctionDefinition) -> Quoted {
f.add_attribute("private");

let fn_abi = create_fn_abi_export(f);
let fn_stub = stub_fn(f);
register_stub(f.module(), fn_stub);
Expand Down Expand Up @@ -225,6 +227,8 @@ pub comptime fn private(f: FunctionDefinition) -> Quoted {

/// Public functions are executed sequencer-side and do not preserve privacy, similar to the EVM.
pub comptime fn public(f: FunctionDefinition) -> Quoted {
f.add_attribute("public");

// We don't want to transform the public_dispatch function.
if f.name() == quote { public_dispatch } {
quote {}
Expand Down
4 changes: 3 additions & 1 deletion noir-projects/aztec-nr/aztec/src/macros/storage/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ pub comptime fn storage(s: StructDefinition) -> Quoted {
/// with signature `fn init<Context>(context: Context) -> Self`, which allows for manual control of storage slot
/// allocation. Similarly, no `StorageLayout` struct will be created.
/// Only a single struct in the entire contract should have this macro (or `storage`) applied to it.
pub comptime fn storage_no_init(_s: StructDefinition) {
pub comptime fn storage_no_init(s: StructDefinition) {
s.add_attribute("storage_no_init");

// All `storage` does is provide the `init` implementation, so we don't need to do anything here. Applying this
// macro however will cause for `macros::utils::module_has_storage` to return true, resulting in the injection of
// the `storage` variable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ contract AppSubscription {
global SUBSCRIPTION_TXS = 5;

#[private]
#['private]
fn entrypoint(payload: DAppPayload, user_address: AztecAddress) {
// Default msg_sender for entrypoints is now Fr.max_value rather than 0 addr (see #7190 & #7404)
assert(context.msg_sender().to_field() == MAX_FIELD_VALUE);
Expand Down Expand Up @@ -65,8 +64,7 @@ contract AppSubscription {
}

#[public]
#['public]
#['initializer]
#[initializer]
fn constructor(
target_address: AztecAddress,
subscription_recipient_address: AztecAddress,
Expand All @@ -82,7 +80,6 @@ contract AppSubscription {
}

#[private]
#['private]
fn subscribe(subscriber: AztecAddress, nonce: Field, expiry_block_number: Field, tx_count: Field) {
assert(tx_count as u64 <= SUBSCRIPTION_TXS as u64);

Expand Down
18 changes: 5 additions & 13 deletions noir-projects/noir-contracts/contracts/auth_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ contract Auth {
}

#[public]
#['public]
#['initializer]
#[initializer]
fn constructor(admin: AztecAddress) {
assert(!admin.is_zero(), "invalid admin");
storage.admin.initialize(admin);
}

// docs:start:shared_mutable_schedule
#[public]
#['public]
fn set_authorized(authorized: AztecAddress) {
assert_eq(storage.admin.read(), context.msg_sender(), "caller is not admin");
storage.authorized.schedule_value_change(authorized);
Expand All @@ -42,8 +40,7 @@ contract Auth {

// docs:start:public_getter
#[public]
#['public]
#['view]
#[view]
fn get_authorized() -> AztecAddress {
// docs:start:shared_mutable_get_current_public
storage.authorized.get_current_value_in_public()
Expand All @@ -52,8 +49,7 @@ contract Auth {
// docs:end:public_getter

#[public]
#['public]
#['view]
#[view]
fn get_scheduled_authorized() -> AztecAddress {
// docs:start:shared_mutable_get_scheduled_public
let (scheduled_value, _block_of_change): (AztecAddress, u32) = storage.authorized.get_scheduled_value_in_public();
Expand All @@ -62,20 +58,17 @@ contract Auth {
}

#[public]
#['public]
#['view]
#[view]
fn get_authorized_delay() -> pub u32 {
storage.authorized.get_current_delay_in_public()
}

#[public]
#['public]
fn set_authorized_delay(new_delay: u32) {
storage.authorized.schedule_delay_change(new_delay);
}

#[private]
#['private]
fn do_private_authorized_thing() {
// Reading a value from authorized in private automatically adds an extra validity condition: the base rollup
// circuit will reject this tx if included in a block past the block horizon, which is as far as the circuit can
Expand All @@ -87,8 +80,7 @@ contract Auth {
}

#[private]
#['private]
#['view]
#[view]
fn get_authorized_in_private() -> AztecAddress {
storage.authorized.get_current_value_in_private()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dep::aztec::macros::aztec;
contract AuthRegistry {
use dep::aztec::{
state_vars::{PublicMutable, Map}, protocol_types::address::AztecAddress,
macros::{storage::storage, functions::{private, public, internal}}
macros::{storage::storage, functions::{private, public, internal, view}}
};
use dep::authwit::auth::{IS_VALID_SELECTOR, compute_authwit_message_hash, assert_current_call_valid_authwit};

Expand All @@ -22,7 +22,6 @@ contract AuthRegistry {
* @param authorize True if the caller is authorized to perform the message hash, false otherwise
*/
#[public]
#['public]
fn set_authorized(message_hash: Field, authorize: bool) {
storage.approved_actions.at(context.msg_sender()).at(message_hash).write(authorize);
}
Expand All @@ -35,7 +34,6 @@ contract AuthRegistry {
* @param reject True if all actions should be rejected, false otherwise
*/
#[public]
#['public]
fn set_reject_all(reject: bool) {
storage.reject_all.at(context.msg_sender()).write(reject);
}
Expand All @@ -51,7 +49,6 @@ contract AuthRegistry {
* @return `IS_VALID_SELECTOR` if the action was consumed, revert otherwise
*/
#[public]
#['public]
fn consume(on_behalf_of: AztecAddress, inner_hash: Field) -> Field {
assert_eq(false, storage.reject_all.at(on_behalf_of).read(), "rejecting all");

Expand Down Expand Up @@ -82,7 +79,6 @@ contract AuthRegistry {
* @param authorize True if the message hash should be authorized, false otherwise
*/
#[private]
#['private]
fn set_authorized_private(approver: AztecAddress, message_hash: Field, authorize: bool) {
assert_current_call_valid_authwit(&mut context, approver);
AuthRegistry::at(context.this_address())._set_authorized(approver, message_hash, authorize).enqueue(&mut context);
Expand All @@ -97,8 +93,7 @@ contract AuthRegistry {
* @param authorize True if the caller is authorized to perform the message hash, false otherwise
*/
#[public]
#['public]
#['internal]
#[internal]
fn _set_authorized(approver: AztecAddress, message_hash: Field, authorize: bool) {
storage.approved_actions.at(approver).at(message_hash).write(authorize);
}
Expand All @@ -110,8 +105,7 @@ contract AuthRegistry {
* @return True if all actions are rejected, false otherwise
*/
#[public]
#['public]
#['view]
#[view]
fn is_reject_all(on_behalf_of: AztecAddress) -> bool {
storage.reject_all.at(on_behalf_of).read()
}
Expand All @@ -124,8 +118,7 @@ contract AuthRegistry {
* @return True if the caller is authorized to perform the action, false otherwise
*/
#[public]
#['public]
#['view]
#[view]
fn is_consumable(on_behalf_of: AztecAddress, message_hash: Field) -> bool {
storage.approved_actions.at(on_behalf_of).at(message_hash).read()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ contract AuthWitTest {
use dep::authwit::auth::{assert_inner_hash_valid_authwit, assert_inner_hash_valid_authwit_public};

#[private]
#['private]
fn consume(on_behalf_of: AztecAddress, inner_hash: Field) {
assert_inner_hash_valid_authwit(&mut context, on_behalf_of, inner_hash);
}

#[public]
#['public]
fn consume_public(on_behalf_of: AztecAddress, inner_hash: Field) {
assert_inner_hash_valid_authwit_public(&mut context, on_behalf_of, inner_hash);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ contract AvmInitializerTest {
* Storage
************************************************************************/
#[public]
#['public]
#['initializer]
#[initializer]
fn constructor() {
storage.immutable.initialize(42);
}

#[public]
#['public]
fn read_storage_immutable() -> pub Field {
storage.immutable.read()
}
Expand Down
Loading
Loading