From 6751b630bdf86da70486aceda76e17a98814fc25 Mon Sep 17 00:00:00 2001 From: Oliver Nordbjerg Date: Tue, 21 Nov 2023 03:22:59 +0100 Subject: [PATCH 1/2] fix: serialize block num w/o leading zeros --- crates/rpc-types/src/eth/block.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/rpc-types/src/eth/block.rs b/crates/rpc-types/src/eth/block.rs index 8135a693e63..648cad252a1 100644 --- a/crates/rpc-types/src/eth/block.rs +++ b/crates/rpc-types/src/eth/block.rs @@ -308,7 +308,9 @@ impl Serialize for BlockNumberOrTag { S: Serializer, { match *self { - BlockNumberOrTag::Number(ref x) => serializer.serialize_str(&format!("0x{x:x}")), + BlockNumberOrTag::Number(ref x) => { + serializer.serialize_str(&format!("0x{:x}", x.to::())) + }, BlockNumberOrTag::Latest => serializer.serialize_str("latest"), BlockNumberOrTag::Finalized => serializer.serialize_str("finalized"), BlockNumberOrTag::Safe => serializer.serialize_str("safe"), From 1e2fbd12b82b6c3c7ed4c5dce8798910dc3f85ba Mon Sep 17 00:00:00 2001 From: Oliver Nordbjerg Date: Tue, 21 Nov 2023 03:48:55 +0100 Subject: [PATCH 2/2] fix: use `u64` for block numbers --- crates/providers/src/provider.rs | 6 +++--- crates/rpc-types/src/eth/block.rs | 18 ++++++++---------- crates/rpc-types/src/eth/filter.rs | 6 +++--- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/crates/providers/src/provider.rs b/crates/providers/src/provider.rs index 5b5b5c8f50c..1d7e335f1b2 100644 --- a/crates/providers/src/provider.rs +++ b/crates/providers/src/provider.rs @@ -51,7 +51,7 @@ pub trait TempProvider: Send + Sync { Self: Sync; /// Gets the last block number available. - async fn get_block_number(&self) -> TransportResult + async fn get_block_number(&self) -> TransportResult where Self: Sync; @@ -289,7 +289,7 @@ impl TempProvider for Provider { } /// Gets the last block number available. - async fn get_block_number(&self) -> TransportResult + async fn get_block_number(&self) -> TransportResult where Self: Sync, { @@ -718,7 +718,7 @@ mod providers_test { let anvil = Anvil::new().spawn(); let provider = Provider::try_from(&anvil.endpoint()).unwrap(); let num = provider.get_block_number().await.unwrap(); - assert_eq!(U64::ZERO, num) + assert_eq!(0, num) } #[tokio::test] diff --git a/crates/rpc-types/src/eth/block.rs b/crates/rpc-types/src/eth/block.rs index 648cad252a1..452f53474d5 100644 --- a/crates/rpc-types/src/eth/block.rs +++ b/crates/rpc-types/src/eth/block.rs @@ -247,12 +247,12 @@ pub enum BlockNumberOrTag { /// Pending block (not yet part of the blockchain) Pending, /// Block by number from canon chain - Number(U64), + Number(u64), } impl BlockNumberOrTag { /// Returns the numeric block number if explicitly set - pub const fn as_number(&self) -> Option { + pub const fn as_number(&self) -> Option { match *self { BlockNumberOrTag::Number(num) => Some(num), _ => None, @@ -292,13 +292,13 @@ impl BlockNumberOrTag { impl From for BlockNumberOrTag { fn from(num: u64) -> Self { - BlockNumberOrTag::Number(U64::from(num)) + BlockNumberOrTag::Number(num) } } impl From for BlockNumberOrTag { fn from(num: U64) -> Self { - BlockNumberOrTag::Number(num) + num.to::().into() } } @@ -308,9 +308,7 @@ impl Serialize for BlockNumberOrTag { S: Serializer, { match *self { - BlockNumberOrTag::Number(ref x) => { - serializer.serialize_str(&format!("0x{:x}", x.to::())) - }, + BlockNumberOrTag::Number(ref x) => serializer.serialize_str(&format!("0x{x:x}")), BlockNumberOrTag::Latest => serializer.serialize_str("latest"), BlockNumberOrTag::Finalized => serializer.serialize_str("finalized"), BlockNumberOrTag::Safe => serializer.serialize_str("safe"), @@ -342,7 +340,7 @@ impl FromStr for BlockNumberOrTag { "pending" => Self::Pending, _number => { if let Some(hex_val) = s.strip_prefix("0x") { - let number = U64::from_str_radix(hex_val, 16); + let number = u64::from_str_radix(hex_val, 16); BlockNumberOrTag::Number(number?) } else { return Err(HexStringMissingPrefixError::default().into()); @@ -420,13 +418,13 @@ impl BlockId { impl From for BlockId { fn from(num: u64) -> Self { - BlockNumberOrTag::Number(U64::from(num)).into() + BlockNumberOrTag::Number(num).into() } } impl From for BlockId { fn from(num: U64) -> Self { - BlockNumberOrTag::Number(num).into() + BlockNumberOrTag::Number(num.to()).into() } } diff --git a/crates/rpc-types/src/eth/filter.rs b/crates/rpc-types/src/eth/filter.rs index 6c714e05d0e..d61b10e11b6 100644 --- a/crates/rpc-types/src/eth/filter.rs +++ b/crates/rpc-types/src/eth/filter.rs @@ -455,12 +455,12 @@ impl Filter { } /// Returns the numeric value of the `toBlock` field - pub fn get_to_block(&self) -> Option { + pub fn get_to_block(&self) -> Option { self.block_option.get_to_block().and_then(|b| b.as_number()) } /// Returns the numeric value of the `fromBlock` field - pub fn get_from_block(&self) -> Option { + pub fn get_from_block(&self) -> Option { self.block_option.get_from_block().and_then(|b| b.as_number()) } @@ -759,7 +759,7 @@ impl FilteredParams { } /// Returns true if the filter matches the given block number - pub fn filter_block_range(&self, block_number: U64) -> bool { + pub fn filter_block_range(&self, block_number: u64) -> bool { if self.filter.is_none() { return true; }