Emitting Events
Events in Aztec work similarly to Ethereum events in the sense that they are a way for contracts to communicate with the outside world. They are emitted by contracts and stored inside each instance of an AztecNode.
Aztec events are currently represented as raw data and are not ABI encoded. ABI encoded events are a feature that will be added in the future.
Unlike on Ethereum, there are 2 types of events supported by Aztec: encrypted and unencrypted.
Encrypted Events
Call emit
To emit encrypted logs you can import the encode_and_encrypt
or encode_and_encrypt_with_keys
functions and pass them into the emit
function after inserting a note. An example can be seen in the reference token contract's transfer function:
// TODO: constrain encryption below - we are using unconstrained here only becuase of the following Noir issue
// https://github.com/noir-lang/noir/issues/5771
storage.balances.at(from).sub(from, amount).emit(encode_and_encrypt_note_unconstrained(
&mut context,
from_ovpk_m,
from,
from,
));
Source code: noir-projects/noir-contracts/contracts/token_contract/src/main.nr#L433-L442
Furthermore, if not emitting the note, one should explicitly discard
the value returned from the note creation.
Successfully process the encrypted event
One of the functions of the PXE is constantly loading encrypted logs from the AztecNode
and decrypting them.
When new encrypted logs are obtained, the PXE will try to decrypt them using the private encryption key of all the accounts registered inside PXE.
If the decryption is successful, the PXE will store the decrypted note inside a database.
If the decryption fails, the specific log will be discarded.
For the PXE to successfully process the decrypted note we need to compute the note's 'note hash' and 'nullifier'.
Aztec.nr enables smart contract developers to design custom notes, meaning developers can also customize how a note's note hash and nullifier should be computed. Because of this customizability, and because there will be a potentially-unlimited number of smart contracts deployed to Aztec, an PXE needs to be 'taught' how to compute the custom note hashes and nullifiers for a particular contract. This is done by a function called compute_note_hash_and_optionally_a_nullifier
, which is automatically injected into every contract when compiled.
Unencrypted Events
Unencrypted events are events which can be read by anyone. They can be emitted only by public functions.
Call emit_unencrypted_log
To emit unencrypted logs you don't need to import any library. You call the context method emit_unencrypted_log
:
context.emit_unencrypted_log(/*message=*/ value);
context.emit_unencrypted_log(/*message=*/ [10, 20, 30]);
context.emit_unencrypted_log(/*message=*/ "Hello, world!");
Source code: noir-projects/noir-contracts/contracts/test_contract/src/main.nr#L367-L371
Querying the unencrypted event
Once emitted, unencrypted events are stored in AztecNode and can be queried by anyone:
// Get the unencrypted logs from the last block
const fromBlock = await pxe.getBlockNumber();
const logFilter = {
fromBlock,
toBlock: fromBlock + 1,
};
const unencryptedLogs = (await pxe.getUnencryptedLogs(logFilter)).logs;
Source code: yarn-project/end-to-end/src/fixtures/utils.ts#L612-L620
Costs
All event data is pushed to Ethereum as calldata by the sequencer and for this reason the cost of emitting an event is non-trivial.
In the Sandbox, an encrypted note has a fixed overhead of 4 field elements (to broadcast an ephemeral public key, a contract address, and a storage slot); plus a variable number of field elements depending on the type of note being emitted.
A ValueNote
, for example, currently uses 3 fields elements (plus the fixed overhead of 4). That's roughly 7 * 32 = 224
bytes of information.
// ValueNote is used as fn parameter in the Claim contract, so it has to implement the Serialize trait.
// It is important that the order of these annotations is preserved so that derive(Serialize) runs AFTER the note macro, which injects the note header.
#[note]
#[derive(Serialize)]
pub struct ValueNote {
value: Field,
owner: AztecAddress,
randomness: Field,
}
Source code: noir-projects/aztec-nr/value-note/src/value_note.nr#L18-L28
- There are plans to compress encrypted note data further.
- There are plans to adopt EIP-4844 blobs to reduce the cost of data submission further.