Skip to content

Commit

Permalink
fix: minor bug while trimming hash, test added (#435)
Browse files Browse the repository at this point in the history
Co-authored-by: mohiiit <[email protected]>
  • Loading branch information
Mohiiit and mohiiit authored Dec 23, 2024
1 parent 80c5ea2 commit 47b5031
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next release

- 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
Expand Down
6 changes: 2 additions & 4 deletions crates/client/eth/src/state_update.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
16 changes: 0 additions & 16 deletions crates/client/eth/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion crates/client/sync/src/l2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion crates/client/sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockImporter>,
Expand Down
13 changes: 0 additions & 13 deletions crates/client/sync/src/utils.rs

This file was deleted.

39 changes: 39 additions & 0 deletions crates/primitives/utils/src/hash.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 3 additions & 1 deletion crates/primitives/utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 47b5031

Please sign in to comment.