diff --git a/.changes/fixed/3187.md b/.changes/fixed/3187.md new file mode 100644 index 00000000000..02db15c563c --- /dev/null +++ b/.changes/fixed/3187.md @@ -0,0 +1 @@ +Use `String` for block type in GraphQL response extensions \ No newline at end of file diff --git a/crates/client/src/reqwest_ext.rs b/crates/client/src/reqwest_ext.rs index 100dd4da8f7..9341806231c 100644 --- a/crates/client/src/reqwest_ext.rs +++ b/crates/client/src/reqwest_ext.rs @@ -10,10 +10,18 @@ use fuel_core_types::{ }, fuel_types::BlockHeight, }; +use serde::{ + Deserialize, + de::{ + self, + Deserializer, + }, +}; use std::{ future::Future, marker::PhantomData, pin::Pin, + str::FromStr, }; #[derive(Debug, Clone, serde::Serialize)] @@ -24,6 +32,7 @@ pub struct ExtensionsRequest { #[derive(Debug, Clone, serde::Deserialize)] pub struct ExtensionsResponse { pub required_fuel_block_height: Option, + #[serde(default, deserialize_with = "deserialize_block_height_option")] pub current_fuel_block_height: Option, pub fuel_block_height_precondition_failed: Option, pub current_stf_version: Option, @@ -58,6 +67,22 @@ impl FuelOperation { } } +fn deserialize_block_height_option<'de, D>( + deserializer: D, +) -> Result, D::Error> +where + D: Deserializer<'de>, +{ + let value = Option::::deserialize(deserializer)?; + match value { + None => Ok(None), + Some(value) => { + let height = BlockHeight::from_str(&value).map_err(de::Error::custom)?; + Ok(Some(height)) + } + } +} + #[cfg(not(target_arch = "wasm32"))] type BoxFuture<'a, T> = Pin + Send + 'a>>; diff --git a/crates/fuel-core/src/graphql_api/extensions/chain_state_info.rs b/crates/fuel-core/src/graphql_api/extensions/chain_state_info.rs index 967635992a3..d41436ab923 100644 --- a/crates/fuel-core/src/graphql_api/extensions/chain_state_info.rs +++ b/crates/fuel-core/src/graphql_api/extensions/chain_state_info.rs @@ -118,11 +118,8 @@ fn set_current_state( Value::Number(current_stf_version.into()), ); - let current_block_height: u32 = *current_block_height; - extensions.set( - CURRENT_FUEL_BLOCK_HEIGHT, - Value::Number(current_block_height.into()), - ); + let block_height_str = block_to_trimmed_string(current_block_height); + extensions.set(CURRENT_FUEL_BLOCK_HEIGHT, Value::String(block_height_str)); } trait SetExtensionsResponse { @@ -141,6 +138,21 @@ impl SetExtensionsResponse for BTreeMap { } } +fn block_to_trimmed_string(block_height: BlockHeight) -> String { + let mut block_height_str = block_height.to_string(); + + // trim leading zeros + let cut = block_height_str + .as_bytes() + .iter() + .position(|&b| b != b'0') + // if it's all zeros, keep a single "0" + .unwrap_or(block_height_str.len().saturating_sub(1)); + + block_height_str.drain(..cut); + block_height_str +} + #[cfg(test)] mod tests { use super::*;