From 96ec5cbd037c62278a461f2041e4fccda22a7e7c Mon Sep 17 00:00:00 2001 From: Francisco Krause Arnim Date: Fri, 2 Feb 2024 18:49:22 -0300 Subject: [PATCH 01/16] feat: add l1 token address endpoint --- core/lib/web3_decl/src/namespaces/zks.rs | 3 +++ .../web3/backend_jsonrpsee/namespaces/zks.rs | 7 +++++++ .../src/api_server/web3/namespaces/zks.rs | 16 +++++++++++++--- .../lib/zksync_core/src/api_server/web3/state.rs | 2 +- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/core/lib/web3_decl/src/namespaces/zks.rs b/core/lib/web3_decl/src/namespaces/zks.rs index 0c808c043f18..72892f8e640e 100644 --- a/core/lib/web3_decl/src/namespaces/zks.rs +++ b/core/lib/web3_decl/src/namespaces/zks.rs @@ -121,4 +121,7 @@ pub trait ZksNamespace { keys: Vec, l1_batch_number: L1BatchNumber, ) -> RpcResult; + + #[method(name = "getBaseTokenL1Address")] + async fn get_base_token_l1_address(&self) -> RpcResult
; } diff --git a/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/namespaces/zks.rs b/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/namespaces/zks.rs index 0d3d5bcea32e..afbcbc9b0465 100644 --- a/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/namespaces/zks.rs +++ b/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/namespaces/zks.rs @@ -20,6 +20,7 @@ use zksync_web3_decl::{ use crate::api_server::web3::{backend_jsonrpsee::into_jsrpc_error, ZksNamespace}; #[async_trait] + impl ZksNamespaceServer for ZksNamespace { async fn estimate_fee(&self, req: CallRequest) -> RpcResult { self.estimate_fee_impl(req).await.map_err(into_jsrpc_error) @@ -172,4 +173,10 @@ impl ZksNamespaceServer for ZksNamespace { .await .map_err(into_jsrpc_error) } + + async fn get_base_token_l1_address(&self) -> RpcResult
{ + self.get_base_token_l1_address_impl() + .await + .map_err(into_jsrpc_error) + } } diff --git a/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs b/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs index 9718b6e52a8e..740b2637eecd 100644 --- a/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs +++ b/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, convert::TryInto}; +use std::{collections::HashMap, convert::TryInto, env, str::FromStr}; use bigdecimal::{BigDecimal, Zero}; use zksync_dal::StorageProcessor; @@ -16,8 +16,8 @@ use zksync_types::{ l2_to_l1_log::L2ToL1Log, tokens::ETHEREUM_ADDRESS, transaction_request::CallRequest, - AccountTreeId, L1BatchNumber, MiniblockNumber, StorageKey, Transaction, L1_MESSENGER_ADDRESS, - L2_ETH_TOKEN_ADDRESS, REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_BYTE, U256, U64, + AccountTreeId, L1BatchNumber, MiniblockNumber, StorageKey, Transaction, H160, + L1_MESSENGER_ADDRESS, L2_ETH_TOKEN_ADDRESS, REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_BYTE, U256, U64, }; use zksync_utils::{address_to_h256, ratio_to_big_decimal_normalized}; use zksync_web3_decl::{ @@ -646,4 +646,14 @@ impl ZksNamespace { storage_proof, }) } + + #[tracing::instrument(skip_all)] + pub async fn get_base_token_l1_address_impl(&self) -> Result { + const METHOD_NAME: &str = "get_base_token_l1_address_impl"; + let l1_token_env_var = env::var("CONTRACTS_BASE_TOKEN_ADDR") + .map_err(|_err| internal_error(METHOD_NAME, "Missing env variable for base token"))?; + Ok(H160::from_str(&l1_token_env_var).map_err(|_err| { + internal_error(METHOD_NAME, "Set value for base token is not an address") + })?) + } } diff --git a/core/lib/zksync_core/src/api_server/web3/state.rs b/core/lib/zksync_core/src/api_server/web3/state.rs index 0964f21c48bf..71155bc23cdb 100644 --- a/core/lib/zksync_core/src/api_server/web3/state.rs +++ b/core/lib/zksync_core/src/api_server/web3/state.rs @@ -14,7 +14,7 @@ use zksync_config::configs::{api::Web3JsonRpcConfig, chain::NetworkConfig, Contr use zksync_dal::{ConnectionPool, StorageProcessor}; use zksync_types::{ api, l2::L2Tx, transaction_request::CallRequest, Address, L1BatchNumber, L1ChainId, L2ChainId, - MiniblockNumber, H256, U256, U64, + MiniblockNumber, H160, H256, U256, U64, }; use zksync_web3_decl::{error::Web3Error, types::Filter}; From ecbf36d2d37f9905ff6c7a0070d13606457add40 Mon Sep 17 00:00:00 2001 From: Francisco Krause Arnim Date: Thu, 15 Feb 2024 16:08:37 -0300 Subject: [PATCH 02/16] fix: address PR comments, test target --- core/bin/external_node/src/config/mod.rs | 8 +++++++- core/lib/config/src/configs/contracts.rs | 5 +++++ core/lib/env_config/src/contracts.rs | 6 ++++++ .../lib/zksync_core/src/api_server/web3/namespaces/zks.rs | 7 +------ core/lib/zksync_core/src/api_server/web3/state.rs | 2 ++ 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/core/bin/external_node/src/config/mod.rs b/core/bin/external_node/src/config/mod.rs index d8d26eea8bb2..955ae3d48b5c 100644 --- a/core/bin/external_node/src/config/mod.rs +++ b/core/bin/external_node/src/config/mod.rs @@ -31,6 +31,7 @@ pub struct RemoteENConfig { pub l2_testnet_paymaster_addr: Option
, pub l2_chain_id: L2ChainId, pub l1_chain_id: L1ChainId, + pub base_token_addr: Address, } impl RemoteENConfig { @@ -66,7 +67,10 @@ impl RemoteENConfig { .context("Failed to fetch L1 chain ID")? .as_u64(), ); - + let base_token_addr = client + .get_base_token_l1_address() + .await + .context("Failed to fetch base token address")?; Ok(Self { bridgehub_proxy_addr, diamond_proxy_addr, @@ -77,6 +81,7 @@ impl RemoteENConfig { l2_weth_bridge_addr: bridges.l2_weth_bridge, l2_chain_id, l1_chain_id, + base_token_addr, }) } } @@ -521,6 +526,7 @@ impl From for InternalApiConfig { l2_testnet_paymaster_addr: config.remote.l2_testnet_paymaster_addr, req_entities_limit: config.optional.req_entities_limit, fee_history_limit: config.optional.fee_history_limit, + base_token_address: config.remote.base_token_addr, } } } diff --git a/core/lib/config/src/configs/contracts.rs b/core/lib/config/src/configs/contracts.rs index 147c20fa50b6..b73173e7cef3 100644 --- a/core/lib/config/src/configs/contracts.rs +++ b/core/lib/config/src/configs/contracts.rs @@ -1,3 +1,5 @@ +use std::str::FromStr; + // External uses use serde::Deserialize; // Workspace uses @@ -49,6 +51,7 @@ pub struct ContractsConfig { pub fri_recursion_leaf_level_vk_hash: H256, pub prover_at_genesis: ProverAtGenesis, pub snark_wrapper_vk_hash: H256, + pub base_token_addr: Address, } impl ContractsConfig { @@ -91,6 +94,8 @@ impl ContractsConfig { governance_addr: Address::repeat_byte(0x13), prover_at_genesis: ProverAtGenesis::Fri, snark_wrapper_vk_hash: H256::repeat_byte(0x09), + base_token_addr: Address::from_str("0x0000000000000000000000000000000000000001") + .unwrap(), } } } diff --git a/core/lib/env_config/src/contracts.rs b/core/lib/env_config/src/contracts.rs index ba4d5f4a7f12..e2f956802bc3 100644 --- a/core/lib/env_config/src/contracts.rs +++ b/core/lib/env_config/src/contracts.rs @@ -10,6 +10,9 @@ impl FromEnv for ContractsConfig { #[cfg(test)] mod tests { + use std::str::FromStr; + + use zksync_basic_types::Address; use zksync_config::configs::contracts::ProverAtGenesis; use super::*; @@ -70,6 +73,8 @@ mod tests { snark_wrapper_vk_hash: hash( "0x4be443afd605a782b6e56d199df2460a025c81b3dea144e135bece83612563f2", ), + base_token_addr: Address::from_str("0x0000000000000000000000000000000000000001") + .unwrap(), } } @@ -105,6 +110,7 @@ CONTRACTS_FRI_RECURSION_NODE_LEVEL_VK_HASH="0x5a3ef282b21e12fe1f4438e5bb158fc506 CONTRACTS_FRI_RECURSION_LEAF_LEVEL_VK_HASH="0x72167c43a46cf38875b267d67716edc4563861364a3c03ab7aee73498421e828" CONTRACTS_PROVER_AT_GENESIS="fri" CONTRACTS_SNARK_WRAPPER_VK_HASH="0x4be443afd605a782b6e56d199df2460a025c81b3dea144e135bece83612563f2" +CONTRACTS_BASE_TOKEN_ADDR=0x0000000000000000000000000000000000000001 "#; lock.set_env(config); diff --git a/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs b/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs index 740b2637eecd..467d265b6a1e 100644 --- a/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs +++ b/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs @@ -649,11 +649,6 @@ impl ZksNamespace { #[tracing::instrument(skip_all)] pub async fn get_base_token_l1_address_impl(&self) -> Result { - const METHOD_NAME: &str = "get_base_token_l1_address_impl"; - let l1_token_env_var = env::var("CONTRACTS_BASE_TOKEN_ADDR") - .map_err(|_err| internal_error(METHOD_NAME, "Missing env variable for base token"))?; - Ok(H160::from_str(&l1_token_env_var).map_err(|_err| { - internal_error(METHOD_NAME, "Set value for base token is not an address") - })?) + Ok(self.state.api_config.base_token_address) } } diff --git a/core/lib/zksync_core/src/api_server/web3/state.rs b/core/lib/zksync_core/src/api_server/web3/state.rs index 71155bc23cdb..92ef9c51eae6 100644 --- a/core/lib/zksync_core/src/api_server/web3/state.rs +++ b/core/lib/zksync_core/src/api_server/web3/state.rs @@ -86,6 +86,7 @@ pub struct InternalApiConfig { pub l2_testnet_paymaster_addr: Option
, pub req_entities_limit: usize, pub fee_history_limit: u64, + pub base_token_address: Address, } impl InternalApiConfig { @@ -112,6 +113,7 @@ impl InternalApiConfig { l2_testnet_paymaster_addr: contracts_config.l2_testnet_paymaster_addr, req_entities_limit: web3_config.req_entities_limit(), fee_history_limit: web3_config.fee_history_limit(), + base_token_address: contracts_config.base_token_addr, } } } From 513903ceb5c64aa0d460a3f44538ceaeaee530ba Mon Sep 17 00:00:00 2001 From: Francisco Krause Arnim Date: Thu, 15 Feb 2024 17:47:14 -0300 Subject: [PATCH 03/16] lint: cargo fmt --- core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs | 6 +++--- core/lib/zksync_core/src/api_server/web3/state.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs b/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs index 467d265b6a1e..5155b4cce4c8 100644 --- a/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs +++ b/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, convert::TryInto, env, str::FromStr}; +use std::{collections::HashMap, convert::TryInto}; use bigdecimal::{BigDecimal, Zero}; use zksync_dal::StorageProcessor; @@ -16,8 +16,8 @@ use zksync_types::{ l2_to_l1_log::L2ToL1Log, tokens::ETHEREUM_ADDRESS, transaction_request::CallRequest, - AccountTreeId, L1BatchNumber, MiniblockNumber, StorageKey, Transaction, H160, - L1_MESSENGER_ADDRESS, L2_ETH_TOKEN_ADDRESS, REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_BYTE, U256, U64, + AccountTreeId, L1BatchNumber, MiniblockNumber, StorageKey, Transaction, L1_MESSENGER_ADDRESS, + L2_ETH_TOKEN_ADDRESS, REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_BYTE, U256, U64, }; use zksync_utils::{address_to_h256, ratio_to_big_decimal_normalized}; use zksync_web3_decl::{ diff --git a/core/lib/zksync_core/src/api_server/web3/state.rs b/core/lib/zksync_core/src/api_server/web3/state.rs index 92ef9c51eae6..1d23e4aadabe 100644 --- a/core/lib/zksync_core/src/api_server/web3/state.rs +++ b/core/lib/zksync_core/src/api_server/web3/state.rs @@ -14,7 +14,7 @@ use zksync_config::configs::{api::Web3JsonRpcConfig, chain::NetworkConfig, Contr use zksync_dal::{ConnectionPool, StorageProcessor}; use zksync_types::{ api, l2::L2Tx, transaction_request::CallRequest, Address, L1BatchNumber, L1ChainId, L2ChainId, - MiniblockNumber, H160, H256, U256, U64, + MiniblockNumber, H256, U256, U64, }; use zksync_web3_decl::{error::Web3Error, types::Filter}; From dcaa5ebdde66d00e8736e15b096d6cd66e6542b8 Mon Sep 17 00:00:00 2001 From: Francisco Krause Arnim Date: Fri, 16 Feb 2024 13:43:34 -0300 Subject: [PATCH 04/16] feat: address pr comments for EN config --- core/bin/external_node/src/config/mod.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/core/bin/external_node/src/config/mod.rs b/core/bin/external_node/src/config/mod.rs index 955ae3d48b5c..b33473f2b376 100644 --- a/core/bin/external_node/src/config/mod.rs +++ b/core/bin/external_node/src/config/mod.rs @@ -1,7 +1,6 @@ -use std::{env, time::Duration}; - use anyhow::Context; use serde::Deserialize; +use std::{env, str::FromStr, time::Duration}; use url::Url; use zksync_basic_types::{Address, L1ChainId, L2ChainId}; use zksync_core::api_server::{ @@ -9,8 +8,12 @@ use zksync_core::api_server::{ web3::{state::InternalApiConfig, Namespace}, }; use zksync_types::api::BridgeAddresses; +use zksync_web3_decl::jsonrpsee::core::ClientError; use zksync_web3_decl::{ - jsonrpsee::http_client::{HttpClient, HttpClientBuilder}, + jsonrpsee::{ + http_client::{HttpClient, HttpClientBuilder}, + types::error::ErrorCode, + }, namespaces::{EthNamespaceClient, ZksNamespaceClient}, }; @@ -67,10 +70,12 @@ impl RemoteENConfig { .context("Failed to fetch L1 chain ID")? .as_u64(), ); - let base_token_addr = client - .get_base_token_l1_address() - .await - .context("Failed to fetch base token address")?; + let base_token_addr = match client.get_base_token_l1_address().await { + Err(ClientError::Call(err)) if ErrorCode::MethodNotFound.code() == err.code() => { + Address::from_str("0x0000000000000000000000000000000000000001")? + } + response => response.context("Failed to fetch base token address")?, + }; Ok(Self { bridgehub_proxy_addr, diamond_proxy_addr, From 260ab9c00e336d9037c19d4743f82f1b3b5e7c4b Mon Sep 17 00:00:00 2001 From: Francisco Krause Arnim Date: Fri, 16 Feb 2024 13:45:00 -0300 Subject: [PATCH 05/16] chore: zk fmt --- core/bin/external_node/src/config/mod.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/bin/external_node/src/config/mod.rs b/core/bin/external_node/src/config/mod.rs index b33473f2b376..8ede3044ce44 100644 --- a/core/bin/external_node/src/config/mod.rs +++ b/core/bin/external_node/src/config/mod.rs @@ -1,6 +1,7 @@ +use std::{env, str::FromStr, time::Duration}; + use anyhow::Context; use serde::Deserialize; -use std::{env, str::FromStr, time::Duration}; use url::Url; use zksync_basic_types::{Address, L1ChainId, L2ChainId}; use zksync_core::api_server::{ @@ -8,9 +9,9 @@ use zksync_core::api_server::{ web3::{state::InternalApiConfig, Namespace}, }; use zksync_types::api::BridgeAddresses; -use zksync_web3_decl::jsonrpsee::core::ClientError; use zksync_web3_decl::{ jsonrpsee::{ + core::ClientError, http_client::{HttpClient, HttpClientBuilder}, types::error::ErrorCode, }, From c10fba596503daae1e04909d67ba2e654a413fd2 Mon Sep 17 00:00:00 2001 From: Francisco Krause Arnim Date: Tue, 5 Mar 2024 18:01:20 -0300 Subject: [PATCH 06/16] fix: pr comments --- .../src/api_server/web3/backend_jsonrpsee/namespaces/zks.rs | 2 -- core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/namespaces/zks.rs b/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/namespaces/zks.rs index a30e8e57b565..621fa49a4208 100644 --- a/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/namespaces/zks.rs +++ b/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/namespaces/zks.rs @@ -19,7 +19,6 @@ use zksync_web3_decl::{ use crate::api_server::web3::{backend_jsonrpsee::into_jsrpc_error, ZksNamespace}; #[async_trait] - impl ZksNamespaceServer for ZksNamespace { async fn estimate_fee(&self, req: CallRequest) -> RpcResult { self.estimate_fee_impl(req).await.map_err(into_jsrpc_error) @@ -169,7 +168,6 @@ impl ZksNamespaceServer for ZksNamespace { async fn get_base_token_l1_address(&self) -> RpcResult
{ self.get_base_token_l1_address_impl() - .await .map_err(into_jsrpc_error) } } diff --git a/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs b/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs index f9ad7234de3c..4b59c5af425c 100644 --- a/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs +++ b/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs @@ -629,7 +629,7 @@ impl ZksNamespace { } #[tracing::instrument(skip_all)] - pub async fn get_base_token_l1_address_impl(&self) -> Result { + pub fn get_base_token_l1_address_impl(&self) -> Result { Ok(self.state.api_config.base_token_address) } } From cebb35b8de371ad376e21dce3081fab684ba7a2a Mon Sep 17 00:00:00 2001 From: Francisco Krause Arnim Date: Thu, 7 Mar 2024 20:52:35 -0300 Subject: [PATCH 07/16] fix: more pr comments --- core/bin/external_node/src/config/mod.rs | 4 +++- core/lib/config/src/configs/contracts.rs | 2 +- core/lib/env_config/src/contracts.rs | 3 +-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/core/bin/external_node/src/config/mod.rs b/core/bin/external_node/src/config/mod.rs index 863babdf1cb4..668a844cee46 100644 --- a/core/bin/external_node/src/config/mod.rs +++ b/core/bin/external_node/src/config/mod.rs @@ -58,7 +58,9 @@ impl RemoteENConfig { .await?; let base_token_addr = match client.get_base_token_l1_address().await { Err(ClientError::Call(err)) if ErrorCode::MethodNotFound.code() == err.code() => { - Address::from_str("0x0000000000000000000000000000000000000001")? + // This is the fallback case for when the EN tries to interact + // with a node that does not implement the zks_baseTokenL1Address endpoint. + Address::zero() } response => response.context("Failed to fetch base token address")?, }; diff --git a/core/lib/config/src/configs/contracts.rs b/core/lib/config/src/configs/contracts.rs index f7ce951a3420..edb7c93e628b 100644 --- a/core/lib/config/src/configs/contracts.rs +++ b/core/lib/config/src/configs/contracts.rs @@ -10,7 +10,6 @@ pub enum ProverAtGenesis { Fri, Old, } - /// Data about deployed contracts. #[derive(Debug, Deserialize, Clone, PartialEq)] pub struct ContractsConfig { @@ -41,6 +40,7 @@ pub struct ContractsConfig { pub fri_recursion_leaf_level_vk_hash: H256, pub prover_at_genesis: ProverAtGenesis, pub snark_wrapper_vk_hash: H256, + #[serde(default = "Address::zero")] pub base_token_addr: Address, // These contracts will be used after shared bridge integration. pub bridgehub_proxy_addr: Option
, diff --git a/core/lib/env_config/src/contracts.rs b/core/lib/env_config/src/contracts.rs index f1cf0b12ac13..c43f03c8240d 100644 --- a/core/lib/env_config/src/contracts.rs +++ b/core/lib/env_config/src/contracts.rs @@ -72,8 +72,7 @@ mod tests { snark_wrapper_vk_hash: hash( "0x4be443afd605a782b6e56d199df2460a025c81b3dea144e135bece83612563f2", ), - base_token_addr: Address::from_str("0x0000000000000000000000000000000000000001") - .unwrap(), + base_token_addr: addr("0x0000000000000000000000000000000000000000"), } } From aa317d26242d0d51f720e695ec274cad5c07dcc2 Mon Sep 17 00:00:00 2001 From: Francisco Krause Arnim Date: Thu, 7 Mar 2024 20:55:19 -0300 Subject: [PATCH 08/16] nit: remove FromStr trait import --- core/bin/external_node/src/config/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/bin/external_node/src/config/mod.rs b/core/bin/external_node/src/config/mod.rs index 668a844cee46..74e836221e98 100644 --- a/core/bin/external_node/src/config/mod.rs +++ b/core/bin/external_node/src/config/mod.rs @@ -1,4 +1,4 @@ -use std::{env, str::FromStr, time::Duration}; +use std::{env, time::Duration}; use anyhow::Context; use serde::Deserialize; From c0843dd1196494fa08276f8d2900baa8e85a9783 Mon Sep 17 00:00:00 2001 From: Francisco Krause Arnim Date: Thu, 7 Mar 2024 21:48:23 -0300 Subject: [PATCH 09/16] feat: Use 0x..00 when eth is base token --- core/lib/constants/src/contracts.rs | 5 +++++ .../lib/zksync_core/src/api_server/web3/namespaces/zks.rs | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/core/lib/constants/src/contracts.rs b/core/lib/constants/src/contracts.rs index cd071252bcf8..80aff2807f09 100644 --- a/core/lib/constants/src/contracts.rs +++ b/core/lib/constants/src/contracts.rs @@ -120,3 +120,8 @@ pub const MINT_AND_BURN_ADDRESS: H160 = H160::zero(); // The `storage_log.value` database value for a contract that was deployed in a failed transaction. pub const FAILED_CONTRACT_DEPLOYMENT_BYTECODE_HASH: H256 = H256::zero(); + +pub const ETHEREUM_SHARED_BRIDGE_ADDRESS: Address = H160([ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x01, +]); diff --git a/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs b/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs index 4b59c5af425c..c23bf2323b34 100644 --- a/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs +++ b/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs @@ -2,7 +2,7 @@ use std::{collections::HashMap, convert::TryInto}; use zksync_dal::StorageProcessor; use zksync_mini_merkle_tree::MiniMerkleTree; -use zksync_system_constants::DEFAULT_L2_TX_GAS_PER_PUBDATA_BYTE; +use zksync_system_constants::{DEFAULT_L2_TX_GAS_PER_PUBDATA_BYTE, ETHEREUM_SHARED_BRIDGE_ADDRESS}; use zksync_types::{ api::{ BlockDetails, BridgeAddresses, GetLogsFilter, L1BatchDetails, L2ToL1LogProof, Proof, @@ -630,6 +630,10 @@ impl ZksNamespace { #[tracing::instrument(skip_all)] pub fn get_base_token_l1_address_impl(&self) -> Result { - Ok(self.state.api_config.base_token_address) + let base_token_addr = self.state.api_config.base_token_address; + if base_token_addr == ETHEREUM_SHARED_BRIDGE_ADDRESS { + return Ok(ETHEREUM_ADDRESS); + } + return Ok(base_token_addr); } } From b274827e7a1c73cc6c2821f48a2d691a45ecc5de Mon Sep 17 00:00:00 2001 From: Francisco Krause Arnim Date: Thu, 7 Mar 2024 21:50:39 -0300 Subject: [PATCH 10/16] fix: remove unnecessary FromStr and Address imports --- core/lib/env_config/src/contracts.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/core/lib/env_config/src/contracts.rs b/core/lib/env_config/src/contracts.rs index c43f03c8240d..4aae0636b068 100644 --- a/core/lib/env_config/src/contracts.rs +++ b/core/lib/env_config/src/contracts.rs @@ -10,9 +10,6 @@ impl FromEnv for ContractsConfig { #[cfg(test)] mod tests { - use std::str::FromStr; - - use zksync_basic_types::Address; use zksync_config::configs::contracts::ProverAtGenesis; use super::*; From 479bff75186dcff0f30f7efe751922fa7a274edf Mon Sep 17 00:00:00 2001 From: Francisco Krause Arnim Date: Thu, 7 Mar 2024 22:00:47 -0300 Subject: [PATCH 11/16] fix: use ETHEREUM_ADDRESS constant --- core/bin/external_node/src/config/mod.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/core/bin/external_node/src/config/mod.rs b/core/bin/external_node/src/config/mod.rs index 74e836221e98..5179db8b5fed 100644 --- a/core/bin/external_node/src/config/mod.rs +++ b/core/bin/external_node/src/config/mod.rs @@ -13,10 +13,14 @@ use zksync_core::{ }, consensus, }; -use zksync_types::api::BridgeAddresses; +use zksync_types::{api::BridgeAddresses, ETHEREUM_ADDRESS}; use zksync_web3_decl::{ error::ClientRpcContext, - jsonrpsee::http_client::{HttpClient, HttpClientBuilder}, + jsonrpsee::{ + core::ClientError, + http_client::{HttpClient, HttpClientBuilder}, + types::error::ErrorCode, + }, namespaces::{EthNamespaceClient, ZksNamespaceClient}, }; @@ -60,7 +64,7 @@ impl RemoteENConfig { Err(ClientError::Call(err)) if ErrorCode::MethodNotFound.code() == err.code() => { // This is the fallback case for when the EN tries to interact // with a node that does not implement the zks_baseTokenL1Address endpoint. - Address::zero() + ETHEREUM_ADDRESS } response => response.context("Failed to fetch base token address")?, }; From a78f6865d537e9dfa947c0fce6bef1aa6b9217d0 Mon Sep 17 00:00:00 2001 From: Francisco Krause Arnim Date: Fri, 8 Mar 2024 17:56:21 -0300 Subject: [PATCH 12/16] fix: cargo test build --- core/lib/config/src/configs/contracts.rs | 8 ++------ core/lib/constants/src/contracts.rs | 1 + core/lib/contracts/src/lib.rs | 8 ++++---- core/lib/env_config/Cargo.toml | 1 + core/lib/env_config/src/contracts.rs | 5 ++--- core/lib/protobuf_config/src/contracts.rs | 6 ++++++ .../zksync_core/src/api_server/web3/namespaces/zks.rs | 11 +++++------ core/lib/zksync_core/src/api_server/web3/state.rs | 2 +- 8 files changed, 22 insertions(+), 20 deletions(-) diff --git a/core/lib/config/src/configs/contracts.rs b/core/lib/config/src/configs/contracts.rs index 1ed90cf929ae..2cad7a727805 100644 --- a/core/lib/config/src/configs/contracts.rs +++ b/core/lib/config/src/configs/contracts.rs @@ -1,5 +1,3 @@ -use std::str::FromStr; - // External uses use serde::Deserialize; // Workspace uses @@ -40,8 +38,7 @@ pub struct ContractsConfig { pub fri_recursion_leaf_level_vk_hash: H256, pub prover_at_genesis: ProverAtGenesis, pub snark_wrapper_vk_hash: H256, - #[serde(default = "Address::zero")] - pub base_token_addr: Address, + pub base_token_addr: Option
, // These contracts will be used after shared bridge integration. pub bridgehub_proxy_addr: Option
, pub bridgehub_impl_addr: Option
, @@ -85,8 +82,7 @@ impl ContractsConfig { governance_addr: Address::repeat_byte(0x13), prover_at_genesis: ProverAtGenesis::Fri, snark_wrapper_vk_hash: H256::repeat_byte(0x09), - base_token_addr: Address::from_str("0x0000000000000000000000000000000000000001") - .unwrap(), + base_token_addr: Some(Address::repeat_byte(0x14)), bridgehub_proxy_addr: Some(Address::repeat_byte(0x14)), bridgehub_impl_addr: Some(Address::repeat_byte(0x15)), state_transition_proxy_addr: Some(Address::repeat_byte(0x16)), diff --git a/core/lib/constants/src/contracts.rs b/core/lib/constants/src/contracts.rs index 80aff2807f09..f3ffab83f4a6 100644 --- a/core/lib/constants/src/contracts.rs +++ b/core/lib/constants/src/contracts.rs @@ -121,6 +121,7 @@ pub const MINT_AND_BURN_ADDRESS: H160 = H160::zero(); // The `storage_log.value` database value for a contract that was deployed in a failed transaction. pub const FAILED_CONTRACT_DEPLOYMENT_BYTECODE_HASH: H256 = H256::zero(); +// When the base token is ethereum, this is the value used for the contract bridge address. pub const ETHEREUM_SHARED_BRIDGE_ADDRESS: Address = H160([ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, diff --git a/core/lib/contracts/src/lib.rs b/core/lib/contracts/src/lib.rs index 9990e542ce72..197b541cb89c 100644 --- a/core/lib/contracts/src/lib.rs +++ b/core/lib/contracts/src/lib.rs @@ -39,10 +39,10 @@ const MULTICALL3_CONTRACT_FILE: &str = "contracts/l1-contracts/artifacts/cache/solpp-generated-contracts/dev-contracts/Multicall3.sol/Multicall3.json"; const VERIFIER_CONTRACT_FILE: &str = "contracts/l1-contracts/artifacts/cache/solpp-generated-contracts/state-transition/Verifier.sol/Verifier.json"; -// const IERC20_CONTRACT_FILE: &str = -// "contracts/l1-contracts/artifacts/cache/solpp-generated-contracts/common/interfaces/IERC20.sol/IERC20.json"; -// const FAIL_ON_RECEIVE_CONTRACT_FILE: &str = -// "contracts/l1-contracts/artifacts/cache/solpp-generated-contracts/zksync/dev-contracts/FailOnReceive.sol/FailOnReceive.json"; +const IERC20_CONTRACT_FILE: &str = + "contracts/l1-contracts/artifacts/cache/solpp-generated-contracts/common/interfaces/IERC20.sol/IERC20.json"; +const FAIL_ON_RECEIVE_CONTRACT_FILE: &str = + "contracts/l1-contracts/artifacts/cache/solpp-generated-contracts/zksync/dev-contracts/FailOnReceive.sol/FailOnReceive.json"; const L2_BRIDGE_CONTRACT_FILE: &str = "contracts/l2-contracts/artifacts-zk/contracts-preprocessed/bridge/interfaces/IL2Bridge.sol/IL2Bridge.json"; const LOADNEXT_CONTRACT_FILE: &str = diff --git a/core/lib/env_config/Cargo.toml b/core/lib/env_config/Cargo.toml index a084a3768bfd..7f14e3e4cc83 100644 --- a/core/lib/env_config/Cargo.toml +++ b/core/lib/env_config/Cargo.toml @@ -12,6 +12,7 @@ categories = ["cryptography"] [dependencies] zksync_basic_types = { path = "../../lib/basic_types" } zksync_config = { path = "../../lib/config" } +zksync_system_constants = { path = "../../lib/constants" } anyhow = "1.0" serde = "1.0" diff --git a/core/lib/env_config/src/contracts.rs b/core/lib/env_config/src/contracts.rs index 4aae0636b068..83df80650cb0 100644 --- a/core/lib/env_config/src/contracts.rs +++ b/core/lib/env_config/src/contracts.rs @@ -1,5 +1,4 @@ use zksync_config::ContractsConfig; - use crate::{envy_load, FromEnv}; impl FromEnv for ContractsConfig { @@ -11,7 +10,7 @@ impl FromEnv for ContractsConfig { #[cfg(test)] mod tests { use zksync_config::configs::contracts::ProverAtGenesis; - + use zksync_system_constants::ETHEREUM_SHARED_BRIDGE_ADDRESS; use super::*; use crate::test_utils::{addr, hash, EnvMutex}; @@ -69,7 +68,7 @@ mod tests { snark_wrapper_vk_hash: hash( "0x4be443afd605a782b6e56d199df2460a025c81b3dea144e135bece83612563f2", ), - base_token_addr: addr("0x0000000000000000000000000000000000000000"), + base_token_addr: Some(ETHEREUM_SHARED_BRIDGE_ADDRESS), } } diff --git a/core/lib/protobuf_config/src/contracts.rs b/core/lib/protobuf_config/src/contracts.rs index 4ab4a1186119..f6f4bbf118e4 100644 --- a/core/lib/protobuf_config/src/contracts.rs +++ b/core/lib/protobuf_config/src/contracts.rs @@ -143,6 +143,12 @@ impl ProtoRepr for proto::Contracts { .map(|x| parse_h160(x)) .transpose() .context("transparent_proxy_admin_addr")?, + base_token_addr: self + .base_token_address + .as_ref() + .map(|x| parse_h160(x)) + .transpose() + .context("base_token_addr")?, }) } diff --git a/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs b/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs index c23bf2323b34..8cc182e080fb 100644 --- a/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs +++ b/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs @@ -2,7 +2,7 @@ use std::{collections::HashMap, convert::TryInto}; use zksync_dal::StorageProcessor; use zksync_mini_merkle_tree::MiniMerkleTree; -use zksync_system_constants::{DEFAULT_L2_TX_GAS_PER_PUBDATA_BYTE, ETHEREUM_SHARED_BRIDGE_ADDRESS}; +use zksync_system_constants::DEFAULT_L2_TX_GAS_PER_PUBDATA_BYTE; use zksync_types::{ api::{ BlockDetails, BridgeAddresses, GetLogsFilter, L1BatchDetails, L2ToL1LogProof, Proof, @@ -630,10 +630,9 @@ impl ZksNamespace { #[tracing::instrument(skip_all)] pub fn get_base_token_l1_address_impl(&self) -> Result { - let base_token_addr = self.state.api_config.base_token_address; - if base_token_addr == ETHEREUM_SHARED_BRIDGE_ADDRESS { - return Ok(ETHEREUM_ADDRESS); - } - return Ok(base_token_addr); + self.state + .api_config + .base_token_address + .ok_or(Web3Error::NotImplemented) } } diff --git a/core/lib/zksync_core/src/api_server/web3/state.rs b/core/lib/zksync_core/src/api_server/web3/state.rs index 9921c289aa0c..eb9ab1a80028 100644 --- a/core/lib/zksync_core/src/api_server/web3/state.rs +++ b/core/lib/zksync_core/src/api_server/web3/state.rs @@ -86,7 +86,7 @@ pub struct InternalApiConfig { pub l2_testnet_paymaster_addr: Option
, pub req_entities_limit: usize, pub fee_history_limit: u64, - pub base_token_address: Address, + pub base_token_address: Option
, pub filters_disabled: bool, } From edc04d3c8233692e5529ea4632d5602150b1421b Mon Sep 17 00:00:00 2001 From: Francisco Krause Arnim Date: Fri, 8 Mar 2024 18:05:32 -0300 Subject: [PATCH 13/16] chore: zk fmt --- core/lib/env_config/src/contracts.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/lib/env_config/src/contracts.rs b/core/lib/env_config/src/contracts.rs index 83df80650cb0..d24be2f65e2a 100644 --- a/core/lib/env_config/src/contracts.rs +++ b/core/lib/env_config/src/contracts.rs @@ -1,4 +1,5 @@ use zksync_config::ContractsConfig; + use crate::{envy_load, FromEnv}; impl FromEnv for ContractsConfig { @@ -11,6 +12,7 @@ impl FromEnv for ContractsConfig { mod tests { use zksync_config::configs::contracts::ProverAtGenesis; use zksync_system_constants::ETHEREUM_SHARED_BRIDGE_ADDRESS; + use super::*; use crate::test_utils::{addr, hash, EnvMutex}; From f544652953edfd66390240d5b294a9a4ba47b106 Mon Sep 17 00:00:00 2001 From: Francisco Krause Arnim Date: Mon, 11 Mar 2024 19:04:55 -0300 Subject: [PATCH 14/16] feat: Handle Web3Error::NotImplemented on EN --- core/bin/external_node/src/config/mod.rs | 15 ++++++++++----- .../src/api_server/web3/backend_jsonrpsee/mod.rs | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/core/bin/external_node/src/config/mod.rs b/core/bin/external_node/src/config/mod.rs index 5179db8b5fed..ec9a92deb9c8 100644 --- a/core/bin/external_node/src/config/mod.rs +++ b/core/bin/external_node/src/config/mod.rs @@ -9,13 +9,13 @@ use zksync_consensus_roles::node; use zksync_core::{ api_server::{ tx_sender::TxSenderConfig, - web3::{state::InternalApiConfig, Namespace}, + web3::{backend_jsonrpsee::into_jsrpc_error, state::InternalApiConfig, Namespace}, }, consensus, }; use zksync_types::{api::BridgeAddresses, ETHEREUM_ADDRESS}; use zksync_web3_decl::{ - error::ClientRpcContext, + error::{ClientRpcContext, Web3Error}, jsonrpsee::{ core::ClientError, http_client::{HttpClient, HttpClientBuilder}, @@ -23,7 +23,6 @@ use zksync_web3_decl::{ }, namespaces::{EthNamespaceClient, ZksNamespaceClient}, }; - pub(crate) mod observability; #[cfg(test)] mod tests; @@ -61,7 +60,13 @@ impl RemoteENConfig { .rpc_context("get_main_contract") .await?; let base_token_addr = match client.get_base_token_l1_address().await { - Err(ClientError::Call(err)) if ErrorCode::MethodNotFound.code() == err.code() => { + Err(ClientError::Call(err)) + if [ + ErrorCode::MethodNotFound.code(), + into_jsrpc_error(Web3Error::NotImplemented).code(), + ] + .contains(&(err.code())) => + { // This is the fallback case for when the EN tries to interact // with a node that does not implement the zks_baseTokenL1Address endpoint. ETHEREUM_ADDRESS @@ -612,7 +617,7 @@ impl From for InternalApiConfig { l2_testnet_paymaster_addr: config.remote.l2_testnet_paymaster_addr, req_entities_limit: config.optional.req_entities_limit, fee_history_limit: config.optional.fee_history_limit, - base_token_address: config.remote.base_token_addr, + base_token_address: Some(config.remote.base_token_addr), filters_disabled: config.optional.filters_disabled, } } diff --git a/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/mod.rs b/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/mod.rs index 6ff938fd1b6c..0ebaa82219f5 100644 --- a/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/mod.rs +++ b/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/mod.rs @@ -14,7 +14,7 @@ use crate::api_server::{tx_sender::SubmitTxError, web3::metrics::API_METRICS}; pub mod batch_limiter_middleware; pub mod namespaces; -pub(crate) fn into_jsrpc_error(err: Web3Error) -> ErrorObjectOwned { +pub fn into_jsrpc_error(err: Web3Error) -> ErrorObjectOwned { let data = match &err { Web3Error::SubmitTransactionError(_, data) => Some(format!("0x{}", hex::encode(data))), _ => None, From 28f65239771d7bc82a9dc72c16983d4d553284ed Mon Sep 17 00:00:00 2001 From: Francisco Krause Arnim Date: Fri, 15 Mar 2024 17:35:04 -0300 Subject: [PATCH 15/16] feat: Handle Web3Error::NotImplemented after merge --- core/bin/external_node/src/config/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/bin/external_node/src/config/mod.rs b/core/bin/external_node/src/config/mod.rs index fca0ccd534d4..73a62ba8e260 100644 --- a/core/bin/external_node/src/config/mod.rs +++ b/core/bin/external_node/src/config/mod.rs @@ -66,7 +66,7 @@ impl RemoteENConfig { ErrorCode::MethodNotFound.code(), // This what Web3Error::NotImplemented gets // casted into in the api server. - ErrorCode::InternalError.code() + ErrorCode::InternalError.code(), ] .contains(&(err.code())) => { From 9cd48b73637720f889275fb1a1920b65f6bac0f2 Mon Sep 17 00:00:00 2001 From: fkrause98 Date: Fri, 15 Mar 2024 20:23:06 -0300 Subject: [PATCH 16/16] fix: update protobuf config --- core/bin/external_node/src/config/mod.rs | 2 +- core/lib/protobuf_config/src/contracts.rs | 4 ++++ core/lib/protobuf_config/src/proto/contracts.proto | 1 + .../src/api_server/web3/backend_jsonrpsee/namespaces/zks.rs | 2 +- 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/core/bin/external_node/src/config/mod.rs b/core/bin/external_node/src/config/mod.rs index 73a62ba8e260..1bcf62869b11 100644 --- a/core/bin/external_node/src/config/mod.rs +++ b/core/bin/external_node/src/config/mod.rs @@ -8,7 +8,7 @@ use zksync_config::ObjectStoreConfig; use zksync_core::{ api_server::{ tx_sender::TxSenderConfig, - web3::{backend_jsonrpsee::into_jsrpc_error, state::InternalApiConfig, Namespace}, + web3::{state::InternalApiConfig, Namespace}, }, consensus, temp_config_store::decode_yaml, diff --git a/core/lib/protobuf_config/src/contracts.rs b/core/lib/protobuf_config/src/contracts.rs index 0e48c7cef384..f4915d5c184d 100644 --- a/core/lib/protobuf_config/src/contracts.rs +++ b/core/lib/protobuf_config/src/contracts.rs @@ -214,6 +214,10 @@ impl ProtoRepr for proto::Contracts { .transparent_proxy_admin_addr .as_ref() .map(|x| x.as_bytes().into()), + base_token_address: this + .base_token_addr + .as_ref() + .map(|x| x.as_bytes().into()) } } } diff --git a/core/lib/protobuf_config/src/proto/contracts.proto b/core/lib/protobuf_config/src/proto/contracts.proto index 7ef122123ca8..264eb54af56e 100644 --- a/core/lib/protobuf_config/src/proto/contracts.proto +++ b/core/lib/protobuf_config/src/proto/contracts.proto @@ -40,4 +40,5 @@ message Contracts { optional bytes state_transition_proxy_addr = 31; // optional; H160 optional bytes state_transition_impl_addr = 32; // optional; H160 optional bytes transparent_proxy_admin_addr = 33; // optional; H160 + optional bytes base_token_address = 34; // optional; H160 } diff --git a/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/namespaces/zks.rs b/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/namespaces/zks.rs index a83ad8e5634b..ba21fba770dc 100644 --- a/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/namespaces/zks.rs +++ b/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/namespaces/zks.rs @@ -170,6 +170,6 @@ impl ZksNamespaceServer for ZksNamespace { async fn get_base_token_l1_address(&self) -> RpcResult
{ self.get_base_token_l1_address_impl() - .map_err(into_jsrpc_error) + .map_err(|err| self.current_method().map_err(err)) } }