Skip to content

Commit

Permalink
Use into() to get BlockHashOrNumber (#997)
Browse files Browse the repository at this point in the history
Use into() to get BlockHashOrNumber
  • Loading branch information
tcoratger authored Apr 22, 2024
1 parent a9e0b78 commit 6479457
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions src/eth_provider/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,7 @@ where
async fn header(&self, block_id: &BlockId) -> EthProviderResult<Option<Header>> {
let block = match block_id {
BlockId::Hash(hash) => BlockHashOrNumber::Hash((*hash).into()),
BlockId::Number(number_or_tag) => {
BlockHashOrNumber::Number(self.tag_into_block_number(*number_or_tag).await?.to())
}
BlockId::Number(number_or_tag) => self.tag_into_block_number(*number_or_tag).await?.to::<u64>().into(),
};

Ok(self.header(block).await?.map(|h| h.header))
Expand Down Expand Up @@ -197,7 +195,7 @@ where
}

async fn block_by_hash(&self, hash: B256, full: bool) -> EthProviderResult<Option<RichBlock>> {
Ok(self.block(BlockHashOrNumber::Hash(hash), full).await?)
Ok(self.block(hash.into(), full).await?)
}

async fn block_by_number(
Expand All @@ -206,11 +204,11 @@ where
full: bool,
) -> EthProviderResult<Option<RichBlock>> {
let block_number = self.tag_into_block_number(number_or_tag).await?;
Ok(self.block(BlockHashOrNumber::Number(block_number.to::<u64>()), full).await?)
Ok(self.block(block_number.to::<u64>().into(), full).await?)
}

async fn block_transaction_count_by_hash(&self, hash: B256) -> EthProviderResult<Option<U256>> {
Ok(if self.block_exists(BlockHashOrNumber::Hash(hash)).await? {
Ok(if self.block_exists(hash.into()).await? {
Some(U256::from(
self.database.count::<StoredTransaction>(into_filter("tx.blockHash", &hash, HASH_PADDING)).await?,
))
Expand All @@ -224,7 +222,7 @@ where
number_or_tag: BlockNumberOrTag,
) -> EthProviderResult<Option<U256>> {
let block_number = self.tag_into_block_number(number_or_tag).await?;
let block_exists = self.block_exists(BlockHashOrNumber::Number(block_number.to::<u64>())).await?;
let block_exists = self.block_exists(block_number.to::<u64>().into()).await?;
if !block_exists {
return Ok(None);
}
Expand Down Expand Up @@ -591,7 +589,7 @@ where
match block_id {
BlockId::Number(maybe_number) => {
let block_number = self.tag_into_block_number(maybe_number).await?;
let block_exists = self.block_exists(BlockHashOrNumber::Number(block_number.to())).await?;
let block_exists = self.block_exists(block_number.to::<u64>().into()).await?;
if !block_exists {
return Ok(None);
}
Expand All @@ -601,7 +599,7 @@ where
Ok(Some(tx.into_iter().map(Into::into).collect()))
}
BlockId::Hash(hash) => {
let block_exists = self.block_exists(BlockHashOrNumber::Hash(hash.block_hash)).await?;
let block_exists = self.block_exists(hash.block_hash.into()).await?;
if !block_exists {
return Ok(None);
}
Expand All @@ -618,10 +616,8 @@ where
block_id: Option<BlockId>,
) -> EthProviderResult<Option<Vec<reth_rpc_types::Transaction>>> {
let block_id = match block_id.unwrap_or(BlockId::Number(BlockNumberOrTag::Latest)) {
BlockId::Number(maybe_number) => {
BlockHashOrNumber::Number(self.tag_into_block_number(maybe_number).await?.to())
}
BlockId::Hash(hash) => BlockHashOrNumber::Hash(hash.block_hash),
BlockId::Number(maybe_number) => self.tag_into_block_number(maybe_number).await?.to::<u64>().into(),
BlockId::Hash(hash) => hash.block_hash.into(),
};
if !self.block_exists(block_id).await? {
return Ok(None);
Expand Down Expand Up @@ -867,10 +863,7 @@ where
// 3. The block number is not found, then we return an error
match number_or_tag {
BlockNumberOrTag::Number(number) => {
let header = self
.header(BlockHashOrNumber::Number(number))
.await?
.ok_or(EthApiError::UnknownBlockNumber)?;
let header = self.header(number.into()).await?.ok_or(EthApiError::UnknownBlockNumber)?;
// If the block hash is zero, then the block corresponds to a Starknet pending block
if header.header.hash.ok_or(EthApiError::UnknownBlock)?.is_zero() {
Ok(starknet::core::types::BlockId::Tag(starknet::core::types::BlockTag::Pending))
Expand Down

0 comments on commit 6479457

Please sign in to comment.