From 57892886c9f025893cd64cabc017efd179a0caed Mon Sep 17 00:00:00 2001 From: Bjerg Date: Tue, 21 Nov 2023 03:53:44 +0100 Subject: [PATCH] fix: use u64 for block numbers (#38) * fix: serialize block num w/o leading zeros * fix: use `u64` for block numbers --- crates/providers/src/provider.rs | 6 +++--- crates/rpc-types/src/eth/block.rs | 14 +++++++------- crates/rpc-types/src/eth/filter.rs | 6 +++--- 3 files changed, 13 insertions(+), 13 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..9819935a5aa 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() } } @@ -342,7 +342,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 +420,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; }