Skip to content

Commit

Permalink
feat: add support for fieldable in events (#7310)
Browse files Browse the repository at this point in the history
Finishes #6951
  • Loading branch information
sklppy88 authored Jul 4, 2024
1 parent c19029a commit 694cebc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ contract ContractInstanceDeployer {
oracle::logs::emit_unencrypted_log_private_internal
};

// @todo This should be using an event, but currently we only support fields in the struct.
// #[aztec(event)]
#[aztec(event)]
struct ContractInstanceDeployed {
DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE: Field,
address: AztecAddress,
version: u8,
salt: Field,
Expand All @@ -21,23 +21,6 @@ contract ContractInstanceDeployer {
deployer: AztecAddress,
}

global CONTRACT_INSTANCE_DEPLOYED_SERIALIZED_SIZE: Field = 8;

impl Serialize<CONTRACT_INSTANCE_DEPLOYED_SERIALIZED_SIZE> for ContractInstanceDeployed {
fn serialize(self: Self) -> [Field; CONTRACT_INSTANCE_DEPLOYED_SERIALIZED_SIZE] {
[
DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE,
self.address.to_field(),
self.version as Field,
self.salt,
self.contract_class_id.to_field(),
self.initialization_hash,
self.public_keys_hash.to_field(),
self.deployer.to_field(),
]
}
}

#[aztec(private)]
fn deploy(
salt: Field,
Expand All @@ -62,7 +45,16 @@ contract ContractInstanceDeployer {
context.push_nullifier(address.to_field(), 0);

// Broadcast the event
let event = ContractInstanceDeployed { contract_class_id, address, public_keys_hash, initialization_hash, salt, deployer, version: 1 };
let event = ContractInstanceDeployed {
DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE,
contract_class_id,
address,
public_keys_hash,
initialization_hash,
salt,
deployer,
version: 1
};

let payload = event.serialize();
dep::aztec::oracle::debug_log::debug_log_format("ContractInstanceDeployed: {}", payload);
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ contract Token {

#[aztec(event)]
struct Transfer {
from: Field,
to: Field,
from: AztecAddress,
to: AztecAddress,
amount: Field,
}

Expand Down Expand Up @@ -338,7 +338,7 @@ contract Token {
storage.balances.sub(from, amount).emit(encode_and_encrypt_note_with_keys_unconstrained(&mut context, from_ovpk, from_ivpk));
storage.balances.add(to, amount).emit(encode_and_encrypt_note_with_keys_unconstrained(&mut context, from_ovpk, to_ivpk));

Transfer { from: from.to_field(), to: to.to_field(), amount: amount.to_field() }.emit(encode_and_encrypt_event_with_keys_unconstrained(&mut context, from_ovpk, to_ivpk));
Transfer { from, to, amount: amount.to_field() }.emit(encode_and_encrypt_event_with_keys_unconstrained(&mut context, from_ovpk, to_ivpk));
}
// docs:end:transfer

Expand Down
26 changes: 23 additions & 3 deletions noir/noir-repo/aztec_macros/src/transforms/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,19 @@ fn generate_trait_impl_serialize(
event_len: u32,
event_fields: &[(String, String)],
) -> Result<NoirTraitImpl, AztecMacroError> {
let field_names =
event_fields.iter().map(|field| format!("self.{}", field.0)).collect::<Vec<String>>();
let field_names = 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 +165,16 @@ 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
19 changes: 16 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,13 @@ 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 +234,13 @@ 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 694cebc

Please sign in to comment.