Skip to content

Commit

Permalink
feat(JSON-RPC,gateway): introduce new GW error that adheres to the sp…
Browse files Browse the repository at this point in the history
…ec (#120)
  • Loading branch information
yair-starkware authored Jul 30, 2024
1 parent 8f36a0e commit b8b23c2
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ criterion = "0.3"
deadqueue = "0.2.4"
defaultmap = "0.5.0"
derive_more = "0.99.17"
enum-assoc = "1.1.0"
enum-iterator = "1.4.1"
ethers = "2.0.3"
ethnum = "1.5.0"
Expand Down
1 change: 1 addition & 0 deletions crates/gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ axum.workspace = true
blockifier = { path = "../blockifier", version = "0.8.0-rc.0", features = ["testing"] }
cairo-lang-starknet-classes.workspace = true
cairo-vm.workspace = true
enum-assoc.workspace = true
hyper.workspace = true
num-traits.workspace = true
papyrus_config = { path = "../papyrus_config", version = "0.4.0-rc.0"}
Expand Down
70 changes: 70 additions & 0 deletions crates/gateway/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
use std::fmt::Display;

use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use blockifier::blockifier::stateful_validator::StatefulValidatorError;
use blockifier::execution::errors::ContractClassError;
use blockifier::state::errors::StateError;
use blockifier::transaction::errors::TransactionExecutionError;
use cairo_vm::types::errors::program_errors::ProgramError;
use enum_assoc::Assoc;
use papyrus_rpc::error::{
unexpected_error,
validation_failure,
JsonRpcError,
CLASS_ALREADY_DECLARED,
CLASS_HASH_NOT_FOUND,
COMPILATION_FAILED,
COMPILED_CLASS_HASH_MISMATCH,
CONTRACT_CLASS_SIZE_IS_TOO_LARGE,
DUPLICATE_TX,
INSUFFICIENT_ACCOUNT_BALANCE,
INSUFFICIENT_MAX_FEE,
INVALID_TRANSACTION_NONCE,
NON_ACCOUNT,
UNSUPPORTED_CONTRACT_CLASS_VERSION,
UNSUPPORTED_TX_VERSION,
};
use serde_json::{Error as SerdeError, Value};
use starknet_api::block::GasPrice;
use starknet_api::core::CompiledClassHash;
Expand Down Expand Up @@ -153,3 +173,53 @@ impl From<RPCStateReaderError> for StateError {
pub fn serde_err_to_state_err(err: SerdeError) -> StateError {
StateError::StateReadError(format!("Failed to parse rpc result {:?}", err.to_string()))
}

/// Error returned by the gateway, adhering to the Starknet RPC error format.
// To get JsonRpcError from GatewaySpecError, use `into_rpc` method.
// TODO(yair): papyrus_rpc has a test that the add_tx functions return the correct error. Make sure
// it is tested when we have a single gateway.
#[derive(Debug, Clone, Eq, PartialEq, Assoc, Error)]
#[func(pub fn into_rpc(self) -> JsonRpcError<String>)]
pub enum GatewaySpecError {
#[assoc(into_rpc = CLASS_HASH_NOT_FOUND)]
ClassHashNotFound,
#[assoc(into_rpc = CLASS_ALREADY_DECLARED)]
ClassAlreadyDeclared,
#[assoc(into_rpc = INVALID_TRANSACTION_NONCE)]
InvalidTransactionNonce,
#[assoc(into_rpc = INSUFFICIENT_MAX_FEE)]
InsufficientMaxFee,
#[assoc(into_rpc = INSUFFICIENT_ACCOUNT_BALANCE)]
InsufficientAccountBalance,
#[assoc(into_rpc = validation_failure(_data))]
ValidationFailure { data: String },
#[assoc(into_rpc = COMPILATION_FAILED)]
CompilationFailed,
#[assoc(into_rpc = CONTRACT_CLASS_SIZE_IS_TOO_LARGE)]
ContractClassSizeIsTooLarge,
#[assoc(into_rpc = NON_ACCOUNT)]
NonAccount,
#[assoc(into_rpc = DUPLICATE_TX)]
DuplicateTx,
#[assoc(into_rpc = COMPILED_CLASS_HASH_MISMATCH)]
CompiledClassHashMismatch,
#[assoc(into_rpc = UNSUPPORTED_TX_VERSION)]
UnsupportedTxVersion,
#[assoc(into_rpc = UNSUPPORTED_CONTRACT_CLASS_VERSION)]
UnsupportedContractClassVersion,
#[assoc(into_rpc = unexpected_error(_data))]
UnexpectedError { data: String },
}

impl Display for GatewaySpecError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let as_rpc = self.clone().into_rpc();
write!(
f,
"{}: {}. data: {}",
as_rpc.code,
as_rpc.message,
serde_json::to_string(&as_rpc.data).unwrap()
)
}
}
3 changes: 3 additions & 0 deletions crates/papyrus_rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use jsonrpsee::server::{ServerBuilder, ServerHandle};
use jsonrpsee::types::error::ErrorCode::InternalError;
use jsonrpsee::types::error::INTERNAL_ERROR_MSG;
use jsonrpsee::types::ErrorObjectOwned;
pub use latest::error;
use papyrus_common::pending_classes::PendingClasses;
use papyrus_common::BlockHashAndNumber;
use papyrus_config::dumping::{append_sub_config_name, ser_param, SerializeConfig};
Expand All @@ -46,6 +47,8 @@ use starknet_client::writer::StarknetGatewayClient;
use starknet_client::RetryConfig;
use tokio::sync::RwLock;
use tracing::{debug, error, info, instrument};
// Aliasing the latest version of the RPC.
use v0_7 as latest;
pub use v0_7::api::CompiledContractClass;
use validator::Validate;

Expand Down

0 comments on commit b8b23c2

Please sign in to comment.