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

feat!: rename sharedimmutable methods #10164

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ contract FPC {

#[private]
fn fee_entrypoint_private(amount: Field, asset: AztecAddress, secret_hash: Field, nonce: Field) {
assert(asset == storage.other_asset.read_private());
assert(asset == storage.other_asset.read());
Token::at(asset).transfer_to_public(context.msg_sender(), context.this_address(), amount, nonce).call(&mut context);
FPC::at(context.this_address()).pay_fee_with_shielded_rebate(amount, asset, secret_hash).enqueue(&mut context);
}
Expand Down
14 changes: 14 additions & 0 deletions docs/docs/migration_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ Aztec is in full-speed development. Literally every version breaks compatibility

## TBD

### [aztec.nr] SharedImmutable renamings

`SharedImmutable::read_private` and `SharedImmutable::read_public` were renamed to simply `read`, since only one of these versions is ever available depending on the current context.

```diff
// In private
- let value = storage.my_var.read_private();
+ let value = storage.my_var.read();

// In public
- let value = storage.my_var.read_public();
+ let value = storage.my_var.read();
```

### [aztec.js] Random addresses are now valid

The `AztecAddress.random()` function now returns valid addresses, i.e. addresses that can receive encrypted messages and therefore have notes be sent to them. `AztecAddress.isValid()` was also added to check for validity of an address.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,10 @@ This function sets the immutable value. It must only be called once during contr
A `SharedImmutable`'s storage **must** only be set once via `initialize`. Attempting to override this by manually accessing the underlying storage slots breaks all properties of the data structure, rendering it useless.
:::

### `read_public`
### `read`

Returns the stored immutable value in a public execution context.
Returns the stored immutable value. This function is available in public, private and unconstrained contexts.

#include_code read_decimals_public /noir-projects/noir-contracts/contracts/token_contract/src/main.nr rust
#include_code read_shared_immutable_private /noir-projects/noir-contracts/contracts/token_contract/src/main.nr rust

### `read_private`

Returns the stored immutable value in a private execution context.

#include_code read_decimals_private /noir-projects/noir-contracts/contracts/token_contract/src/main.nr rust
#include_code read_shared_immutable_private /noir-projects/noir-contracts/contracts/token_contract/src/main.nr rust
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ where
self.context.storage_write(self.storage_slot, value);
}

pub fn read_public(self) -> T {
pub fn read(self) -> T {
self.context.storage_read(self.storage_slot)
}
}
Expand All @@ -54,7 +54,7 @@ impl<T, let T_SERIALIZED_LEN: u32> SharedImmutable<T, UnconstrainedContext>
where
T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN>,
{
pub unconstrained fn read_public(self) -> T {
pub unconstrained fn read(self) -> T {
self.context.storage_read(self.storage_slot)
}
}
Expand All @@ -63,7 +63,7 @@ impl<T, let T_SERIALIZED_LEN: u32> SharedImmutable<T, &mut PrivateContext>
where
T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN>,
{
pub fn read_private(self) -> T {
pub fn read(self) -> T {
let header = self.context.get_header();
let mut fields = [0; T_SERIALIZED_LEN];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ contract AppSubscription {
context.set_as_fee_payer();

// TODO(palla/gas) Assert fee_juice_limit_per_tx is less than this tx gas_limit
let _gas_limit = storage.fee_juice_limit_per_tx.read_private();
let _gas_limit = storage.fee_juice_limit_per_tx.read();

context.end_setup();

// We check that the note is not expired. We do that via the router contract to conceal which contract
// is performing the check.
privately_check_block_number(Comparator.LT, note.expiry_block_number, &mut context);

payload.execute_calls(&mut context, storage.target_address.read_private());
payload.execute_calls(&mut context, storage.target_address.read());
}

#[public]
Expand Down Expand Up @@ -95,11 +95,11 @@ contract AppSubscription {
) {
assert(tx_count as u64 <= SUBSCRIPTION_TXS as u64);

Token::at(storage.subscription_token_address.read_private())
Token::at(storage.subscription_token_address.read())
.transfer_in_private(
context.msg_sender(),
storage.subscription_recipient_address.read_private(),
storage.subscription_price.read_private(),
storage.subscription_recipient_address.read(),
storage.subscription_price.read(),
nonce,
)
.call(&mut context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ contract Claim {
#[private]
fn claim(proof_note: ValueNote, recipient: AztecAddress) {
// 1) Check that the note corresponds to the target contract and belongs to the sender
let target_address = storage.target_contract.read_private();
let target_address = storage.target_contract.read();
assert(
target_address == proof_note.header.contract_address,
"Note does not correspond to the target contract",
Expand All @@ -51,7 +51,7 @@ contract Claim {
context.push_nullifier(nullifier);

// 4) Finally we mint the reward token to the sender of the transaction
Token::at(storage.reward_token.read_private())
Token::at(storage.reward_token.read())
.mint_to_public(recipient, proof_note.value)
.enqueue(&mut context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ contract Crowdfunding {
// 1) Check that the deadline has not passed --> we do that via the router contract to conceal which contract
// is performing the check.
// docs:start:call-check-deadline
let deadline = storage.deadline.read_private();
let deadline = storage.deadline.read();
privately_check_timestamp(Comparator.LT, deadline, &mut context);
// docs:end:call-check-deadline
// docs:start:do-transfer
// 2) Transfer the donation tokens from donor to this contract
let donor = context.msg_sender();
Token::at(storage.donation_token.read_private())
Token::at(storage.donation_token.read())
.transfer_in_private(donor, context.this_address(), amount as Field, 0)
.call(&mut context);
// docs:end:do-transfer
Expand All @@ -101,11 +101,11 @@ contract Crowdfunding {
#[private]
fn withdraw(amount: u64) {
// 1) Check that msg_sender() is the operator
let operator_address = storage.operator.read_private();
let operator_address = storage.operator.read();
assert(context.msg_sender() == operator_address, "Not an operator");

// 2) Transfer the donation tokens from this contract to the operator
Token::at(storage.donation_token.read_private())
Token::at(storage.donation_token.read())
.transfer(operator_address, amount as Field)
.call(&mut context);
// 3) Emit an unencrypted event so that anyone can audit how much the operator has withdrawn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ contract DocsExample {
#[private]
fn match_shared_immutable(account: AztecAddress, points: u8) {
let expected = Leader { account, points };
let read = storage.shared_immutable.read_private();
let read = storage.shared_immutable.read();

assert(read.account == expected.account, "Invalid account");
assert(read.points == expected.points, "Invalid points");
Expand Down Expand Up @@ -138,23 +138,23 @@ contract DocsExample {
#[public]
#[view]
fn get_shared_immutable_constrained_public() -> Leader {
storage.shared_immutable.read_public()
storage.shared_immutable.read()
}

#[public]
fn get_shared_immutable_constrained_public_multiple() -> [Leader; 5] {
let a = storage.shared_immutable.read_public();
let a = storage.shared_immutable.read();
[a, a, a, a, a]
}

#[private]
#[view]
fn get_shared_immutable_constrained_private() -> Leader {
storage.shared_immutable.read_private()
storage.shared_immutable.read()
}

unconstrained fn get_shared_immutable() -> Leader {
storage.shared_immutable.read_public()
storage.shared_immutable.read()
}

#[public]
Expand All @@ -165,8 +165,8 @@ contract DocsExample {
// docs:end:initialize_public_immutable
}

unconstrained fn get_public_immutable() -> Leader {
// docs:start:read_public_immutable
unconstrained fn get_public_immutable() -> Leader {
storage.public_immutable.read()
// docs:end:read_public_immutable
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ contract FeeJuice {
// is a hardcoded constant in the rollup circuits.
#[public]
fn set_portal(portal_address: EthAddress) {
assert(storage.portal_address.read_public().is_zero());
assert(storage.portal_address.read().is_zero());
storage.portal_address.initialize(portal_address);
}

#[private]
fn claim(to: AztecAddress, amount: Field, secret: Field, message_leaf_index: Field) {
let content_hash = get_bridge_gas_msg_hash(to, amount);
let portal_address = storage.portal_address.read_private();
let portal_address = storage.portal_address.read();
assert(!portal_address.is_zero());

// Consume message and emit nullifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ contract FPC {
#[private]
fn fee_entrypoint_private(amount: Field, asset: AztecAddress, nonce: Field) {
// TODO(PR #8022): Once SharedImmutable performs only 1 merkle proof here, we'll save ~4k gates
let settings = storage.settings.read_private();
let settings = storage.settings.read();

assert(asset == settings.other_asset);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,25 @@ contract NFT {
#[public]
#[view]
fn public_get_name() -> pub FieldCompressedString {
storage.name.read_public()
storage.name.read()
}

#[private]
#[view]
fn private_get_name() -> pub FieldCompressedString {
storage.name.read_private()
storage.name.read()
}

#[public]
#[view]
fn public_get_symbol() -> pub FieldCompressedString {
storage.symbol.read_public()
storage.symbol.read()
}

#[private]
#[view]
fn private_get_symbol() -> pub FieldCompressedString {
storage.symbol.read_private()
storage.symbol.read()
}

#[public]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ contract TokenBridge {

#[private]
fn get_portal_address() -> EthAddress {
storage.portal_address.read_private()
storage.portal_address.read()
}

#[public]
fn get_portal_address_public() -> EthAddress {
storage.portal_address.read_public()
storage.portal_address.read()
}

// docs:start:claim_public
Expand All @@ -61,12 +61,12 @@ contract TokenBridge {
context.consume_l1_to_l2_message(
content_hash,
secret,
storage.portal_address.read_public(),
storage.portal_address.read(),
message_leaf_index,
);

// Mint tokens
Token::at(storage.token.read_public()).mint_to_public(to, amount).call(&mut context);
Token::at(storage.token.read()).mint_to_public(to, amount).call(&mut context);
}
// docs:end:claim_public

Expand All @@ -82,10 +82,10 @@ contract TokenBridge {
) {
// Send an L2 to L1 message
let content = get_withdraw_content_hash(recipient, amount, caller_on_l1);
context.message_portal(storage.portal_address.read_public(), content);
context.message_portal(storage.portal_address.read(), content);

// Burn tokens
Token::at(storage.token.read_public())
Token::at(storage.token.read())
.burn_public(context.msg_sender(), amount, nonce)
.call(&mut context);
}
Expand All @@ -108,12 +108,12 @@ contract TokenBridge {
context.consume_l1_to_l2_message(
content_hash,
secret_for_L1_to_L2_message_consumption,
storage.portal_address.read_private(),
storage.portal_address.read(),
message_leaf_index,
);

// Read the token address from storage
let token_address = storage.token.read_private();
let token_address = storage.token.read();

// At last we mint the tokens
// docs:start:call_mint_on_token
Expand All @@ -137,7 +137,7 @@ contract TokenBridge {
) {
// Send an L2 to L1 message
let content = get_withdraw_content_hash(recipient, amount, caller_on_l1);
context.message_portal(storage.portal_address.read_private(), content);
context.message_portal(storage.portal_address.read(), content);

// docs:start:call_assert_token_is_same
// Assert that user provided token address is same as seen in storage.
Expand All @@ -151,7 +151,7 @@ contract TokenBridge {
#[public]
#[view]
fn get_token() -> AztecAddress {
storage.token.read_public()
storage.token.read()
}
// docs:end:get_token

Expand All @@ -160,7 +160,7 @@ contract TokenBridge {
#[internal]
fn _assert_token_is_same(token: AztecAddress) {
assert(
storage.token.read_public().eq(token),
storage.token.read().eq(token),
"Token address is not the same as seen in storage",
);
}
Expand Down
21 changes: 11 additions & 10 deletions noir-projects/noir-contracts/contracts/token_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -117,41 +117,42 @@ contract Token {
#[public]
#[view]
fn public_get_name() -> FieldCompressedString {
storage.name.read_public()
storage.name.read()
}

#[private]
#[view]
fn private_get_name() -> FieldCompressedString {
storage.name.read_private()
storage.name.read()
}

#[public]
#[view]
fn public_get_symbol() -> pub FieldCompressedString {
storage.symbol.read_public()
storage.symbol.read()
}

#[private]
#[view]
fn private_get_symbol() -> pub FieldCompressedString {
storage.symbol.read_private()
storage.symbol.read()
}

// docs:start:read_shared_immutable_private
#[public]
#[view]
fn public_get_decimals() -> pub u8 {
// docs:start:read_decimals_public
storage.decimals.read_public()
// docs:end:read_decimals_public
storage.decimals.read()
}
// docs:end:read_shared_immutable_private

// docs:start:read_shared_immutable_private
#[private]
#[view]
fn private_get_decimals() -> pub u8 {
// docs:start:read_decimals_private
storage.decimals.read_private()
// docs:end:read_decimals_private
storage.decimals.read()
}
// docs:end:read_shared_immutable_private

// docs:start:admin
#[public]
Expand Down
Loading
Loading