Skip to content

Commit

Permalink
feat: change chainId to u64 (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnastasiiaVashchuk authored Oct 6, 2023
1 parent f94b819 commit f14bf68
Show file tree
Hide file tree
Showing 39 changed files with 306 additions and 139 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

15 changes: 8 additions & 7 deletions core/bin/external_node/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ impl RemoteENConfig {
.get_main_contract()
.await
.context("Failed to fetch L1 contract address")?;
let l2_chain_id = L2ChainId(
let l2_chain_id = L2ChainId::try_from(
client
.chain_id()
.await
.context("Failed to fetch L2 chain ID")?
.as_u64() as u16,
);
.as_u64(),
)
.unwrap();
let l1_chain_id = L1ChainId(
client
.l1_chain_id()
Expand Down Expand Up @@ -396,14 +397,14 @@ impl ExternalNodeConfig {
.await
.context("Unable to check L1 chain ID through the configured L1 client")?;

let l2_chain_id: u16 = env_var("EN_L2_CHAIN_ID");
let l2_chain_id: L2ChainId = env_var("EN_L2_CHAIN_ID");
let l1_chain_id: u64 = env_var("EN_L1_CHAIN_ID");
if l2_chain_id != remote.l2_chain_id.0 {
if l2_chain_id != remote.l2_chain_id {
anyhow::bail!(
"Configured L2 chain id doesn't match the one from main node.
Make sure your configuration is correct and you are corrected to the right main node.
Main node L2 chain id: {}. Local config value: {}",
remote.l2_chain_id.0, l2_chain_id
Main node L2 chain id: {:?}. Local config value: {:?}",
remote.l2_chain_id, l2_chain_id
);
}
if l1_chain_id != remote.l1_chain_id.0 {
Expand Down
2 changes: 1 addition & 1 deletion core/bin/system-constants-generator/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub(super) fn get_l2_tx(contract_address: Address, signer: &H256, pubdata_price:
gas_per_pubdata_limit: pubdata_price.into(),
},
U256::from(0),
L2ChainId(270),
L2ChainId::from(270),
signer,
None,
Default::default(),
Expand Down
1 change: 1 addition & 0 deletions core/lib/basic_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ categories = ["cryptography"]
[dependencies]
web3 = { version= "0.19.0", default-features = false, features = ["http-rustls-tls", "test", "signing"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
162 changes: 149 additions & 13 deletions core/lib/basic_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod macros;

pub mod network;

use serde::{Deserialize, Serialize};
use serde::{de, Deserialize, Deserializer, Serialize};
use std::convert::{Infallible, TryFrom, TryInto};
use std::fmt;
use std::num::ParseIntError;
Expand Down Expand Up @@ -76,6 +76,85 @@ impl TryFrom<U256> for AccountTreeId {
}
}

/// ChainId in the ZkSync network.
#[derive(Copy, Clone, Debug, Serialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct L2ChainId(u64);

impl<'de> Deserialize<'de> for L2ChainId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s: String = Deserialize::deserialize(deserializer)?;
s.parse().map_err(de::Error::custom)
}
}

impl FromStr for L2ChainId {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
// Parse the string as a U64
// try to parse as decimal first
let number = match U64::from_dec_str(s) {
Ok(u) => u,
Err(_) => {
// try to parse as hex
s.parse::<U64>()
.map_err(|err| format!("Failed to parse L2ChainId: Err {err}"))?
}
};

if number.as_u64() > L2ChainId::max().0 {
return Err(format!("Too big chain ID. MAX: {}", L2ChainId::max().0));
}
Ok(L2ChainId(number.as_u64()))
}
}

impl L2ChainId {
/// The maximum value of the L2 chain ID.
// 2^53 - 1 is a max safe integer in JS. In ethereum JS libs chain ID should be the safe integer.
// Next arithmetic operation: subtract 36 and divide by 2 comes from `v` calculation:
// v = 2*chainId + 36, that should be save integer as well.
const MAX: u64 = ((1 << 53) - 1 - 36) / 2;

pub fn max() -> Self {
Self(Self::MAX)
}

pub fn as_u64(&self) -> u64 {
self.0
}
}

impl Default for L2ChainId {
fn default() -> Self {
Self(270)
}
}

impl TryFrom<u64> for L2ChainId {
type Error = String;

fn try_from(val: u64) -> Result<Self, Self::Error> {
if val > L2ChainId::max().0 {
return Err(format!(
"Cannot convert given value {} into L2ChainId. It's greater than MAX: {},",
val,
L2ChainId::max().0,
));
}
Ok(Self(val))
}
}

impl From<u32> for L2ChainId {
fn from(value: u32) -> Self {
Self(value as u64)
}
}

basic_type!(
/// zkSync network block sequential index.
MiniblockNumber,
Expand Down Expand Up @@ -112,12 +191,6 @@ basic_type!(
u64
);

basic_type!(
/// ChainId in the ZkSync network.
L2ChainId,
u16
);

#[allow(clippy::derivable_impls)]
impl Default for MiniblockNumber {
fn default() -> Self {
Expand All @@ -139,15 +212,78 @@ impl Default for L1BlockNumber {
}
}

impl Default for L2ChainId {
fn default() -> Self {
Self(270)
}
}

#[allow(clippy::derivable_impls)]
impl Default for PriorityOpId {
fn default() -> Self {
Self(0)
}
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json::from_str;

#[test]
fn test_from_str_valid_decimal() {
let input = "42";
let result = L2ChainId::from_str(input);
assert_eq!(result.unwrap().as_u64(), 42);
}

#[test]
fn test_from_str_valid_hexadecimal() {
let input = "0x2A";
let result = L2ChainId::from_str(input);
assert_eq!(result.unwrap().as_u64(), 42);
}

#[test]
fn test_from_str_too_big_chain_id() {
let input = "18446744073709551615"; // 2^64 - 1
let result = L2ChainId::from_str(input);
assert_eq!(
result,
Err(format!("Too big chain ID. MAX: {}", L2ChainId::max().0))
);
}

#[test]
fn test_from_str_invalid_input() {
let input = "invalid"; // Invalid input that cannot be parsed as a number
let result = L2ChainId::from_str(input);

assert!(result.is_err());
assert!(result
.unwrap_err()
.contains("Failed to parse L2ChainId: Err "));
}

#[test]
fn test_deserialize_valid_decimal() {
let input_json = "\"42\"";

let result: Result<L2ChainId, _> = from_str(input_json);
assert_eq!(result.unwrap().as_u64(), 42);
}

#[test]
fn test_deserialize_valid_hex() {
let input_json = "\"0x2A\"";

let result: Result<L2ChainId, _> = from_str(input_json);
assert_eq!(result.unwrap().as_u64(), 42);
}

#[test]
fn test_deserialize_invalid() {
let input_json = "\"invalid\"";

let result: Result<L2ChainId, serde_json::Error> = from_str(input_json);
assert!(result.is_err());
assert!(result
.unwrap_err()
.to_string()
.contains("Failed to parse L2ChainId: Err Invalid character "));
}
}
6 changes: 3 additions & 3 deletions core/lib/config/src/configs/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::Deserialize;
use std::time::Duration;
// Local uses
use zksync_basic_types::network::Network;
use zksync_basic_types::{Address, H256};
use zksync_basic_types::{Address, L2ChainId, H256};
use zksync_contracts::BaseSystemContractsHashes;

use super::envy_load;
Expand Down Expand Up @@ -47,7 +47,7 @@ pub struct NetworkConfig {
pub zksync_network: String,
/// ID of current zkSync network treated as ETH network ID.
/// Used to distinguish zkSync from other Web3-capable networks.
pub zksync_network_id: u16,
pub zksync_network_id: L2ChainId,
}

impl NetworkConfig {
Expand Down Expand Up @@ -202,7 +202,7 @@ mod tests {
network: NetworkConfig {
network: "localhost".parse().unwrap(),
zksync_network: "localhost".to_string(),
zksync_network_id: 270,
zksync_network_id: L2ChainId::from(270),
},
state_keeper: StateKeeperConfig {
transaction_slots: 50,
Expand Down
4 changes: 2 additions & 2 deletions core/lib/dal/src/blocks_web3_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ mod tests {
for block_id in block_ids {
let block = conn
.blocks_web3_dal()
.get_block_by_web3_block_id(block_id, false, L2ChainId(270))
.get_block_by_web3_block_id(block_id, false, L2ChainId::from(270))
.await;
let block = block.unwrap().unwrap();
assert!(block.transactions.is_empty());
Expand All @@ -650,7 +650,7 @@ mod tests {
for block_id in non_existing_block_ids {
let block = conn
.blocks_web3_dal()
.get_block_by_web3_block_id(block_id, false, L2ChainId(270))
.get_block_by_web3_block_id(block_id, false, L2ChainId::from(270))
.await;
assert!(block.unwrap().is_none());

Expand Down
4 changes: 2 additions & 2 deletions core/lib/dal/src/models/storage_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ impl<'r> FromRow<'r, PgRow> for StorageApiTransaction {
.unwrap_or_default()
.map(U64::from),
access_list: None,
chain_id: U256::from(0),
chain_id: 0,
l1_batch_number: db_row
.try_get::<i64, &str>("l1_batch_number_tx")
.ok()
Expand Down Expand Up @@ -502,7 +502,7 @@ pub fn web3_transaction_select_sql() -> &'static str {

pub fn extract_web3_transaction(db_row: PgRow, chain_id: L2ChainId) -> api::Transaction {
let mut storage_api_tx = StorageApiTransaction::from_row(&db_row).unwrap();
storage_api_tx.inner_api_transaction.chain_id = U256::from(chain_id.0);
storage_api_tx.inner_api_transaction.chain_id = chain_id.as_u64();
storage_api_tx.into()
}

Expand Down
2 changes: 1 addition & 1 deletion core/lib/dal/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub(crate) fn mock_l2_transaction() -> L2Tx {
zksync_types::Nonce(0),
fee,
Default::default(),
L2ChainId(270),
L2ChainId::from(270),
&H256::random(),
None,
Default::default(),
Expand Down
6 changes: 3 additions & 3 deletions core/lib/dal/src/transactions_web3_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ mod tests {
for transaction_id in transaction_ids {
let web3_tx = conn
.transactions_web3_dal()
.get_transaction(transaction_id, L2ChainId(270))
.get_transaction(transaction_id, L2ChainId::from(270))
.await;
let web3_tx = web3_tx.unwrap().unwrap();
assert_eq!(web3_tx.hash, tx_hash);
Expand All @@ -431,7 +431,7 @@ mod tests {
for transaction_id in transactions_with_bogus_index {
let web3_tx = conn
.transactions_web3_dal()
.get_transaction(transaction_id, L2ChainId(270))
.get_transaction(transaction_id, L2ChainId::from(270))
.await;
assert!(web3_tx.unwrap().is_none());
}
Expand All @@ -448,7 +448,7 @@ mod tests {
for transaction_id in transactions_with_bogus_block {
let web3_tx = conn
.transactions_web3_dal()
.get_transaction(transaction_id, L2ChainId(270))
.get_transaction(transaction_id, L2ChainId::from(270))
.await;
assert!(web3_tx.unwrap().is_none());
}
Expand Down
4 changes: 2 additions & 2 deletions core/lib/state/src/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use zksync_types::{
use zksync_utils::u256_to_h256;

/// Network ID we use by defailt for in memory storage.
pub const IN_MEMORY_STORAGE_DEFAULT_NETWORK_ID: u16 = 270;
pub const IN_MEMORY_STORAGE_DEFAULT_NETWORK_ID: u32 = 270;

/// In-memory storage.
#[derive(Debug, Default)]
Expand All @@ -22,7 +22,7 @@ impl InMemoryStorage {
/// Constructs a storage that contains system smart contracts.
pub fn with_system_contracts(bytecode_hasher: impl Fn(&[u8]) -> H256) -> Self {
Self::with_system_contracts_and_chain_id(
L2ChainId(IN_MEMORY_STORAGE_DEFAULT_NETWORK_ID),
L2ChainId::from(IN_MEMORY_STORAGE_DEFAULT_NETWORK_ID),
bytecode_hasher,
)
}
Expand Down
2 changes: 1 addition & 1 deletion core/lib/test_account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Account {
nonce,
fee.unwrap_or_else(|| self.default_fee()),
value,
L2ChainId(270),
L2ChainId::default(),
&self.private_key,
factory_deps,
Default::default(),
Expand Down
2 changes: 1 addition & 1 deletion core/lib/types/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ pub struct Transaction {
pub max_priority_fee_per_gas: Option<U256>,
/// Id of the current chain
#[serde(rename = "chainId")]
pub chain_id: U256,
pub chain_id: u64,
/// Number of the l1 batch this transaction was included within.
#[serde(
rename = "l1BatchNumber",
Expand Down
Loading

0 comments on commit f14bf68

Please sign in to comment.