Skip to content

Commit 22ad5a5

Browse files
Thunkardbanks12
andauthored
feat: View functions with static context enforcing (AztecProtocol#6338)
Closes AztecProtocol#6078 Introduces the `#[aztec(view)]` (open to different naming, @spalladino @rahul-kothari ) modifier to functions, that forces them to be executed in an static context. It also forces generation of a "static only" `CallInterface` for them, trying to spare users from making regular calls to them. ~~Need input from the AVM team (@dbanks12 @fcarreiro) on how to implement the concept in the AVMContext.~~ In order to support direct simulated calls to view functions, the simulate method has been modified to go through the account entrypoint, which has led to implementing retrieval of return values through the whole callstack instead of just taking the latest one. Also adds the ability to navigate from contract interfaces to their implementations via LSP! --------- Co-authored-by: dbanks12 <[email protected]>
1 parent e5cc9dc commit 22ad5a5

89 files changed

Lines changed: 1391 additions & 493 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_opcode.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ enum class OpCode : uint8_t {
9898
// Gadgets
9999
KECCAK,
100100
POSEIDON2,
101+
SHA256,
102+
PEDERSEN,
101103

102104
// Conversions
103105
TORADIXLE,

barretenberg/cpp/src/barretenberg/vm/avm_trace/aztec_constants.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const size_t CONTRACT_INSTANCE_LENGTH = 5;
8383
const size_t CONTRACT_STORAGE_READ_LENGTH = 2;
8484
const size_t CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 2;
8585
const size_t ETH_ADDRESS_LENGTH = 1;
86-
const size_t FUNCTION_DATA_LENGTH = 2;
86+
const size_t FUNCTION_DATA_LENGTH = 3;
8787
const size_t FUNCTION_LEAF_PREIMAGE_LENGTH = 5;
8888
const size_t GLOBAL_VARIABLES_LENGTH = 6 + GAS_FEES_LENGTH;
8989
const size_t APPEND_ONLY_TREE_SNAPSHOT_LENGTH = 2;

docs/docs/migration_notes.md

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,56 @@ keywords: [sandbox, cli, aztec, notes, migration, updating, upgrading]
66

77
Aztec is in full-speed development. Literally every version breaks compatibility with the previous ones. This page attempts to target errors and difficulties you might encounter when upgrading, and how to resolve them.
88

9+
## 0.X.X
10+
11+
### [Aztec.nr] View functions and interface navigation
12+
13+
It is now possible to explicitly state a function doesn't perform any state alterations (including storage, logs, nullifiers and/or messages from L2 to L1) with the `#[aztec(view)]` attribute, similarly to solidity's `view` function modifier.
14+
15+
```diff
16+
#[aztec(public)]
17+
+ #[aztec(view)]
18+
fn get_price(asset_id: Field) -> Asset {
19+
storage.assets.at(asset_id).read()
20+
}
21+
```
22+
23+
View functions only generate a `StaticCallInterface` that doesn't include `.call` or `.enqueue` methods. Also, the denomination `static` has been completely removed from the interfaces, in favor of the more familiar `view`
24+
25+
```diff
26+
+ let price = PriceFeed::at(asset.oracle).get_price(0).view(&mut context).price;
27+
- let price = PriceFeed::at(asset.oracle).get_price(0).static_call(&mut context).price;
28+
```
29+
30+
```diff
31+
#[aztec(private)]
32+
fn enqueue_public_get_value_from_child(target_contract: AztecAddress, value: Field) {
33+
+ StaticChild::at(target_contract).pub_get_value(value).enqueue_view(&mut context);
34+
- StaticChild::at(target_contract).pub_get_value(value).static_enqueue(&mut context);
35+
}
36+
```
37+
38+
Additionally, the Noir LSP will now honor "go to definitions" requests for contract interfaces (Ctrl+click), taking the user to the original function implementation.
39+
40+
### [Aztec.js] Simulate changes
41+
42+
* `.simulate()` now tracks closer the process performed by `.send().wait()`, specifically going through the account contract entrypoint instead of directly calling the intended function.
43+
* `wallet.viewTx(...)` has been renamed to `wallet.simulateUnconstrained(...)` to better clarify what it does.
44+
945
## 0.41.0
1046

1147
### [Aztec.nr] Keys: Token note now stores an owner master nullifying public key hash instead of an owner address
1248

1349
i.e.
1450

15-
struct TokenNote \{
51+
```diff
52+
struct TokenNote {
1653
amount: U128,
17-
```diff
18-
- owner: AztecAddress,
19-
+ npk_m_hash: Field,
20-
```
54+
- owner: AztecAddress,
55+
+ npk_m_hash: Field,
2156
randomness: Field,
22-
\}
57+
}
58+
```
2359

2460
Computing the nullifier similarly changes to use this master nullifying public key hash.
2561

docs/static/img/sandbox_unconstrained_function.svg

Lines changed: 1 addition & 1 deletion
Loading

l1-contracts/src/core/libraries/ConstantsGen.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ library Constants {
111111
uint256 internal constant CONTRACT_STORAGE_READ_LENGTH = 2;
112112
uint256 internal constant CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 2;
113113
uint256 internal constant ETH_ADDRESS_LENGTH = 1;
114-
uint256 internal constant FUNCTION_DATA_LENGTH = 2;
114+
uint256 internal constant FUNCTION_DATA_LENGTH = 3;
115115
uint256 internal constant FUNCTION_LEAF_PREIMAGE_LENGTH = 5;
116116
uint256 internal constant GLOBAL_VARIABLES_LENGTH = 6 + GAS_FEES_LENGTH;
117117
uint256 internal constant APPEND_ONLY_TREE_SNAPSHOT_LENGTH = 2;

noir-projects/aztec-nr/authwit/src/entrypoint/app.nr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use dep::aztec::protocol_types::{constants::GENERATOR_INDEX__SIGNATURE_PAYLOAD,
44
use crate::entrypoint::function_call::{FunctionCall, FUNCTION_CALL_SIZE_IN_BYTES};
55

66
// FUNCTION_CALL_SIZE * ACCOUNT_MAX_CALLS + 1
7-
global APP_PAYLOAD_SIZE: u64 = 17;
7+
global APP_PAYLOAD_SIZE: u64 = 21;
88
// FUNCTION_CALL_SIZE_IN_BYTES * ACCOUNT_MAX_CALLS + 32
9-
global APP_PAYLOAD_SIZE_IN_BYTES: u64 = 420;
9+
global APP_PAYLOAD_SIZE_IN_BYTES: u64 = 424;
1010

1111
global ACCOUNT_MAX_CALLS: u64 = 4;
1212

@@ -62,15 +62,15 @@ impl AppPayload {
6262
call.target_address,
6363
call.function_selector,
6464
call.args_hash,
65-
false,
65+
call.is_static,
6666
false
6767
);
6868
} else {
6969
let _result = context.call_private_function_with_packed_args(
7070
call.target_address,
7171
call.function_selector,
7272
call.args_hash,
73-
false,
73+
call.is_static,
7474
false
7575
);
7676
}

noir-projects/aztec-nr/authwit/src/entrypoint/fee.nr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use dep::aztec::prelude::PrivateContext;
22
use dep::aztec::protocol_types::{constants::GENERATOR_INDEX__FEE_PAYLOAD, hash::pedersen_hash, traits::{Hash, Serialize}};
33
use crate::entrypoint::function_call::FunctionCall;
44

5-
// 2 * 4 (function call) + 1
6-
global FEE_PAYLOAD_SIZE: Field = 9;
5+
// 2 * 5 (FUNCTION_CALL_SIZE) + 1
6+
global FEE_PAYLOAD_SIZE: Field = 11;
77

8-
// 2*97 + 32
9-
global FEE_PAYLOAD_SIZE_IN_BYTES: Field = 226;
8+
// 2 * 98 (FUNCTION_CALL_SIZE_IN_BYTES) + 32
9+
global FEE_PAYLOAD_SIZE_IN_BYTES: Field = 228;
1010

1111
global MAX_FEE_FUNCTION_CALLS = 2;
1212

@@ -58,15 +58,15 @@ impl FeePayload {
5858
call.target_address,
5959
call.function_selector,
6060
call.args_hash,
61-
false,
61+
call.is_static,
6262
false
6363
);
6464
} else {
6565
let _result = context.call_private_function_with_packed_args(
6666
call.target_address,
6767
call.function_selector,
6868
call.args_hash,
69-
false,
69+
call.is_static,
7070
false
7171
);
7272
}

noir-projects/aztec-nr/authwit/src/entrypoint/function_call.nr

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
use dep::aztec::protocol_types::{abis::function_selector::FunctionSelector, address::AztecAddress, traits::Serialize};
22

3-
// 1 (ARGS_HASH) + 1 (FUNCTION_SELECTOR) + 1 (TARGET_ADDRESS) + 1 (IS_PUBLIC)
4-
global FUNCTION_CALL_SIZE: Field = 4;
5-
// 3 * 32 + 1
6-
global FUNCTION_CALL_SIZE_IN_BYTES: Field = 97;
3+
// 1 (ARGS_HASH) + 1 (FUNCTION_SELECTOR) + 1 (TARGET_ADDRESS) + 1 (IS_PUBLIC) + 1 (IS_STATIC)
4+
global FUNCTION_CALL_SIZE: Field = 5;
5+
// 3 * 32 + 2
6+
global FUNCTION_CALL_SIZE_IN_BYTES: Field = 98;
77

88
struct FunctionCall {
99
args_hash: Field,
1010
function_selector: FunctionSelector,
1111
target_address: AztecAddress,
1212
is_public: bool,
13+
is_static: bool,
1314
}
1415

1516
impl Serialize<FUNCTION_CALL_SIZE> for FunctionCall {
1617
fn serialize(self) -> [Field; FUNCTION_CALL_SIZE] {
17-
[self.args_hash, self.function_selector.to_field(), self.target_address.to_field(), self.is_public as Field]
18+
[self.args_hash, self.function_selector.to_field(), self.target_address.to_field(), self.is_public as Field, self.is_static as Field]
1819
}
1920
}
2021

@@ -34,6 +35,7 @@ impl FunctionCall {
3435
bytes[i + 64] = target_address_bytes[i];
3536
}
3637
bytes[96] = self.is_public as u8;
38+
bytes[97] = self.is_static as u8;
3739
bytes
3840
}
3941
}

noir-projects/aztec-nr/aztec/src/context.nr

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ mod private_context;
55
mod public_context;
66
mod avm_context;
77
mod interface;
8+
mod call_interfaces;
89
mod gas;
910

10-
use interface::{
11-
ContextInterface, PrivateCallInterface, PublicCallInterface, PrivateVoidCallInterface,
12-
PublicVoidCallInterface, AvmCallInterface, AvmVoidCallInterface
11+
use interface::ContextInterface;
12+
use call_interfaces::{
13+
PrivateCallInterface, PrivateStaticCallInterface, PublicCallInterface, PublicStaticCallInterface,
14+
PrivateVoidCallInterface, PrivateStaticVoidCallInterface, PublicVoidCallInterface,
15+
PublicStaticVoidCallInterface, AvmCallInterface, AvmStaticCallInterface, AvmVoidCallInterface,
16+
AvmStaticVoidCallInterface
1317
};
1418
use private_context::PrivateContext;
1519
use private_context::PackedReturns;

0 commit comments

Comments
 (0)