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: change chainId to u64 #167

Merged
merged 10 commits into from
Oct 6, 2023
5 changes: 3 additions & 2 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(),
);
)
.unwrap();
let l1_chain_id = L1ChainId(
client
.l1_chain_id()
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
31 changes: 12 additions & 19 deletions core/lib/basic_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,15 @@ impl TryFrom<U256> for AccountTreeId {

/// ChainId in the ZkSync network.
#[derive(Copy, Clone, Debug, Serialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct L2ChainId(pub u64);
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)?;

// Parse the string as a U256
// try to parse as decimal first
let u64 = match U64::from_dec_str(&s) {
Ok(u) => u,
Err(_) => {
// try to parse as hex
s.parse::<U64>().map_err(de::Error::custom)?
}
};

if u64.as_u64() > L2ChainId::max().0 {
return Err(de::Error::custom(format!(
"Too big chain ID. MAX: {}",
L2ChainId::max().0
)));
}
Ok(L2ChainId(u64.as_u64()))
s.parse().map_err(de::Error::custom)
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -139,6 +122,10 @@ impl L2ChainId {
pub fn max() -> Self {
Self(Self::MAX)
}

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

impl Default for L2ChainId {
Expand All @@ -162,6 +149,12 @@ impl TryFrom<u64> for L2ChainId {
}
}

impl From<u32> for L2ChainId {
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
fn from(value: u32) -> Self {
Self(value as u64)
}
}

basic_type!(
/// zkSync network block sequential index.
MiniblockNumber,
Expand Down
2 changes: 1 addition & 1 deletion core/lib/config/src/configs/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ mod tests {
network: NetworkConfig {
network: "localhost".parse().unwrap(),
zksync_network: "localhost".to_string(),
zksync_network_id: zksync_basic_types::L2ChainId(270),
zksync_network_id: zksync_basic_types::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
2 changes: 1 addition & 1 deletion core/lib/dal/src/models/storage_transaction.rs
Original file line number Diff line number Diff line change
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 = 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: u64 = 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/l2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl L2Tx {
pub fn get_rlp_bytes(&self, chain_id: L2ChainId) -> Bytes {
let mut rlp_stream = RlpStream::new();
let tx: TransactionRequest = self.clone().into();
tx.rlp(&mut rlp_stream, chain_id.0, None);
tx.rlp(&mut rlp_stream, chain_id.as_u64(), None);
Bytes(rlp_stream.as_raw().to_vec())
}

Expand Down
2 changes: 1 addition & 1 deletion core/lib/types/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub fn get_system_context_init_logs(chain_id: L2ChainId) -> Vec<StorageLog> {
vec![
StorageLog::new_write_log(
get_system_context_key(SYSTEM_CONTEXT_CHAIN_ID_POSITION),
H256::from_low_u64_be(chain_id.0),
H256::from_low_u64_be(chain_id.as_u64()),
),
StorageLog::new_write_log(
get_system_context_key(SYSTEM_CONTEXT_BLOCK_GAS_LIMIT_POSITION),
Expand Down
Loading
Loading