Aztec 백서

Aztec

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 73쪽

+ context: PublicContext, + storage: Storage<PublicContext>, ) { ... } ``` ### Authwit Test Helper now takes `env`[​](#authwit-test-helper-now-takes-env "Direct link to authwit-test-helper-now-takes-env") The `add_private_authwit_from_call_interface` test helper available in `test::helpers::authwit` now takes a `TestEnvironment` parameter, mirroring `add_public_authwit_from_call_interface`. This adds some unfortunate verbosity, but there are bigger plans to improve authwit usage in Noir tests in the near future. ``` add_private_authwit_from_call_interface( + env, on_behalf_of, caller, call_interface, ); ``` ### Historical block renamed as anchor block[​](#historical-block-renamed-as-anchor-block "Direct link to Historical block renamed as anchor block") A historical block term has been used as a term that denotes the block against which a private part of a tx has been executed. This name is ambiguous and for this reason we've introduced "anchor block". This naming change resulted in quite a few changes and if you've access private context's or utility context's block header you will need to update your code: ``` - let header = context.get_block_header(); + let header = context.get_anchor_block_header(); ``` ### Removed ValueNote utils[​](#removed-valuenote-utils "Direct link to Removed ValueNote utils") The `value_note::utils` module has been removed because it was incorrect to have those in the value note package. For the increment function you can easily just insert the note: ``` - use value_note::utils; - utils::increment(storage.notes.at(owner), value, owner, sender); + let note = ValueNote::new(value, owner); + storage.notes.at(owner).insert(note).emit(&mut context, owner, MessageDelivery.CONSTRAINED_ONCHAIN); ``` ### PrivateMutable: replace / initialize\_or\_replace behaviour change[​](#privatemutable-replace--initialize_or_replace-behaviour-change "Direct link to PrivateMutable: replace / initialize_or_replace behaviour change") **Motivation:** Updating a note used to require reading it first (via `get_note`, which nullifies and recreates it) and then calling `replace` — effectively proving a note twice. Now, `replace` accepts a callback that transforms the current note directly, and `initialize_or_replace` simply uses this updated `replace` internally. This reduces circuit cost while maintaining exactly one current note. **Key points:** 1. `replace(self, new_note)` (old) → `replace(self, f)` (new), where `f` takes the current note and returns a transformed note. 2. `initialize_or_replace(self, note)` (old) → `initialize_or_replace(self, f)` (new), where `f` takes an `Option` with the current note, or `none` if uninitialized. 3. Previous note is automatically nullified before the new note is inserted. 4. `NoteEmission<Note>` still requires `.emit()` or `.discard()`. **Example Migration:** ``` - let current_note = storage.my_var.get_note(); - let new_note = f(current_note); - storage.my_var.replace(new_note); + storage.my_var.replace(|current_note| f(current_note)); ``` ``` - storage.my_var.initialize_or_replace(new_note); + storage.my_var.initialize_or_replace(|_| new_note); ``` This makes it easy and efficient to handle both initialization and current value mutation via `initialize_or_replace`, e.g. if implementing a note that simply counts how many times it has been read: ``` + storage.my_var.initialize_or_replace(|opt_current: Option<Note>| opt_current.unwrap_or(0 /* initial value */) + 1); ``` * The callback can be a closure (inline) or a named function. * Any previous assumptions that replace simply inserts a new\_note directly must be updated. ### Unified oracles into single get\_utility\_context oracle[​](#unified-oracles-into-single-get_utility_context-oracle "Direct link to Unified oracles into single get_utility_context oracle") The following oracles: 1. get\_contract\_address, 2. get\_block\_number, 3. get\_timestamp, 4. get\_chain\_id, 5. get\_version were replaced with a single `get_utility_context` oracle whose return value contains all the values returned from the removed oracles. If you have used one of these removed oracles before, update the import, e.g.: ``` - aztec::oracle::execution::get_chain_id; + aztec::oracle::execution::get_utility_context ``` and get the value out of the returned utility context: ``` - let chain_id = get_chain_id(); + let chain_id = get_utility_context().chain_id(); ``` ### Note emission API changes[​](#note-emission-api-changes "Direct link to Note emission API changes") The note emission API has been significantly reworked to provide clearer semantics around message delivery guarantees. The key changes are: 1. `encode_and_encrypt_note` has been removed in favor of calling `emit` directly with `MessageDelivery.CONSTRAINED_ONCHAIN` 2. `encode_and_encrypt_note_unconstrained` has been removed in favor of calling `emit` directly with `MessageDelivery.UNCONSTRAINED_ONCHAIN` 3. `encode_and_encrypt_note_and_emit_as_offchain_message` has been removed in favor of using `emit` with `MessageDelivery.UNCONSTRAINED_OFFCHAIN` 4. Note emission now takes a `delivery_mode` parameter with the following values: <!-- --> * `CONSTRAINED_ONCHAIN`: For onchain delivery with cryptographic guarantees that recipients can discover and decrypt messages. Uses constrained encryption but is slower to prove. Best for critical messages that contracts need to verify. * `UNCONSTRAINED_ONCHAIN`: For onchain delivery without encryption constraints. Faster proving but trusts the sender. Good when the sender is incentivized to perform encryption correctly (e.g. they are buying something and will only get it if the recipient sees the note). No guarantees that recipients will be able to find or decrypt messages. * `UNCONSTRAINED_OFFCHAIN`: For offchain delivery (e.g. cloud storage) without constraints. Lowest cost since no onchain storage needed. Requires custom infrastructure for delivery. No guarantees that messages will be delivered or that recipients will ever find them. 5. The `context` object no longer needs to be passed to these functions Example migration: First you need to update imports in your contract: ``` - aztec::messages::logs::note::encode_and_encrypt_note; - aztec::messages::logs::note::encode_and_encrypt_note_unconstrained; - aztec::messages::logs::note::encode_and_encrypt_note_and_emit_as_offchain_message; + aztec::messages::message_delivery::MessageDelivery; ``` Then update the emissions: ``` - storage.balances.at(from).sub(from, amount).emit(encode_and_encrypt_note(&mut context, from)); + storage.balances.at(from).sub(from, amount).emit(&mut context, from, MessageDelivery.CONSTRAINED_ONCHAIN); ``` ``` - storage.balances.at(from).add(from, change).emit(encode_and_encrypt_note_unconstrained(&mut context, from)); + storage.balances.at(from).add(from, change).emit(&mut context, from, MessageDelivery.UNCONSTRAINED_ONCHAIN); ``` ``` - storage.balances.at(owner).insert(note).emit(encode_and_encrypt_note_and_emit_as_offchain_message(&mut context, context.msg_sender()); + storage.balances.at(owner).insert(note).emit(&mut context, context.msg_sender(), MessageDelivery.UNCONSTRAINED_OFFCHAIN); ``` ## 2.0.2[​](#202 "Direct link to 2.0.2") ## \[Public functions][​](#public-functions "Direct link to \[Public functions]") The L2 gas cost of the different AVM opcodes have been updated to reflect more realistic proving costs. Developers should review the L2 gas costs of executing public functions and reevaluate any hardcoded L2 gas limits. ## \[Aztec Tools][​](#aztec-tools "Direct link to \[Aztec Tools]") ### Contract compilation now requires two steps[​](#contract-compilation-now-requires-two-steps "Direct link to Contract compilation now requires two steps") The `aztec-nargo` command is now a direct pass-through to vanilla nargo, without any special compilation flags or postprocessing. Contract compilation for Aztec now requires two explicit steps: 1. Compile your contracts with `aztec-nargo compile` 2. Run postprocessing with the new `aztec-postprocess-contract` command The postprocessing step includes: * Transpiling functions for the Aztec VM * Generating verification keys for private functions * Caching verification keys for faster subsequent compilations Update your build scripts accordingly: ``` - aztec-nargo compile + aztec-nargo compile + aztec-postprocess-contract ``` If you're using the `aztec-up` installer, the `aztec-postprocess-contract` command will be automatically installed alongside `aztec-nargo`. ## \[Aztec.js] Mandatory `from`[​](#aztecjs-mandatory-from "Direct link to aztecjs-mandatory-from") As we prepare for a bigger `Wallet` interface refactor and the upcoming `WalletSDK`, a new parameter has been added to contract interactions, which now should indicate *explicitly* the address of the entrypoint (usually the account contract) that will be used to authenticate the request. This will be checked in runtime against the current `this.wallet.getAddress()` value, to ensure consistent behavior while the rest of the API is reworked. ``` - await contract.methods.my_func(arg).send().wait(); + await contract.methods.my_func(arg).send({ from: account1Address }).wait(); ``` ## \[Aztec.nr][​](#aztecnr-1 "Direct link to \[Aztec.nr]") ### `emit_event_in_public_log` function renamed as `emit_event_in_public`[​](#emit_event_in_public_log-function-renamed-as-emit_event_in_public "Direct link to emit_event_in_public_log-function-renamed-as-emit_event_in_public") This change was done to make the naming consistent with the private counterpart (`emit_event_in_private`). ### Private event emission API changes[​](#private-event-emission-api-changes "Direct link to Private event emission API changes") The private event emission API has been significantly reworked to provide clearer semantics around message delivery guarantees. The key changes are: 1. `emit_event_in_private_log` has been renamed to `emit_event_in_private` and now takes a `delivery_mode` parameter instead of `constraints` 2. `emit_event_as_offchain_message` has been removed in favor of using `emit_event_in_private` with `MessageDelivery.UNCONSTRAINED_OFFCHAIN` 3. `PrivateLogContent` enum has been replaced with `MessageDelivery` enum with the following values: <!-- --> * `CONSTRAINED_ONCHAIN`: For onchain delivery with cryptographic guarantees that recipients can discover and decrypt messages. Uses constrained encryption but is slower to prove. Best for critical messages that contracts need to verify. * `UNCONSTRAINED_ONCHAIN`: For onchain delivery without encryption constraints. Faster proving but trusts the sender. Good when the sender is incentivized to perform encryption correctly (e.g. they are buying something and will only get it if the recipient sees the note). No guarantees that recipients will be able to find or decrypt messages. * `UNCONSTRAINED_OFFCHAIN`: For offchain delivery (e.g. cloud storage) without constraints. Lowest cost since no onchain storage needed. Requires custom infrastructure for delivery. No guarantees that messages will be delivered or that recipients will ever find them. ### Contract functions can no longer be `pub` or `pub(crate)`[​](#contract-functions-can-no-longer-be-pub-or-pubcrate "Direct link to contract-functions-can-no-longer-be-pub-or-pubcrate") With the latest changes to `TestEnvironment`, making contract functions have public visibility is no longer required given the new `call_public` and `simulate_utility` functions. To avoid accidental direct invocation, and to reduce confusion with the autogenerated interfaces, we're forbidding them being public. ``` - pub(crate) fn balance_of_private(account: AztecAddress) -> 128 { + fn balance_of_private(account: AztecAddress) -> 128 { ``` ### Notes require you to manually implement or derive Packable[​](#notes-require-you-to-manually-implement-or-derive-packable "Direct link to Notes require you to manually implement or derive Packable") We have decided to drop auto-derivation of `Packable` from the `#[note]` macro because we want to make the macros less magical. With this change you will be forced to either apply `#[derive(Packable)` on your notes: ``` +use aztec::protocol::traits::Packable; +#[derive(Packable)] #[note] pub struct UintNote { owner: AztecAddress, randomness: Field, value: u128, } ``` or to implement it manually yourself: ``` impl Packable for UintNote { let N: u32 = 3; fn pack(self) -> [Field; Self::N] { [self.owner.to_field(), randomness, value as Field] } fn unpack(fields: [Field; Self::N]) -> Self { let owner = AztecAddress::from_field(fields[0]); let randomness = fields[1]; let value = fields[2] as u128; UintNote { owner, randomness, value } } } ``` ### Tagging sender now managed via oracle functions[​](#tagging-sender-now-managed-via-oracle-functions "Direct link to Tagging sender now managed via oracle functions") Now, instead of manually needing to pass a tagging sender as an argument to log emission functions (e.g. `encode_and_encrypt_note`, `encode_and_encrypt_note_unconstrained`, `emit_event_in_private_log`, ...) we automatically load the sender via the `get_sender_for_tags()` oracle. This value is expected to be populated by account contracts that should call `set_sender_for_tags()` in their entry point functions. The changes you need to do in your contracts are quite straightforward. You simply need to drop the `sender` arg from the callsites of the log emission functions. E.g. note emission: ``` storage.balances.at(from).sub(from, amount).emit(encode_and_encrypt_note( &mut context, from, - tagging_sender, )); ``` E.g. private event emission: ``` emit_event_in_private_log( Transfer { from, to, amount }, &mut context, - tagging_sender, to, PrivateLogContent.NO_CONSTRAINTS, ); ``` This change affected arguments `prepare_private_balance_increase` and `mint_to_private` functions on the `Token` contract. Drop the `from` argument when calling these. Example in TypeScript test: ``` - await token.methods.mint_to_private(fundedWallet.getAddress(), alice, mintAmount).send().wait(); + await token.methods.mint_to_private(alice, mintAmount).send().wait(); ``` Example when ``` let token_out_partial_note = Token::at(token_out).prepare_private_balance_increase( sender, - tagging_sender ).call(&mut context); ``` ### SharedMutable -> DelayedPublicMutable[​](#sharedmutable---delayedpublicmutable "Direct link to SharedMutable -> DelayedPublicMutable") The `SharedMutable` state variable has been renamed to `DelayedPublicMutable`. It is a public mutable with a delay before state changes take effect. It can be read in private during the delay period. The name "shared" confuses developers who actually wish to work with so-called "shared private state". Also, we're working on a `DelayedPrivateMutable` which will have similar properties, except writes will be scheduled from private instead. With this new state variable in mind, the new name works nicely. ## \[TXE] - Testing Aztec Contracts using Noir[​](#txe---testing-aztec-contracts-using-noir "Direct link to \[TXE] - Testing Aztec Contracts using Noir") ### Full `TestEnvironment` API overhaul[​](#full-testenvironment-api-overhaul "Direct link to full-testenvironment-api-overhaul") As part of a broader effort to make Noir tests that leverage TXE easier to use and reason about, large parts of it were changed or adapted, resulting in the API now being quite different. No functionality was lost, so it should be possible to migrate any older Noir test to use the new API. #### Network State Manipulation[​](#network-state-manipulation "Direct link to Network State Manipulation") * `committed_timestamp` removed: this function did not work correctly * `private_at_timestamp`: this function was not really meaningful: private contexts are built from block numbers, not timestamps * `pending_block_number` was renamed to `next_block_number`. `pending_timestamp` was removed since it was confusing and not useful * `committed_block_number` was renamed to `last_block_number` * `advance_timestamp_to` and `advance_timestamp_by` were renamed to `set_next_block_timestamp` and `advance_next_block_timestamp_by` respectively * `advance_block_to` was renamed to `mine_block_at`, which takes a timestamp instead of a target block number * `advance_block_by` was renamed to `mine_block`, which now mines a single block #### Account Management[​](#account-management "Direct link to Account Management") * `create_account` was renamed to `create_light_account` * `create_account_contract` was renamed to `create_contract_account` #### Contract Deployment[​](#contract-deployment "Direct link to Contract Deployment") * `deploy_self` removed: merged into `deploy` * `deploy` now accepts both local and external contracts #### Contract Interactions[​](#contract-interactions "Direct link to Contract Interactions") The old way of calling contract functions is gone. Contract functions are now invoked via the `call_private`, `view_private`, `call_public`, `view_public` and `simulate_utility` `TestEnvironment` methods. These take a `CallInterface`, like their old counterparts, but now also take an explicit `from` parameter (for the `call` variants - this is left out of the `view` and `simulate` methods for simplicity). #### Raw Context Access[​](#raw-context-access "Direct link to Raw Context Access") The `private` and `public` methods are gone. Private, public and utility contexts can now be crated with the `private_context`, `public_context` and `utility_context` functions, all of which takes a callback function that is called with the corresponding context. This functions are expected to be defined in-line as lambdas, and contain the user-defined test logic. This helps delineate where contexts begin and end. Contexts automatically mine blocks on closing, when appropriate. #### Error-expecting Functions[​](#error-expecting-functions "Direct link to Error-expecting Functions") `assert_public_call_revert` and variants have been removed. Use `#[test(should_fail_with = "message")]` instead. #### Example Migration[​](#example-migration "Direct link to Example Migration") The following are two tests using the older version of `TestEnvironment`: ``` #[test] unconstrained fn initial_empty_value() { let mut env = TestEnvironment::new(); // Setup without account contracts. We are not using authwits here, so dummy accounts are enough let admin = env.create_account(1); let initializer_call_interface = Auth::interface().constructor(admin); let auth_contract = env.deploy_self("Auth").with_public_void_initializer(admin, initializer_call_interface); let auth_contract_address = auth_contract.to_address(); env.impersonate(admin); let authorized = Auth::at(auth_contract_address).get_authorized().view(&mut env.public()); assert_eq(authorized, AztecAddress::from_field(0)); } #[test] unconstrained fn non_admin_cannot_set_authorized() { let mut env = TestEnvironment::new(); // Setup without account contracts. We are not using authwits here, so dummy accounts are enough let admin = env.create_account(1); let other = env.create_account(2); let initializer_call_interface = Auth::interface().constructor(admin); let auth_contract = env.deploy_self("Auth").with_public_void_initializer(admin, initializer_call_interface); let auth_contract_address = auth_contract.to_address(); env.impersonate(other); env.assert_public_call_fails(Auth::at(auth_contract_address).set_authorized(to_authorize)); } ``` These now look like this: ``` #[test] unconstrained fn authorized_initially_unset() { let mut env = TestEnvironment::new(); let admin = env.create_light_account(); // Manual secret management gone let auth_contract_address = env.deploy("Auth").with_public_initializer(admin, Auth::interface().constructor(admin)); // deploy_self re
프로젝트로 돌아가기