Skip to content

Commit

Permalink
use more compact format for alloy BlockId (#1473)
Browse files Browse the repository at this point in the history
use more compact format for alloy BlockId
  • Loading branch information
tcoratger authored Oct 21, 2024
1 parent 439d4e1 commit ec203ad
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/pool/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::providers::eth_provider::{
use alloy_rpc_types::BlockNumberOrTag;
use reth_chainspec::ChainSpec;
use reth_primitives::{
BlockId, GotExpected, InvalidTransactionError, SealedBlock, EIP1559_TX_TYPE_ID, EIP2930_TX_TYPE_ID,
EIP4844_TX_TYPE_ID, LEGACY_TX_TYPE_ID,
GotExpected, InvalidTransactionError, SealedBlock, EIP1559_TX_TYPE_ID, EIP2930_TX_TYPE_ID, EIP4844_TX_TYPE_ID,
LEGACY_TX_TYPE_ID,
};
use reth_revm::DatabaseRef;
use reth_transaction_pool::{
Expand Down Expand Up @@ -314,7 +314,7 @@ where
}

// Fetch the account state for the Pending block
let db = EthDatabase::new(Arc::new(&self.provider), BlockId::from(BlockNumberOrTag::Pending));
let db = EthDatabase::new(Arc::new(&self.provider), BlockNumberOrTag::Pending.into());
let account = match db.basic_ref(transaction.sender()) {
Ok(account) => account.unwrap_or_default(),
Err(err) => return TransactionValidationOutcome::Error(*transaction.hash(), Box::new(err)),
Expand Down
8 changes: 4 additions & 4 deletions src/providers/alchemy_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use auto_impl::auto_impl;
use eyre::Result;
use futures::future::join_all;
use mongodb::bson::doc;
use reth_primitives::{BlockId, BlockNumberOrTag};
use reth_primitives::BlockNumberOrTag;

#[async_trait]
#[auto_impl(Arc, &)]
Expand Down Expand Up @@ -40,7 +40,7 @@ impl<P: EthereumProvider> AlchemyDataProvider<P> {
impl<P: EthereumProvider + Send + Sync + 'static> AlchemyProvider for AlchemyDataProvider<P> {
async fn token_balances(&self, address: Address, contract_addresses: Vec<Address>) -> EthApiResult<TokenBalances> {
// Set the block ID to the latest block
let block_id = BlockId::Number(BlockNumberOrTag::Latest);
let block_id = BlockNumberOrTag::Latest.into();

Ok(TokenBalances {
address,
Expand All @@ -60,7 +60,7 @@ impl<P: EthereumProvider + Send + Sync + 'static> AlchemyProvider for AlchemyDat
/// Retrieves the metadata for a given token.
async fn token_metadata(&self, contract_address: Address) -> EthApiResult<TokenMetadata> {
// Set the block ID to the latest block
let block_id = BlockId::Number(BlockNumberOrTag::Latest);
let block_id = BlockNumberOrTag::Latest.into();
// Create a new instance of `EthereumErc20`
let token = EthereumErc20::new(contract_address, &self.eth_provider);

Expand All @@ -75,7 +75,7 @@ impl<P: EthereumProvider + Send + Sync + 'static> AlchemyProvider for AlchemyDat
/// Retrieves the allowance of a given owner for a spender.
async fn token_allowance(&self, contract_address: Address, owner: Address, spender: Address) -> EthApiResult<U256> {
// Set the block ID to the latest block
let block_id = BlockId::Number(BlockNumberOrTag::Latest);
let block_id = BlockNumberOrTag::Latest.into();
// Create a new instance of `EthereumErc20`
let token = EthereumErc20::new(contract_address, &self.eth_provider);
// Retrieve the allowance for the given owner and spender
Expand Down
4 changes: 2 additions & 2 deletions src/providers/debug_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl<P: EthereumProvider + Send + Sync + 'static> DebugProvider for DebugDataPro
let provider = Arc::new(&self.eth_provider);
let tracer = TracerBuilder::new(provider)
.await?
.with_block_id(BlockId::Number(block_number))
.with_block_id(block_number.into())
.await?
.with_tracing_options(opts.unwrap_or_default().into())
.build()?;
Expand All @@ -201,7 +201,7 @@ impl<P: EthereumProvider + Send + Sync + 'static> DebugProvider for DebugDataPro
) -> EthApiResult<Vec<TraceResult>> {
let tracer = TracerBuilder::new(Arc::new(&self.eth_provider))
.await?
.with_block_id(BlockId::Hash(block_hash.into()))
.with_block_id(block_hash.into())
.await?
.with_tracing_options(opts.unwrap_or_default().into())
.build()?;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/eth_provider/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ where
block_id: Option<BlockId>,
) -> EthApiResult<Option<Vec<WithOtherFields<Transaction>>>> {
let block_hash_or_number = self
.block_id_into_block_number_or_hash(block_id.unwrap_or(BlockId::Number(BlockNumberOrTag::Latest)))
.block_id_into_block_number_or_hash(block_id.unwrap_or_else(|| BlockNumberOrTag::Latest.into()))
.await?;
if !self.database().block_exists(block_hash_or_number).await? {
return Ok(None);
Expand Down
4 changes: 2 additions & 2 deletions src/providers/eth_provider/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ where
#[instrument(skip_all, ret)]
pub async fn to_starknet_block_id(
&self,
block_id: impl Into<Option<BlockId>>,
block_id: Option<BlockId>,
) -> EthApiResult<starknet::core::types::BlockId> {
match block_id.into() {
match block_id {
Some(BlockId::Hash(hash)) => {
Ok(EthBlockId::new(BlockId::Hash(hash)).try_into().map_err(EthereumDataFormatError::from)?)
}
Expand Down
4 changes: 2 additions & 2 deletions src/tracing/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl<P: EthereumProvider + Send + Sync + Clone> TracerBuilder<P, Floating> {
return Err(EthApiError::TransactionNotFound(transaction_hash));
}

self.with_block_id(BlockId::Number(transaction.block_number.unwrap().into())).await
self.with_block_id(transaction.block_number.unwrap().into()).await
}

/// Fetches a block from the Ethereum provider given a block id
Expand Down Expand Up @@ -262,7 +262,7 @@ mod tests {
// Create a TracerBuilder with the mock provider
let builder = TracerBuilder::new(Arc::new(&mock_provider)).await.unwrap();
// Attempt to use the builder with a specific block hash, expecting an error
let result = builder.block(BlockId::Hash(B256::repeat_byte(1).into())).await;
let result = builder.block(B256::repeat_byte(1).into()).await;
// Check that the result is an UnknownBlock error
assert!(matches!(result, Err(EthApiError::UnknownBlock(_))));
}
Expand Down
15 changes: 7 additions & 8 deletions tests/tests/eth_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,20 +682,19 @@ async fn test_to_starknet_block_id(#[future] katana: Katana, _setup: ()) {
let transaction = katana.most_recent_transaction().unwrap();

// When: Convert block number identifier to StarkNet block identifier
let block_id = alloy_rpc_types::BlockId::Number(BlockNumberOrTag::Number(transaction.block_number.unwrap()));
let pending_starknet_block_id = eth_provider.to_starknet_block_id(block_id).await.unwrap();
let pending_starknet_block_id =
eth_provider.to_starknet_block_id(Some(transaction.block_number.unwrap().into())).await.unwrap();

// When: Convert block hash identifier to StarkNet block identifier
let some_block_hash = alloy_rpc_types::BlockId::Hash(RpcBlockHash::from(transaction.block_hash.unwrap()));
let some_starknet_block_hash = eth_provider.to_starknet_block_id(some_block_hash).await.unwrap();
let some_starknet_block_hash =
eth_provider.to_starknet_block_id(Some(transaction.block_hash.unwrap().into())).await.unwrap();

// When: Convert block tag identifier to StarkNet block identifier
let pending_block_tag = alloy_rpc_types::BlockId::Number(BlockNumberOrTag::Pending);
let pending_block_tag_starknet = eth_provider.to_starknet_block_id(pending_block_tag).await.unwrap();
let pending_block_tag_starknet =
eth_provider.to_starknet_block_id(Some(BlockNumberOrTag::Pending.into())).await.unwrap();

// When: Attempt to convert an unknown block number identifier to StarkNet block identifier
let unknown_block_number = alloy_rpc_types::BlockId::Number(BlockNumberOrTag::Number(u64::MAX));
let unknown_starknet_block_number = eth_provider.to_starknet_block_id(unknown_block_number).await;
let unknown_starknet_block_number = eth_provider.to_starknet_block_id(Some(u64::MAX.into())).await;

// Then: Ensure the converted StarkNet block identifiers match the expected values
assert_eq!(pending_starknet_block_id, starknet::core::types::BlockId::Number(transaction.block_number.unwrap()));
Expand Down

0 comments on commit ec203ad

Please sign in to comment.