From 6a5d59b1e0254795e4f43dd59fd7ebacd2f9b45f Mon Sep 17 00:00:00 2001 From: nicolas <48695862+merklefruit@users.noreply.github.com> Date: Fri, 6 Dec 2024 10:21:05 +0100 Subject: [PATCH 1/2] chore: use more specific error types --- bolt-sidecar/src/api/commitments/handlers.rs | 6 +++--- bolt-sidecar/src/api/commitments/spec.rs | 11 +++++++++++ bolt-sidecar/src/driver.rs | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/bolt-sidecar/src/api/commitments/handlers.rs b/bolt-sidecar/src/api/commitments/handlers.rs index d9cbd541b..db8a5a73f 100644 --- a/bolt-sidecar/src/api/commitments/handlers.rs +++ b/bolt-sidecar/src/api/commitments/handlers.rs @@ -65,9 +65,9 @@ pub async fn rpc_entrypoint( }; // Parse the inclusion request from the parameters - let mut inclusion_request: InclusionRequest = serde_json::from_value(request_json) - .map_err(|e| RejectionError::ValidationFailed(e.to_string())) - .inspect_err(|e| error!("Failed to parse inclusion request: {:?}", e))?; + let mut inclusion_request = serde_json::from_value::(request_json) + .map_err(RejectionError::Json) + .inspect_err(|err| error!(?err, "Failed to parse inclusion request"))?; debug!(?inclusion_request, "New inclusion request"); diff --git a/bolt-sidecar/src/api/commitments/spec.rs b/bolt-sidecar/src/api/commitments/spec.rs index 31478abdd..6893644bc 100644 --- a/bolt-sidecar/src/api/commitments/spec.rs +++ b/bolt-sidecar/src/api/commitments/spec.rs @@ -34,6 +34,9 @@ pub enum CommitmentError { /// Duplicate request. #[error("Duplicate request")] Duplicate, + /// No available public key to sign commitment request with for a given slot. + #[error("No available public key to sign request with (slot {0})")] + NoAvailablePubkeyForSlot(u64), /// Internal server error. #[error("Internal server error")] Internal, @@ -73,6 +76,11 @@ impl IntoResponse for CommitmentError { Json(JsonResponse::from_error(-32002, self.to_string())), ) .into_response(), + Self::NoAvailablePubkeyForSlot(_) => ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(JsonResponse::from_error(-32008, self.to_string())), + ) + .into_response(), Self::NoSignature => { (StatusCode::BAD_REQUEST, Json(JsonResponse::from_error(-32003, self.to_string()))) .into_response() @@ -117,6 +125,9 @@ pub enum RejectionError { /// State validation failed for this request. #[error("Validation failed: {0}")] ValidationFailed(String), + /// JSON parsing error. + #[error("JSON parsing error: {0}")] + Json(#[from] serde_json::Error), } /// Implements the commitments-API: diff --git a/bolt-sidecar/src/driver.rs b/bolt-sidecar/src/driver.rs index 062f3dd7e..0dcee953c 100644 --- a/bolt-sidecar/src/driver.rs +++ b/bolt-sidecar/src/driver.rs @@ -317,7 +317,7 @@ impl SidecarDriver { self.constraints_client.find_signing_key(validator_pubkey, available_pubkeys) else { error!(%target_slot, "No available public key to sign constraints with"); - let _ = response.send(Err(CommitmentError::Internal)); + let _ = response.send(Err(CommitmentError::NoAvailablePubkeyForSlot(target_slot))); return; }; From 3623003edfa7ff0713b35e36711f901933c35383 Mon Sep 17 00:00:00 2001 From: nicolas <48695862+merklefruit@users.noreply.github.com> Date: Fri, 6 Dec 2024 10:41:25 +0100 Subject: [PATCH 2/2] chore: revert internal error external res --- bolt-sidecar/src/api/commitments/spec.rs | 18 ++++++++---------- bolt-sidecar/src/driver.rs | 2 +- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/bolt-sidecar/src/api/commitments/spec.rs b/bolt-sidecar/src/api/commitments/spec.rs index 6893644bc..b996837a9 100644 --- a/bolt-sidecar/src/api/commitments/spec.rs +++ b/bolt-sidecar/src/api/commitments/spec.rs @@ -1,5 +1,11 @@ use alloy::primitives::SignatureError; -use axum::{extract::rejection::JsonRejection, http::StatusCode, response::IntoResponse, Json}; +use axum::{ + body::Body, + extract::rejection::JsonRejection, + http::{Response, StatusCode}, + response::IntoResponse, + Json, +}; use thiserror::Error; use crate::{ @@ -34,9 +40,6 @@ pub enum CommitmentError { /// Duplicate request. #[error("Duplicate request")] Duplicate, - /// No available public key to sign commitment request with for a given slot. - #[error("No available public key to sign request with (slot {0})")] - NoAvailablePubkeyForSlot(u64), /// Internal server error. #[error("Internal server error")] Internal, @@ -61,7 +64,7 @@ pub enum CommitmentError { } impl IntoResponse for CommitmentError { - fn into_response(self) -> axum::http::Response { + fn into_response(self) -> Response { match self { Self::Rejected(err) => { (StatusCode::BAD_REQUEST, Json(JsonResponse::from_error(-32000, err.to_string()))) @@ -76,11 +79,6 @@ impl IntoResponse for CommitmentError { Json(JsonResponse::from_error(-32002, self.to_string())), ) .into_response(), - Self::NoAvailablePubkeyForSlot(_) => ( - StatusCode::INTERNAL_SERVER_ERROR, - Json(JsonResponse::from_error(-32008, self.to_string())), - ) - .into_response(), Self::NoSignature => { (StatusCode::BAD_REQUEST, Json(JsonResponse::from_error(-32003, self.to_string()))) .into_response() diff --git a/bolt-sidecar/src/driver.rs b/bolt-sidecar/src/driver.rs index 0dcee953c..062f3dd7e 100644 --- a/bolt-sidecar/src/driver.rs +++ b/bolt-sidecar/src/driver.rs @@ -317,7 +317,7 @@ impl SidecarDriver { self.constraints_client.find_signing_key(validator_pubkey, available_pubkeys) else { error!(%target_slot, "No available public key to sign constraints with"); - let _ = response.send(Err(CommitmentError::NoAvailablePubkeyForSlot(target_slot))); + let _ = response.send(Err(CommitmentError::Internal)); return; };