mithril_aggregator/http_server/routes/
middlewares.rs

1use slog::{debug, Logger};
2use std::convert::Infallible;
3use std::sync::Arc;
4use warp::Filter;
5
6use mithril_common::api_version::APIVersionProvider;
7
8use crate::database::repository::SignerGetter;
9use crate::dependency_injection::EpochServiceWrapper;
10use crate::event_store::{EventMessage, TransmitterService};
11use crate::http_server::routes::http_server_child_logger;
12use crate::http_server::routes::router::{RouterConfig, RouterState};
13use crate::services::{CertifierService, MessageService, ProverService, SignedEntityService};
14use crate::{
15    MetricsService, SignerRegisterer, SingleSignatureAuthenticator, VerificationKeyStorer,
16};
17
18/// Extract a value from the configuration
19pub fn extract_config<D: Clone + Send>(
20    state: &RouterState,
21    extract: fn(&RouterConfig) -> D,
22) -> impl Filter<Extract = (D,), Error = Infallible> + Clone {
23    let config_value = extract(&state.configuration);
24    warp::any().map(move || config_value.clone())
25}
26
27/// With logger middleware
28pub(crate) fn with_logger(
29    router_state: &RouterState,
30) -> impl Filter<Extract = (Logger,), Error = Infallible> + Clone {
31    let logger = http_server_child_logger(&router_state.dependencies.root_logger);
32    warp::any().map(move || logger.clone())
33}
34
35/// Log to apply each time a route is called
36///
37/// Example of log produced: `POST /aggregator/register-signatures 202 Accepted`
38pub(crate) fn log_route_call(
39    router_state: &RouterState,
40) -> warp::log::Log<impl Fn(warp::log::Info<'_>) + Clone> {
41    let logger = http_server_child_logger(&router_state.dependencies.root_logger);
42    warp::log::custom(move |info| {
43        debug!(
44            logger,
45            "{} {} {}",
46            info.method(),
47            info.path(),
48            info.status()
49        )
50    })
51}
52
53/// With signer registerer middleware
54pub fn with_signer_registerer(
55    router_state: &RouterState,
56) -> impl Filter<Extract = (Arc<dyn SignerRegisterer>,), Error = Infallible> + Clone {
57    let signer_register = router_state.dependencies.signer_registerer.clone();
58    warp::any().map(move || signer_register.clone())
59}
60
61/// With signer getter middleware
62pub fn with_signer_getter(
63    router_state: &RouterState,
64) -> impl Filter<Extract = (Arc<dyn SignerGetter>,), Error = Infallible> + Clone {
65    let signer_getter = router_state.dependencies.signer_getter.clone();
66    warp::any().map(move || signer_getter.clone())
67}
68
69/// With Event transmitter middleware
70pub fn with_event_transmitter(
71    router_state: &RouterState,
72) -> impl Filter<Extract = (Arc<TransmitterService<EventMessage>>,), Error = Infallible> + Clone {
73    let event_transmitter = router_state.dependencies.event_transmitter.clone();
74    warp::any().map(move || event_transmitter.clone())
75}
76
77/// With certifier service middleware
78pub fn with_certifier_service(
79    router_state: &RouterState,
80) -> impl Filter<Extract = (Arc<dyn CertifierService>,), Error = Infallible> + Clone {
81    let certifier_service = router_state.dependencies.certifier_service.clone();
82    warp::any().map(move || certifier_service.clone())
83}
84
85/// With epoch service middleware
86pub fn with_epoch_service(
87    router_state: &RouterState,
88) -> impl Filter<Extract = (EpochServiceWrapper,), Error = Infallible> + Clone {
89    let epoch_service = router_state.dependencies.epoch_service.clone();
90    warp::any().map(move || epoch_service.clone())
91}
92
93/// With signed entity service
94pub fn with_signed_entity_service(
95    router_state: &RouterState,
96) -> impl Filter<Extract = (Arc<dyn SignedEntityService>,), Error = Infallible> + Clone {
97    let signed_entity_service = router_state.dependencies.signed_entity_service.clone();
98    warp::any().map(move || signed_entity_service.clone())
99}
100
101/// With verification key store
102pub fn with_verification_key_store(
103    router_state: &RouterState,
104) -> impl Filter<Extract = (Arc<dyn VerificationKeyStorer>,), Error = Infallible> + Clone {
105    let verification_key_store = router_state.dependencies.verification_key_store.clone();
106    warp::any().map(move || verification_key_store.clone())
107}
108
109/// With API version provider
110pub fn with_api_version_provider(
111    router_state: &RouterState,
112) -> impl Filter<Extract = (Arc<APIVersionProvider>,), Error = Infallible> + Clone {
113    let api_version_provider = router_state.dependencies.api_version_provider.clone();
114    warp::any().map(move || api_version_provider.clone())
115}
116
117/// With Message service
118pub fn with_http_message_service(
119    router_state: &RouterState,
120) -> impl Filter<Extract = (Arc<dyn MessageService>,), Error = Infallible> + Clone {
121    let message_service = router_state.dependencies.message_service.clone();
122    warp::any().map(move || message_service.clone())
123}
124
125/// With Prover service
126pub fn with_prover_service(
127    router_state: &RouterState,
128) -> impl Filter<Extract = (Arc<dyn ProverService>,), Error = Infallible> + Clone {
129    let prover_service = router_state.dependencies.prover_service.clone();
130    warp::any().map(move || prover_service.clone())
131}
132
133/// With Single Signature Authenticator
134pub fn with_single_signature_authenticator(
135    router_state: &RouterState,
136) -> impl Filter<Extract = (Arc<SingleSignatureAuthenticator>,), Error = Infallible> + Clone {
137    let single_signer_authenticator = router_state
138        .dependencies
139        .single_signer_authenticator
140        .clone();
141    warp::any().map(move || single_signer_authenticator.clone())
142}
143
144/// With Metrics service
145pub fn with_metrics_service(
146    router_state: &RouterState,
147) -> impl Filter<Extract = (Arc<MetricsService>,), Error = Infallible> + Clone {
148    let metrics_service = router_state.dependencies.metrics_service.clone();
149    warp::any().map(move || metrics_service.clone())
150}
151
152pub mod validators {
153    use crate::http_server::validators::ProverTransactionsHashValidator;
154
155    use super::*;
156
157    /// With Prover Transactions Hash Validator
158    pub fn with_prover_transactions_hash_validator(
159        router_state: &RouterState,
160    ) -> impl Filter<Extract = (ProverTransactionsHashValidator,), Error = Infallible> + Clone {
161        let max_hashes = router_state
162            .configuration
163            .cardano_transactions_prover_max_hashes_allowed_by_request;
164
165        warp::any().map(move || ProverTransactionsHashValidator::new(max_hashes))
166    }
167}