Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Aug 23, 2023
1 parent c0f2820 commit bc5652e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
61 changes: 59 additions & 2 deletions docs/docs/dev_docs/contracts/events.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,67 @@
## Events
Events in Aztec works similarily to Ethereum events in a sense that they are a way for contracts to communicate with the outside world.
They are emitted by contracts and stored inside 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: encrypted and unencrypted.

### Encrypted Events
Encrypted events can only be emitted by private functions and are encrypted using a public key of a recipient.
For this reason it is necessary to register a recipient in the AztecRPC server before encrypting the events for them.
Recipients can be registered using the Aztec CLI or Aztec.js:

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

<Tabs groupId="events">
<TabItem value="cli" label="Aztec CLI">

```bash
aztec-cli register-recipient --address 0x147392a39e593189902458f4303bc6e0a39128c5a1c1612f76527a162d36d529 --public-key 0x26e193aef4f83c70651485b5526c6d01a36d763223ab24efd1f9ff91b394ac0c20ad99d0ef669dc0dde8d5f5996c63105de8e15c2c87d8260b9e6f02f72af622 --partial-address 0x200e9a6c2d2e8352012e51c6637659713d336405c29386c7c4ac56779ab54fa7
```

</TabItem>
<TabItem value="js" label="Aztec.js">

```ts
const aztecAddress = AztecAddress.fromString("0x147392a39e593189902458f4303bc6e0a39128c5a1c1612f76527a162d36d529");
const publicKey = Point.fromString("0x26e193aef4f83c70651485b5526c6d01a36d763223ab24efd1f9ff91b394ac0c20ad99d0ef669dc0dde8d5f5996c63105de8e15c2c87d8260b9e6f02f72af622");
const partialAddress = Fr.fromString("0x200e9a6c2d2e8352012e51c6637659713d336405c29386c7c4ac56779ab54fa7");

const completeAddress = CompleteAddress.create(aztecAddress, publicKey, partialKey);
await aztecRpc.registerRecipient(completeAddress);
```

</TabItem>
</Tabs>

At this point we only allow emitting note spending info through encrypted events.
In the future we will allow emitting arbitrary information.
(If you currently emit arbitrary information, AztecRPC server will fail to process it and it will not be queryable.)


### Constraining events

### Unencrypted Events
Unencrypted events are events which can be read by anyone.
They can be emitted by both public and private functions.
It's important for a developer to consider whether it's acceptable for the unencrypted event to be emitted from private functions as it might pose a significant privacy leak.

### Encrypted Events
Once emitted, unencrypted events are stored in AztecNode and can be queried by anyone:
<Tabs groupId="events">
<TabItem value="cli" label="Aztec CLI">

```bash
aztec-cli get-logs --from 5 --limit 1
```

</TabItem>
<TabItem value="js" label="Aztec.js">

#include_code logs /yarn-project/end-to-end/src/e2e_public_token_contract.test.ts typescript

</TabItem>
</Tabs>

### Costs

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/dev_docs/getting_started/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ View result: [
Finally, we can use the CLI's `get-logs` command to retrieve unencrypted logs emitted by the contract:

```
% aztec-cli get-logs 5 1
% aztec-cli get-logs --from 5 --limit 1
Logs found:
Coins transferred
Expand Down
4 changes: 4 additions & 0 deletions yarn-project/end-to-end/src/e2e_public_token_contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ describe('e2e_public_token_contract', () => {
};

const expectLogsFromLastBlockToBe = async (logMessages: string[]) => {
// docs:start:logs

const l2BlockNum = await aztecRpcServer.getBlockNumber();
const unencryptedLogs = await aztecRpcServer.getUnencryptedLogs(l2BlockNum, 1);

// docs:end:logs
const unrolledLogs = L2BlockL2Logs.unrollLogs(unencryptedLogs);
const asciiLogs = unrolledLogs.map(log => log.toString('ascii'));

Expand Down

0 comments on commit bc5652e

Please sign in to comment.