-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Use string in extensions Block Height to be consistent #3187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 5 commits
d3b2990
a0043c0
e2260ea
765f693
3612ec7
6ca4a2d
8e62650
b669b67
228516c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Use `String` for block type in GraphQL response extensions |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,13 @@ use fuel_core_types::{ | |
| }, | ||
| fuel_types::BlockHeight, | ||
| }; | ||
| use serde::{ | ||
| Deserialize, | ||
| de::{ | ||
| self, | ||
| Deserializer, | ||
| }, | ||
| }; | ||
| use std::{ | ||
| future::Future, | ||
| marker::PhantomData, | ||
|
|
@@ -24,6 +31,7 @@ pub struct ExtensionsRequest { | |
| #[derive(Debug, Clone, serde::Deserialize)] | ||
| pub struct ExtensionsResponse { | ||
| pub required_fuel_block_height: Option<BlockHeight>, | ||
| #[serde(deserialize_with = "deserialize_block_height_option")] | ||
| pub current_fuel_block_height: Option<BlockHeight>, | ||
| pub fuel_block_height_precondition_failed: Option<bool>, | ||
| pub current_stf_version: Option<StateTransitionBytecodeVersion>, | ||
|
|
@@ -58,6 +66,27 @@ impl<Operation> FuelOperation<Operation> { | |
| } | ||
| } | ||
|
|
||
| fn deserialize_block_height_option<'de, D>( | ||
| deserializer: D, | ||
| ) -> Result<Option<BlockHeight>, D::Error> | ||
| where | ||
| D: Deserializer<'de>, | ||
| { | ||
| let value = Option::<String>::deserialize(deserializer)?; | ||
| match value { | ||
| None => Ok(None), | ||
| Some(value) => { | ||
| let parsed = value.parse::<u32>().map_err(|_| { | ||
|
||
| de::Error::invalid_value( | ||
| de::Unexpected::Str(&value), | ||
| &"a numeric block height string", | ||
| ) | ||
| })?; | ||
| Ok(Some(BlockHeight::new(parsed))) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(not(target_arch = "wasm32"))] | ||
| type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,11 +118,8 @@ fn set_current_state<E>( | |
| 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<String, Value> { | |
| } | ||
| } | ||
|
|
||
| fn block_to_trimmed_string(block_height: BlockHeight) -> String { | ||
| let mut block_height_str = u32::from(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 | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary leading zero trimming is dead codeLow Severity The
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... that's not what I was getting. I was getting leading zeros and that's why I added it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trimmed hex string breaks BlockHeight deserialization roundtripHigh Severity
Additional Locations (1) |
||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.