Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Jul 3, 2024
1 parent ef795b7 commit 6c33c0f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ contract TestLog {

#[aztec(event)]
struct ExampleEvent1 {
value2: Field,
value3: Field,
value2: AztecAddress,
value3: u8,
}

#[aztec(storage)]
Expand Down Expand Up @@ -69,7 +69,7 @@ contract TestLog {
)
);

let event1 = ExampleEvent1 { value2: preimages[2], value3: preimages[3] };
let event1 = ExampleEvent1 { value2: AztecAddress::from_field(preimages[2]), value3: preimages[3] as u8 };

event1.emit(
encode_and_encrypt_event_with_randomness(
Expand All @@ -88,7 +88,7 @@ contract TestLog {

event0.emit(encode_event(&mut context));

let event1 = ExampleEvent1 { value2: preimages[2], value3: preimages[3] };
let event1 = ExampleEvent1 { value2: AztecAddress::from_field(preimages[2]), value3: preimages[3] as u8};

event1.emit(encode_event(&mut context));
}
Expand Down
30 changes: 28 additions & 2 deletions noir/noir-repo/aztec_macros/src/transforms/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,20 @@ fn generate_trait_impl_serialize(
event_fields: &[(String, String)],
) -> Result<NoirTraitImpl, AztecMacroError> {
let field_names =
event_fields.iter().map(|field| format!("self.{}", field.0)).collect::<Vec<String>>();
event_fields.iter().map(|field| {
let field_type = field.1.as_str();
match field_type {
"Field" => format!("self.{}", field.0),
"bool" |
"u8" |
"u32" |
"u64" |
"i8" |
"i32" |
"i64" => format!("self.{} as Field", field.0),
_ => format!("self.{}.to_field()", field.0)
}
}).collect::<Vec<String>>();
let field_input = field_names.join(",");

let trait_impl_source = format!(
Expand Down Expand Up @@ -154,7 +167,20 @@ fn generate_trait_impl_deserialize(
let field_names: Vec<String> = event_fields
.iter()
.enumerate()
.map(|(index, field)| format!("{}: fields[{}]", field.0, index))
.map(|(index, field)| {
let field_type = field.1.as_str();
match field_type {
"Field" => format!("{}: fields[{}]", field.0, index),
"bool" |
"u8" |
"u32" |
"u64" |
"i8" |
"i32" |
"i64" => format!("{}: fields[{}] as {}", field.0, index, field_type),
_ => format!("{}: {}::from_field(fields[{}])", field.0, field.1, index)
}
})
.collect::<Vec<String>>();
let field_input = field_names.join(",");

Expand Down
15 changes: 12 additions & 3 deletions yarn-project/end-to-end/src/e2e_event_logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ describe('Logs', () => {

// We expect the fields to have been populated correctly
expect(event1?.value2).toStrictEqual(preimage[2]);
expect(event1?.value3).toStrictEqual(preimage[3]);
// We get the last byte here because value3 is of type u8
expect(event1?.value3).toStrictEqual(new Fr(preimage[3].toBuffer().subarray(31)));

// Again, trying to decode another event with mismatching data does not yield anything
const badEvent1 = TestLogContract.events.ExampleEvent0.decode(decryptedLog1!.payload);
Expand Down Expand Up @@ -189,7 +190,11 @@ describe('Logs', () => {

const exampleEvent1Sort = (a: ExampleEvent1, b: ExampleEvent1) => (a.value2 > b.value2 ? 1 : -1);
expect(collectedEvent1s.sort(exampleEvent1Sort)).toStrictEqual(
preimage.map(preimage => ({ value2: preimage[2], value3: preimage[3] })).sort(exampleEvent1Sort),
preimage.map(preimage => ({
value2: preimage[2],
// We get the last byte here because value3 is of type u8
value3: new Fr(preimage[3].toBuffer().subarray(31))
})).sort(exampleEvent1Sort),
);
});

Expand Down Expand Up @@ -227,7 +232,11 @@ describe('Logs', () => {

const exampleEvent1Sort = (a: ExampleEvent1, b: ExampleEvent1) => (a.value2 > b.value2 ? 1 : -1);
expect(collectedEvent1s.sort(exampleEvent1Sort)).toStrictEqual(
preimage.map(preimage => ({ value2: preimage[2], value3: preimage[3] })).sort(exampleEvent1Sort),
preimage.map(preimage => ({
value2: preimage[2],
// We get the last byte here because value3 is of type u8
value3: new Fr(preimage[3].toBuffer().subarray(31))
})).sort(exampleEvent1Sort),
);
});
});
Expand Down

0 comments on commit 6c33c0f

Please sign in to comment.