Skip to content

Commit

Permalink
chore: cleanup core api default body limit (#336)
Browse files Browse the repository at this point in the history
Update axum's version. Check breaking
[changelog](https://github.com/tokio-rs/axum/blob/main/axum/CHANGELOG.md#060-25-november-2022).

Remove Tower `RequestBodyLimitLayer` middleware and use
`DefaultBodyLimit::max` (see TODO and
tokio-rs/axum#1397).
  • Loading branch information
malexandru-rdx committed Mar 16, 2023
2 parents fad2bdc + d6e58f1 commit c25a100
Show file tree
Hide file tree
Showing 25 changed files with 79 additions and 62 deletions.
33 changes: 22 additions & 11 deletions core-rust/Cargo.lock

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

6 changes: 1 addition & 5 deletions core-rust/core-api-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ opentelemetry-jaeger = { version = "0.17", features = ["rt-tokio"] }

### TYPES FOR THE SERVER
# Axum - see https://docs.rs/axum/latest/axum/#required-dependencies
axum = { version = "0.5.15", features = ["http1", "json"] }
# Tower HTTP - Axum middleware
tower-http = { version = "0.3.4", default-features = false, features = [
"limit",
] }
axum = { version = "0.6.6", features = ["http1", "json"] }
# Hyper - see https://docs.rs/axum/latest/axum/#required-dependencies
hyper = { version = "0.14.20", features = ["server", "http1"] }
# Tokio - see https://docs.rs/tokio/latest/tokio/
Expand Down
9 changes: 9 additions & 0 deletions core-rust/core-api-server/src/core_api/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,12 @@ pub(crate) fn detailed_error<E: ErrorDetails>(
details: Some(details.into()),
}
}

pub(crate) fn length_limit_error<E: ErrorDetails>() -> ResponseError<E> {
ResponseError {
status_code: StatusCode::PAYLOAD_TOO_LARGE,
public_error_message: "length limit exceeded".into(),
trace: None,
details: None,
}
}
28 changes: 19 additions & 9 deletions core-rust/core-api-server/src/core_api/extractors.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
use axum::{
async_trait,
body::HttpBody,
extract::{rejection::JsonRejection, FromRequest, RequestParts},
extract::{
rejection::{BytesRejection, FailedToBufferBody, JsonRejection},
FromRequest,
},
http::Request,
response::IntoResponse,
};
use serde::Serialize;

use super::{client_error, ResponseError};
use super::{client_error, length_limit_error, ResponseError};

// We define our own `Json` extractor that customizes the error from `axum::Json`

#[derive(Debug)]
pub(crate) struct Json<T>(pub T);
pub use axum::Extension; // Re-export Extension so that it can be used easily
pub use axum::extract::State; // Re-export State so that it can be used easily

#[async_trait]
impl<B, T> FromRequest<B> for Json<T>
impl<S, B, T> FromRequest<S, B> for Json<T>
where
axum::Json<T>: FromRequest<B, Rejection = JsonRejection>,
B: HttpBody + Send,
axum::Json<T>: FromRequest<S, B, Rejection = JsonRejection>,
S: Send + Sync,
B: HttpBody + Send + 'static,
{
type Rejection = ResponseError<()>;

async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
match axum::Json::<T>::from_request(req).await {
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
match axum::Json::<T>::from_request(req, state).await {
Ok(value) => Ok(Self(value.0)),
Err(rejection) => Err(client_error(format!("{rejection:?}"))),
Err(rejection) => match rejection {
JsonRejection::BytesRejection(BytesRejection::FailedToBufferBody(
FailedToBufferBody::LengthLimitError(_),
)) => Err(length_limit_error()),
_ => Err(client_error(format!("{rejection:?}"))),
},
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::core_api::*;

#[tracing::instrument(level = "debug", skip(state), err(Debug))]
pub(crate) async fn handle_mempool_list(
Extension(state): Extension<CoreApiState>,
State(state): State<CoreApiState>,
Json(request): Json<models::MempoolListRequest>,
) -> Result<Json<models::MempoolListResponse>, ResponseError<()>> {
let state_manager = state.state_manager.read();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use state_manager::jni::state_manager::ActualStateManager;
use super::to_api_notarized_transaction;

pub(crate) async fn handle_mempool_transaction(
state: Extension<CoreApiState>,
state: State<CoreApiState>,
request: Json<models::MempoolTransactionRequest>,
) -> Result<Json<models::MempoolTransactionResponse>, ResponseError<()>> {
core_api_read_handler(state, request, handle_mempool_list_internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use state_manager::query::{dump_component_state, VaultData};
use super::map_to_descendent_id;

pub(crate) async fn handle_state_access_controller(
state: Extension<CoreApiState>,
state: State<CoreApiState>,
request: Json<models::StateAccessControllerRequest>,
) -> Result<Json<models::StateAccessControllerResponse>, ResponseError<()>> {
core_api_read_handler(state, request, handle_state_access_controller_internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use state_manager::jni::state_manager::ActualStateManager;

#[tracing::instrument(skip(state), err(Debug))]
pub(crate) async fn handle_state_clock(
state: Extension<CoreApiState>,
state: State<CoreApiState>,
request: Json<models::StateClockRequest>,
) -> Result<Json<models::StateClockResponse>, ResponseError<()>> {
core_api_read_handler(state, request, handle_state_clock_internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use state_manager::jni::state_manager::ActualStateManager;
use state_manager::query::{dump_component_state, VaultData};

pub(crate) async fn handle_state_component(
state: Extension<CoreApiState>,
state: State<CoreApiState>,
request: Json<models::StateComponentRequest>,
) -> Result<Json<models::StateComponentResponse>, ResponseError<()>> {
core_api_read_handler(state, request, handle_state_component_internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use state_manager::jni::state_manager::ActualStateManager;

#[tracing::instrument(skip(state), err(Debug))]
pub(crate) async fn handle_state_epoch(
state: Extension<CoreApiState>,
state: State<CoreApiState>,
request: Json<models::StateEpochRequest>,
) -> Result<Json<models::StateEpochResponse>, ResponseError<()>> {
core_api_read_handler(state, request, handle_state_epoch_internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::core_api::models::StateNonFungibleResponse;
use state_manager::jni::state_manager::ActualStateManager;

pub(crate) async fn handle_state_non_fungible(
state: Extension<CoreApiState>,
state: State<CoreApiState>,
request: Json<models::StateNonFungibleRequest>,
) -> Result<Json<StateNonFungibleResponse>, ResponseError<()>> {
core_api_read_handler(state, request, handle_state_non_fungible_internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use radix_engine_interface::api::types::{
use state_manager::jni::state_manager::ActualStateManager;

pub(crate) async fn handle_state_package(
state: Extension<CoreApiState>,
state: State<CoreApiState>,
request: Json<models::StatePackageRequest>,
) -> Result<Json<models::StatePackageResponse>, ResponseError<()>> {
core_api_read_handler(state, request, handle_state_package_internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use radix_engine_interface::api::types::{AccessRulesOffset, NodeModuleId, RENode
use state_manager::jni::state_manager::ActualStateManager;

pub(crate) async fn handle_state_resource(
state: Extension<CoreApiState>,
state: State<CoreApiState>,
request: Json<models::StateResourceRequest>,
) -> Result<Json<models::StateResourceResponse>, ResponseError<()>> {
core_api_read_handler(state, request, handle_state_resource_internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use state_manager::query::{dump_component_state, VaultData};
use super::map_to_descendent_id;

pub(crate) async fn handle_state_validator(
state: Extension<CoreApiState>,
state: State<CoreApiState>,
request: Json<models::StateValidatorRequest>,
) -> Result<Json<models::StateValidatorResponse>, ResponseError<()>> {
core_api_read_handler(state, request, handle_state_validator_internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use state_manager::jni::state_manager::ActualStateManager;

#[tracing::instrument(err(Debug), skip(state))]
pub(crate) async fn handle_status_network_configuration(
state: Extension<CoreApiState>,
state: State<CoreApiState>,
) -> Result<Json<models::NetworkConfigurationResponse>, ResponseError<()>> {
core_api_handler_empty_request(state, handle_status_network_configuration_internal)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use state_manager::CommittedTransactionIdentifiers;

#[tracing::instrument(skip(state), err(Debug))]
pub(crate) async fn handle_status_network_status(
state: Extension<CoreApiState>,
state: State<CoreApiState>,
request: Json<models::NetworkStatusRequest>,
) -> Result<Json<models::NetworkStatusResponse>, ResponseError<()>> {
core_api_read_handler(state, request, handle_status_network_status_internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use transaction::model::{

#[tracing::instrument(skip(state))]
pub(crate) async fn handle_stream_transactions(
state: Extension<CoreApiState>,
state: State<CoreApiState>,
request: Json<models::StreamTransactionsRequest>,
) -> Result<Json<models::StreamTransactionsResponse>, ResponseError<()>> {
core_api_read_handler(state, request, handle_stream_transactions_internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ macro_rules! args_from_bytes_vec {

#[tracing::instrument(level = "debug", skip_all, err(Debug))]
pub(crate) async fn handle_transaction_callpreview(
Extension(state): Extension<CoreApiState>,
State(state): State<CoreApiState>,
Json(request): Json<models::TransactionCallPreviewRequest>,
) -> Result<Json<models::TransactionCallPreviewResponse>, ResponseError<()>> {
let state_manager = state.state_manager.read();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use super::{
};

pub(crate) async fn handle_transaction_parse(
state: Extension<CoreApiState>,
state: State<CoreApiState>,
request: Json<models::TransactionParseRequest>,
) -> Result<Json<models::TransactionParseResponse>, ResponseError<()>> {
core_api_read_handler(state, request, handle_transaction_parse_internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use transaction::manifest;
use transaction::model::PreviewFlags;

pub(crate) async fn handle_transaction_preview(
state: Extension<CoreApiState>,
state: State<CoreApiState>,
request: Json<models::TransactionPreviewRequest>,
) -> Result<Json<models::TransactionPreviewResponse>, ResponseError<()>> {
core_api_read_handler(state, request, handle_preview_internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use state_manager::store::traits::*;

#[tracing::instrument(skip(state), err(Debug))]
pub(crate) async fn handle_transaction_receipt(
state: Extension<CoreApiState>,
state: State<CoreApiState>,
request: Json<models::TransactionReceiptRequest>,
) -> Result<Json<models::TransactionReceiptResponse>, ResponseError<()>> {
core_api_read_handler(state, request, handle_transaction_receipt_internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use state_manager::store::traits::*;

#[tracing::instrument(err(Debug), skip(state))]
pub(crate) async fn handle_transaction_status(
state: Extension<CoreApiState>,
state: State<CoreApiState>,
request: Json<models::TransactionStatusRequest>,
) -> Result<Json<models::TransactionStatusResponse>, ResponseError<()>> {
core_api_read_handler(state, request, handle_transaction_status_internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl ErrorDetails for TransactionSubmitErrorDetails {

#[tracing::instrument(level = "debug", skip(state), err(Debug))]
pub(crate) async fn handle_transaction_submit(
Extension(state): Extension<CoreApiState>,
State(state): State<CoreApiState>,
Json(request): Json<models::TransactionSubmitRequest>,
) -> Result<Json<models::TransactionSubmitResponse>, ResponseError<TransactionSubmitErrorDetails>> {
let mut state_manager = state.state_manager.write();
Expand Down
6 changes: 3 additions & 3 deletions core-rust/core-api-server/src/core_api/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use radix_engine::types::{RENodeId, SubstateId, SubstateOffset};
use radix_engine_interface::api::types::NodeModuleId;
use state_manager::jni::state_manager::ActualStateManager;

use super::{CoreApiState, Extension, Json, MappingError, ResponseError};
use super::{CoreApiState, Json, MappingError, ResponseError, State};

pub(crate) fn core_api_handler_empty_request<Response>(
Extension(state): Extension<CoreApiState>,
State(state): State<CoreApiState>,
method: impl FnOnce(&mut ActualStateManager) -> Result<Response, ResponseError<()>>,
) -> Result<Json<Response>, ResponseError<()>> {
let mut state_manager = state.state_manager.write();
Expand All @@ -17,7 +17,7 @@ pub(crate) fn core_api_handler_empty_request<Response>(

#[tracing::instrument(skip_all)]
pub(crate) fn core_api_read_handler<Request, Response>(
Extension(state): Extension<CoreApiState>,
State(state): State<CoreApiState>,
Json(request_body): Json<Request>,
method: impl FnOnce(&ActualStateManager, Request) -> Result<Response, ResponseError<()>>,
) -> Result<Json<Response>, ResponseError<()>> {
Expand Down

0 comments on commit c25a100

Please sign in to comment.