Skip to content

Commit

Permalink
feat!: contract_abi-exports (#5386)
Browse files Browse the repository at this point in the history
# Goal

This PR aims to expose arbitrary types and values resulting from
contract compilation in the resulting JSON artifact, in a way that is
not tied to aztec-specific features or even smart contracts at all.

# Problem

Up until now, Noir compiled crates that used the `contract` keyword with
a specific flow, which also added additional structs and metadata to the
output such as whatever structs were marked with the `#[event]`
attribute. This coupled Noir to smart contract specific constructs,
which were propagated through the compiler (from the parser to the
actual compilation output). For
#5079 and several
other tasks that aim to reduce the mental load and improve the general
devex of our users, we ***need*** to expose several other structs that
are even more specific to aztec, which would only compromise the
generality of the compiler further.

# Proposed solution

The introduction of a new attribute `#[abi(tag)]` that can be applied to
both `structs` and `global` top-level statements, and export types (with
the current `ABIType` format) and values (with the new `ABIValue`
format) in a way that can be interpreted by components further
downstream (for example, our typescript codegen). This way, the noir
compiler doesn't know (or care) about whatever gets included in the
artifact.

The `events` contract artifact key gets replaced by:

```typescript
outputs: {
    structs: Record<string, ABIType[]>;
    globals: Record<string, ABIValue[]>;
};
```

# What this approach allows

- Removing the smart contract specific attribute `#[event]`, replacing
it by a more general `#[abi(events)]`.
- Substantial devex improvements, such as exposing storage layout, note
ids:

![image](https://github.com/AztecProtocol/aztec-packages/assets/5404052/dba1d6ca-1286-4d4d-912e-f222d3414f32)
...or even private function return values prior to macro processing for
decoding `.view` calls
#2665

---------

Co-authored-by: esau <152162806+sklppy88@users.noreply.github.com>
Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 4, 2024
1 parent d827705 commit 745d522
Show file tree
Hide file tree
Showing 95 changed files with 641 additions and 324 deletions.
6 changes: 3 additions & 3 deletions avm-transpiler/src/transpile_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct TranspiledContract {
pub name: String,
// Functions can be ACIR or AVM
pub functions: Vec<AvmOrAcirContractFunction>,
pub events: serde_json::Value,
pub outputs: serde_json::Value,
pub file_map: serde_json::Value,
//pub warnings: serde_json::Value,
}
Expand All @@ -29,7 +29,7 @@ pub struct CompiledAcirContract {
pub noir_version: String,
pub name: String,
pub functions: Vec<AcirContractFunction>,
pub events: serde_json::Value,
pub outputs: serde_json::Value,
pub file_map: serde_json::Value,
//pub warnings: serde_json::Value,
}
Expand Down Expand Up @@ -113,7 +113,7 @@ impl From<CompiledAcirContract> for TranspiledContract {
noir_version: contract.noir_version,
name: contract.name,
functions, // some acir, some transpiled avm functions
events: contract.events,
outputs: contract.outputs,
file_map: contract.file_map,
//warnings: contract.warnings,
}
Expand Down
1 change: 1 addition & 0 deletions boxes/boxes/react/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ contract BoxReact {

use dep::value_note::value_note::{ValueNote, VALUE_NOTE_LEN};

#[aztec(storage)]
struct Storage {
numbers: Map<AztecAddress, PrivateMutable<ValueNote>>,
}
Expand Down
1 change: 1 addition & 0 deletions boxes/boxes/vanilla/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ contract Vanilla {

use dep::value_note::value_note::{ValueNote, VALUE_NOTE_LEN};

#[aztec(storage)]
struct Storage {
numbers: Map<AztecAddress, PrivateMutable<ValueNote>>,
}
Expand Down
5 changes: 1 addition & 4 deletions docs/docs/developers/contracts/references/storage/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ Public state follows the Ethereum style account model, where each contract has i

## Storage struct

:::info
The struct **must** be called `Storage` for the Aztec.nr library to properly handle it (this will be relaxed in the future).
:::

```rust
#[aztec(storage)]
struct Storage {
// public state variables
// private state variables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ To learn more about how storage works in Aztec, read [the concepts](../../../../

[See the storage reference](../../references/storage/main.md).

:::info
The struct **must** be called `Storage` for the Aztec.nr library to properly handle it (this will be relaxed in the future).
:::

```rust
#[aztec(storage)]
struct Storage {
// public state variables
// private state variables
Expand Down
28 changes: 3 additions & 25 deletions docs/docs/developers/sandbox/references/cheat_codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,18 +461,11 @@ The baseSlot is specified in the Aztec.nr contract.
#### Example

```rust
#[aztec(storage)]
struct Storage {
balances: Map<AztecAddress, PublicMutable<Field>>,
}

impl Storage {
fn init() -> Self {
Storage {
balances: Map::new(1, |slot| PublicMutable::new(slot)),
}
}
}

contract Token {
...
}
Expand All @@ -499,18 +492,11 @@ Note: One Field element occupies a storage slot. Hence, structs with multiple fi
#### Example

```rust
#[aztec(storage)]
struct Storage {
balances: Map<AztecAddress, PublicMutable<Field>>,
}

impl Storage {
fn init(context: Context) -> Self {
Storage {
balances: Map::new(context, 1, |context, slot| PublicMutable::new(context, slot)),
}
}
}

contract Token {
...
}
Expand Down Expand Up @@ -538,20 +524,12 @@ Note: One Field element occupies a storage slot. Hence, structs with multiple fi

#### Example
```rust
#[aztec(storage)]
struct Storage {
...
pending_shields: Set<TransparentNote, TRANSPARENT_NOTE_LEN>,
}

impl Storage {
fn init() -> Self {
Storage {
...
pending_shields: Set::new(context, 5, TransparentNoteMethods),
}
}
}

contract Token {
...
}
Expand Down
2 changes: 1 addition & 1 deletion l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ library Constants {
uint256 internal constant DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE =
0x85864497636cf755ae7bde03f267ce01a520981c21c3682aaf82a631;
uint256 internal constant DEPLOYER_CONTRACT_ADDRESS =
0x1ad693effc2a4a40fdf5911ff29e19d2b6cd3cf73e6c3685519a6fb783144dcb;
0x0ccb2a7150ed29533f211e223f98450cf4769ff8938e9d4ad303f71c5e302600;
uint256 internal constant L1_TO_L2_MESSAGE_ORACLE_CALL_LENGTH = 17;
uint256 internal constant MAX_NOTE_FIELDS_LENGTH = 20;
uint256 internal constant GET_NOTE_ORACLE_RETURN_LENGTH = 23;
Expand Down
10 changes: 5 additions & 5 deletions l1-contracts/test/fixtures/empty_block_0.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"l2ToL1Messages": []
},
"block": {
"archive": "0x122e56310a16134c202e94b3ba9cb74de123f7ec493e601d2f0b6da00cf57a4a",
"archive": "0x0335fb6ef6a421a73ba4b4f124f6bf1101333cdb1ade05bc7cac1e83885a8df4",
"body": "0x00000000",
"txsEffectsHash": "0x00df6b1c97b9e01113fa0363d9ff71c85addc74e92b22d433b2fb082d2493896",
"decodedHeader": {
Expand All @@ -23,8 +23,8 @@
"chainId": 31337,
"timestamp": 0,
"version": 1,
"coinbase": "0x66440eb666440eb666440eb666440eb666440eb6",
"feeRecipient": "0x061ca689507c7f1ccc68c2ad086c9d5d94f50869cb1b718c6a46aaf77a4500ba"
"coinbase": "0x89c70d339816591366d159b0ea442faf8af62682",
"feeRecipient": "0x0ff93b075ef4b1480ce807a2c3c0b9ca9b3bca8de09a33ae658acb21e8107f50"
},
"lastArchive": {
"nextAvailableLeafIndex": 1,
Expand All @@ -51,8 +51,8 @@
}
}
},
"header": "0x1e3523d3bd50ae6204e1ec2ee1bdf8af4c6217ec80900052d2cf4259379dd13000000001000000000000000000000000000000000000000000000000000000000000000100df6b1c97b9e01113fa0363d9ff71c85addc74e92b22d433b2fb082d249389600089a9d421a82c4a25f7acbebe69e638d5b064fa8a60e018793dcb0be53752c0007638bb56b6dda2b64b8f76841114ac3a87a1820030e2e16772c4d294879c31864fcdaa80ff2719154fa7c8a9050662972707168d69eac9db6fd3110829f800000001016642d9ccd8346c403aa4c3fa451178b22534a27035cdaa6ec34ae53b29c50cb000000800bcfa3e9f1a8922ee92c6dc964d6595907c1804a86753774322b468f69d4f278000001000572c8db882674dd026b8877fbba1b700a4407da3ae9ce5fa43215a28163362b000000800000000000000000000000000000000000000000000000000000000000007a6900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000066440eb666440eb666440eb666440eb666440eb6061ca689507c7f1ccc68c2ad086c9d5d94f50869cb1b718c6a46aaf77a4500ba",
"publicInputsHash": "0x00b833c76dd93572e83879eeb4085a3cd71f24831384fb4d3fe531ed87b859e3",
"header": "0x1e3523d3bd50ae6204e1ec2ee1bdf8af4c6217ec80900052d2cf4259379dd13000000001000000000000000000000000000000000000000000000000000000000000000100df6b1c97b9e01113fa0363d9ff71c85addc74e92b22d433b2fb082d249389600089a9d421a82c4a25f7acbebe69e638d5b064fa8a60e018793dcb0be53752c0007638bb56b6dda2b64b8f76841114ac3a87a1820030e2e16772c4d294879c31864fcdaa80ff2719154fa7c8a9050662972707168d69eac9db6fd3110829f800000001016642d9ccd8346c403aa4c3fa451178b22534a27035cdaa6ec34ae53b29c50cb000000800bcfa3e9f1a8922ee92c6dc964d6595907c1804a86753774322b468f69d4f278000001000572c8db882674dd026b8877fbba1b700a4407da3ae9ce5fa43215a28163362b000000800000000000000000000000000000000000000000000000000000000000007a6900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000089c70d339816591366d159b0ea442faf8af626820ff93b075ef4b1480ce807a2c3c0b9ca9b3bca8de09a33ae658acb21e8107f50",
"publicInputsHash": "0x00e2216979cc60a156f5a3ed2270286ad640960492fce44e17abc29d154bb01b",
"numTxs": 0
}
}
14 changes: 7 additions & 7 deletions l1-contracts/test/fixtures/empty_block_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"l2ToL1Messages": []
},
"block": {
"archive": "0x0d53ab5014f578345d853cd208bda74db64358fef8e5f646240db05893f6e780",
"archive": "0x220677d238ea4407e12e9c0caa76bf6753ecfa88d10a80107f73f6005b5d5c3b",
"body": "0x00000000",
"txsEffectsHash": "0x00df6b1c97b9e01113fa0363d9ff71c85addc74e92b22d433b2fb082d2493896",
"decodedHeader": {
Expand All @@ -21,14 +21,14 @@
"globalVariables": {
"blockNumber": 2,
"chainId": 31337,
"timestamp": 1711637579,
"timestamp": 1712154039,
"version": 1,
"coinbase": "0x66440eb666440eb666440eb666440eb666440eb6",
"feeRecipient": "0x061ca689507c7f1ccc68c2ad086c9d5d94f50869cb1b718c6a46aaf77a4500ba"
"coinbase": "0x89c70d339816591366d159b0ea442faf8af62682",
"feeRecipient": "0x0ff93b075ef4b1480ce807a2c3c0b9ca9b3bca8de09a33ae658acb21e8107f50"
},
"lastArchive": {
"nextAvailableLeafIndex": 2,
"root": "0x122e56310a16134c202e94b3ba9cb74de123f7ec493e601d2f0b6da00cf57a4a"
"root": "0x0335fb6ef6a421a73ba4b4f124f6bf1101333cdb1ade05bc7cac1e83885a8df4"
},
"stateReference": {
"l1ToL2MessageTree": {
Expand All @@ -51,8 +51,8 @@
}
}
},
"header": "0x122e56310a16134c202e94b3ba9cb74de123f7ec493e601d2f0b6da00cf57a4a00000002000000000000000000000000000000000000000000000000000000000000000100df6b1c97b9e01113fa0363d9ff71c85addc74e92b22d433b2fb082d249389600089a9d421a82c4a25f7acbebe69e638d5b064fa8a60e018793dcb0be53752c0007638bb56b6dda2b64b8f76841114ac3a87a1820030e2e16772c4d294879c31864fcdaa80ff2719154fa7c8a9050662972707168d69eac9db6fd3110829f800000002016642d9ccd8346c403aa4c3fa451178b22534a27035cdaa6ec34ae53b29c50cb000001000bcfa3e9f1a8922ee92c6dc964d6595907c1804a86753774322b468f69d4f278000001800572c8db882674dd026b8877fbba1b700a4407da3ae9ce5fa43215a28163362b000000c00000000000000000000000000000000000000000000000000000000000007a6900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000006605844b66440eb666440eb666440eb666440eb666440eb6061ca689507c7f1ccc68c2ad086c9d5d94f50869cb1b718c6a46aaf77a4500ba",
"publicInputsHash": "0x00035031752fa374209f1ec4bc08613cae25ba65779c10130681421f6e71c6d9",
"header": "0x0335fb6ef6a421a73ba4b4f124f6bf1101333cdb1ade05bc7cac1e83885a8df400000002000000000000000000000000000000000000000000000000000000000000000100df6b1c97b9e01113fa0363d9ff71c85addc74e92b22d433b2fb082d249389600089a9d421a82c4a25f7acbebe69e638d5b064fa8a60e018793dcb0be53752c0007638bb56b6dda2b64b8f76841114ac3a87a1820030e2e16772c4d294879c31864fcdaa80ff2719154fa7c8a9050662972707168d69eac9db6fd3110829f800000002016642d9ccd8346c403aa4c3fa451178b22534a27035cdaa6ec34ae53b29c50cb000001000bcfa3e9f1a8922ee92c6dc964d6595907c1804a86753774322b468f69d4f278000001800572c8db882674dd026b8877fbba1b700a4407da3ae9ce5fa43215a28163362b000000c00000000000000000000000000000000000000000000000000000000000007a690000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000660d65b789c70d339816591366d159b0ea442faf8af626820ff93b075ef4b1480ce807a2c3c0b9ca9b3bca8de09a33ae658acb21e8107f50",
"publicInputsHash": "0x00dbd8f31a147084243681f36208343c03b5415482040df2682a6ad175c3de0c",
"numTxs": 0
}
}
22 changes: 11 additions & 11 deletions l1-contracts/test/fixtures/mixed_block_0.json

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions l1-contracts/test/fixtures/mixed_block_1.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ contract AppSubscription {

use crate::subscription_note::{SubscriptionNote, SUBSCRIPTION_NOTE_LEN};

#[aztec(storage)]
struct Storage {
// The following is only needed in private but we use ShareImmutable here instead of PrivateImmutable because
// the value can be publicly known and SharedImmutable provides us with a better devex here because we don't
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ contract AvmInitializerTest {
use dep::aztec::state_vars::PublicImmutable;
use dep::aztec::protocol_types::address::AztecAddress;

#[aztec(storage)]
struct Storage {
immutable: PublicImmutable<Field>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ contract AvmTest {
// avm lib
use dep::aztec::avm::hash::{keccak256, poseidon, sha256};

#[aztec(storage)]
struct Storage {
single: PublicMutable<Field>,
list: PublicMutable<Note>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ contract Benchmarking {

use dep::aztec::{context::Context};

#[aztec(storage)]
struct Storage {
notes: Map<AztecAddress, PrivateSet<ValueNote>>,
balances: Map<AztecAddress, PublicMutable<Field>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ contract CardGame {
use crate::cards::{PACK_CARDS, Deck, Card, get_pack_cards, compute_deck_strength};
use crate::game::{NUMBER_OF_PLAYERS, NUMBER_OF_CARDS_DECK, PLAYABLE_CARDS, PlayerEntry, Game, GAME_SERIALIZED_LEN};

#[aztec(storage)]
struct Storage {
collections: Map<AztecAddress, Deck>,
game_decks: Map<Field, Map<AztecAddress, Deck>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ contract Child {
};
use dep::value_note::value_note::ValueNote;

#[aztec(storage)]
struct Storage {
current_value: PublicMutable<Field>,
a_private_value: PrivateSet<ValueNote>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ contract Claim {
use dep::value_note::value_note::ValueNote;
use interfaces::Token;

#[aztec(storage)]
struct Storage {
// Address of a contract based on whose notes we distribute the rewards
target_contract: SharedImmutable<AztecAddress>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dep::aztec::protocol_types::{
traits::Serialize
};

// #[event]
// #[aztec(event)]
struct ContractClassRegistered {
contract_class_id: ContractClassId,
version: Field,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Serialize<MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS + 3> for
}
}

// #[event]
// #[aztec(event)]
struct ClassPrivateFunctionBroadcasted {
contract_class_id: ContractClassId,
artifact_metadata_hash: Field,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Serialize<MAX_PACKED_BYTECODE_SIZE_PER_UNCONSTRAINED_FUNCTION_IN_FIELDS + 2
}
}

// #[event]
// #[aztec(event)]
struct ClassUnconstrainedFunctionBroadcasted {
contract_class_id: ContractClassId,
artifact_metadata_hash: Field,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dep::aztec::protocol_types::{
constants::DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE, traits::Serialize
};

// #[event]
// #[aztec(event)]
struct ContractInstanceDeployed {
address: AztecAddress,
version: u8,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ contract Counter {
// docs:end:imports

// docs:start:storage_struct
#[aztec(storage)]
struct Storage {
counters: Map<AztecAddress, EasyPrivateUint>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ contract Crowdfunding {
use dep::value_note::value_note::ValueNote;
use interfaces::Token;

#[event]
#[aztec(event)]
struct WithdrawalProcessed {
who: AztecAddress,
amount: u64,
Expand All @@ -21,6 +21,7 @@ contract Crowdfunding {
}
}

#[aztec(storage)]
struct Storage {
// Token used for donations (e.g. DAI)
donation_token: SharedImmutable<AztecAddress>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ contract DelegatedOn {
};
use dep::value_note::value_note::ValueNote;

#[aztec(storage)]
struct Storage {
current_value: PublicMutable<Field>,
a_private_value: PrivateSet<ValueNote>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ contract Delegator {
};
use dep::value_note::value_note::ValueNote;

#[aztec(storage)]
struct Storage {
current_value: PublicMutable<Field>,
a_private_value: PrivateSet<ValueNote>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ contract DocsExample {
use crate::options::create_account_card_getter_options;
use crate::types::{card_note::{CardNote, CARD_NOTE_LEN}, leader::Leader};

#[aztec(storage)]
struct Storage {
// Shows how to create a custom struct in PublicMutable
// docs:start:storage-leader-declaration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ contract EasyPrivateToken {
use dep::value_note::{balance_utils, value_note::ValueNote};
use dep::easy_private_state::EasyPrivateUint;

#[aztec(storage)]
struct Storage {
balances: Map<AztecAddress, EasyPrivateUint>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ contract EasyPrivateVoting {
use dep::aztec::context::Context;
// docs:end:imports
// docs:start:storage_struct
#[aztec(storage)]
struct Storage {
admin: PublicMutable<AztecAddress>, // admin can end vote
tally: Map<Field, PublicMutable<Field>>, // we will store candidate as key and number of votes as value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ contract EcdsaAccount {

use crate::ecdsa_public_key_note::EcdsaPublicKeyNote;

#[aztec(storage)]
struct Storage {
public_key: PrivateImmutable<EcdsaPublicKeyNote>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ contract Escrow {

use dep::address_note::address_note::AddressNote;

#[aztec(storage)]
struct Storage {
owner: PrivateImmutable<AddressNote>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ contract FPC {

use crate::interfaces::Token;

#[aztec(storage)]
struct Storage {
other_asset: SharedImmutable<AztecAddress>,
gas_token_address: SharedImmutable<AztecAddress>,
Expand Down
Loading

0 comments on commit 745d522

Please sign in to comment.