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

zksync-era integration docs #211

Merged
merged 14 commits into from
Sep 3, 2024
Merged

zksync-era integration docs #211

merged 14 commits into from
Sep 3, 2024

Conversation

MarcosNicolau
Copy link
Contributor

@MarcosNicolau MarcosNicolau commented Aug 21, 2024

Adds documentation about the integration with zksync-era.

@MarcosNicolau MarcosNicolau changed the title Add basic doc layout zksync-era integration docs Aug 21, 2024
@MarcosNicolau MarcosNicolau marked this pull request as ready for review August 22, 2024 16:24

- **Operator/Sequencer**: the server that initializes the vm, injects the Bootloader bytecode, receives transactions and pushes them into the vm(bootloader memory to be more specific) and start batches and seals them.
- **Bootloader**: a system contract that receives an array of transactions which are processed, validated, executed, and then, the final state is published in the l1.
- **era_vm**: the virtual machine where the Bootloader(and so all the transactions bytecode) gets executed.
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
- **era_vm**: the virtual machine where the Bootloader(and so all the transactions bytecode) gets executed.
- **era_vm**: the virtual machine where the Bootloader (and so all the transactions bytecode) gets executed.


The initial validation of the batch is necessary, since, as we'll see below, the Bootloader starts with its memory pre-filled with any data the operator wants. That is why it needs to validate its correctness.

For more details, you can see the [main loop](https://github.com/matter-labs/era-contracts/blob/main/system-contracts/Bootloader/bootloader.yul#L3962-L3965) or the [full contract code](https://github.com/matter-labs/era-contracts/blob/main/system-contracts/bootloader/bootloader.yu).
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
For more details, you can see the [main loop](https://github.com/matter-labs/era-contracts/blob/main/system-contracts/Bootloader/bootloader.yul#L3962-L3965) or the [full contract code](https://github.com/matter-labs/era-contracts/blob/main/system-contracts/bootloader/bootloader.yu).
For more details, you can see the [main loop](https://github.com/matter-labs/era-contracts/blob/main/system-contracts/bootloader/bootloader.yul#L3962-L3965) or the [full contract code](https://github.com/matter-labs/era-contracts/blob/main/system-contracts/bootloader/bootloader.yul).


2. There isn't any consensus or spec about how storage should be implemented. We came up with this API because it is what we thought was more convenient for the requirement. But, for example, the vm1 implements a query logic, where the operator will react based on the provided params.

[Here](https://github.com/lambdaclass/zksync-era/blob/era_vm_integration_v2/core/lib/multivm/src/versions/era_vm/vm.rs#L726) you can take a look a the implementation of this trait in detail.
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
[Here](https://github.com/lambdaclass/zksync-era/blob/era_vm_integration_v2/core/lib/multivm/src/versions/era_vm/vm.rs#L726) you can take a look a the implementation of this trait in detail.
[Here](https://github.com/lambdaclass/zksync-era/blob/era_vm_integration_v2/core/lib/multivm/src/versions/era_vm/vm.rs#L726) you can take a look at the implementation of this trait in detail.

Comment on lines +117 to +119
### Rollbacks and snapshots

In the `era_vm`, when a transaction encounters a panic or reverts, the vm needs to roll back the changes, restoring only a part of the [state](https://github.com/lambdaclass/era_vm/blob/main/src/state.rs#L43-L60) to its previous frame. Remember that frames are created under `near_call` and `far_call` opcodes. Currently, rollbacks are perform using snapshots which are just copies of the current state. If a rollback is necessary, the state is restored from these snapshots.
Copy link
Contributor

Choose a reason for hiding this comment

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

This section may be better suited here.

Copy link
Contributor Author

@MarcosNicolau MarcosNicolau Aug 26, 2024

Choose a reason for hiding this comment

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

The idea was to recall the idea of rollbacks in era_vm to emphasize the difference with bootloader rollbacks.

Copy link
Contributor

@juan518munoz juan518munoz left a comment

Choose a reason for hiding this comment

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

Looks good! Re-request review after the typos are ironed out for approval 🚀


## Introduction

So far we have been talking only about the `era_vm`. But you should know that the vm is only a small part of the zk stack. The zk stack is composed of many critical components. In this section, we are only going to be interested in one particular component: the **nodes**, which can further be decomposed into the following units:
Copy link
Member

Choose a reason for hiding this comment

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

we are only going to be interested in one particular component: the nodes, which can further be decomposed into the following units:

The word node is a bit overloaded, I would rephrase this as saying the component we are interested in is the Operator/Sequencer, explain what it is and talk about both the bootloader and the VM that it uses


The Bootloader is a special system contract whose hash resides on L1, but its code isn't stored on either L1 or L2. Instead, it’s compiled from `.yul` to `era_vm` assembly using `zksolc` when the operator first initializes the VM (more on that below).

The Bootloader, unlike Ethereum, takes an array of transactions(a batch) and executes all of them in one run (unless specified not to, that is, if the execution mode of the vm is set to OneTx). This approach allows the transaction batch to be posted on the l1 as just a single one, making the processing on Ethereum cheaper, since taxes and gas can be distributed among all the transactions within the posted batch.
Copy link
Member

@jrchatruc jrchatruc Aug 26, 2024

Choose a reason for hiding this comment

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

The Bootloader, unlike Ethereum

I would remove the unlike Ethereum part here since it's a little confusing, Ethereum also executes batches (blocks) of transactions even though it does not have the concept of a bootloader.

since taxes and gas can be distributed among all the transactions within the posted batch.

since gas can be distributed among all the transactions within the posted batch and and data publishing costs can be reduced by posting only state diffs

At the most basic level, the Bootloader performs the following steps:

1. Reads the initial batch information and makes a call to the SystemContext contract to validate the batch.
2. Loops through all transactions and executes them until the `execute` flag is set to $0$, at that point, it jumps to step 3. <!-- Here we could also add that the transaction gets processed based on where it came from (l1 or l2) -->
Copy link
Member

Choose a reason for hiding this comment

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

Let's add a small comment about this, doesn't need to explain how l1/l2 transactions differ from each other but at least mention them


## Operator/sequencer

Currently, the operator is a centralized server (there are plans to make a decentralized consensus for operators) which can be thought of as the node entry point, its responsibilities are:
Copy link
Member

Choose a reason for hiding this comment

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

which can be thought of as the node entry point

Again, I would remove this since the word node is too overloaded (right now the word node and operator` are usually interchangeable actually)


This storage is saved in the VM state as a pointer. Here’s a brief explanation of each function:

- **decommit**: given a hash it returns a contract bytecode from the database.
Copy link
Member

@jrchatruc jrchatruc Aug 26, 2024

Choose a reason for hiding this comment

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

given a hash it returns a contract bytecode from the database.

given a contract hash it returns its corresponding bytecode (if it exists) from the database

}
```

This allows us to query a key that belongs to the current executing address.
Copy link
Member

Choose a reason for hiding this comment

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

This allows us to query a key that belongs to the current executing address.

This allows us to query a storage key belonging to any desired contract through its address

- **refunds**: A list of refund amounts that have been calculated during execution.
- **read_storage_slots**: A set of storage keys that have been read during execution, used to calculate gas fees and refunds.
- **written_storage_slots**: A set of storage keys that have been written to during execution, used to calculate gas fees and refunds.
- **decommitted_hashes**: Stores the hashes that have been the decommited through the whole execution. When decommiting a hash in a `far_call` or `decommit`, we check if the has been already decommited, if true then the decommit is free of charge.
Copy link
Member

Choose a reason for hiding this comment

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

Stores the hashes that have been the decommited through the whole execution.

Stores the hashes that have been decommited through the whole execution.

we check if the has been already decommited,

we check if it has already been decommited,


This allows us to query a key that belongs to the current executing address.

2. There isn't any consensus or spec about how storage should be implemented. We came up with this API because it is what we thought was more convenient for the requirement. But, for example, the vm1 implements a query logic, where the operator will react based on the provided params.
Copy link
Member

Choose a reason for hiding this comment

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

But, for example, the vm1 implements a query logic, where the operator will react based on the provided params.

Can we give a concrete example of what this looks like?

Copy link

github-actions bot commented Aug 27, 2024

Ergs comparison results:

╔═╡ Size (-%) ╞════════════════╡ All M3B3 ╞═╗
║ Mean                                0.000 ║
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                                 NaN ║
╠═╡ Cycles (-%) ╞══════════════╡ All M3B3 ╞═╣
║ Mean                              100.000 ║
║ Best                              100.000 ║
║ Worst                               0.000 ║
║ Total                             100.000 ║
╠═╡ Ergs (-%) ╞════════════════╡ All M3B3 ╞═╣
║ Mean                              100.000 ║
║ Best                              100.000 ║
║ Worst                               0.000 ║
║ Total                             100.000 ║
╚═══════════════════════════════════════════╝

╔═╡ Size (-%) ╞════════════════╡ All MzB3 ╞═╗
║ Mean                                0.000 ║
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                                 NaN ║
╠═╡ Cycles (-%) ╞══════════════╡ All MzB3 ╞═╣
║ Mean                                0.000 ║
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                                 NaN ║
╠═╡ Ergs (-%) ╞════════════════╡ All MzB3 ╞═╣
║ Mean                                0.000 ║
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                                 NaN ║
╚═══════════════════════════════════════════╝

╔═╡ Size (-%) ╞═════╡ EVMInterpreter M3B3 ╞═╗
║ Mean                                0.000 ║
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                                 NaN ║
╠═╡ Cycles (-%) ╞═══╡ EVMInterpreter M3B3 ╞═╣
║ Mean                              100.000 ║
║ Best                              100.000 ║
║ Worst                               0.000 ║
║ Total                             100.000 ║
╠═╡ Ergs (-%) ╞═════╡ EVMInterpreter M3B3 ╞═╣
║ Mean                              100.000 ║
║ Best                              100.000 ║
║ Worst                               0.000 ║
║ Total                             100.000 ║
╠═╡ Ergs/gas ╞══════╡ EVMInterpreter M3B3 ╞═╣
║ ADD                                 0.000 ║
║ MUL                                 0.000 ║
║ SUB                                 0.000 ║
║ DIV                                 0.000 ║
║ SDIV                                0.000 ║
║ MOD                                 0.000 ║
║ SMOD                                0.000 ║
║ ADDMOD                              0.000 ║
║ MULMOD                              0.000 ║
║ EXP                                 0.000 ║
║ SIGNEXTEND                          0.000 ║
║ LT                                  0.000 ║
║ GT                                  0.000 ║
║ SLT                                 0.000 ║
║ SGT                                 0.000 ║
║ EQ                                  0.000 ║
║ ISZERO                              0.000 ║
║ AND                                 0.000 ║
║ OR                                  0.000 ║
║ XOR                                 0.000 ║
║ NOT                                 0.000 ║
║ BYTE                                0.000 ║
║ SHL                                 0.000 ║
║ SHR                                 0.000 ║
║ SAR                                 0.000 ║
║ SGT                                 0.000 ║
║ SHA3                                0.000 ║
║ ADDRESS                             0.000 ║
║ BALANCE                             0.000 ║
║ ORIGIN                              0.000 ║
║ CALLER                              0.000 ║
║ CALLVALUE                           0.000 ║
║ CALLDATALOAD                        0.000 ║
║ CALLDATASIZE                        0.000 ║
║ CALLDATACOPY                        0.000 ║
║ CODESIZE                            0.000 ║
║ CODECOPY                            0.000 ║
║ GASPRICE                            0.000 ║
║ EXTCODESIZE                         0.000 ║
║ EXTCODECOPY                         0.000 ║
║ RETURNDATASIZE                      0.000 ║
║ RETURNDATACOPY                      0.000 ║
║ EXTCODEHASH                         0.000 ║
║ BLOCKHASH                           0.000 ║
║ COINBASE                            0.000 ║
║ TIMESTAMP                           0.000 ║
║ NUMBER                              0.000 ║
║ PREVRANDAO                          0.000 ║
║ GASLIMIT                            0.000 ║
║ CHAINID                             0.000 ║
║ SELFBALANCE                         0.000 ║
║ BASEFEE                             0.000 ║
║ POP                                 0.000 ║
║ MLOAD                               0.000 ║
║ MSTORE                              0.000 ║
║ MSTORE8                             0.000 ║
║ SLOAD                               0.000 ║
║ SSTORE                              0.000 ║
║ JUMP                                0.000 ║
║ JUMPI                               0.000 ║
║ PC                                  0.000 ║
║ MSIZE                               0.000 ║
║ GAS                                 0.000 ║
║ JUMPDEST                            0.000 ║
║ PUSH0                               0.000 ║
║ PUSH1                               0.000 ║
║ PUSH2                               0.000 ║
║ PUSH4                               0.000 ║
║ PUSH5                               0.000 ║
║ PUSH6                               0.000 ║
║ PUSH7                               0.000 ║
║ PUSH8                               0.000 ║
║ PUSH9                               0.000 ║
║ PUSH10                              0.000 ║
║ PUSH11                              0.000 ║
║ PUSH12                              0.000 ║
║ PUSH13                              0.000 ║
║ PUSH14                              0.000 ║
║ PUSH15                              0.000 ║
║ PUSH16                              0.000 ║
║ PUSH17                              0.000 ║
║ PUSH18                              0.000 ║
║ PUSH19                              0.000 ║
║ PUSH20                              0.000 ║
║ PUSH21                              0.000 ║
║ PUSH22                              0.000 ║
║ PUSH23                              0.000 ║
║ PUSH24                              0.000 ║
║ PUSH25                              0.000 ║
║ PUSH26                              0.000 ║
║ PUSH27                              0.000 ║
║ PUSH28                              0.000 ║
║ PUSH29                              0.000 ║
║ PUSH30                              0.000 ║
║ PUSH31                              0.000 ║
║ PUSH32                              0.000 ║
║ DUP1                                0.000 ║
║ DUP2                                0.000 ║
║ DUP3                                0.000 ║
║ DUP4                                0.000 ║
║ DUP5                                0.000 ║
║ DUP6                                0.000 ║
║ DUP7                                0.000 ║
║ DUP8                                0.000 ║
║ DUP9                                0.000 ║
║ DUP10                               0.000 ║
║ DUP11                               0.000 ║
║ DUP12                               0.000 ║
║ DUP13                               0.000 ║
║ DUP14                               0.000 ║
║ DUP15                               0.000 ║
║ DUP16                               0.000 ║
║ SWAP1                               0.000 ║
║ SWAP2                               0.000 ║
║ SWAP3                               0.000 ║
║ SWAP4                               0.000 ║
║ SWAP5                               0.000 ║
║ SWAP6                               0.000 ║
║ SWAP7                               0.000 ║
║ SWAP8                               0.000 ║
║ SWAP9                               0.000 ║
║ SWAP10                              0.000 ║
║ SWAP11                              0.000 ║
║ SWAP12                              0.000 ║
║ SWAP13                              0.000 ║
║ SWAP14                              0.000 ║
║ SWAP15                              0.000 ║
║ SWAP16                              0.000 ║
║ CALL                                0.000 ║
║ STATICCALL                          0.000 ║
║ DELEGATECALL                        0.000 ║
║ CREATE                              0.000 ║
║ CREATE2                             0.000 ║
║ RETURN                              0.000 ║
║ REVERT                              0.000 ║
╠═╡ Ergs/gas (-%) ╞═╡ EVMInterpreter M3B3 ╞═╣
║ ADD                               100.000 ║
║ MUL                               100.000 ║
║ SUB                               100.000 ║
║ DIV                               100.000 ║
║ SDIV                              100.000 ║
║ MOD                               100.000 ║
║ SMOD                              100.000 ║
║ ADDMOD                            100.000 ║
║ MULMOD                            100.000 ║
║ EXP                               100.000 ║
║ SIGNEXTEND                        100.000 ║
║ LT                                100.000 ║
║ GT                                100.000 ║
║ SLT                               100.000 ║
║ SGT                               100.000 ║
║ EQ                                100.000 ║
║ ISZERO                            100.000 ║
║ AND                               100.000 ║
║ OR                                100.000 ║
║ XOR                               100.000 ║
║ NOT                               100.000 ║
║ BYTE                              100.000 ║
║ SHL                               100.000 ║
║ SHR                               100.000 ║
║ SAR                               100.000 ║
║ SGT                               100.000 ║
║ SHA3                              100.000 ║
║ ADDRESS                           100.000 ║
║ BALANCE                           100.000 ║
║ ORIGIN                            100.000 ║
║ CALLER                            100.000 ║
║ CALLVALUE                         100.000 ║
║ CALLDATALOAD                      100.000 ║
║ CALLDATASIZE                      100.000 ║
║ CALLDATACOPY                      100.000 ║
║ CODESIZE                          100.000 ║
║ CODECOPY                          100.000 ║
║ GASPRICE                          100.000 ║
║ EXTCODESIZE                       100.000 ║
║ EXTCODECOPY                       100.000 ║
║ RETURNDATASIZE                    100.000 ║
║ RETURNDATACOPY                    100.000 ║
║ EXTCODEHASH                       100.000 ║
║ BLOCKHASH                         100.000 ║
║ COINBASE                          100.000 ║
║ TIMESTAMP                         100.000 ║
║ NUMBER                            100.000 ║
║ PREVRANDAO                        100.000 ║
║ GASLIMIT                          100.000 ║
║ CHAINID                           100.000 ║
║ SELFBALANCE                       100.000 ║
║ BASEFEE                           100.000 ║
║ POP                               100.000 ║
║ MLOAD                             100.000 ║
║ MSTORE                            100.000 ║
║ MSTORE8                           100.000 ║
║ SLOAD                             100.000 ║
║ SSTORE                            100.000 ║
║ JUMP                              100.000 ║
║ JUMPI                             100.000 ║
║ PC                                100.000 ║
║ MSIZE                             100.000 ║
║ GAS                               100.000 ║
║ JUMPDEST                          100.000 ║
║ PUSH0                             100.000 ║
║ PUSH1                             100.000 ║
║ PUSH2                             100.000 ║
║ PUSH4                             100.000 ║
║ PUSH5                             100.000 ║
║ PUSH6                             100.000 ║
║ PUSH7                             100.000 ║
║ PUSH8                             100.000 ║
║ PUSH9                             100.000 ║
║ PUSH10                            100.000 ║
║ PUSH11                            100.000 ║
║ PUSH12                            100.000 ║
║ PUSH13                            100.000 ║
║ PUSH14                            100.000 ║
║ PUSH15                            100.000 ║
║ PUSH16                            100.000 ║
║ PUSH17                            100.000 ║
║ PUSH18                            100.000 ║
║ PUSH19                            100.000 ║
║ PUSH20                            100.000 ║
║ PUSH21                            100.000 ║
║ PUSH22                            100.000 ║
║ PUSH23                            100.000 ║
║ PUSH24                            100.000 ║
║ PUSH25                            100.000 ║
║ PUSH26                            100.000 ║
║ PUSH27                            100.000 ║
║ PUSH28                            100.000 ║
║ PUSH29                            100.000 ║
║ PUSH30                            100.000 ║
║ PUSH31                            100.000 ║
║ PUSH32                            100.000 ║
║ DUP1                              100.000 ║
║ DUP2                              100.000 ║
║ DUP3                              100.000 ║
║ DUP4                              100.000 ║
║ DUP5                              100.000 ║
║ DUP6                              100.000 ║
║ DUP7                              100.000 ║
║ DUP8                              100.000 ║
║ DUP9                              100.000 ║
║ DUP10                             100.000 ║
║ DUP11                             100.000 ║
║ DUP12                             100.000 ║
║ DUP13                             100.000 ║
║ DUP14                             100.000 ║
║ DUP15                             100.000 ║
║ DUP16                             100.000 ║
║ SWAP1                             100.000 ║
║ SWAP2                             100.000 ║
║ SWAP3                             100.000 ║
║ SWAP4                             100.000 ║
║ SWAP5                             100.000 ║
║ SWAP6                             100.000 ║
║ SWAP7                             100.000 ║
║ SWAP8                             100.000 ║
║ SWAP9                             100.000 ║
║ SWAP10                            100.000 ║
║ SWAP11                            100.000 ║
║ SWAP12                            100.000 ║
║ SWAP13                            100.000 ║
║ SWAP14                            100.000 ║
║ SWAP15                            100.000 ║
║ SWAP16                            100.000 ║
║ CALL                              100.000 ║
║ STATICCALL                        100.000 ║
║ DELEGATECALL                      100.000 ║
║ CREATE                            100.000 ║
║ CREATE2                           100.000 ║
║ RETURN                            100.000 ║
║ REVERT                            100.000 ║
╚═══════════════════════════════════════════╝

@jrchatruc jrchatruc merged commit 690218a into main Sep 3, 2024
21 checks passed
@jrchatruc jrchatruc deleted the docs-zk-integration branch September 3, 2024 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants