Skip to content
Merged
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
14 changes: 6 additions & 8 deletions noir-projects/aztec-nr/aztec/src/macros/functions/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ pub(crate) comptime fn transform_private(f: FunctionDefinition) -> Quoted {
};

// All private functions perform message discovery, since they may need to access notes. This is slightly
// inefficient and could be improved by only doing it once we actually attempt to read any.
// inefficient and could be improved by only doing it once we actually attempt to read any. Note that the message
// discovery call syncs private events as well. We do not sync those here if there are no notes because we don't
// have an API that would access events from private functions.
let message_discovery_call = if NOTES.len() > 0 {
create_message_discovery_call()
} else {
Expand Down Expand Up @@ -303,13 +305,9 @@ pub(crate) comptime fn transform_utility(f: FunctionDefinition) -> Quoted {
};

// All utility functions perform message discovery, since they may need to access private notes that would be
// found during this process. This is slightly inefficient and could be improved by only doing it once we actually
// attempt to read any.
let message_discovery_call = if NOTES.len() > 0 {
create_message_discovery_call()
} else {
quote {}
};
// found during this process or they may be used to sync private events from TypeScript
// (`sync_private_state` function gets invoked by PXE::getPrivateEvents function).
let message_discovery_call = create_message_discovery_call();

// Inject context creation, storage initialization, and message discovery call at the beginning of the function
// body.
Expand Down
1 change: 1 addition & 0 deletions noir-projects/noir-contracts/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ members = [
"contracts/test/benchmarking_contract",
"contracts/test/child_contract",
"contracts/test/counter_contract",
"contracts/test/event_only_contract",
"contracts/test/import_test_contract",
"contracts/test/invalid_account_contract",
"contracts/test/no_constructor_contract",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "event_only_contract"
authors = [""]
compiler_version = ">=0.25.0"
type = "contract"

[dependencies]
aztec = { path = "../../../../aztec-nr/aztec" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use aztec::macros::aztec;

/// This contract is used to test that the private event synchronization is working correctly even when the contract
/// doesn't work with notes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// doesn't work with notes.
/// doesn't use notes.

"doesn't work with notes" sounds like notes are broken, or that if we added them the contract with break.

#[aztec]
contract EventOnly {
use aztec::{
event::event_interface::EventInterface,
macros::{events::event, functions::private},
messages::logs::event::encode_and_encrypt_event_unconstrained,
protocol_types::traits::Serialize,
};
use std::meta::derive;

#[derive(Serialize)]
#[event]
struct TestEvent {
value: Field,
}

#[private]
fn emit_event_for_msg_sender(value: Field) {
let sender = context.msg_sender();
TestEvent { value }.emit(encode_and_encrypt_event_unconstrained(
&mut context,
sender,
sender,
));
}
}
41 changes: 41 additions & 0 deletions yarn-project/end-to-end/src/e2e_event_only.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { type AccountWalletWithSecretKey, Fr } from '@aztec/aztec.js';
import { EventOnlyContract, type TestEvent } from '@aztec/noir-test-contracts.js/EventOnly';

import { jest } from '@jest/globals';

import { ensureAccountsPubliclyDeployed, setup } from './fixtures/utils.js';

const TIMEOUT = 120_000;

/// Tests that a private event can be obtained for a contract that does not work with notes.
describe('EventOnly', () => {
let eventOnlyContract: EventOnlyContract;
jest.setTimeout(TIMEOUT);

let wallets: AccountWalletWithSecretKey[];
let teardown: () => Promise<void>;

beforeAll(async () => {
({ teardown, wallets } = await setup(2));
await ensureAccountsPubliclyDeployed(wallets[0], wallets.slice(0, 2));
eventOnlyContract = await EventOnlyContract.deploy(wallets[0]).send().deployed();
});

afterAll(() => teardown());

it('emits and retrieves a private event for a contract with no notes', async () => {
const value = Fr.random();
const tx = await eventOnlyContract.methods.emit_event_for_msg_sender(value).send().wait();

const events = await wallets[0].getPrivateEvents<TestEvent>(
eventOnlyContract.address,
EventOnlyContract.events.TestEvent,
tx.blockNumber!,
1,
[wallets[0].getAddress()],
);

expect(events.length).toBe(1);
expect(events[0].value).toBe(value.toBigInt());
});
});