Skip to content

Commit

Permalink
fix(starknet_gateway): convert mempool errors to the correct gw error (
Browse files Browse the repository at this point in the history
  • Loading branch information
yair-starkware authored Jan 1, 2025
1 parent bc81737 commit 3563c1c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
41 changes: 41 additions & 0 deletions crates/starknet_gateway/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use starknet_api::block::GasPrice;
use starknet_api::transaction::fields::{Resource, ResourceBounds};
use starknet_api::StarknetApiError;
use starknet_gateway_types::errors::GatewaySpecError;
use starknet_mempool_types::communication::{MempoolClientError, MempoolClientResult};
use starknet_mempool_types::errors::MempoolError;
use thiserror::Error;
use tracing::{debug, error, warn};

use crate::compiler_version::{VersionId, VersionIdError};
use crate::rpc_objects::{RpcErrorCode, RpcErrorResponse};
Expand Down Expand Up @@ -75,6 +78,44 @@ impl From<StatelessTransactionValidatorError> for GatewaySpecError {
}
}

/// Converts a mempool client result to a gateway result. Some errors variants are unreachable in
/// Gateway context, and some are not considered errors from the gateway's perspective.
pub fn mempool_client_result_to_gw_spec_result(
value: MempoolClientResult<()>,
) -> GatewayResult<()> {
let err = match value {
Ok(()) => return Ok(()),
Err(err) => err,
};
match err {
MempoolClientError::ClientError(client_error) => {
error!("Mempool client error: {}", client_error);
Err(GatewaySpecError::UnexpectedError { data: "Internal error".to_owned() })
}
MempoolClientError::MempoolError(mempool_error) => {
debug!("Mempool error: {}", mempool_error);
match mempool_error {
MempoolError::DuplicateNonce { .. }
| MempoolError::NonceTooLarge { .. }
| MempoolError::NonceTooOld { .. } => {
Err(GatewaySpecError::InvalidTransactionNonce)
}
MempoolError::DuplicateTransaction { .. } => Err(GatewaySpecError::DuplicateTx),
MempoolError::P2pPropagatorClientError { .. } => {
// Not an error from the gateway's perspective.
warn!("P2P propagator client error: {}", mempool_error);
Ok(())
}
MempoolError::TransactionNotFound { .. } => {
// This error is not expected to happen within the gateway, only from other
// mempool clients.
unreachable!("Unexpected mempool error in gateway context: {}", mempool_error);
}
}
}
}
}

pub type StatelessTransactionValidatorResult<T> = Result<T, StatelessTransactionValidatorError>;

pub type StatefulTransactionValidatorResult<T> = Result<T, GatewaySpecError>;
Expand Down
7 changes: 2 additions & 5 deletions crates/starknet_gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use tracing::{error, info, instrument, Span};

use crate::compilation::GatewayCompiler;
use crate::config::{GatewayConfig, RpcStateReaderConfig};
use crate::errors::GatewayResult;
use crate::errors::{mempool_client_result_to_gw_spec_result, GatewayResult};
use crate::rpc_state_reader::RpcStateReaderFactory;
use crate::state_reader::StateReaderFactory;
use crate::stateful_transaction_validator::StatefulTransactionValidator;
Expand Down Expand Up @@ -93,10 +93,7 @@ impl Gateway {
let add_tx_args = self.business_logic.add_tx(tx).await?;
let tx_hash = add_tx_args.tx.tx_hash();
let add_tx_args = AddTransactionArgsWrapper { args: add_tx_args, p2p_message_metadata };
self.mempool_client.add_tx(add_tx_args).await.map_err(|e| {
error!("Failed to send tx to mempool: {}", e);
GatewaySpecError::UnexpectedError { data: "Internal server error".to_owned() }
})?;
mempool_client_result_to_gw_spec_result(self.mempool_client.add_tx(add_tx_args).await)?;
// TODO: Also return `ContractAddress` for deploy and `ClassHash` for Declare.
Ok(tx_hash)
}
Expand Down

0 comments on commit 3563c1c

Please sign in to comment.