Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use result for TransactionCompact::fill. #12170

Merged
7 changes: 0 additions & 7 deletions crates/optimism/rpc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use alloy_rpc_types::error::EthRpcErrorCode;
use jsonrpsee_types::error::INTERNAL_ERROR_CODE;
use reth_optimism_evm::OptimismBlockExecutionError;
use reth_primitives::revm_primitives::{InvalidTransaction, OptimismInvalidTransaction};
use reth_provider::ProviderError;
use reth_rpc_eth_api::AsEthApiError;
use reth_rpc_eth_types::EthApiError;
use reth_rpc_server_types::result::{internal_rpc_err, rpc_err};
Expand Down Expand Up @@ -114,9 +113,3 @@ impl From<SequencerClientError> for jsonrpsee_types::error::ErrorObject<'static>
)
}
}

impl From<ProviderError> for OpEthApiError {
fn from(error: ProviderError) -> Self {
Self::Eth(error.into())
}
}
16 changes: 4 additions & 12 deletions crates/optimism/rpc/src/eth/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,13 @@ where
N: FullNodeComponents,
{
type Transaction = Transaction;
type TransactionError = OpEthApiError;
type Error = OpEthApiError;

fn fill(
&self,
tx: TransactionSignedEcRecovered,
tx_info: TransactionInfo,
) -> Result<Self::Transaction, Self::TransactionError> {
//let signed_tx = tx.clone().into_signed();
//let hash = tx.hash;
//
//let mut inner =
// EthTxBuilder.fill(tx, tx_info).expect("EthTxBuilder.fill should be infallible");
//
//if signed_tx.is_deposit() {
// inner.gas_price = Some(signed_tx.max_fee_per_gas())
//}
) -> Result<Self::Transaction, Self::Error> {
let from = tx.signer();
let TransactionSigned { transaction, signature, hash } = tx.into_signed();

Expand All @@ -115,7 +106,8 @@ where
let deposit_receipt_version = self
.inner
.provider()
.receipt_by_hash(hash)?
.receipt_by_hash(hash)
.map_err(Self::Error::from_eth_err)?
.and_then(|receipt| receipt.deposit_receipt_version);

let TransactionInfo { block_hash, block_number, index: transaction_index, .. } = tx_info;
Expand Down
7 changes: 5 additions & 2 deletions crates/rpc/rpc-eth-api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ pub type RpcBlock<T> = Block<RpcTransaction<T>, <T as Network>::HeaderResponse>;
/// Adapter for network specific receipt type.
pub type RpcReceipt<T> = <T as Network>::ReceiptResponse;

/// Adapter for network specific error type.
pub type RpcError<T> = <T as EthApiTypes>::Error;

/// Helper trait holds necessary trait bounds on [`EthApiTypes`] to implement `eth` API.
pub trait FullEthApiTypes:
EthApiTypes<
TransactionCompat: TransactionCompat<
Transaction = RpcTransaction<Self::NetworkTypes>,
TransactionError = Self::Error,
Error = RpcError<Self>,
>,
>
{
Expand All @@ -54,7 +57,7 @@ impl<T> FullEthApiTypes for T where
T: EthApiTypes<
TransactionCompat: TransactionCompat<
Transaction = RpcTransaction<T::NetworkTypes>,
TransactionError = T::Error,
Error = RpcError<T>,
>,
>
{
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/rpc-eth-types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl TransactionSource {
pub fn into_transaction<T: TransactionCompat>(
self,
resp_builder: &T,
) -> Result<T::Transaction, T::TransactionError> {
) -> Result<T::Transaction, T::Error> {
match self {
Self::Pool(tx) => from_recovered(tx, resp_builder),
Self::Block { transaction, index, block_hash, block_number, base_fee } => {
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/rpc-types-compat/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub fn from_block_full<T: TransactionCompat>(

from_recovered_with_block_context::<T>(signed_tx_ec_recovered, tx_info, tx_resp_builder)
})
.collect::<Result<Vec<_>, T::TransactionError>>()
.collect::<Result<Vec<_>, T::Error>>()
.expect("fill should be infallible");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this expect still needs to go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this one what do you think is the best idea? I was thinking to add TransactionCompatError inside alloy_rpc_eth::BlockError and maping the error to BlockError. Which would require making a pr to alloy.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes indeed it does require a pr to alloy, you're right. suggest making a pr to alloy adding error variant BlockError::Custom(Box<dyn Error>) or BlockError::Custom(String) - would you please open the pr?

not to block this pr until next alloy release, let's log the error here, and would you please open an issue to propagate the error here instead when the alloy change is available.

Copy link
Member

@emhane emhane Nov 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually makes most sense to change the signature of from_block_full to return TransactionCompat::Error, and add trait bound From<BlockError> to the associated type


Ok(from_block_with_transactions(
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 @@ -20,7 +20,7 @@ pub fn from_recovered_with_block_context<T: TransactionCompat>(
tx: TransactionSignedEcRecovered,
tx_info: TransactionInfo,
resp_builder: &T,
) -> Result<T::Transaction, T::TransactionError> {
) -> Result<T::Transaction, T::Error> {
resp_builder.fill(tx, tx_info)
}

Expand All @@ -29,7 +29,7 @@ pub fn from_recovered_with_block_context<T: TransactionCompat>(
pub fn from_recovered<T: TransactionCompat>(
tx: TransactionSignedEcRecovered,
resp_builder: &T,
) -> Result<T::Transaction, T::TransactionError> {
) -> Result<T::Transaction, T::Error> {
resp_builder.fill(tx, TransactionInfo::default())
}

Expand All @@ -45,15 +45,15 @@ pub trait TransactionCompat: Send + Sync + Unpin + Clone + fmt::Debug {
+ fmt::Debug;

/// RPC transaction error type.
type TransactionError: error::Error;
type Error: error::Error;

/// Create a new rpc transaction result for a _pending_ signed transaction, setting block
/// environment related fields to `None`.
fn fill(
&self,
tx: TransactionSignedEcRecovered,
tx_inf: TransactionInfo,
) -> Result<Self::Transaction, Self::TransactionError>;
) -> Result<Self::Transaction, Self::Error>;

/// Truncates the input of a transaction to only the first 4 bytes.
// todo: remove in favour of using constructor on `TransactionResponse` or similar
Expand Down
12 changes: 7 additions & 5 deletions crates/rpc/rpc/src/eth/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use tokio::{
sync::{mpsc::Receiver, Mutex},
time::MissedTickBehavior,
};
use tracing::trace;
use tracing::{error, trace};

/// The maximum number of headers we read at once when handling a range filter.
const MAX_HEADERS_RANGE: u64 = 1_000; // with ~530bytes per header this is ~500kb
Expand Down Expand Up @@ -625,10 +625,12 @@ 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)
.expect("fill should be infallible"),
);
match from_recovered(tx.transaction.to_recovered_transaction(), &self.tx_resp_builder) {
Ok(tx) => pending_txs.push(tx),
Err(err) => {
error!(target: "rpc", %err, "Failed to fill txn with block context");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
error!(target: "rpc", %err, "Failed to fill txn with block context");
error!(target: "rpc::filter", %err, "Failed to fill txn with block context");

nitpick, makes the target more helpful

nonetheless, this error should be propagated, but can be handled separately, could you please open an issue

}
}
}
FilterChanges::Transactions(pending_txs)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/rpc/rpc/src/eth/helpers/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ where
{
type Transaction = <Ethereum as Network>::TransactionResponse;

type TransactionError = EthApiError;
type Error = EthApiError;

fn fill(
&self,
tx: TransactionSignedEcRecovered,
tx_info: TransactionInfo,
) -> Result<Self::Transaction, Self::TransactionError> {
) -> Result<Self::Transaction, Self::Error> {
let from = tx.signer();
let TransactionSigned { transaction, signature, hash } = tx.into_signed();

Expand Down
22 changes: 14 additions & 8 deletions crates/rpc/rpc/src/eth/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,20 @@ where
match params {
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,
)
.expect("fill should be infallible"),
))
let stream = pubsub
.full_pending_transaction_stream()
.filter_map(|tx| {
let tx_value = match from_recovered(
tx.transaction.to_recovered_transaction(),
&tx_resp_builder,
){
Ok(tx) => Some(EthSubscriptionResult::FullTransaction(Box::new(tx))),
Err(err) => {
tracing::error!(target = "rpc", %err, "Failed to fill transaction with block context");
None
}
};
std::future::ready(tx_value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, this error should be propagated, or, sent as a message i.e. add new variant SubscriptionMessageInner::Error(Box<dyn Error>). please open an issue.

});
return pipe_from_stream(accepted_sink, stream).await
}
Expand Down