-
Notifications
You must be signed in to change notification settings - Fork 615
docs: events #1768
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
Merged
Merged
docs: events #1768
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bc5652e
WIP
benesjan b1a0a11
typo
benesjan 790f053
WIP
benesjan b2ac93a
final touches
benesjan 7945e86
Apply suggestions from code review
benesjan b8b9849
docs: calling Aztec RPC Server consistently
benesjan b6325d8
docs: using danger keyword
benesjan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,104 @@ | ||
| ## Events | ||
| Events in Aztec works similarly 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 each instance ofAztecNode. | ||
|
benesjan marked this conversation as resolved.
Outdated
|
||
| 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. | ||
|
benesjan marked this conversation as resolved.
Outdated
|
||
|
|
||
| Unlike on Ethereum, there are 2 types of events supported by Aztec: 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. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This reminded me that we need to broadcast account data, when an account contract is deployed, so that the public key and partial address can be grabbed automatically by the simulator (from the public node). #1778 |
||
| 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> | ||
|
|
||
| > **NOTE**: If a note recipient is one of the accounts inside the Aztec RPC, we don't need to register it as a recipient because we already have the public key available. | ||
|
benesjan marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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.) | ||
|
benesjan marked this conversation as resolved.
Outdated
|
||
|
|
||
| To emit encrypted logs first import the `emit_encrypted_log` utility function inside your contract: | ||
|
|
||
| #include_code encrypted_import /yarn-project/noir-libs/value-note/src/utils.nr rust | ||
|
|
||
| Then you can call the function: | ||
|
|
||
| #include_code encrypted /yarn-project/noir-libs/value-note/src/utils.nr rust | ||
|
|
||
| ### 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. | ||
|
benesjan marked this conversation as resolved.
Outdated
|
||
|
|
||
| ### 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> | ||
|
|
||
| To emit unencrypted logs first import the `emit_unencrypted_log` utility function inside your contract: | ||
|
|
||
| #include_code unencrypted_import /yarn-project/noir-contracts/src/contracts/private_token_contract/src/main.nr rust | ||
|
|
||
| Then you can call the function: | ||
|
|
||
| #include_code unencrypted /yarn-project/noir-contracts/src/contracts/private_token_contract/src/main.nr rust | ||
|
|
||
| ### Costs | ||
|
|
||
| Explain L1 cost to emit an event. | ||
| All the event data is pushed on Ethereum by the sequencer and for this reason the cost of emitting an event is non-trivial. | ||
|
benesjan marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Processing events | ||
| Both the encrypted and unencrypted events are stored in AztecNode. | ||
| Unencrypted logs can be queried by anyone as we described above in the [Unencrypted Events](#unencrypted-events) section. | ||
|
|
||
| Encrypted logs need to first be decrypted: | ||
|
|
||
| ### Decrypting | ||
| One function of AztecRPC server is constantly loading encrypted logs from AztecNode and trying to decrypt them. | ||
| When new encrypted logs are obtained, AztecRPC server will try to decrypt them using the private encryption key of all the accounts registered inside AztecRPC server. | ||
| If the decryption is successful, AztecRPC server will store the decrypted note inside a database. | ||
|
benesjan marked this conversation as resolved.
Outdated
|
||
| If the decryption fails, the specific log will be discarded. | ||
|
|
||
| For the AztecRPC server to successfully process the decrypted note we need to compute note hash and nullifier. | ||
|
benesjan marked this conversation as resolved.
Outdated
|
||
| Because we want to support arbitrary ways of computing these values, Noir developers need to implement a `compute_note_hash_and_nullifier` function inside their contracts. | ||
|
benesjan marked this conversation as resolved.
Outdated
|
||
|
|
||
| This is an example implementation inside the `PrivateTokenContract`: | ||
|
|
||
| ### Stev | ||
| #include_code compute_note_hash_and_nullifier /yarn-project/noir-contracts/src/contracts/private_token_contract/src/main.nr rust | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.