Skip to content

Commit

Permalink
Update axum to v0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
Tumas committed Jan 9, 2025
1 parent f6c5c0b commit b6aec4e
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 165 deletions.
43 changes: 12 additions & 31 deletions Cargo.lock

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

9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ assert-json-diff = '2'
async-channel = '1'
async-trait = '0.1'
asynchronous-codec = '0.7'
axum = { version = '0.7' }
axum-extra = { version = '0.9', features = ['typed-header', 'query'] }
axum = { version = '0.8' }
axum-extra = { version = '0.10', features = ['typed-header', 'query'] }
base64 = '0.22'
bincode = '1'
bit_field = '0.10'
Expand Down Expand Up @@ -405,7 +405,10 @@ semver = '1'
serde = { version = '1', features = ['derive', 'rc'] }
serde-aux = '4'
serde_json = { version = '1', features = ['preserve_order'] }
serde_qs = { version = '0.13', features = ['axum'] }
# TODO: check if serde_qs has merged axum v0.8 support
# https://github.com/samscott89/serde_qs/pull/118
# serde_qs = { version = '0.13', features = ['axum'] }
serde_qs = { git = 'https://github.com/Atrox/serde_qs', rev = '2cfa3ee0af44ad8e78ed6bd6d0e7ac913f51f382', features = ['axum'] }
serde_repr = '0.1'
serde_with = '3'
# TODO: replace serde_yaml with alternative, since it is deprecated:
Expand Down
16 changes: 1 addition & 15 deletions http_api/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ struct EthErrorResponse<'error> {
)]
#[cfg(test)]
mod tests {
use axum::{extract::rejection::MissingJsonContentType, Error as AxumError};
use axum::extract::rejection::MissingJsonContentType;
use itertools::Itertools as _;
use serde_json::{json, Result, Value};
use test_case::test_case;
Expand Down Expand Up @@ -327,18 +327,4 @@ mod tests {
],
);
}

// `axum_extra::extract::QueryRejection` and `axum::Error` duplicate error sources.
// `axum::Error` has not been fixed in any version yet.
// `axum::extract::rejection::QueryRejection` is even worse and harder to work around.
#[test]
fn error_sources_does_not_yield_triplicates_from_query_rejection() {
let axum_error = AxumError::new("error");
let error = Error::InvalidQuery(QueryRejection::FailedToDeserializeQueryString(axum_error));

assert_eq!(
error.sources().map(ToString::to_string).collect_vec(),
["invalid query string", "error"],
);
}
}
65 changes: 21 additions & 44 deletions http_api/src/extractors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::sync::Arc;

use anyhow::{Error as AnyhowError, Result};
use axum::{
async_trait,
body::{Body, Bytes},
extract::{FromRef, FromRequest, FromRequestParts, Path},
http::{request::Parts, Request},
Expand Down Expand Up @@ -47,8 +46,7 @@ use crate::{
// They all use `FromStr`, whereas the one for `Path` uses `DeserializeOwned`.
pub struct EthPath<T>(pub T);

#[async_trait]
impl<S> FromRequestParts<S> for EthPath<BlockId> {
impl<S: Sync> FromRequestParts<S> for EthPath<BlockId> {
type Rejection = Error;

async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -63,8 +61,7 @@ impl<S> FromRequestParts<S> for EthPath<BlockId> {
}
}

#[async_trait]
impl<S> FromRequestParts<S> for EthPath<StateId> {
impl<S: Sync> FromRequestParts<S> for EthPath<StateId> {
type Rejection = Error;

async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -79,8 +76,7 @@ impl<S> FromRequestParts<S> for EthPath<StateId> {
}
}

#[async_trait]
impl<S> FromRequestParts<S> for EthPath<PeerId> {
impl<S: Sync> FromRequestParts<S> for EthPath<PeerId> {
type Rejection = Error;

async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -95,8 +91,7 @@ impl<S> FromRequestParts<S> for EthPath<PeerId> {
}
}

#[async_trait]
impl<S> FromRequestParts<S> for EthPath<ValidatorId> {
impl<S: Sync> FromRequestParts<S> for EthPath<ValidatorId> {
type Rejection = Error;

async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -111,8 +106,7 @@ impl<S> FromRequestParts<S> for EthPath<ValidatorId> {
}
}

#[async_trait]
impl<S> FromRequestParts<S> for EthPath<Epoch> {
impl<S: Sync> FromRequestParts<S> for EthPath<Epoch> {
type Rejection = Error;

async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -127,8 +121,7 @@ impl<S> FromRequestParts<S> for EthPath<Epoch> {
}
}

#[async_trait]
impl<S> FromRequestParts<S> for EthPath<(StateId, ValidatorId)> {
impl<S: Sync> FromRequestParts<S> for EthPath<(StateId, ValidatorId)> {
type Rejection = Error;

async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -153,8 +146,7 @@ impl<S> FromRequestParts<S> for EthPath<(StateId, ValidatorId)> {

pub struct EthQuery<T>(pub T);

#[async_trait]
impl<S, T: DeserializeOwned + 'static> FromRequestParts<S> for EthQuery<T> {
impl<S: Sync, T: DeserializeOwned + 'static> FromRequestParts<S> for EthQuery<T> {
type Rejection = Error;

async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -169,8 +161,7 @@ impl<S, T: DeserializeOwned + 'static> FromRequestParts<S> for EthQuery<T> {
// This has multiple `FromRequest` impls to make error messages more specific.
pub struct EthJson<T>(pub T);

#[async_trait]
impl<S> FromRequest<S, Body> for EthJson<Box<ProposerSlashing>> {
impl<S: Sync> FromRequest<S, Body> for EthJson<Box<ProposerSlashing>> {
type Rejection = Error;

async fn from_request(request: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -182,8 +173,7 @@ impl<S> FromRequest<S, Body> for EthJson<Box<ProposerSlashing>> {
}
}

#[async_trait]
impl<S> FromRequest<S, Body> for EthJson<Box<SignedVoluntaryExit>> {
impl<S: Sync> FromRequest<S, Body> for EthJson<Box<SignedVoluntaryExit>> {
type Rejection = Error;

async fn from_request(request: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -195,8 +185,7 @@ impl<S> FromRequest<S, Body> for EthJson<Box<SignedVoluntaryExit>> {
}
}

#[async_trait]
impl<S, P: Preset> FromRequest<S, Body> for EthJson<Box<AttesterSlashing<P>>> {
impl<S: Sync, P: Preset> FromRequest<S, Body> for EthJson<Box<AttesterSlashing<P>>> {
type Rejection = Error;

async fn from_request(request: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -208,8 +197,7 @@ impl<S, P: Preset> FromRequest<S, Body> for EthJson<Box<AttesterSlashing<P>>> {
}
}

#[async_trait]
impl<S, P: Preset> FromRequest<S, Body> for EthJson<Vec<Arc<Attestation<P>>>> {
impl<S: Sync, P: Preset> FromRequest<S, Body> for EthJson<Vec<Arc<Attestation<P>>>> {
type Rejection = Error;

async fn from_request(request: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -221,8 +209,7 @@ impl<S, P: Preset> FromRequest<S, Body> for EthJson<Vec<Arc<Attestation<P>>>> {
}
}

#[async_trait]
impl<S> FromRequest<S, Body> for EthJson<Vec<Value>> {
impl<S: Sync> FromRequest<S, Body> for EthJson<Vec<Value>> {
type Rejection = Error;

async fn from_request(request: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -234,8 +221,7 @@ impl<S> FromRequest<S, Body> for EthJson<Vec<Value>> {
}
}

#[async_trait]
impl<S> FromRequest<S, Body> for EthJson<Vec<ValidatorIndex>> {
impl<S: Sync> FromRequest<S, Body> for EthJson<Vec<ValidatorIndex>> {
type Rejection = Error;

async fn from_request(request: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -250,8 +236,7 @@ impl<S> FromRequest<S, Body> for EthJson<Vec<ValidatorIndex>> {
}
}

#[async_trait]
impl<S> FromRequest<S, Body> for EthJson<Vec<ValidatorId>> {
impl<S: Sync> FromRequest<S, Body> for EthJson<Vec<ValidatorId>> {
type Rejection = Error;

async fn from_request(request: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -263,8 +248,7 @@ impl<S> FromRequest<S, Body> for EthJson<Vec<ValidatorId>> {
}
}

#[async_trait]
impl<S> FromRequest<S, Body> for EthJson<ValidatorIdsAndStatusesBody> {
impl<S: Sync> FromRequest<S, Body> for EthJson<ValidatorIdsAndStatusesBody> {
type Rejection = Error;

async fn from_request(request: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -276,8 +260,7 @@ impl<S> FromRequest<S, Body> for EthJson<ValidatorIdsAndStatusesBody> {
}
}

#[async_trait]
impl<S> FromRequest<S, Body> for EthJson<Vec<SyncCommitteeSubscription>> {
impl<S: Sync> FromRequest<S, Body> for EthJson<Vec<SyncCommitteeSubscription>> {
type Rejection = Error;

async fn from_request(request: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -289,8 +272,7 @@ impl<S> FromRequest<S, Body> for EthJson<Vec<SyncCommitteeSubscription>> {
}
}

#[async_trait]
impl<S> FromRequest<S, Body> for EthJson<Vec<BeaconCommitteeSubscription>> {
impl<S: Sync> FromRequest<S, Body> for EthJson<Vec<BeaconCommitteeSubscription>> {
type Rejection = Error;

async fn from_request(request: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -302,8 +284,7 @@ impl<S> FromRequest<S, Body> for EthJson<Vec<BeaconCommitteeSubscription>> {
}
}

#[async_trait]
impl<S, P: Preset> FromRequest<S, Body> for EthJson<Vec<Arc<SignedAggregateAndProof<P>>>> {
impl<S: Sync, P: Preset> FromRequest<S, Body> for EthJson<Vec<Arc<SignedAggregateAndProof<P>>>> {
type Rejection = Error;

async fn from_request(request: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -315,8 +296,7 @@ impl<S, P: Preset> FromRequest<S, Body> for EthJson<Vec<Arc<SignedAggregateAndPr
}
}

#[async_trait]
impl<S, P: Preset> FromRequest<S, Body> for EthJson<Vec<SignedContributionAndProof<P>>> {
impl<S: Sync, P: Preset> FromRequest<S, Body> for EthJson<Vec<SignedContributionAndProof<P>>> {
type Rejection = Error;

async fn from_request(request: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -328,8 +308,7 @@ impl<S, P: Preset> FromRequest<S, Body> for EthJson<Vec<SignedContributionAndPro
}
}

#[async_trait]
impl<S> FromRequest<S, Body> for EthJson<Vec<ProposerData>> {
impl<S: Sync> FromRequest<S, Body> for EthJson<Vec<ProposerData>> {
type Rejection = Error;

async fn from_request(request: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -341,8 +320,7 @@ impl<S> FromRequest<S, Body> for EthJson<Vec<ProposerData>> {
}
}

#[async_trait]
impl<S> FromRequest<S, Body> for EthJson<Vec<SignedValidatorRegistrationV1>> {
impl<S: Sync> FromRequest<S, Body> for EthJson<Vec<SignedValidatorRegistrationV1>> {
type Rejection = Error;

async fn from_request(request: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
Expand All @@ -356,7 +334,6 @@ impl<S> FromRequest<S, Body> for EthJson<Vec<SignedValidatorRegistrationV1>> {

pub struct EthJsonOrSsz<T>(pub T);

#[async_trait]
impl<S, T> FromRequest<S, Body> for EthJsonOrSsz<T>
where
Arc<Config>: FromRef<S>,
Expand Down
Loading

0 comments on commit b6aec4e

Please sign in to comment.