Skip to content

Commit

Permalink
fix: use u64 for block numbers (#38)
Browse files Browse the repository at this point in the history
* fix: serialize block num w/o leading zeros

* fix: use `u64` for block numbers
  • Loading branch information
onbjerg authored Nov 21, 2023
1 parent 8a5f495 commit 5789288
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions crates/providers/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub trait TempProvider: Send + Sync {
Self: Sync;

/// Gets the last block number available.
async fn get_block_number(&self) -> TransportResult<U64>
async fn get_block_number(&self) -> TransportResult<u64>
where
Self: Sync;

Expand Down Expand Up @@ -289,7 +289,7 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
}

/// Gets the last block number available.
async fn get_block_number(&self) -> TransportResult<U64>
async fn get_block_number(&self) -> TransportResult<u64>
where
Self: Sync,
{
Expand Down Expand Up @@ -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]
Expand Down
14 changes: 7 additions & 7 deletions crates/rpc-types/src/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<U64> {
pub const fn as_number(&self) -> Option<u64> {
match *self {
BlockNumberOrTag::Number(num) => Some(num),
_ => None,
Expand Down Expand Up @@ -292,13 +292,13 @@ impl BlockNumberOrTag {

impl From<u64> for BlockNumberOrTag {
fn from(num: u64) -> Self {
BlockNumberOrTag::Number(U64::from(num))
BlockNumberOrTag::Number(num)
}
}

impl From<U64> for BlockNumberOrTag {
fn from(num: U64) -> Self {
BlockNumberOrTag::Number(num)
num.to::<u64>().into()
}
}

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -420,13 +420,13 @@ impl BlockId {

impl From<u64> for BlockId {
fn from(num: u64) -> Self {
BlockNumberOrTag::Number(U64::from(num)).into()
BlockNumberOrTag::Number(num).into()
}
}

impl From<U64> for BlockId {
fn from(num: U64) -> Self {
BlockNumberOrTag::Number(num).into()
BlockNumberOrTag::Number(num.to()).into()
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/rpc-types/src/eth/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,12 @@ impl Filter {
}

/// Returns the numeric value of the `toBlock` field
pub fn get_to_block(&self) -> Option<U64> {
pub fn get_to_block(&self) -> Option<u64> {
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<U64> {
pub fn get_from_block(&self) -> Option<u64> {
self.block_option.get_from_block().and_then(|b| b.as_number())
}

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 5789288

Please sign in to comment.