Skip to content

Commit

Permalink
feat: add noir-compiler checks to aztec_macros (#4031)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

This PR moves the checks applied in
[noir_compiler](https://github.com/AztecProtocol/aztec-packages/blob/c38dbd2fe13dfcf019da3877c17ca8d148f45233/yarn-project/noir-compiler/src/contract-interface-gen/abi.ts#L92-L97)
to `aztec_macros`.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
TomAFrench authored Jan 12, 2024
1 parent a1fc3c7 commit 420a5c7
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions aztec_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ impl MacroProcessor for AztecMacro {
}
}

const FUNCTION_TREE_HEIGHT: u32 = 5;
const MAX_CONTRACT_FUNCTIONS: usize = 2_usize.pow(FUNCTION_TREE_HEIGHT);

#[derive(Debug, Clone)]
pub enum AztecMacroError {
AztecNotFound,
AztecComputeNoteHashAndNullifierNotFound { span: Span },
AztecContractHasTooManyFunctions { span: Span },
AztecContractConstructorMissing { span: Span },
}

impl From<AztecMacroError> for MacroError {
Expand All @@ -50,6 +55,16 @@ impl From<AztecMacroError> for MacroError {
secondary_message: None,
span: Some(span),
},
AztecMacroError::AztecContractHasTooManyFunctions { span } => MacroError {
primary_message: format!("Contract can only have a maximum of {} functions", MAX_CONTRACT_FUNCTIONS),
secondary_message: None,
span: Some(span),
},
AztecMacroError::AztecContractConstructorMissing { span } => MacroError {
primary_message: "Contract must have a constructor function".to_owned(),
secondary_message: None,
span: Some(span),
},
}
}
}
Expand Down Expand Up @@ -340,6 +355,28 @@ fn transform_module(
has_transformed_module = true;
}
}

if has_transformed_module {
// We only want to run these checks if the macro processor has found the module to be an Aztec contract.

if module.functions.len() > MAX_CONTRACT_FUNCTIONS {
let crate_graph = &context.crate_graph[crate_id];
return Err((
AztecMacroError::AztecContractHasTooManyFunctions { span: Span::default() }.into(),
crate_graph.root_file_id,
));
}

let constructor_defined = module.functions.iter().any(|func| func.name() == "constructor");
if !constructor_defined {
let crate_graph = &context.crate_graph[crate_id];
return Err((
AztecMacroError::AztecContractConstructorMissing { span: Span::default() }.into(),
crate_graph.root_file_id,
));
}
}

Ok(has_transformed_module)
}

Expand Down

0 comments on commit 420a5c7

Please sign in to comment.