Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Sep 4, 2024
1 parent 6ed4d3c commit 5f2d479
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
22 changes: 16 additions & 6 deletions docs/docs/aztec/glossary/call_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,31 @@ Unlike the EVM however, private execution doesn't revert in the traditional way:

Since public execution can only be performed by the sequencer, public functions cannot be executed in a private context. It is possible however to _enqueue_ a public function call during private execution, requesting the sequencer to run it during inclusion of the transaction. It will be [executed in public](#public-execution) normally, including the possibility to enqueue static public calls.

Since the public call is made asynchronously, any return values or side effects are not available during private execution. If the public function fails once executed, the entire transaction is reverted inncluding state changes caused by the private part, such as new notes or nullifiers. Note that this does result in gas being spent, like in the case of the EVM.
Since the public call is made asynchronously, any return values or side effects are not available during private execution. If the public function fails once executed, the entire transaction is reverted including state changes caused by the private part, such as new notes or nullifiers. Note that this does result in gas being spent, like in the case of the EVM.

#include_code enqueue_public /noir-projects/noir-contracts/contracts/lending_contract/src/main.nr rust

It is also possible to create public functions that can _only_ be invoked by privately enqueing a call from the same contract, which can very useful to update public state after private exection (e.g. update a token's supply after privately minting). This is achieved by annotating functions with `#[aztec(internal)]`.
It is also possible to create public functions that can _only_ be invoked by privately enqueueing a call from the same contract, which can very useful to update public state after private execution (e.g. update a token's supply after privately minting). This is achieved by annotating functions with `#[aztec(internal)]`.

A common pattern is to enqueue public calls to check some validity condition on public state, e.g. that a deadline has not expired or that some public value is set.

#include_code enqueueing /noir-projects/noir-contracts/contracts/router_contract/src/main.nr rust

Note that this reveals what public function is being called on what contract.
For this reason we've created a canonical router contract which implements some of the checks commonly performed.
This conceals what contract performed the public call as the `context.msg_sender()` in the public function is the router itself (since the router's private function enqueued the public call).

An example of how a deadline can be checked using the router contract follows:

#include_code call-check-deadline /noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr rust

#include_code deadline /noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr rust
This is what the implementation of the check timestamp functionality looks like:

:::warning
Calling public functions privately leaks some privacy! The caller of the function and all arguments will be revelead, so exercise care when mixing the private and public domains. To learn about alternative ways to access public state privately, look into [Shared State](../../reference/developer_references/smart_contract_reference/storage/shared_state.md).
:::
#include_code check_timestamp /noir-projects/noir-contracts/contracts/router_contract/src/main.nr rust

Even with the router contract achieving good privacy is hard.
This is especially the case when the value being checked is unique and stored in the contract's public storage.
For this reason it is encouraged to try to avoid public function calls and instead privately read [Shared State](../../reference/developer_references/smart_contract_reference/storage/shared_state.md) when possible.

### Public Execution

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
contract Router {
use aztec::utils::comparison::assert_comparison;

// docs:start:check_timestamp
/// Enqueues a public call that asserts that `lhs` (left side of the comparison) timestamp satisfies
/// the `operation` with respect to the current timestamp.
#[aztec(private)]
Expand All @@ -19,12 +20,15 @@ contract Router {
let rhs = context.timestamp() as Field;
assert_comparison(lhs_field, operation, rhs, "Timestamp mismatch.");
}
// docs:end:check_timestamp

/// Enqueues a public call that asserts that `lhs` (left side of the comparison) block number satisfies
/// the `operation` with respect to the current block number.
#[aztec(private)]
fn check_block_number(lhs: Field, operation: u8) {
// docs:start:enqueueing
Router::at(context.this_address())._check_block_number(lhs, operation).enqueue_view(&mut context);
// docs:end:enqueueing
}

#[aztec(public)]
Expand Down

0 comments on commit 5f2d479

Please sign in to comment.