Skip to content

Commit

Permalink
use expect for erorrs
Browse files Browse the repository at this point in the history
  • Loading branch information
lakshya-sky committed Oct 30, 2024
1 parent ef43d8c commit c90a97b
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 44 deletions.
24 changes: 4 additions & 20 deletions crates/optimism/rpc/src/eth/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use alloy_consensus::Transaction as _;
use alloy_primitives::{Bytes, B256};
use alloy_rpc_types::TransactionInfo;
use derive_more::derive::Display;
use op_alloy_consensus::DepositTransaction;
use op_alloy_rpc_types::Transaction;
use reth_node_api::FullNodeComponents;
Expand All @@ -19,18 +18,6 @@ use reth_transaction_pool::{PoolTransaction, TransactionOrigin, TransactionPool}

use crate::{OpEthApi, SequencerClient};

/// Bundled errors variants thrown by `TransactionCompat`.
#[derive(Clone, Debug, Display, PartialEq, Eq)]
pub enum TransactionError {
/// Error when building an Ethereum transaction.
#[display("error building transaction")]
EthTxBuilderError,
/// Provider error.
ProviderError(ProviderError),
}

impl core::error::Error for TransactionError {}

impl<N> EthTransactions for OpEthApi<N>
where
Self: LoadTransaction<Provider: BlockReaderIdExt>,
Expand Down Expand Up @@ -90,7 +77,7 @@ where
N: FullNodeComponents,
{
type Transaction = Transaction;
type TransactionError = TransactionError;
type TransactionError = ProviderError;

fn fill(
&self,
Expand All @@ -100,10 +87,8 @@ where
let signed_tx = tx.clone().into_signed();
let hash = tx.hash;

let mut inner = EthTxBuilder
.fill(tx, tx_info)
.map_err(|_| Self::TransactionError::EthTxBuilderError)?
.inner;
let mut inner =
EthTxBuilder.fill(tx, tx_info).expect("EthTxBuilder.fill should be infallible").inner;

if signed_tx.is_deposit() {
inner.gas_price = Some(signed_tx.max_fee_per_gas())
Expand All @@ -112,8 +97,7 @@ where
let deposit_receipt_version = self
.inner
.provider()
.receipt_by_hash(hash)
.map_err(Self::TransactionError::ProviderError)?
.receipt_by_hash(hash)?
.and_then(|receipt| receipt.deposit_receipt_version);

Ok(Transaction {
Expand Down
6 changes: 3 additions & 3 deletions crates/rpc/rpc-eth-api/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,9 @@ where
hash: B256,
) -> RpcResult<Option<RpcTransaction<T::NetworkTypes>>> {
trace!(target: "rpc::eth", ?hash, "Serving eth_getTransactionByHash");
Ok(EthTransactions::transaction_by_hash(self, hash)
.await?
.map(|tx| tx.into_transaction(self.tx_resp_builder())))
Ok(EthTransactions::transaction_by_hash(self, hash).await?.map(|tx| {
tx.into_transaction(self.tx_resp_builder()).expect("failed to fill transaction")
}))
}

/// Handler for: `eth_getRawTransactionByBlockHashAndIndex`
Expand Down
19 changes: 13 additions & 6 deletions crates/rpc/rpc-eth-api/src/helpers/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,14 @@ pub trait EthTransactions: LoadTransaction<Provider: BlockReaderIdExt> {
index: Some(index as u64),
};

return Ok(Some(from_recovered_with_block_context(
tx.clone().with_signer(*signer),
tx_info,
self.tx_resp_builder(),
)))
return Ok(Some(
from_recovered_with_block_context(
tx.clone().with_signer(*signer),
tx_info,
self.tx_resp_builder(),
)
.expect("fill should be infallible"),
));
}
}

Expand All @@ -236,7 +239,10 @@ pub trait EthTransactions: LoadTransaction<Provider: BlockReaderIdExt> {
RpcNodeCore::pool(self).get_transaction_by_sender_and_nonce(sender, nonce)
{
let transaction = tx.transaction.clone().into_consensus();
return Ok(Some(from_recovered(transaction.into(), self.tx_resp_builder())));
return Ok(Some(
from_recovered(transaction.into(), self.tx_resp_builder())
.expect("fill should be infallible"),
));
}
}

Expand Down Expand Up @@ -292,6 +298,7 @@ pub trait EthTransactions: LoadTransaction<Provider: BlockReaderIdExt> {
tx_info,
self.tx_resp_builder(),
)
.expect("fill should be infallible")
})
})
.ok_or(EthApiError::HeaderNotFound(block_id).into())
Expand Down
5 changes: 4 additions & 1 deletion crates/rpc/rpc-eth-types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ impl TransactionSource {
}

/// Conversion into network specific transaction type.
pub fn into_transaction<T: TransactionCompat>(self, resp_builder: &T) -> T::Transaction {
pub fn into_transaction<T: TransactionCompat>(
self,
resp_builder: &T,
) -> Result<T::Transaction, T::TransactionError> {
match self {
Self::Pool(tx) => from_recovered(tx, resp_builder),
Self::Block { transaction, index, block_hash, block_number, base_fee } => {
Expand Down
3 changes: 2 additions & 1 deletion crates/rpc/rpc-types-compat/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ pub fn from_block_full<T: TransactionCompat>(

from_recovered_with_block_context::<T>(signed_tx_ec_recovered, tx_info, tx_resp_builder)
})
.collect::<Vec<_>>();
.collect::<Result<Vec<_>, T::TransactionError>>()
.expect("failed to fill transactions");

Ok(from_block_with_transactions(
block_length,
Expand Down
8 changes: 4 additions & 4 deletions crates/rpc/rpc-types-compat/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ pub fn from_recovered_with_block_context<T: TransactionCompat>(
tx: TransactionSignedEcRecovered,
tx_info: TransactionInfo,
resp_builder: &T,
) -> T::Transaction {
resp_builder.fill(tx, tx_info).unwrap()
) -> Result<T::Transaction, T::TransactionError> {
resp_builder.fill(tx, tx_info)
}

/// Create a new rpc transaction result for a _pending_ signed transaction, setting block
/// environment related fields to `None`.
pub fn from_recovered<T: TransactionCompat>(
tx: TransactionSignedEcRecovered,
resp_builder: &T,
) -> T::Transaction {
resp_builder.fill(tx, TransactionInfo::default()).unwrap()
) -> Result<T::Transaction, T::TransactionError> {
resp_builder.fill(tx, TransactionInfo::default())
}

/// Builds RPC transaction w.r.t. network.
Expand Down
8 changes: 4 additions & 4 deletions crates/rpc/rpc/src/eth/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,10 +625,10 @@ where
let mut prepared_stream = self.txs_stream.lock().await;

while let Ok(tx) = prepared_stream.try_recv() {
pending_txs.push(from_recovered(
tx.transaction.to_recovered_transaction(),
&self.tx_resp_builder,
))
pending_txs.push(
from_recovered(tx.transaction.to_recovered_transaction(), &self.tx_resp_builder)
.expect("fill should be infallible"),
);
}
FilterChanges::Transactions(pending_txs)
}
Expand Down
11 changes: 7 additions & 4 deletions crates/rpc/rpc/src/eth/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,13 @@ where
Params::Bool(true) => {
// full transaction objects requested
let stream = pubsub.full_pending_transaction_stream().map(|tx| {
EthSubscriptionResult::FullTransaction(Box::new(from_recovered(
tx.transaction.to_recovered_transaction(),
&tx_resp_builder,
)))
EthSubscriptionResult::FullTransaction(Box::new(
from_recovered(
tx.transaction.to_recovered_transaction(),
&tx_resp_builder,
)
.expect("fill should be infallible"),
))
});
return pipe_from_stream(accepted_sink, stream).await
}
Expand Down
3 changes: 2 additions & 1 deletion crates/rpc/rpc/src/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ where
{
content.entry(tx.sender()).or_default().insert(
tx.nonce().to_string(),
from_recovered(tx.clone().into_consensus().into(), resp_builder),
from_recovered(tx.clone().into_consensus().into(), resp_builder)
.expect("fill should be infallible"),
);
}

Expand Down

0 comments on commit c90a97b

Please sign in to comment.