bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 12쪽 중 12쪽
lues
as its input. Interpreters are instantiated with some local state to execute a frame. When an interpreter sees a call to another contract,
return/revert from current frame, or contract creation it triggers special functionality to process it, as a potentially different
interpreter should be run.
System
: Common for all environments and the bootloader. Provides an abstract interface for
low-level handling of IO (storage, events,
L1 messages, oracles) and memory management. The system communicates with the external oracle (non-determinism source), which is needed to read block data
, and also for some IO operations, e.g. to perform the initial read for a storage slot.
This modular design enables us to isolate a minimal interface required to implement an Execution Environment. In addition, the system abstraction
makes the storage model customizable and allows for different instances of the entire system.
Running Environments
As mentioned before, we have two targets for ZKsync OS. However, this is not just a compilation target, but also how some system primitives
are handled.
The two running environments are:
Forward Running Mode:
To be used in the sequencer. In such mode we expect code to be run on the usual platform with OS,
so default memory allocator can be used (as it’s part of the OS). For non-determinism source, we can just pass Oracle's Rust implementation as a
bootloader input. Some code can be skipped in this mode as well (e.g. merkle proof verification for the storage reads).
Proving Running Mode:
To be used during proof generation. The code runs on a pure RISC-V platform without an OS,
so memory management must be handled manually.
Additionally, special care is needed to pass external data into the RISC-V machine due to the absence of standard non-determinism sources.
All behavior must be fully deterministic and provable.
System Resources
In ZKsync OS, the concept of "resources" is required to limit and charge for both computation (primarily proving) and data usage.
This is more complex than it may initially appear: ZKsync OS is designed to be EVM gas-equivalent,
meaning that EVM code execution should follow the same gas schedule as on Ethereum.
However, the EVM gas schedule does not accurately reflect the cost of ZK proof generation.
To address this mismatch, ZKsync OS introduces
double accounting
: tracking both
Execution Environment (EE) gas-equivalent to EVM gas, and a "native" computational resource that models the cost of proving.
L1 Integration
ZKsync OS is designed for use in ZK rollups and validiums, where state transition correctness must be verified on the settlement
layer (referred to here as L1 for simplicity).
Specifically, a state commitment is stored on L1. For each block or batch, a proof is generated to verify that a valid state transition has occurred
from a known L1 state commitment to a new one based on some set of inputs. This means the state pre and post transition must be included in
the public inputs (or preimage) of the ZK proof.
In addition, the public input will include other components required for:
Messaging
Data availability (DA) validation
Input validation
ZKsync OS also includes a messaging mechanism that enables trustless communication between L1 and L2, fully compatible with
EraVM
. This includes L1 to L2 transactions and L2 to L1 messages.
Pubdata compression
Spec for the pubdata compression algorithm used in ZKsync
Double Resource Accounting
Introduction to Double Resource Accounting
## /zksync-protocol/zksyncos/double-accounting
ZKsync Docs
Double Resource Accounting
Introduction to Double Resource Accounting
ZKsync OS implements a double resource accounting model, where both a Execution Environment resource (EVM gas, for instance) and native resources are tracked.
The interface to interact with resources can be found in
resources.rs
.
Execution Environment Resource: Ergs
The EE resource is called Ergs, and corresponds to a model for computation performed by Execution Environments.
The current ZKsync OS design assumes every EE will use essentially the same resource for this, but potentially with a different conversion rate.
For example, a unit of EVM gas is currently equivalent to 256 ergs, while a unit of EraVM gas might use a smaller constant for this conversion.
The gas-related fields of ZKsync OS transactions are interpreted as referring to EVM gas.
In general, any reference to the word "gas" in the codebase or docs refer to EVM gas.
Ergs passed to a new EE frame during a call/deployment can be limited as in EVM, in which case the caller will keep the rest of the ergs available.
Native Resource
The native resource models the offchain cost of processing a transaction. Currently, this is dominated by proving and publishing data.
A good intuition for it is "how many risc-v cycles it takes to prove a given computation".
If a transaction execution runs out of native resources, the entire transaction is reverted.
If the same happens during transaction validation, the transaction is considered invalid.
The native resources are passed fully from frame to frame, a call cannot set a limit on how much of it the callee can spend.
Native Resource Limit
In order to avoid having to use a new custom transaction format, the native resource limit (or native limit) is derived
from the transaction's gas limit and fee. That is, the native limit can be incremented by either increasing the gas limit
or the gas price of the transaction (using priority fee). The idea of using both these fields is to allow users re-use presigned transactions
with a fixed EVM-computed gas limit.
However, if a wallet uses the standard
eth_estimateGas
RPC, the gas estimation will assume no priority fee is added,
and will add enough gas to make sure the transaction has enough native resource to go through.
More precisely, the logic for native resource limit derivation and charging is:
Let:
gasPrice
be the transaction's gas price (base fee + priority fee).
nativePrice
be a constant set by the operator, reflecting the "cost of processing a single cycle".
Note: for L1->L2 transactions we use a code constant instead of one provided by operator.
gasLimit
be the transaction's (EVM) gas limit.
First we define the ratio between EVM gas and native resource as:
nativePerGas := gasPrice/nativePrice
Note: For call simulation we use a constant for it, as gasPrice might be set to 0.
Next we define the limit for the native resource as:
nativeLimit := gasLimit * nativePerGas
Then we process the transaction, charging both Ergs for EE execution and native resource for any kind of computation (EE, bootloader or system work).
If execution doesn't run out of native resources, we first charge for pubdata from native resource.
Then we compute the difference between the implicit gas used derived from native resource consumption and the gas used by EEs from the ergs used.
We call this value
deltaGas
.
deltaGas := (nativeUsed / nativePerGas) - gasUsed
If
deltaGas > 0
, we add it to
gasUsed
and charge it from ergs. This ensures that gas estimation will include additional gas to cover for
native resources using just base fee. We expect the base fee to be enough to cover most transactions without the need of additional gas.
Finally, refund any remaining gas left is refunded as usual.
ZKsync OS Overview
Introduction to ZKsync OS
Bootloader
Learn about Bootloader, the entrypoint of the system
## /zksync-protocol/zksyncos/bootloader
ZKsync Docs
Bootloader
Learn about Bootloader, the entrypoint of the system
The bootloader is the component responsible for implementing the general blockchain protocol. Roughly, this means:
Initializing the system.
Reading the block context from the oracle.
Reading and parsing the first transaction.
Validating the transaction.
Executing the transaction.
Saving the transaction result.
Repeat from 3 until there are no more transactions.
Finalizing the block.
This component is, as the name suggest, the entrypoint of the system.
The function
run_prepared
implements this
top-level main loop.
Configuration
The bootloader can be configured with the following parameters
(found in the
BasicBootloaderExecutionConfig
struct):
ONLY_SIMULATE
: skips the
validation
step
when processing a transaction. Used for call simulation in the node.
IS_PROVING_ENVIRONMENT
: to skip some checks during sequencing.
SPECIAL_ADDRESS_SPACE_BOUND
: the range of address (starting from 0) where system contracts can be deployed.
AA_ENABLED
: whether native account abstraction is enabled.
In addition, the
basic_bootloader
crate has the following compilation flags:
code_in_kernel_space
: to enable normal contract execution for addresses in the range
[0, SPECIAL_ADDRESS_SPACE_BOUND]
.
transfers_to_kernel_space
: to enable token transfers to addresses in the range
[0, SPECIAL_ADDRESS_SPACE_BOUND]
. Note: the bootloader itself
has a special formal address (0x8001) that is always allowed to receive token transfers. This is used to collect fees.
charge_priority_fee
: to enable charging for the EIP-1559 tip (priority fee) on top of the base fee.
evm-compatibility
: enables all the previous flags, needed for the EVM test suite.
Code Execution
For transaction execution, the bootloader has to execute some contract code. This contract code corresponds to one of the
supported VMs, and is executed through the
Execution Environment (EE)
module.
A contract call is executed through an interplay between the bootloader and (potentially different) execution environments. Indeed, a contract
executing in a given EE can call to contracts that run on a different EE or to
a
System Hook
. This interplay is described
under
Runner Flow
.
Block Header
At the end of the execution the bootloader outputs the block header.
For the block header, the Ethereum format will be used. However, some of the fields will be set differently in the first version for simplification.
The block header should determine the block fully, i.e. include all the inputs needed to execute the block.
Ethereum
Field Name
Ethereum value
ZKsync OS value
Comments
parent_hash
previous block hash
previous block hash
owners_hash
0x1dcc4de8dec75d7aab85b567
b6ccd41ad312451b948a7413f
0a142fd40d49347 (post merge)
0x1dcc4de8dec75d7aab
85b567b6ccd41ad312451b
948a7413f0a142fd40d49347
hash of empty RLP list
beneficiary
block proposer
Operator (fee) address
state_root
state commitment
0
transactions_root
transactions trie (Patricia Merkle tree) root
transactions rolling hash
receipts_root
receipts trie (Patricia Merkle tree) root
0
logs_bloom
2048-bit bloom filter over logs’ addresses and topics
0
difficulty
0 (post merge)
0
number
block number
block number
gas_limit
block gas limit
constant, not defined yet, 10–15 M most likely
gas_used
block gas used
block gas used
TBD — with or without pubdata
timestamp
block timestamp
block timestamp
extra_data
any extra data included by proposer
TBD, possibly gas_per_pubdata
mix_hash
beacon-chain-provided random, (post merge)
0
after consensus will be provided random
nonce
0 (post merge)
0
base_fee_per_gas
base_fee_per_gas
base_fee_per_gas
Double Resource Accounting
Introduction to Double Resource Accounting
Execution Environments
Learn about the ZKsync OS Execution Environments (EE)프로젝트로 돌아가기