From ef72ca7ff439c596702cd78bc1b74fca7fb99c6c Mon Sep 17 00:00:00 2001 From: Pia Date: Fri, 26 Jul 2024 11:42:50 -0400 Subject: [PATCH] hotfix: hex string to mmr id --- crates/primitives/src/block/header.rs | 8 ++++---- crates/primitives/src/processed_types/mmr.rs | 15 ++------------- crates/primitives/src/utils.rs | 11 +++++++++++ 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/crates/primitives/src/block/header.rs b/crates/primitives/src/block/header.rs index dc27ef9c..8426d93c 100644 --- a/crates/primitives/src/block/header.rs +++ b/crates/primitives/src/block/header.rs @@ -7,7 +7,7 @@ use alloy::{ use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable}; use serde::{Deserialize, Serialize}; -use crate::processed_types::mmr::MMRMeta; +use crate::{processed_types::mmr::MMRMeta, utils::hex_string_to_uint}; // ============================================================================= // Header (credit: https://github.com/paradigmxyz/reth/blob/main/crates/primitives/src/header.rs#L133) @@ -441,7 +441,7 @@ pub struct MMRDataFromIndexer { #[derive(Serialize, Deserialize, Debug, Clone)] pub struct MMRMetaFromIndexer { - pub mmr_id: u64, + pub mmr_id: String, pub mmr_peaks: Vec, pub mmr_root: String, pub mmr_size: u64, @@ -450,7 +450,7 @@ pub struct MMRMetaFromIndexer { impl From<&MMRMetaFromIndexer> for MMRMeta { fn from(value: &MMRMetaFromIndexer) -> Self { Self { - id: value.mmr_id, + id: hex_string_to_uint(&value.mmr_id), peaks: value.mmr_peaks.clone(), root: value.mmr_root.to_string(), size: value.mmr_size, @@ -483,7 +483,7 @@ pub struct MMRDataFromNewIndexer { #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub struct MMRMetaFromNewIndexer { - pub mmr_id: u64, + pub mmr_id: String, pub mmr_peaks: Vec, pub mmr_root: String, pub mmr_size: u64, diff --git a/crates/primitives/src/processed_types/mmr.rs b/crates/primitives/src/processed_types/mmr.rs index 403f319e..d01cfb3b 100644 --- a/crates/primitives/src/processed_types/mmr.rs +++ b/crates/primitives/src/processed_types/mmr.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use crate::block::header::MMRMetaFromNewIndexer; +use crate::{block::header::MMRMetaFromNewIndexer, utils::hex_string_to_uint}; #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Eq, Hash)] pub struct MMRMeta { @@ -22,21 +22,10 @@ impl MMRMeta { } } -impl From for MMRMetaFromNewIndexer { - fn from(val: MMRMeta) -> Self { - MMRMetaFromNewIndexer { - mmr_id: val.id, - mmr_root: val.root, - mmr_size: val.size, - mmr_peaks: val.peaks, - } - } -} - impl From for MMRMeta { fn from(val: MMRMetaFromNewIndexer) -> Self { MMRMeta { - id: val.mmr_id, + id: hex_string_to_uint(&val.mmr_id), root: val.mmr_root, size: val.mmr_size, peaks: val.mmr_peaks, diff --git a/crates/primitives/src/utils.rs b/crates/primitives/src/utils.rs index fe2eddbd..1178cf9a 100644 --- a/crates/primitives/src/utils.rs +++ b/crates/primitives/src/utils.rs @@ -44,6 +44,11 @@ pub fn tx_index_to_tx_key(tx_index: u64) -> String { format!("0x{}", hex::encode(binding)) } +pub fn hex_string_to_uint(hex_string: &str) -> u64 { + let hex_string = hex_string.trim_start_matches("0x"); + u64::from_str_radix(hex_string, 16).expect("Cannot convert hex string to uint") +} + #[cfg(test)] mod tests { use super::*; @@ -122,4 +127,10 @@ mod tests { assert_eq!(tx_key, expected_tx_key); } + #[test] + fn test_hex_string_to_uint() { + let hex_string = "0x1b"; + let result = hex_string_to_uint(hex_string); + assert_eq!(result, 27); + } }