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

Port docs to kl/sync-layer-reorg #997

Merged
merged 67 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
f3c8bb6
starting porting docs
StanislavBreadless Oct 25, 2024
9dea7bb
describe rollup da
StanislavBreadless Oct 25, 2024
8b553c9
a rename
StanislavBreadless Oct 25, 2024
c3eaf32
new diagrams
kelemeno Oct 26, 2024
e5054d0
add in new diagram
kelemeno Oct 26, 2024
9e437bf
fix old docs
StanislavBreadless Oct 26, 2024
2e9fd92
rollup da pic
kelemeno Oct 26, 2024
eabacb5
port notion
kelemeno Oct 26, 2024
8b37216
Merge branch 'sb-port-docs-to-reorg' of ssh://github.com/matter-labs/…
kelemeno Oct 26, 2024
9e4d256
Merge pull request #1010 from matter-labs/kl/upgrade-diagrams
kelemeno Oct 26, 2024
a1b1106
restructure docs
kelemeno Oct 26, 2024
98c2ca6
Merge branch 'sb-port-docs-to-reorg' of ssh://github.com/matter-labs/…
kelemeno Oct 26, 2024
9717e0a
renaming
kelemeno Oct 26, 2024
abbfdaf
Merge pull request #1011 from matter-labs/kl/port-notion
kelemeno Oct 26, 2024
d94284f
imgae folders
kelemeno Oct 26, 2024
30a442a
Merge pull request #1013 from matter-labs/kl/img-folders
kelemeno Oct 26, 2024
3a35c9f
deleted some old docs
StanislavBreadless Oct 26, 2024
bd2afab
new structure
kelemeno Oct 26, 2024
f176dee
move more around
kelemeno Oct 26, 2024
c6017b2
Merge branch 'sb-port-docs-to-reorg' of ssh://github.com/matter-labs/…
kelemeno Oct 26, 2024
6ad22d6
Merge pull request #1016 from matter-labs/kl/split_docs
kelemeno Oct 26, 2024
7919510
port upgrade docs
StanislavBreadless Oct 26, 2024
678b5ab
merge with base
StanislavBreadless Oct 26, 2024
f15cef3
comment out file
StanislavBreadless Oct 26, 2024
2c308cb
replace a file
StanislavBreadless Oct 26, 2024
aaf3793
another reorg
kelemeno Oct 26, 2024
1faa17b
remove old issues and move more about chain admin
StanislavBreadless Oct 26, 2024
676ef25
remove unused text
StanislavBreadless Oct 26, 2024
479fbbd
Merge branch 'sb-port-docs-to-reorg' of ssh://github.com/matter-labs/…
kelemeno Oct 26, 2024
47e8223
some more changes
kelemeno Oct 26, 2024
37f5da8
typos
kelemeno Oct 26, 2024
9bd2583
Merge pull request #1017 from matter-labs/kl/really_split_docs
kelemeno Oct 26, 2024
1e592d8
some small image and typo fixes
kelemeno Oct 26, 2024
5a15fbd
more changes etc
kelemeno Oct 26, 2024
633e7f3
Merge pull request #1020 from matter-labs/kl/doc-reorg-2
kelemeno Oct 27, 2024
67a78bb
some reording
StanislavBreadless Oct 27, 2024
8f7425a
update history
StanislavBreadless Oct 27, 2024
18f6e55
some fixme/cleanup
StanislavBreadless Oct 27, 2024
39adc69
links and reading order
kelemeno Oct 27, 2024
cb577e2
Merge branch 'sb-port-docs-to-reorg' of ssh://github.com/matter-labs/…
kelemeno Oct 27, 2024
e16768a
add an issue fix
StanislavBreadless Oct 27, 2024
77703e3
fix some fixmes
StanislavBreadless Oct 27, 2024
b8b3df5
more link fixes
StanislavBreadless Oct 27, 2024
6e93fa9
fix constants and more links
StanislavBreadless Oct 27, 2024
bfb0c07
fix links
StanislavBreadless Oct 27, 2024
6f188d3
more todos fixed
StanislavBreadless Oct 27, 2024
a6af5bc
remove todos from contracts
StanislavBreadless Oct 27, 2024
473bffa
comment on general ugprade process
StanislavBreadless Oct 27, 2024
5ea8a06
sync with base
StanislavBreadless Oct 27, 2024
d1b1e45
fmt
StanislavBreadless Oct 27, 2024
1100a4d
more lints
StanislavBreadless Oct 27, 2024
6d87d5a
more lints
StanislavBreadless Oct 27, 2024
6c1d24e
fmt
StanislavBreadless Oct 27, 2024
dfc7595
raid's comment
kelemeno Oct 27, 2024
620285a
Merge branch 'sb-port-docs-to-reorg' of ssh://github.com/matter-labs/…
kelemeno Oct 27, 2024
bfda290
spellcheck
StanislavBreadless Oct 27, 2024
ec36f3c
fix more lint
StanislavBreadless Oct 27, 2024
9571efa
Merge remote-tracking branch 'origin/sb-port-docs-to-reorg' into sb-p…
StanislavBreadless Oct 27, 2024
d179768
(feat): add dead-links jobs
Oct 27, 2024
33d84ef
(fix): installation
Oct 27, 2024
1505139
(fix): yaml flow
Oct 27, 2024
2c80631
(fix): lint & flow
Oct 27, 2024
30e04a8
(fix): dead links && make CI fail with a dead link
Oct 27, 2024
57ec871
(fix): linting
Oct 27, 2024
6f06427
(fix): remove saving to file
Oct 27, 2024
81f9f9a
(fix): renaming
Oct 27, 2024
209cd09
(fix): Overview.md to lowercase
Oct 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
308 changes: 18 additions & 290 deletions docs/Overview.md

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# ZK Stack contracts specs

The order of the files here only roughly represents the order of reading. A lot of topics are intertwined, so it is recommended to read everything first to have a complete picture and then refer to specific documents for more details.

- [Glossary](./glossary.md)
- [Overview](./overview.md)
- Contracts of an individual chain
- [ZK Chain basics](./settlement_contracts/zkchain_basics.md)
- Data availability
- [Custom DA support](./settlement_contracts/data_availability/custom_da.md)
- [Rollup DA support](./settlement_contracts/data_availability/rollup_da.md)
- [Standard pubdata format](./settlement_contracts/data_availability/standard_pubdata_format.md)
- [State diff compression v1 spec](./settlement_contracts/data_availability/state_diff_compression_v1_spec.md)
- L1->L2 transaction handling
- [Processing of L1->L2 transactions](./settlement_contracts/priority_queue/processing_of_l1->l2_txs.md)
- [Priority queue](./settlement_contracts/priority_queue/priority-queue.md)
- Chain Management
- [Chain type manager](./chain_management/chain_type_manager.md)
- [Admin role](./chain_management/admin_role.md)
- [Chain genesis](./chain_management/chain_genesis.md)
- [Standard Upgrade process](./chain_management/upgrade_process.md)
- Bridging
- Bridgehub
- [Overview of the bridgehub functionality](./bridging/bridgehub/overview.md)
- [Asset Router](./bridging/asset_router/Overview.md)
- L2 System Contracts
- [System contracts bootloader description](./l2_system_contracts/system_contracts_bootloader_description.md)
- [Batches and blocks on ZKsync](./l2_system_contracts/batches_and_blocks_on_zksync.md)
- [Elliptic curve precompiles](./l2_system_contracts/elliptic_curve_precompiles.md)
- [ZKsync fee model](./l2_system_contracts/zksync_fee_model.md)
- Gateway
- [General overview](./gateway/overview.md)
- [Chain migration](./gateway/chain_migration.md)
- [L1->L3 messaging via gateway](./gateway/messaging_via_gateway.md)
- [L3->L1 messaging via gateway](./gateway/nested_l3_l1_messaging.md)
- [Gateway protocol versioning](./gateway/gateway_protocol_upgrades.md)
- [DA handling on Gateway](./gateway/gateway_da.md)
- Upgrade history
- [Gateway upgrade diff](./upgrade_history/gateway_upgrade/gateway_diff_review.md)
- [Gateway upgrade process](./upgrade_history/gateway_upgrade/upgrade_process.md)

![Reading order](./img/reading_order.png)

# Invariants/tricky places to look out for

This section is for auditors of the codebase. It includes some of the important invariants that the system relies on and which if broken could have bad consequences.

- Assuming that the accepting CTM is correct & efficient, the L1→GW part of the L1→GW→L3 transaction never fails. It is assumed that the provided max amount for gas is always enough for any transaction that can realistically come from L1.
- GW → L1 migration never fails. If it is possible to get into a state where the migration is not possible to finish, then the chain is basically lost. There are some exceptions where for now it is the expected behavior. (check out the “Migration invariants & protocol upgradability” section)
- The general consistency of chains when migration between different settlement layers is done. Including the feasibility of emergency upgrades, etc. I.e. whether the whole system is thought-through.
- Preimage attacks in the L3→L1 tree, we apply special prefixes to ensure that the tree structure is fixed, i.e. all logs are 88 bytes long (this is for backwards compatibility reasons). For batch leafs and chain id leafs we use special prefixes.

Check warning on line 51 in docs/README.md

View workflow job for this annotation

GitHub Actions / typos

"leafs" should be "leaves".

Check warning on line 51 in docs/README.md

View workflow job for this annotation

GitHub Actions / typos

"leafs" should be "leaves".
- Data availability guarantees. Whether rollup users can always restore all their storage slots, etc. An example of a potential tricky issue can be found in “Security notes for Gateway-based rollups” [in this document](./gateway/gateway_da.md).
33 changes: 33 additions & 0 deletions docs/bridging/asset_router/Overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Overview of Custom Asset Bridging with the Asset Router

[back to readme](../../README.md)

Bridges are completely separate contracts from the ZKChains and system contracts. They are a wrapper for L1 <-> L2 communication on both L1 and L2. Upon locking assets on one layer, a request is sent to mint these bridged assets on the other layer.
Upon burning assets on one layer, a request is sent to unlock them on the other.

Custom asset bridging is a new bridging model that allows to:

1. Minimize the effort needed by custom tokens to be able to become part of the elastic chain ecosystem. Before, each custom token would have to build its own bridge, but now just custom asset deployment trackers / asset handler is needed. This is achieved by building a modular bridge which separates the logic of L1<>L2 messaging from the holding of the asset.
2. Unify the interfaces between L1 and L2 bridge contracts, paving the way for easy cross chain bridging. It will especially become valuable once interop is enabled.

#### New concepts

- assetId => identifier to track bridged assets across chains. This is used to link messages to specific asset handlers in the AssetRouters.
- AssetHandler => contract that manages liquidity (burns/mints, locks/unlocks, etc.) for specific token (or a set of them) on a chain. Every asset
- AssetDeploymentTracker => contract that manages the deployment of asset handlers across chains. This is the contract that registers these asset handlers in the AssetRouters.

### Normal flow

Assets Handlers are registered in the Routers based on their assetId. The assetId is used to identify the asset when bridging, it is sent with the cross-chain transaction data and Router routes the data to the appropriate Handler. If the asset handler is not registered in the L2 Router, then the L1->L2 bridging transaction will fail on the L2 (expect for NTV assets, see below).

`assetId = keccak256(chainId, asset deployment tracker = msg.sender, additionalData)`

Asset registration is handled by the AssetDeploymentTracker. It is expected that this contract is deployed on the L1. Registration of the assetHandler on a ZKChain can be permissionless depending on the Asset (e.g. the AssetHandler can be deployed on the chain at a predefined address, this can message the L1 ADT, which can then register the asset in the Router). Registering the L1 Handler in the L1 Router can be done via a direct function call from the L1 Deployment Tracker. Registration in the L2 Router is done indirectly via the L1 Router.

![Asset Registration](./img/custom_asset_handler_registration.png)

The Native Token Vault is a special case of the Asset Handler, as we want it to support automatic bridging. This means it should be possible to bridge a L1 token to an L2 without deploying the Token contract beforehand and without registering it in the L2 Router. For NTV assets, L1->L2 transactions where the AssetHandler is not registered will not fail, but the message will be automatically be forwarded to the L2NTV. Here the contract checks that the asset is indeed deployed by the L1NTV, by checking that the assetId contains the correct ADT address (note, for NTV assets the ADT is the NTV and the used address is the L2NTV address). If the assetId is correct, the token contract is deployed.

### Read more

You can read more in the more in-depth about L1 and L2 asset routers and the default asset handler that is Native Token Vault [here](./asset_router.md).
48 changes: 48 additions & 0 deletions docs/bridging/asset_router/asset_router.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# AssetRouters (L1/L2) and NativeTokenVault

[back to readme](../../README.md)

The main job of the asset router is to be the central point of coordination for bridging. All crosschain token bridging is done between asset routers only and once the message reaches asset router, it then routes it to the corresponding asset handler.

In order to make this easier, all L2 chains have the asset router located on the same address on every chain. It is `0x10003` and it is pre-deployed contract. More on how it is deployed can be seen in the [Chain Genesis](../../chain_management/chain_genesis.md) section.

The endgame is to have L1 asset router have the same functionality as the L2 one. This is not the case yet, but some progress has been made: L2AssetRouter can now bridge L2-native assets to L1, from which it could be bridged to other chains in the ecosystem.

The specifics of the L2AssetRouter is the need to interact with the previously deployed L2SharedBridgeLegacy if it was already present. It has less “rights” than the L1AssetRouter: at the moment it is assumed that all asset deployment trackers are from L1, the only way to register an asset handler on L2 is to make an L1→L2 transaction.

> Note, that today registering new asset deployment trackers will be permissioned, but the plan is to make it permissionless in the future
>

The specifics of the L1AssetRouter come from the need to be backwards compatible with the old L1SharedBridge. Yes, it will not share the same storage, but it will inherit the need to be backwards compatible with the current SDK. Also, L1AssetRouter needs to facilitate L1-only operations, such as recovering from failed deposits.

Also, L1AssetRouter is the only base token bridge contract that can participate in initiation of cross chain transactions via the bridgehub. This will change in the future with the support of interop.

### L1Nullifier

While the endgoal is to unify L1 and L2 asset routers, in reality, it may not be that easy: while L2 asset routers get called by L1→L2 transactions, L1 ones don't and require manual finalization of transactions, which involves proof verification, etc. To move this logic outside of the L1AssetRouter, it was moved into a separate L1Nullifier contract.

*This is the contract the previous L1SharedBridge will be upgraded to, so it should have the backwards compatible storage.*

### NativeTokenVault (L1/L2)

NativeTokenVault is an asset handler that is available on all chains and is also predeployed. It is provides the functionality of the most basic bridging: locking funds on one chain and minting the bridged equivalent on the other one. On L2 chains NTV is predeployed at the `0x10004` address.

The L1 and L2 versions of the NTV are almost identical in functionality, the main differences come from the differences of the deployment functionality in L1 and L2 envs, where the former uses standard CREATE2 and the latter uses low level calls to `CONTRACT_DEPLOYER`system contract.

Also, the L1NTV has the following specifics:

- It operates the `chainBalance` mapping, ensuring that the chains do not go beyond their balances.
- It allows recovering from failed L1→L2 transfers.
- It needs to both be able to retrieve funds from the former L1SharedBridge (now this contract has L1Nullifier in its place), but also needs to support the old SDK that gives out allowance to the “l1 shared bridge” value returned from the API, i.e. in our case this is will the L1AssetRouter.

### L2SharedBridgeLegacy

L2AssetRouter has to be pre-deployed onto a specific address. The old L2SharedBridge will be upgraded to L2SharedBridgeLegacy contract. The main purpose of this contract is to ensure compatibility with the incoming deposits and re-route them to the shared bridge.

This contract is never deployed for new chains.

### Summary

![image.png](./img/bridge_contracts.png)
> New bridge contracts
>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
kelemeno marked this conversation as resolved.
Show resolved Hide resolved
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading