Skip to content

Commit

Permalink
feat!: contract_abi-exports (AztecProtocol/aztec-packages#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
AztecProtocol/aztec-packages#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
AztecProtocol/aztec-packages#2665

---------

Co-authored-by: esau <[email protected]>
Co-authored-by: Tom French <[email protected]>
  • Loading branch information
3 people committed Apr 4, 2024
1 parent a0f7474 commit 0322464
Show file tree
Hide file tree
Showing 73 changed files with 958 additions and 1,864 deletions.
2 changes: 1 addition & 1 deletion .aztec-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bb719200034e3bc6db09fb56538dadca4203abf4
745d5229db86b2188f52ab7ccc8f568aef8f5797
3 changes: 1 addition & 2 deletions .github/Cross.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
passthrough = [
"HOME",
"RUST_BACKTRACE",
"BARRETENBERG_BIN_DIR",
"BLNS_JSON_PATH"
"BARRETENBERG_BIN_DIR"
]
volumes = [
"HOME",
Expand Down
14 changes: 1 addition & 13 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,6 @@ jobs:
- name: Install Yarn dependencies
uses: ./.github/actions/setup

- name: Install wasm-bindgen-cli
uses: taiki-e/install-action@v2
with:
tool: [email protected]

- name: Install wasm-opt
run: |
npm i wasm-opt -g
- name: Query new noir version
id: noir-version
run: |
Expand Down Expand Up @@ -116,20 +107,17 @@ jobs:
if: ${{ always() }}

needs:
- release-please
- update-acvm-workspace-package-versions
- update-docs

env:
# We treat any skipped or failing jobs as a failure for the workflow as a whole.
FAIL: ${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') || contains(needs.*.result, 'skipped') }}
FAIL: ${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') || contains(needs.*.result, 'skipped') }

steps:
- name: Add warning to sticky comment
uses: marocchino/sticky-pull-request-comment@v2
with:
# We need to specify the PR on which to make the comment as workflow is triggered by push.
number: ${{ fromJSON(needs.release-please.outputs.release-pr).number }}
# delete the comment in case failures have been fixed
delete: ${{ !env.FAIL }}
message: "The release workflow has not completed successfully. Releasing now will result in a broken release"
Expand Down
46 changes: 22 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions aztec_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ use transforms::{

use noirc_frontend::{
hir::def_collector::dc_crate::{UnresolvedFunctions, UnresolvedTraitImpl},
macros_api::{
CrateId, FileId, HirContext, MacroError, MacroProcessor, SecondaryAttribute, SortedModule,
Span,
},
macros_api::{CrateId, FileId, HirContext, MacroError, MacroProcessor, SortedModule, Span},
};

use utils::{
Expand Down Expand Up @@ -90,15 +87,18 @@ fn transform_module(module: &mut SortedModule) -> Result<bool, AztecMacroError>
let mut has_transformed_module = false;

// Check for a user defined storage struct
let storage_defined = check_for_storage_definition(module);
let storage_implemented = check_for_storage_implementation(module);

if storage_defined && !storage_implemented {
generate_storage_implementation(module)?;
let maybe_storage_struct_name = check_for_storage_definition(module)?;
let storage_defined = maybe_storage_struct_name.is_some();

if let Some(storage_struct_name) = maybe_storage_struct_name {
if !check_for_storage_implementation(module, &storage_struct_name) {
generate_storage_implementation(module, &storage_struct_name)?;
}
}

for structure in module.types.iter() {
if structure.attributes.iter().any(|attr| matches!(attr, SecondaryAttribute::Event)) {
for structure in module.types.iter_mut() {
if structure.attributes.iter().any(|attr| is_custom_attribute(attr, "aztec(event)")) {
module.impls.push(generate_selector_impl(structure));
has_transformed_module = true;
}
Expand Down
8 changes: 5 additions & 3 deletions aztec_macros/src/transforms/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use crate::{
chained_dep,
utils::{
ast_utils::{
call, expression, ident, ident_path, make_statement, make_type, path, variable_path,
call, expression, ident, ident_path, is_custom_attribute, make_statement, make_type,
path, variable_path,
},
constants::SIGNATURE_PLACEHOLDER,
errors::AztecMacroError,
Expand All @@ -38,7 +39,8 @@ use crate::{
/// This allows developers to emit events without having to write the signature of the event every time they emit it.
/// The signature cannot be known at this point since types are not resolved yet, so we use a signature placeholder.
/// It'll get resolved after by transforming the HIR.
pub fn generate_selector_impl(structure: &NoirStruct) -> TypeImpl {
pub fn generate_selector_impl(structure: &mut NoirStruct) -> TypeImpl {
structure.attributes.push(SecondaryAttribute::Abi("events".to_string()));
let struct_type =
make_type(UnresolvedTypeData::Named(path(structure.name.clone()), vec![], true));

Expand Down Expand Up @@ -174,7 +176,7 @@ pub fn transform_events(
) -> Result<(), (AztecMacroError, FileId)> {
for struct_id in collect_crate_structs(crate_id, context) {
let attributes = context.def_interner.struct_attributes(&struct_id);
if attributes.iter().any(|attr| matches!(attr, SecondaryAttribute::Event)) {
if attributes.iter().any(|attr| is_custom_attribute(attr, "aztec(event)")) {
transform_event(struct_id, &mut context.def_interner)?;
}
}
Expand Down
Loading

0 comments on commit 0322464

Please sign in to comment.