Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: packing events #11544

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions noir-projects/aztec-nr/aztec/src/event/event_interface.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dep::protocol_types::abis::event_selector::EventSelector;
use dep::protocol_types::traits::Serialize;
use dep::protocol_types::traits::Packable;

pub trait EventInterface<let N: u32>: Serialize<N> {
pub trait EventInterface<let N: u32>: Packable<N> {
fn to_be_bytes(self) -> [u8; N * 32 + 32];
fn get_event_type_id() -> EventSelector;
fn emit<Env>(self, emit: fn[Env](Self) -> ());
Expand Down
12 changes: 6 additions & 6 deletions noir-projects/aztec-nr/aztec/src/macros/events/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use protocol_types::meta::generate_serialize_to_fields;
comptime fn generate_event_interface(s: StructDefinition) -> Quoted {
let name = s.name();
let typ = s.as_type();
let (serialization_fields, _) =
generate_serialize_to_fields(quote { self }, typ, &[quote {self.header}], false);
let content_len = serialization_fields.len();
let (packing_fields, _) =
generate_serialize_to_fields(quote { self }, typ, &[quote {self.header}], true);
let content_len = packing_fields.len();

let event_type_id = compute_event_selector(s);

Expand All @@ -20,10 +20,10 @@ comptime fn generate_event_interface(s: StructDefinition) -> Quoted {
buffer[i] = event_type_id_bytes[i];
}

let serialized_event = Serialize::<$content_len>::serialize(self);
let packed_event = Packable::<$content_len>::pack(self);

for i in 0..serialized_event.len() {
let bytes: [u8; 32] = serialized_event[i].to_be_bytes();
for i in 0..packed_event.len() {
let bytes: [u8; 32] = packed_event[i].to_be_bytes();
for j in 0..32 {
buffer[32 + i * 32 + j] = bytes[j];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ where
{
let selector = Event::get_event_type_id();

let serialized_event = event.serialize();
let packed_event = event.pack();
let mut emitted_log = [0; N + 1];

// We put the selector in the "last" place, to avoid reading or assigning to an expression in an index
for i in 0..serialized_event.len() {
emitted_log[i] = serialized_event[i];
for i in 0..packed_event.len() {
emitted_log[i] = packed_event[i];
}

emitted_log[serialized_event.len()] = selector.to_field();
emitted_log[packed_event.len()] = selector.to_field();

context.emit_public_log(emitted_log);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ contract ContractInstanceDeployer {
constants::{DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE, REGISTERER_CONTRACT_ADDRESS},
contract_class_id::ContractClassId,
public_keys::PublicKeys,
traits::Serialize,
traits::Packable,
utils::arrays::array_concat,
};
use dep::contract_class_registerer::ContractClassRegisterer;
use std::meta::derive;

#[derive(Serialize)]
// The following line has been added here because derivation of Packable does not handle imports.
use dep::aztec::protocol_types::public_keys::{IvpkM, NpkM, OvpkM, TpkM};

#[derive(Packable)]
#[event]
struct ContractInstanceDeployed {
DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE: Field,
Expand Down Expand Up @@ -56,8 +59,8 @@ contract ContractInstanceDeployer {
// kind: 'point'
// }

impl Serialize<15> for ContractInstanceDeployed {
fn serialize(self) -> [Field; 15] {
impl ContractInstanceDeployed {
fn serialize_non_standard(self) -> [Field; 15] {
[
self.DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE,
self.address.to_field(),
Expand Down Expand Up @@ -117,10 +120,7 @@ contract ContractInstanceDeployer {
version: 1,
};

// This is a hack to get around there being two implementations of `Serialize`.
// Can be replaced with the below once fixed
// let payload = event.serialize();
let payload = Serialize::<15>::serialize(event);
let payload = event.serialize_non_standard();
dep::aztec::oracle::debug_log::debug_log_format("ContractInstanceDeployed: {}", payload);

let padded_log = array_concat(payload, [0; 3]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ contract Crowdfunding {
storage::storage,
},
prelude::{AztecAddress, PrivateSet, PublicImmutable},
protocol_types::traits::Serialize,
protocol_types::traits::Packable,
unencrypted_logs::unencrypted_event_emission::encode_event,
utils::comparison::Comparator,
};
Expand All @@ -24,7 +24,7 @@ contract Crowdfunding {
use token::Token;
// docs:end:all-deps

#[derive(Serialize)]
#[derive(Packable)]
#[event]
struct WithdrawalProcessed {
who: AztecAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract NFT {
AztecAddress, Map, NoteGetterOptions, NoteViewerOptions, PrivateContext, PrivateSet,
PublicContext, PublicImmutable, PublicMutable,
},
protocol_types::{point::Point, traits::Serialize},
protocol_types::{point::Point, traits::Packable},
utils::comparison::Comparator,
};
use dep::compressed_string::FieldCompressedString;
Expand All @@ -35,7 +35,7 @@ contract NFT {

// TODO(#8467): Rename this to Transfer - calling this NFTTransfer to avoid export conflict with the Transfer event
// in the Token contract.
#[derive(Serialize)]
#[derive(Packable)]
#[event]
struct NFTTransfer {
from: AztecAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract Test {
use dep::aztec::protocol_types::{
constants::{MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, PRIVATE_LOG_SIZE_IN_FIELDS},
point::Point,
traits::Serialize,
traits::{Packable, Serialize},
utils::arrays::array_concat,
};

Expand Down Expand Up @@ -50,7 +50,7 @@ contract Test {
// Imported just to test note type ids
use dep::uint_note::uint_note::UintNote;

#[derive(Serialize)]
#[derive(Packable)]
#[event]
struct ExampleEvent {
value0: Field,
Expand Down Expand Up @@ -310,7 +310,7 @@ contract Test {
.call(&mut context);

// Emit a log with non-encrypted content for testing purpose.
let leaky_log = array_concat(event.serialize(), [0; PRIVATE_LOG_SIZE_IN_FIELDS - 5]);
let leaky_log = array_concat(event.pack(), [0; PRIVATE_LOG_SIZE_IN_FIELDS - 5]);
context.emit_private_log(leaky_log);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ contract TestLog {
use dep::aztec::encrypted_logs::encrypted_event_emission::encode_and_encrypt_event;
use dep::aztec::macros::{events::event, functions::{private, public}, storage::storage};
use dep::aztec::prelude::PrivateSet;
use dep::aztec::protocol_types::{address::AztecAddress, traits::Serialize};
use dep::aztec::protocol_types::{address::AztecAddress, traits::Packable};
use dep::aztec::unencrypted_logs::unencrypted_event_emission::encode_event;
use dep::value_note::value_note::ValueNote;
use std::meta::derive;

#[derive(Serialize)]
#[derive(Packable)]
#[event]
struct ExampleEvent0 {
value0: Field,
value1: Field,
}

#[derive(Serialize)]
#[derive(Packable)]
#[event]
struct ExampleEvent1 {
value2: AztecAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract Token {
prelude::{
AztecAddress, FunctionSelector, Map, PublicContext, PublicImmutable, PublicMutable,
},
protocol_types::{point::Point, traits::Serialize},
protocol_types::{point::Point, traits::{Packable, Serialize}},
};

use dep::uint_note::uint_note::UintNote;
Expand All @@ -60,7 +60,7 @@ contract Token {
// an overall small circuit regardless.
global RECURSIVE_TRANSFER_CALL_MAX_NOTES: u32 = 8;

#[derive(Serialize)]
#[derive(Packable)]
#[event]
struct Transfer {
from: AztecAddress,
Expand Down
Loading