Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 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 crates/starknet_transaction_prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ reqwest.workspace = true
rstest.workspace = true
serde = { workspace = true, features = ["derive"] }
starknet-types-core.workspace = true
strum = { workspace = true, features = ["derive"] }
tower_ohttp = { workspace = true, features = ["testing"] }
tracing-test.workspace = true

Expand Down
82 changes: 59 additions & 23 deletions crates/starknet_transaction_prover/src/server/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
//!
//! Error codes follow Starknet RPC specification v0.10.
//!
//! When adding a new error type, also update:
//! - The OpenRPC spec in starknet-specs: `proving-api/starknet_proving_api_openrpc.json`
//! - The spec validation test: `server/rpc_spec_test.rs` (`test_error_responses_match_spec`)
//! Service-defined errors are declared as [`ServiceErrorCode`] variants — add new errors there.
//! The spec conformance test (`server/rpc_spec_test.rs`, `test_error_responses_match_spec`)
//! iterates the enum and fails until the OpenRPC spec in starknet-specs
//! (`proving-api/starknet_proving_api_openrpc.json`) documents the new error.

use jsonrpsee::types::error::ErrorCode::InternalError;
use jsonrpsee::types::error::INTERNAL_ERROR_MSG;
use jsonrpsee::types::ErrorObjectOwned;
#[cfg(test)]
use strum::EnumIter;

use crate::errors::{
ProofProviderError,
Expand All @@ -17,43 +20,76 @@ use crate::errors::{
VirtualSnosProverError,
};

// Starknet RPC v0.10 error codes.
/// Every JSON-RPC error the proving service itself defines, one variant per error documented in
/// the proving-api OpenRPC spec. Each error's code and canonical message live only here; the
/// constructor functions below are thin wrappers. The service can also return the standard
/// JSON-RPC internal error (-32603) and pass-through upstream Starknet errors, which are not
/// spec-enumerated and deliberately not variants.
#[derive(Clone, Copy)]
#[cfg_attr(test, derive(EnumIter))]
pub(crate) enum ServiceErrorCode {
BlockNotFound,
AccountValidationFailed,
InvalidTransactionInput,
UnsupportedTxType,
/// Blocked by the external compliance check.
TransactionBlocked,
ServiceBusy,
}

impl ServiceErrorCode {
pub(crate) fn code(self) -> i32 {
match self {
Self::BlockNotFound => 24,
Self::AccountValidationFailed => 55,
Self::InvalidTransactionInput => 1000,
Self::UnsupportedTxType => 1001,
Self::TransactionBlocked => 10000,
Self::ServiceBusy => -32005,
}
}

fn message(self) -> &'static str {
match self {
Self::BlockNotFound => "Block not found",
Self::AccountValidationFailed => "Account validation failed",
Self::InvalidTransactionInput => "Invalid transaction input",
Self::UnsupportedTxType => "the transaction type is not supported",
Self::TransactionBlocked => "Transaction blocked",
Self::ServiceBusy => "Service is busy",
}
}

fn error_object(self, data: Option<String>) -> ErrorObjectOwned {
ErrorObjectOwned::owned(self.code(), self.message(), data)
}
}

/// Block not found (code 24).
pub fn block_not_found() -> ErrorObjectOwned {
ErrorObjectOwned::owned(24, "Block not found", None::<()>)
ServiceErrorCode::BlockNotFound.error_object(None)
}

/// Account validation failed (code 55).
pub fn validation_failure(data: String) -> ErrorObjectOwned {
ErrorObjectOwned::owned(55, "Account validation failed", Some(data))
ServiceErrorCode::AccountValidationFailed.error_object(Some(data))
}

/// Unsupported transaction type (code 1001).
pub fn unsupported_tx_type(data: String) -> ErrorObjectOwned {
ErrorObjectOwned::owned(1001, "the transaction type is not supported", Some(data))
ServiceErrorCode::UnsupportedTxType.error_object(Some(data))
}

/// Invalid transaction input (code 1000).
pub fn invalid_transaction_input(data: String) -> ErrorObjectOwned {
ErrorObjectOwned::owned(1000, "Invalid transaction input", Some(data))
ServiceErrorCode::InvalidTransactionInput.error_object(Some(data))
}

/// Transaction blocked by external compliance check (code 10000).
pub fn transaction_blocked() -> ErrorObjectOwned {
ErrorObjectOwned::owned(10000, "Transaction blocked", None::<()>)
ServiceErrorCode::TransactionBlocked.error_object(None)
}

/// Service is busy — too many concurrent proving requests (code -32005).
pub fn service_busy(max_concurrent: usize) -> ErrorObjectOwned {
ErrorObjectOwned::owned(
-32005,
"Service is busy",
Some(format!(
"The proving service is at capacity ({max_concurrent} concurrent request(s)). Please \
retry later."
)),
)
ServiceErrorCode::ServiceBusy.error_object(Some(format!(
"The proving service is at capacity ({max_concurrent} concurrent request(s)). Please \
retry later."
)))
}

/// Creates an internal server error with the given message.
Expand Down
62 changes: 42 additions & 20 deletions crates/starknet_transaction_prover/src/server/rpc_spec_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ use starknet_api::transaction::fields::{
ValidResourceBounds,
};
use starknet_types_core::felt::Felt;
use strum::IntoEnumIterator;

use crate::config::ProverConfig;
use crate::proving::virtual_snos_prover::RpcVirtualSnosProver;
use crate::server::errors;
use crate::server::errors::ServiceErrorCode;
use crate::server::mock_rpc::MockProvingRpc;
use crate::server::rpc_api::ProvingRpcServer;
use crate::server::rpc_impl::{ProvingRpcServerImpl, SPEC_VERSION};
Expand Down Expand Up @@ -458,32 +460,52 @@ async fn test_prove_transaction_rejects_pending_block_id(
SpecError::from_spec(&resolve_spec_error("BLOCK_NOT_FOUND")).assert_matches(&actual_error);
}

/// Exhaustive: a new [`ServiceErrorCode`] variant fails to compile here until its spec key is
/// wired in.
fn spec_key(error_code: ServiceErrorCode) -> &'static str {
match error_code {
ServiceErrorCode::BlockNotFound => "BLOCK_NOT_FOUND",
ServiceErrorCode::AccountValidationFailed => "ACCOUNT_VALIDATION_FAILED",
ServiceErrorCode::UnsupportedTxType => "UNSUPPORTED_TX_TYPE",
ServiceErrorCode::InvalidTransactionInput => "INVALID_TRANSACTION_INPUT",
ServiceErrorCode::ServiceBusy => "SERVICE_BUSY",
ServiceErrorCode::TransactionBlocked => "TRANSACTION_BLOCKED",
}
}

/// Built via the production constructors so the spec assertions exercise the real
/// code/message/data wiring.
fn sample_error_object(error_code: ServiceErrorCode) -> ErrorObjectOwned {
match error_code {
ServiceErrorCode::BlockNotFound => errors::block_not_found(),
ServiceErrorCode::AccountValidationFailed => {
errors::validation_failure("sample data".to_string())
}
ServiceErrorCode::UnsupportedTxType => errors::unsupported_tx_type("Declare".to_string()),
ServiceErrorCode::InvalidTransactionInput => {
errors::invalid_transaction_input("test field invalid".to_string())
}
ServiceErrorCode::ServiceBusy => errors::service_busy(2),
ServiceErrorCode::TransactionBlocked => errors::transaction_blocked(),
}
}

#[test]
// TODO(Avi): Add an error enum to make this test exhastive.
fn test_error_responses_match_spec() {
let test_cases: Vec<(&str, ErrorObjectOwned)> = vec![
("BLOCK_NOT_FOUND", errors::block_not_found()),
("ACCOUNT_VALIDATION_FAILED", errors::validation_failure("test".to_string())),
("UNSUPPORTED_TX_TYPE", errors::unsupported_tx_type("Declare".to_string())),
("SERVICE_BUSY", errors::service_busy(2)),
(
"INVALID_TRANSACTION_INPUT",
errors::invalid_transaction_input("test field invalid".to_string()),
),
("TRANSACTION_BLOCKED", errors::transaction_blocked()),
];

// Completeness guard: ensure all spec errors (from method error arrays) have a test case.
let spec_error_keys: HashSet<&str> = SPEC_ERRORS.keys().map(|k| k.as_str()).collect();
let tested_error_keys: HashSet<&str> = test_cases.iter().map(|(key, _)| *key).collect();
let enum_keys: HashSet<&'static str> = ServiceErrorCode::iter().map(spec_key).collect();
assert_eq!(
tested_error_keys, spec_error_keys,
"Test cases don't cover all spec errors. Update the test_cases list above."
enum_keys.len(),
ServiceErrorCode::iter().count(),
"Duplicate spec_key for ServiceErrorCode variants",
);

for (spec_key, actual) in &test_cases {
SpecError::from_spec(&resolve_spec_error(spec_key)).assert_matches(actual);
for error_code in ServiceErrorCode::iter() {
let spec_entry = resolve_spec_error(spec_key(error_code));
SpecError::from_spec(&spec_entry).assert_matches(&sample_error_object(error_code));
}

let spec_keys: HashSet<&str> = SPEC_ERRORS.keys().map(String::as_str).collect();
assert_eq!(enum_keys, spec_keys, "ServiceErrorCode and OpenRPC spec are out of sync");
}

/// Helper: sends a prove_transaction request and asserts it returns the expected error.
Expand Down
Loading