Skip to content

Commit

Permalink
Merge branch 'gj/storage_in_contract_artifact' of github.com:AztecPro…
Browse files Browse the repository at this point in the history
…tocol/aztec-packages into gj/storage_layout
  • Loading branch information
Thunkar committed Mar 22, 2024
2 parents 14b4fde + 5048795 commit d958ec9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/aztec/src/prelude.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
state_vars::{
map::Map, private_immutable::PrivateImmutable, private_mutable::PrivateMutable,
public_immutable::PublicImmutable, public_mutable::PublicMutable, private_set::PrivateSet,
shared_immutable::SharedImmutable
shared_immutable::SharedImmutable, storage::Storable, storage::StorableNote
},
log::{emit_unencrypted_log, emit_encrypted_log}, context::PrivateContext,
note::{
Expand Down
10 changes: 10 additions & 0 deletions noir-projects/aztec-nr/aztec/src/state_vars/storage.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@ trait Storage<T> where T: Serialize<N> + Deserialize<N> {
self.storage_slot
}
}

struct Storable<N> {
slot: Field,
typ: str<N>
}

struct StorableNote<N> {
id: Field,
typ: str<N>
}
71 changes: 71 additions & 0 deletions noir/noir-repo/aztec_macros/src/transforms/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,74 @@ pub fn assign_storage_slots(
}
Ok(())
}

pub fn generate_storage_layout(
module: &mut SortedModule,
storage_struct_name: &String,
) -> Result<(), AztecMacroError> {
let definition = module
.types
.iter()
.find(|r#struct| r#struct.name.0.contents == *storage_struct_name)
.unwrap();

let mut generic_args = vec![];
let mut storable_fields = vec![];

definition.fields.iter().enumerate().for_each(|(index, (field_ident, _))| {
storable_fields.push(format!("{}: Storable<N{}>", field_ident, index));
generic_args.push(format!("N{}", index));
});

let storage_fields_source = format!(
"
struct StorageFields<{}> {{
{}
}}
",
generic_args.join(", "),
storable_fields.join(",\n")
);

let field_constructors = definition
.fields
.iter()
.flat_map(|field| {
generate_storage_field_constructor(field, slot_zero.clone())
.map(|expression| (field.0.clone(), expression))
})
.collect();

let storage_constructor_statement = make_statement(StatementKind::Expression(expression(
ExpressionKind::constructor((chained_path!(storage_struct_name), field_constructors)),
)));

let init = NoirFunction::normal(FunctionDefinition::normal(
&ident("init"),
&vec![],
&[(
ident("context"),
make_type(UnresolvedTypeData::Named(
chained_dep!("aztec", "context", "Context"),
vec![],
true,
)),
)],
&BlockExpression(vec![storage_constructor_statement]),
&[],
&return_type(chained_path!("Self")),
));

let storage_impl = TypeImpl {
object_type: UnresolvedType {
typ: UnresolvedTypeData::Named(chained_path!(storage_struct_name), vec![], true),
span: Some(Span::default()),
},
type_span: Span::default(),
generics: vec![],
methods: vec![(init, Span::default())],
};
module.impls.push(storage_impl);

Ok(())
}

0 comments on commit d958ec9

Please sign in to comment.