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

feat: gateway preparation #3006

Merged
merged 31 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
8546f1c
reduce sync-layer-stable diff
perekopskiy Oct 3, 2024
48fd0e6
fix test
perekopskiy Oct 3, 2024
aff2e98
nits
perekopskiy Oct 7, 2024
cd015b6
move bridge address validation to SK persistance
perekopskiy Oct 7, 2024
068d654
fix en
perekopskiy Oct 7, 2024
847c3da
fix
perekopskiy Oct 7, 2024
f9e4c90
rework pubdata approach
perekopskiy Oct 8, 2024
379b5c3
unify naming
perekopskiy Oct 8, 2024
96f3af2
merge main
perekopskiy Oct 8, 2024
b00be9d
fix commitment
perekopskiy Oct 8, 2024
ecd8e24
fix
perekopskiy Oct 8, 2024
380c11d
resolve conflicts
perekopskiy Oct 9, 2024
b0d6af7
update evm commitment test json
perekopskiy Oct 9, 2024
8106288
another test json fix
perekopskiy Oct 9, 2024
f7133a6
Resolve conflicts
perekopskiy Oct 14, 2024
d4f5d67
Merge branch 'main' into sync-layer-reduce-diff
perekopskiy Oct 14, 2024
d8c6f4c
fix after merge
perekopskiy Oct 14, 2024
b8cee5e
Resolve conflicts
perekopskiy Oct 17, 2024
3304679
Resolve conflicts
perekopskiy Oct 17, 2024
01b89ee
put pubdata_builder to finish_batch
perekopskiy Oct 18, 2024
79c4ba2
move stuff to multivm
perekopskiy Oct 18, 2024
0984283
update tests
perekopskiy Oct 18, 2024
26bb438
fix tests
perekopskiy Oct 18, 2024
a56e0ef
update protocol version in json commitment test
perekopskiy Oct 18, 2024
4fbc3bb
remove pubdata_params from oneshot env
perekopskiy Oct 18, 2024
8ade31b
Resolve conflicts
perekopskiy Oct 22, 2024
0825647
remove finish_batch from dump playback
perekopskiy Oct 22, 2024
b22e77c
Address comments
perekopskiy Oct 22, 2024
f8c6863
nits
perekopskiy Oct 22, 2024
37d1e0f
resolve conflicts
perekopskiy Oct 23, 2024
fadb376
fix bootloader tests
perekopskiy Oct 24, 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
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions core/bin/external_node/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ pub(crate) struct RemoteENConfig {
// the `l2_erc20_bridge_addr` and `l2_shared_bridge_addr` are basically the same contract, but with
// a different name, with names adapted only for consistency.
pub l1_shared_bridge_proxy_addr: Option<Address>,
/// Contract address that serves as a shared bridge on L2.
/// It is expected that `L2SharedBridge` is used before gateway upgrade, and `L2AssetRouter` is used after.
pub l2_shared_bridge_addr: Option<Address>,
/// Address of `L2SharedBridge` that was used before gateway upgrade.
/// `None` if chain genesis used post-gateway protocol version.
pub l2_legacy_shared_bridge_addr: Option<Address>,
pub l1_erc20_bridge_proxy_addr: Option<Address>,
pub l2_erc20_bridge_addr: Option<Address>,
pub l1_weth_bridge_addr: Option<Address>,
Expand Down Expand Up @@ -189,6 +194,7 @@ impl RemoteENConfig {
l2_erc20_bridge_addr: l2_erc20_default_bridge,
l1_shared_bridge_proxy_addr: bridges.l1_shared_default_bridge,
l2_shared_bridge_addr: l2_erc20_shared_bridge,
l2_legacy_shared_bridge_addr: bridges.l2_legacy_shared_bridge,
l1_weth_bridge_addr: bridges.l1_weth_bridge,
l2_weth_bridge_addr: bridges.l2_weth_bridge,
base_token_addr,
Expand Down Expand Up @@ -218,6 +224,7 @@ impl RemoteENConfig {
l1_shared_bridge_proxy_addr: Some(Address::repeat_byte(5)),
l1_weth_bridge_addr: None,
l2_shared_bridge_addr: Some(Address::repeat_byte(6)),
l2_legacy_shared_bridge_addr: Some(Address::repeat_byte(7)),
l1_batch_commit_data_generator_mode: L1BatchCommitmentMode::Rollup,
dummy_verifier: true,
}
Expand Down Expand Up @@ -1403,6 +1410,7 @@ impl From<&ExternalNodeConfig> for InternalApiConfig {
l2_erc20_default_bridge: config.remote.l2_erc20_bridge_addr,
l1_shared_default_bridge: config.remote.l1_shared_bridge_proxy_addr,
l2_shared_default_bridge: config.remote.l2_shared_bridge_addr,
l2_legacy_shared_bridge: config.remote.l2_legacy_shared_bridge_addr,
l1_weth_bridge: config.remote.l1_weth_bridge_addr,
l2_weth_bridge: config.remote.l2_weth_bridge_addr,
},
Expand Down
20 changes: 16 additions & 4 deletions core/bin/external_node/src/node_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use zksync_node_framework::{
service::{ZkStackService, ZkStackServiceBuilder},
};
use zksync_state::RocksdbStorageOptions;
use zksync_types::L2_NATIVE_TOKEN_VAULT_ADDRESS;

use crate::{config::ExternalNodeConfig, metrics::framework::ExternalNodeMetricsLayer, Component};

Expand Down Expand Up @@ -192,11 +193,22 @@ impl ExternalNodeBuilder {
// compression.
const OPTIONAL_BYTECODE_COMPRESSION: bool = true;

let l2_shared_bridge_addr = self
.config
.remote
.l2_shared_bridge_addr
.context("Missing `l2_shared_bridge_addr`")?;
let l2_legacy_shared_bridge_addr = if l2_shared_bridge_addr == L2_NATIVE_TOKEN_VAULT_ADDRESS
{
// System has migrated to `L2_NATIVE_TOKEN_VAULT_ADDRESS`, use legacy shared bridge address from main node.
self.config.remote.l2_legacy_shared_bridge_addr
} else {
// System hasn't migrated on `L2_NATIVE_TOKEN_VAULT_ADDRESS`, we can safely use `l2_shared_bridge_addr`.
Some(l2_shared_bridge_addr)
};

let persistence_layer = OutputHandlerLayer::new(
self.config
.remote
.l2_shared_bridge_addr
.expect("L2 shared bridge address is not set"),
l2_legacy_shared_bridge_addr,
self.config.optional.l2_block_seal_queue_capacity,
)
.with_pre_insert_txs(true) // EN requires txs to be pre-inserted.
Expand Down
1 change: 1 addition & 0 deletions core/bin/snapshots_creator/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ async fn create_l2_block(
base_fee_per_gas: 0,
gas_per_pubdata_limit: 0,
batch_fee_input: Default::default(),
pubdata_params: Default::default(),
base_system_contracts_hashes: Default::default(),
protocol_version: Some(Default::default()),
virtual_blocks: 0,
Expand Down
30 changes: 22 additions & 8 deletions core/bin/system-constants-generator/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use zksync_contracts::{
};
use zksync_multivm::{
interface::{
pubdata::pubdata_params_to_builder,
storage::{InMemoryStorage, StorageView, WriteStorage},
tracer::VmExecutionStopReason,
L1BatchEnv, L2BlockEnv, SystemEnv, TxExecutionMode, VmExecutionMode, VmFactory,
Expand All @@ -21,11 +22,12 @@ use zksync_multivm::{
zk_evm_latest::aux_structures::Timestamp,
};
use zksync_types::{
block::L2BlockHasher, ethabi::Token, fee::Fee, fee_model::BatchFeeInput, l1::L1Tx, l2::L2Tx,
utils::storage_key_for_eth_balance, AccountTreeId, Address, Execute, K256PrivateKey,
L1BatchNumber, L1TxCommonData, L2BlockNumber, L2ChainId, Nonce, ProtocolVersionId, StorageKey,
Transaction, BOOTLOADER_ADDRESS, SYSTEM_CONTEXT_ADDRESS, SYSTEM_CONTEXT_GAS_PRICE_POSITION,
SYSTEM_CONTEXT_TX_ORIGIN_POSITION, U256, ZKPORTER_IS_AVAILABLE,
block::L2BlockHasher, commitment::PubdataParams, ethabi::Token, fee::Fee,
fee_model::BatchFeeInput, l1::L1Tx, l2::L2Tx, utils::storage_key_for_eth_balance,
AccountTreeId, Address, Execute, K256PrivateKey, L1BatchNumber, L1TxCommonData, L2BlockNumber,
L2ChainId, Nonce, ProtocolVersionId, StorageKey, Transaction, BOOTLOADER_ADDRESS,
SYSTEM_CONTEXT_ADDRESS, SYSTEM_CONTEXT_GAS_PRICE_POSITION, SYSTEM_CONTEXT_TX_ORIGIN_POSITION,
U256, ZKPORTER_IS_AVAILABLE,
};
use zksync_utils::{bytecode::hash_bytecode, bytes_to_be_words, u256_to_h256};

Expand Down Expand Up @@ -264,7 +266,14 @@ pub(super) fn execute_internal_transfer_test() -> u32 {
output: tracer_result.clone(),
}
.into_tracer_pointer();
let mut vm: Vm<_, HistoryEnabled> = Vm::new(l1_batch, system_env, storage_view.to_rc_ptr());

let pubdata_builder = pubdata_params_to_builder(PubdataParams::default());
let mut vm: Vm<_, HistoryEnabled> = Vm::new(
l1_batch,
system_env,
storage_view.to_rc_ptr(),
Some(pubdata_builder),
);
let result = vm.inspect(&mut tracer.into(), VmExecutionMode::Bootloader);

assert!(!result.result.is_failed(), "The internal call has reverted");
Expand Down Expand Up @@ -317,9 +326,14 @@ pub(super) fn execute_user_txs_in_test_gas_vm(
default_validation_computational_gas_limit: BATCH_COMPUTATIONAL_GAS_LIMIT,
chain_id: L2ChainId::default(),
};
let pubdata_builder = pubdata_params_to_builder(PubdataParams::default());

let mut vm: Vm<_, HistoryEnabled> =
Vm::new(l1_batch, system_env, Rc::new(RefCell::new(storage_view)));
let mut vm: Vm<_, HistoryEnabled> = Vm::new(
l1_batch,
system_env,
Rc::new(RefCell::new(storage_view)),
Some(pubdata_builder),
);

let mut total_gas_refunded = 0;
for tx in txs {
Expand Down
6 changes: 3 additions & 3 deletions core/bin/zksync_server/src/node_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,7 @@ impl MainNodeBuilder {
let wallets = self.wallets.clone();
let sk_config = try_load_config!(self.configs.state_keeper_config);
let persistence_layer = OutputHandlerLayer::new(
self.contracts_config
.l2_shared_bridge_addr
.context("L2 shared bridge address")?,
self.contracts_config.l2_legacy_shared_bridge_addr,
sk_config.l2_block_seal_queue_capacity,
)
.with_protective_reads_persistence_enabled(sk_config.protective_reads_persistence_enabled);
Expand All @@ -250,6 +248,8 @@ impl MainNodeBuilder {
sk_config.clone(),
try_load_config!(self.configs.mempool_config),
try_load_config!(wallets.state_keeper),
self.contracts_config.l2_da_validator_addr,
self.genesis_config.l1_batch_commit_data_generator_mode,
);
let db_config = try_load_config!(self.configs.db_config);
let experimental_vm_config = self
Expand Down
24 changes: 23 additions & 1 deletion core/lib/basic_types/src/commitment.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::str::FromStr;

use serde::{Deserialize, Serialize};
use strum::{Display, EnumIter};

use crate::{
ethabi,
web3::contract::{Detokenize, Error as ContractError},
U256,
Address, U256,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, EnumIter, Display)]
Expand Down Expand Up @@ -41,3 +43,23 @@ impl Detokenize for L1BatchCommitmentMode {
}
}
}

impl FromStr for L1BatchCommitmentMode {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Rollup" => Ok(Self::Rollup),
"Validium" => Ok(Self::Validium),
_ => {
Err("Incorrect l1 batch commitment mode type; expected one of `Rollup`, `Validium`")
}
}
}
}

#[derive(Default, Copy, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PubdataParams {
pub l2_da_validator_address: Address,
pub pubdata_type: L1BatchCommitmentMode,
}
7 changes: 7 additions & 0 deletions core/lib/basic_types/src/protocol_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub enum ProtocolVersionId {
Version23,
Version24,
Version25,
Version26,
}

impl ProtocolVersionId {
Expand Down Expand Up @@ -120,6 +121,7 @@ impl ProtocolVersionId {
ProtocolVersionId::Version23 => VmVersion::Vm1_5_0SmallBootloaderMemory,
ProtocolVersionId::Version24 => VmVersion::Vm1_5_0IncreasedBootloaderMemory,
ProtocolVersionId::Version25 => VmVersion::Vm1_5_0IncreasedBootloaderMemory,
ProtocolVersionId::Version26 => VmVersion::VmGateway,
perekopskiy marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -137,6 +139,10 @@ impl ProtocolVersionId {
self <= &Self::Version22
}

pub fn is_pre_gateway(&self) -> bool {
self <= &Self::Version25
}

pub fn is_1_4_0(&self) -> bool {
self >= &ProtocolVersionId::Version18 && self < &ProtocolVersionId::Version20
}
Expand Down Expand Up @@ -275,6 +281,7 @@ impl From<ProtocolVersionId> for VmVersion {
ProtocolVersionId::Version23 => VmVersion::Vm1_5_0SmallBootloaderMemory,
ProtocolVersionId::Version24 => VmVersion::Vm1_5_0IncreasedBootloaderMemory,
ProtocolVersionId::Version25 => VmVersion::Vm1_5_0IncreasedBootloaderMemory,
ProtocolVersionId::Version26 => VmVersion::VmGateway,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions core/lib/basic_types/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum VmVersion {
Vm1_4_2,
Vm1_5_0SmallBootloaderMemory,
Vm1_5_0IncreasedBootloaderMemory,
VmGateway,
}

impl VmVersion {
Expand Down
9 changes: 9 additions & 0 deletions core/lib/config/src/configs/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ pub struct ContractsConfig {
pub diamond_proxy_addr: Address,
pub validator_timelock_addr: Address,
pub l1_shared_bridge_proxy_addr: Option<Address>,
/// Contract address that serves as a shared bridge on L2.
/// It is expected that `L2SharedBridge` is used before gateway upgrade, and `L2AssetRouter` is used after.
pub l2_shared_bridge_addr: Option<Address>,
/// Address of `L2SharedBridge` that was used before gateway upgrade.
/// `None` if chain genesis used post-gateway protocol version.
/// If present it will be used as L2 token deployer address.
pub l2_legacy_shared_bridge_addr: Option<Address>,
pub l1_erc20_bridge_proxy_addr: Option<Address>,
pub l2_erc20_bridge_addr: Option<Address>,
pub l1_weth_bridge_proxy_addr: Option<Address>,
Expand All @@ -40,6 +46,7 @@ pub struct ContractsConfig {
// Used by the RPC API and by the node builder in wiring the BaseTokenRatioProvider layer.
pub base_token_addr: Option<Address>,
pub chain_admin_addr: Option<Address>,
pub l2_da_validator_addr: Option<Address>,
}

impl ContractsConfig {
Expand All @@ -53,6 +60,7 @@ impl ContractsConfig {
l2_erc20_bridge_addr: Some(Address::repeat_byte(0x0c)),
l1_shared_bridge_proxy_addr: Some(Address::repeat_byte(0x0e)),
l2_shared_bridge_addr: Some(Address::repeat_byte(0x0f)),
l2_legacy_shared_bridge_addr: Some(Address::repeat_byte(0x19)),
l1_weth_bridge_proxy_addr: Some(Address::repeat_byte(0x0b)),
l2_weth_bridge_addr: Some(Address::repeat_byte(0x0c)),
l2_testnet_paymaster_addr: Some(Address::repeat_byte(0x11)),
Expand All @@ -61,6 +69,7 @@ impl ContractsConfig {
base_token_addr: Some(Address::repeat_byte(0x14)),
ecosystem_contracts: Some(EcosystemContracts::for_tests()),
chain_admin_addr: Some(Address::repeat_byte(0x18)),
l2_da_validator_addr: Some(Address::repeat_byte(0x1a)),
}
}
}
2 changes: 2 additions & 0 deletions core/lib/config/src/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,15 @@ impl Distribution<configs::ContractsConfig> for EncodeDist {
l2_erc20_bridge_addr: self.sample_opt(|| rng.gen()),
l1_shared_bridge_proxy_addr: self.sample_opt(|| rng.gen()),
l2_shared_bridge_addr: self.sample_opt(|| rng.gen()),
l2_legacy_shared_bridge_addr: self.sample_opt(|| rng.gen()),
l1_weth_bridge_proxy_addr: self.sample_opt(|| rng.gen()),
l2_weth_bridge_addr: self.sample_opt(|| rng.gen()),
l2_testnet_paymaster_addr: self.sample_opt(|| rng.gen()),
l1_multicall3_addr: rng.gen(),
ecosystem_contracts: self.sample(rng),
base_token_addr: self.sample_opt(|| rng.gen()),
chain_admin_addr: self.sample_opt(|| rng.gen()),
l2_da_validator_addr: self.sample_opt(|| rng.gen()),
}
}
}
Expand Down
26 changes: 25 additions & 1 deletion core/lib/constants/src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,36 @@ pub const EVM_GAS_MANAGER_ADDRESS: Address = H160([
0x00, 0x00, 0x80, 0x13,
]);

/// Note, that the `Create2Factory` is explicitly deployed on a non-system-contract address.
pub const CREATE2_FACTORY_ADDRESS: Address = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00,
]);

pub const L2_GENESIS_UPGRADE_ADDRESS: Address = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x01,
]);

pub const L2_BRIDGEHUB_ADDRESS: Address = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x02,
]);

pub const L2_ASSET_ROUTER_ADDRESS: Address = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x03,
]);

pub const L2_NATIVE_TOKEN_VAULT_ADDRESS: Address = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x04,
]);

pub const L2_MESSAGE_ROOT_ADDRESS: Address = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x05,
]);

pub const ERC20_TRANSFER_TOPIC: H256 = H256([
221, 242, 82, 173, 27, 226, 200, 155, 105, 194, 176, 104, 252, 55, 141, 170, 149, 43, 167, 241,
99, 196, 161, 22, 40, 245, 90, 77, 245, 35, 179, 239,
Expand Down
1 change: 1 addition & 0 deletions core/lib/constants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod contracts;
pub mod crypto;
pub mod ethereum;
pub mod fees;
pub mod message_root;
pub mod system_context;
pub mod system_logs;
pub mod trusted_slots;
Expand Down
5 changes: 5 additions & 0 deletions core/lib/constants/src/message_root.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Position of `FullTree::_height` in `MessageRoot`'s storage layout.
pub const AGG_TREE_HEIGHT_KEY: usize = 3;

// Position of `FullTree::nodes` in `MessageRoot`'s storage layout.
pub const AGG_TREE_NODES_KEY: usize = 5;
11 changes: 4 additions & 7 deletions core/lib/constants/src/system_logs.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
/// The key of the system log with value of the L2->L1 logs tree root hash
pub const L2_TO_L1_LOGS_TREE_ROOT_KEY: u32 = 0;

/// The key of the system log with value of the state diff hash
pub const STATE_DIFF_HASH_KEY: u32 = 2;
/// The key of the system log with value of the state diff hash for pre-gateway protocol versions
pub const STATE_DIFF_HASH_KEY_PRE_GATEWAY: u32 = 2;

/// The key of the system log with value of the first blob linear hash
pub const BLOB1_LINEAR_HASH_KEY: u32 = 7;

/// The key of the system log with value of the second blob linear hash
pub const BLOB2_LINEAR_HASH_KEY: u32 = 8;
/// The key of the system log with value of the first blob linear hash for pre-gateway protocol versions
pub const BLOB1_LINEAR_HASH_KEY_PRE_GATEWAY: u32 = 7;
Loading
Loading