bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 12쪽 중 11쪽
sync-airbender
The ZKsync Chain
The ZKsync Chain is the continuation of Ethereum's rollup centric roadmap.
Ethereum provides security via DA and verification of proofs, but the question of execution is left to the rollups.
In order to have the best UX, it is necessary to solve interoperability and the free flow of assets between chains.
We do this via the Shared Bridge Contract on L1 which stores Ether and ERC20 tokens for all ZKsync Chains (implementing custom bridges is
still possible).
With the Shared Bridge, the chains are able to trust each other because they are managed by the same Chain Type Manager (CTM) contract, using the
same VM and proof system.
For more details, check out the
ZKsync Chains
page.
Getting started with ZKsync protocol
Dive deep into ZKsync Protocol, covering everything from rollups to system contracts and fee structures.
Bridging assets
## /zksync-protocol/rollup/bridging-assets
ZKsync Docs
Bridging assets
Users can deposit and withdraw assets from a ZKsync Chain using bridges, for example any of the
multiple available for ZKsync Era
.
Under the hood, bridging is implemented by having two contracts
(one deployed to L1, and the second deployed to L2)
communicating with each other using L1 <-> L2 interoperability.
Developers are free to build their own
custom bridge for any token
however, we provide default bridges
(one for ETH and one for ERC20 tokens), which can be used for basic bridging.
Addresses of tokens on L2 will always differ from the same token L1 address. Also
note, that tokens bridged via the default bridge only support standard ERC20 functionality, i.e. rebase tokens and other custom behavior are not supported.
Default bridges
You can get the default bridge addresses using the
zks_getBridgeContracts
endpoint or
getDefaultBridgeAddresses
method of
Provider
. Similar methods are available in the other SDKs.
Deposits (to L2)
Users must call the
deposit
method on the L1 bridge contract, which triggers the following actions:
The user's L1 tokens will be sent to the L1 bridge and become locked there.
The L1 bridge initiates a transaction to the L2 bridge using L1 -> L2 communication.
Within the L2 transaction, tokens will be minted and sent to the specified address on L2.
If the token does not exist on ZKsync yet, a new contract is deployed for it.
Given the L2 token address is deterministic (based on the original L1 address,
name and symbol), it doesn't matter who is the first person bridging it, the new L2 address will be the same.
For every executed L1 -> L2 transaction, there will be an L2 -> L1 log message confirming its execution.
Lastly, the
finalizeDeposit
method is called and it finalizes the deposit and mints funds on L2.
You can find example scripts to deposit ETH and ERC20 tokens using the default bridges in the how-to section of the docs.
Withdrawals (to L1)
To provide additional security,
withdrawals in ZKsync Chains are delayed for 3 hours
.
See Governance proposal
.
For more information, read the
withdrawal delay guide
.
Users must call the
withdraw
method on the L2 bridge contract, which will trigger the following actions:
L2 tokens will be burned.
An L2 -> L1 message with the information about the withdrawal will be sent.
After that, the withdrawal action will be available to be finalized by anyone in
the L1 bridge (by proving the inclusion of the L2 -> L1 message, which is done when calling the
finalizeWithdrawal
method on the L1 bridge contract).
After the method is called, the funds are unlocked from the L1 bridge and sent to the withdrawal recipient.
On the testnet environment, we automatically finalize all withdrawals, i.e., for
every withdrawal, we will take care of it by making an L1 transaction that proves the inclusion for each message.
Custom bridges on L1 and L2
To build a custom bridge, create a regular Solidity contract which extends one of
the interfaces mentioned below for the layer. The interfaces provide access to the ZKsync SDK deposit and withdraw implementations.
L1:
IL1ERC20Bridge.sol
For more information, check out our example
L1 custom bridge implementation
.
L2:
IL2SharedBridge.sol
Adding Tokens to the Bridge UI
No action is required to add tokens to the bridge UI. All tokens are automatically
recognized based on user balances. If you desire for your token to display an icon or price, refer to the Token Listing Guide.
ZKsync protocol overview
Learn about ZK Rollups
Finality
Explore the concept of finality in blockchain systems and learn about the steps involved in achieving transaction settlement.
## /zksync-protocol/rollup/finality
ZKsync Docs
Finality
Explore the concept of finality in blockchain systems and learn about the steps involved in achieving transaction settlement.
Finality in blockchain systems refers to the point at which a transaction is considered irreversible and permanently part of the blockchain.
This concept is crucial for ensuring transaction security and reliability.
Finality on Ethereum
On Ethereum, finality is typically achieved after 2
epochs
,
which translates to approximately 13 minutes under normal network conditions.
This duration allows for sufficient block confirmations to prevent reversals and ensure that transactions are settled securely.
Finality on ZKsync Chains
ZKsync Chains, as a Layer 2s, tie their finality and security mechanisms to those of the underlying Layer 1, Ethereum.
The steps involved in reaching finality in ZKsync Chains include:
Batch Formation
: Transactions are collected and grouped into a batch. This step generally takes a few minutes.
Batch Commitment
: The complete batch is committed to the Ethereum blockchain.
Proof Generation
: A cryptographic proof that validates the entire batch is generated. This process typically takes about an hour.
Proof Submission
: The generated proof is submitted to an Ethereum smart contract for verification.
Batch Finalization
: The batch undergoes a final verification and is settled on Ethereum.
This step includes
a delay of approximately 3 hours as a security measure
.
Overall, the complete finality time for a transaction on ZKsync Chains is around 3 hours, aligning with the finality of the corresponding Ethereum block.
Instant confirmations
While the full finality process on ZKsync Chains can take a few hours, transactions are treated with instant confirmation for user convenience:
Immediate Transaction Display
: Once submitted, transactions are quickly shown in the user interface and API as unconfirmed.
Immediate Asset Usability
: Users can immediately utilize the transferred assets for further transactions,
which may even be included in the same ZKsync Chain batch.
This feature enables a seamless user experience,
although more cautious users may opt to wait for the transaction to reach full finality
or pass any intermediate steps before fully trusting the transaction's completion.
Bridging assets
Data availability
An in-depth look at how ZKsync ensures data availability through state diffs and compresses data to optimize L1 submissions, plus tools for reconstructing L2 state from L1 public data.
## /zksync-protocol/rollup/data-availability
ZKsync Docs
Data availability
An in-depth look at how ZKsync ensures data availability through state diffs and compresses data to optimize L1 submissions, plus tools for reconstructing L2 state from L1 public data.
Data availability is a cornerstone of ZKsync's architecture,
ensuring that the entire Layer 2 (L2) state can be
reconstructed
from the data submitted to Ethereum's Layer 1 (L1).
This process not only secures the network but also optimizes cost-efficiency through innovative data management techniques.
State diffs: Optimizing storage slots
Instead of submitting detailed transaction data, ZKsync Chains focuses on posting
state diffs
to L1.
These diffs represent changes in the blockchain's state, enabling ZKsync to efficiently manage how data is stored and referenced:
Efficient Use of Storage Slots
: Changes to the same storage slots across multiple transactions can be grouped,
reducing the amount of data that needs to be sent to L1 and thereby lowering gas costs.
Compression Techniques
: All data sent to L1, including state diffs, is compressed to further reduce costs.
Read more about ZKsync's compression methods
.
Additional data posted to L1
In addition to state diffs, ZKsync Chains also posts other crucial information to ensure comprehensive data availability:
L2 to L1 Logs and Messages
: These ensure that communications and events are recorded and accessible.
Published Bytecodes
: The bytecodes of deployed smart contracts are made available, crucial for contract interaction and verification.
Compressed State Diffs
: Further optimizes data management by reducing the size of state changes posted to L1.
Validiums: Balancing Security and Cost
When a chain opts not to post its data on-chain, it operates under a model known as a
validium
.
This approach significantly reduces costs by keeping data off-chain but introduces risks related to data accessibility and security.
Recreating L2 State From L1 Pubdata
ZKsync provides tools to validate and reconstruct the L2 state from data available on L1. Here's how this process is typically managed:
Basic Flow
First, we need to filter all of the transactions to the L1 ZKsync contract for only the
commitBlocks
transactions
where the proposed block has been referenced by a corresponding
executeBlocks
call
(the reason for this is that a committed or even proven block can be reverted but an executed one cannot).
Once we have all the committed blocks that have been executed, we then will pull the transaction input and the relevant fields.
The kinds of pubdata we’ll pull from transaction data:
L2 to L1 Logs
L2 to L1 Messages
Published Bytecodes
Compressed State Diffs
Key components for state reconstruction
State Diffs
State diffs are essential for understanding changes within the blockchain's state, represented as key-value pairs:
naive way: ( storage_slot, address, value )
actual: ( derived_key, value )
compressed: ( derived_key or enumeration index, compressed_value )
Format
: Typically presented as
(derived_key, value)
,
where
derived_key
is a hash of the storage slot and address, and
value
represents the storage value.
Compression and Enumeration
: After the initial post,
derived_key
can be replaced with an enumeration index to optimize data size.
The deeper meaning is that an enumeration key is the leaf index in our storage Merkle tree.
Contract Bytecodes
The handling of contract bytecodes involves:
Compression and Indexing
: Opcodes are chunked, indexed, and compressed by the server-side operator before being verified and sent to L1.
Verification and Storage
: A system contract ensures the accuracy of the compression before submission,
with uncompressed bytecode hashes stored in
AccountStorage
for reference.
This process is split into 2 different parts:
the server side operator
handling the compression
the system contract
verifying that the compression is correct before sending to L1.
The compressed bytecode makes it way up through
factoryDeps
and the hash of uncompressed bytecode is stored on the
AccountStorage
contract
so the hash of the uncompressed bytecode will be part of the state diffs
Finality
Explore the concept of finality in blockchain systems and learn about the steps involved in achieving transaction settlement.
Pubdata compression
Spec for the pubdata compression algorithm used in ZKsync
## /zksync-protocol/rollup/pubdata-compression
ZKsync Docs
Pubdata compression
Spec for the pubdata compression algorithm used in ZKsync
The most basic strategy to publish state diffs is to publish those in either of the following two forms:
When a key is updated for the first time —
<key, value>
, where key is 32-byte derived key
and the value is new 32-byte value of the slot.
When a key is updated for the second time and more —
<enumeration_index, value>
,
where the
enumeration_index
is an 8-byte id of the slot and the value is the new 32-byte value of the slot.
This compression strategy will utilize a similar idea for treating keys and values separately
and it will be focused on the efficient compression of keys and values separately.
Keys
Keys will be packed in the same way as they were before.
The only change is that we’ll avoid using the 8-byte enumeration index and will pack it to the minimal necessary number of bytes.
This number will be part of the pubdata.
Once a key has been used, it can already use the 4 or 5 byte enumeration index
and it is very hard to have something cheaper for keys that has been used already.
The opportunity comes when remembering the ids for accounts to spare some bytes on nonce/balance key,
but ultimately the complexity may not be worth it.
There is some room for optimization of the keys that are being written for the first time,
however, optimizing those is more complex and achieves only a one-time effect
(when the key is published for the first time), so they may be in scope of the future upgrades.
Values
Values are much easier to compress since they usually contain only zeroes.
Also, we can leverage the nature of how those values are changed.
For instance, if nonce has been increased only by 1, we do not need to write the entire 32-byte new value,
we can just tell that the slot has been
increased
and then supply only the 1-byte value by which it was increased.
This way instead of 32 bytes we need to publish only 2 bytes: first byte to denote which operation has been applied
and the second by to denote the number by which the addition has been made.
We have the following 4 types of changes:
Add
,
Sub,
Transform
,
NoCompression
where:
NoCompression
denotes that the whole 32 byte will be provided.
Add
denotes that the value has been increased. (modulo 2^256)
Sub
denotes that the value has been decreased. (modulo 2^256)
Transform
denotes the value just has been changed
(i.e. we disregard any potential relation between the previous and the new value,
though the new value might be small enough to save up on the number of bytes).
Where the byte size of the output can be anywhere from 0 to 31
(also 0 makes sense for
Transform
, since it denotes that it has been zeroed out).
For
NoCompression
the whole 32 byte value is used.
So the format of the pubdata is the following:
Part 1. Header.
<version = 1 byte>
— this will enable easier automated unpacking in the future. Currently, it will be only equal to
1
.
<total_logs_len = 3 bytes>
— we need only 3 bytes to describe the total length of the L2→L1 logs.
<the number of bytes used for derived keys = 1 byte>
. It should be equal to the minimal required bytes to represent the enum indexes for repeated writes.
Part 2. Initial writes.
<num_of_initial_writes = 2 bytes>
- the number of initial writes.
Since each initial write publishes at least 32 bytes for key,
then
2^16 * 32 = 2097152
will be enough for a lot of time
(right now with the limit of 120kb it will take more than 15 L1 txs to use up all the space there).
Then for each
<key, value>
pair for each initial write:
print key as 32-byte derived key.
packing type as a 1 byte value, which consists of 5 bits to denote the length of the packing
and 3 bits to denote the type of the packing (either
Add
,
Sub
,
Transform
or
NoCompression
).
The packed value itself.
Part 3. Repeated writes.
Note, that there is no need to write the number of repeated writes,
since we know that until the end of the pubdata, all the writes will be repeated ones.
For each
<key, value>
pair for each repeated write:
print key as derived key by using the number of bytes provided in the header.
packing type as a 1 byte value, which consists of 5 bits to denote the length of the packing
and 3 bits to denote the type of the packing (either
Add
,
Sub
,
Transform
or
NoCompression
).
The packed value itself.
Impact
This setup allows us to achieve nearly 75% packing for values, and 50% gains overall in terms of the storage logs based on historical data.
Encoding of packing type
Since we have
32 * 3 + 1
ways to pack a state diff, we need at least 7 bits to present the packing type.
To make parsing easier, we will use 8 bits, i.e. 1 byte.
We will use the first 5 bits to represent the length of the bytes (from 0 to 31 inclusive) to be used.
The other 3 bits will be used to represent the type of the packing:
Add
,
Sub
,
Transform
,
NoCompression
.
Worst case scenario
The worst case scenario for such packing is when we have to pack a completely random new value,
i.e. it will take us 32 bytes to pack + 1 byte to denote which type it is.
However, for such a write the user will anyway pay at least for 32 bytes.
Adding an additional byte is roughly 3% increase, which will likely be barely felt by users,
most of which use storage slots for balances, etc, which will consume only 7-9 bytes for packed value.
Why do we need to repeat the same packing method id
You might have noticed that for each pair
<key, value>
to describe value
we always first write the packing type and then write the packed value.
However, the reader might ask, it is more efficient to just supply the packing id once
and then list all the pairs
<key, value>
which use such packing.
I.e. instead of listing
(key = 0, type = 1, value = 1), (key = 1, type = 1, value = 3), (key = 2, type = 1, value = 4), …
Just write:
type = 1, (key = 0, value = 1), (key = 1, value = 3), (key = 2, value = 4), …
There are two reasons for it:
A minor reason: sometimes it is less efficient in case the packing is used for very few slots
(since for correct unpacking we need to provide the number of slots for each packing type).
A fundamental reason: currently enum indices are stored directly in the merkle tree &
have very strict order of incrementing enforced by the circuits and (they are given in order by pairs
(address, key)
),
which are generally not accessible from pubdata.
All this means that we are not allowed to change the order of “first writes” above,
so indexes for them are directly recoverable from their order, and so we can not permute them.
If we were to reorder keys without supplying the new enumeration indices for them, the state would be unrecoverable.
Always supplying the new enum index may add additional 5 bytes for each key,
which might negate the compression benefits in a lot of cases.
Even if the compression will still be beneficial, the added complexity may not be worth it.
That being said, we
could
rearrange those for
repeated
writes, but for now we stick to the same value compression format for simplicity.
Data availability
An in-depth look at how ZKsync ensures data availability through state diffs and compresses data to optimize L1 submissions, plus tools for reconstructing L2 state from L1 public data.
ZKsync OS Overview
Introduction to ZKsync OS
## /zksync-protocol/zksyncos
ZKsync Docs
ZKsync OS Overview
Introduction to ZKsync OS
ZKsync OS is a system-level implementation for ZKsync's state transition function.
Under ZKsync's new architecture, execution is decoupled from proving.
ZKsync OS acts as the operation layer in this new architecture.
It takes block data and an initial state as input and computes the
new state after the application of the block.
ZKsync OS is implemented as a Rust program that will be compiled to two targets. This first one, x86, is used for running in the sequencer.
The second, RISC-V, is fed as an input to the
ZKsync Airbender
prover to produce the validity proof of the state transition.
Components of ZKsync OS
ZKsync OS is designed to support multiple VMs.
The main components of ZKsync OS are:
Bootloader
: The entry point program. It initializes the system and then
runs transactions using two components: the system and the execution environment interpreters.
Execution Environments
: Regular interpreters that take bytecode,
calldata, resources (similar to gas) and some other call context va프로젝트로 돌아가기