diff --git a/CHANGELOG.md b/CHANGELOG.md index 634b5dbfe..3319bd0a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Next release - chore: remove redundant dead_code attribute +- fix: trim hash of eth state was failing with 0x0 - fix: devnet accounts getting deployed in sequencer mode - fix(rpc): fix BroadcastedDeclareTxn V3 in starknet-types-rpc - fix: oracle need condition diff --git a/crates/client/eth/src/state_update.rs b/crates/client/eth/src/state_update.rs index c38bce421..2164f9bf4 100644 --- a/crates/client/eth/src/state_update.rs +++ b/crates/client/eth/src/state_update.rs @@ -1,14 +1,12 @@ use std::sync::Arc; use crate::client::{L1BlockMetrics, StarknetCoreContract}; -use crate::{ - client::EthereumClient, - utils::{convert_log_state_update, trim_hash}, -}; +use crate::{client::EthereumClient, utils::convert_log_state_update}; use anyhow::Context; use futures::StreamExt; use mc_db::MadaraBackend; use mp_utils::service::ServiceContext; +use mp_utils::trim_hash; use serde::Deserialize; use starknet_types_core::felt::Felt; diff --git a/crates/client/eth/src/utils.rs b/crates/client/eth/src/utils.rs index 8fc3bf965..07f0b4b90 100644 --- a/crates/client/eth/src/utils.rs +++ b/crates/client/eth/src/utils.rs @@ -32,14 +32,6 @@ pub fn felt_to_u256(felt: Felt) -> U256 { U256::from_be_bytes(felt.to_bytes_be()) } -pub fn trim_hash(hash: &Felt) -> String { - let hash_str = format!("{:#x}", hash); - let hash_len = hash_str.len(); - let prefix = &hash_str[..6 + 2]; - let suffix = &hash_str[hash_len - 6..]; - format!("{}...{}", prefix, suffix) -} - #[cfg(test)] mod eth_client_conversion_tests { use super::*; @@ -86,12 +78,4 @@ mod eth_client_conversion_tests { assert_eq!(result, expected, "u256_to_felt failed for input: {}", input); } - - #[rstest] - #[case(30000000000000, "0x1b48eb...57e000")] - #[case(12345678123456789, "0x2bdc54...0f5915")] - fn trim_hash_works(#[case] input: u128, #[case] expected: &str) { - let trimmed = trim_hash(&Felt::from(input)); - assert_eq!(trimmed, expected); - } } diff --git a/crates/client/sync/src/l2.rs b/crates/client/sync/src/l2.rs index b4ae85ab7..b2a6041c0 100644 --- a/crates/client/sync/src/l2.rs +++ b/crates/client/sync/src/l2.rs @@ -3,7 +3,6 @@ use crate::fetch::fetchers::fetch_pending_block_and_updates; use crate::fetch::fetchers::WarpUpdateConfig; use crate::fetch::l2_fetch_task; use crate::fetch::L2FetchConfig; -use crate::utils::trim_hash; use anyhow::Context; use futures::{stream, StreamExt}; use mc_block_import::{ @@ -17,6 +16,7 @@ use mp_block::BlockId; use mp_block::BlockTag; use mp_gateway::error::SequencerError; use mp_utils::service::ServiceContext; +use mp_utils::trim_hash; use mp_utils::PerfStopwatch; use starknet_api::core::ChainId; use starknet_types_core::felt::Felt; diff --git a/crates/client/sync/src/lib.rs b/crates/client/sync/src/lib.rs index 588ed353b..2cda8af8c 100644 --- a/crates/client/sync/src/lib.rs +++ b/crates/client/sync/src/lib.rs @@ -15,7 +15,6 @@ pub mod l2; pub mod metrics; #[cfg(test)] pub mod tests; -pub mod utils; pub struct SyncConfig { pub block_importer: Arc, diff --git a/crates/client/sync/src/utils.rs b/crates/client/sync/src/utils.rs deleted file mode 100644 index 04330745a..000000000 --- a/crates/client/sync/src/utils.rs +++ /dev/null @@ -1,13 +0,0 @@ -use starknet_types_core::felt::Felt; - -pub fn trim_hash(hash: &Felt) -> String { - let hash_str = format!("{:#x}", hash); - - if hash_str.len() <= 12 { - hash_str.to_string() - } else { - let prefix = &hash_str[..6]; - let suffix = &hash_str[hash_str.len() - 6..]; - format!("{}...{}", prefix, suffix) - } -} diff --git a/crates/primitives/utils/src/hash.rs b/crates/primitives/utils/src/hash.rs new file mode 100644 index 000000000..d88ddfe11 --- /dev/null +++ b/crates/primitives/utils/src/hash.rs @@ -0,0 +1,39 @@ +use starknet_types_core::felt::Felt; + +/// Formats a hash as a shortened hexadecimal string with prefix and suffix. +/// +/// If the hash string is 12 characters or shorter, returns the full string. +/// Otherwise, returns a string in the format "0xabcd...ef1234" where the middle +/// is replaced with "...". +/// +/// # Arguments +/// * `hash` - The Felt hash to format +/// +/// # Returns +/// A formatted string representation of the hash +pub fn trim_hash(hash: &Felt) -> String { + let hash_str = format!("{:#x}", hash); + + if hash_str.len() <= 12 { + hash_str + } else { + let prefix = &hash_str[..6]; + let suffix = &hash_str[hash_str.len() - 6..]; + format!("{}...{}", prefix, suffix) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use rstest::*; + + #[rstest] + #[case(0, "0x0")] + #[case(30000000000000, "0x1b48...57e000")] + #[case(12345678123456789, "0x2bdc...0f5915")] + fn trim_hash_works(#[case] input: u128, #[case] expected: &str) { + let trimmed = trim_hash(&Felt::from(input)); + assert_eq!(trimmed, expected); + } +} diff --git a/crates/primitives/utils/src/lib.rs b/crates/primitives/utils/src/lib.rs index d38d5098c..0e4886a9a 100644 --- a/crates/primitives/utils/src/lib.rs +++ b/crates/primitives/utils/src/lib.rs @@ -1,12 +1,14 @@ #![allow(clippy::new_without_default)] pub mod crypto; +pub mod hash; pub mod parsers; pub mod serde; pub mod service; - use std::time::{Duration, Instant}; +pub use hash::trim_hash; + use tokio::sync::oneshot; /// Prefer this compared to [`tokio::spawn_blocking`], as spawn_blocking creates new OS threads and