Skip to content

Commit

Permalink
hotfix: hex string to mmr id
Browse files Browse the repository at this point in the history
  • Loading branch information
rkdud007 committed Jul 26, 2024
1 parent e144ee2 commit ef72ca7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions crates/primitives/src/block/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<String>,
pub mmr_root: String,
pub mmr_size: u64,
Expand All @@ -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,
Expand Down Expand Up @@ -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<String>,
pub mmr_root: String,
pub mmr_size: u64,
Expand Down
15 changes: 2 additions & 13 deletions crates/primitives/src/processed_types/mmr.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -22,21 +22,10 @@ impl MMRMeta {
}
}

impl From<MMRMeta> 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<MMRMetaFromNewIndexer> 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,
Expand Down
11 changes: 11 additions & 0 deletions crates/primitives/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit ef72ca7

Please sign in to comment.