11// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
22// SPDX-License-Identifier: Apache-2.0
33
4- use alloy:: dyn_abi:: Eip712Domain ;
54use anyhow;
65use axum:: extract:: MatchedPath ;
76use axum:: extract:: Request as ExtractRequest ;
8- use axum:: http:: { Method , Request } ;
97use axum:: {
8+ http:: { Method , Request } ,
9+ middleware:: { from_fn, from_fn_with_state} ,
1010 response:: IntoResponse ,
1111 routing:: { get, post} ,
12- Json , Router ,
12+ serve , Json , Router , ServiceExt ,
1313} ;
14- use axum:: { serve, ServiceExt } ;
1514use build_info:: BuildInfo ;
1615use indexer_attestation:: AttestationSigner ;
1716use indexer_monitor:: {
18- attestation_signers, dispute_manager , escrow_accounts , indexer_allocations , DeploymentDetails ,
19- EscrowAccounts , SubgraphClient ,
17+ attestation_signers, deployment_to_allocation , dispute_manager , escrow_accounts ,
18+ indexer_allocations , DeploymentDetails , SubgraphClient ,
2019} ;
2120use prometheus:: TextEncoder ;
2221use reqwest:: StatusCode ;
@@ -30,17 +29,23 @@ use thegraph_core::{Address, Attestation};
3029use tokio:: net:: TcpListener ;
3130use tokio:: signal;
3231use tokio:: sync:: watch:: Receiver ;
32+ use tower:: ServiceBuilder ;
3333use tower_governor:: { governor:: GovernorConfigBuilder , GovernorLayer } ;
3434use tower_http:: validate_request:: ValidateRequestHeaderLayer ;
3535use tower_http:: { cors, cors:: CorsLayer , normalize_path:: NormalizePath , trace:: TraceLayer } ;
3636use tracing:: warn;
3737use tracing:: { error, info, info_span} ;
3838
39- use crate :: routes:: health;
40- use crate :: routes:: request_handler;
41- use crate :: routes:: static_subgraph_request_handler;
42- use crate :: tap:: IndexerTapContext ;
43- use crate :: wallet:: public_key;
39+ use crate :: {
40+ metrics:: { HANDLER_FAILURE , HANDLER_HISTOGRAM } ,
41+ middleware:: {
42+ allocation_middleware, deployment_middleware, labels_middleware, receipt_middleware,
43+ sender_middleware, AllocationState , MetricsMiddlewareLayer , SenderState ,
44+ } ,
45+ routes:: { health, request_handler, static_subgraph_request_handler} ,
46+ tap:: IndexerTapContext ,
47+ wallet:: public_key,
48+ } ;
4449use indexer_config:: Config ;
4550
4651use super :: SubgraphService ;
@@ -93,10 +98,6 @@ pub struct IndexerServiceState {
9398 pub attestation_signers : Receiver < HashMap < Address , AttestationSigner > > ,
9499 pub tap_manager : Manager < IndexerTapContext > ,
95100 pub service_impl : SubgraphService ,
96-
97- // tap
98- pub escrow_accounts : Receiver < EscrowAccounts > ,
99- pub domain_separator : Eip712Domain ,
100101}
101102
102103const HTTP_CLIENT_TIMEOUT : Duration = Duration :: from_secs ( 30 ) ;
@@ -257,7 +258,7 @@ pub async fn run(options: IndexerServiceOptions) -> Result<(), anyhow::Error> {
257258
258259 let checks = IndexerTapContext :: get_checks (
259260 database,
260- allocations,
261+ allocations. clone ( ) ,
261262 escrow_accounts. clone ( ) ,
262263 domain_separator. clone ( ) ,
263264 timestamp_error_tolerance,
@@ -276,8 +277,6 @@ pub async fn run(options: IndexerServiceOptions) -> Result<(), anyhow::Error> {
276277 attestation_signers,
277278 tap_manager,
278279 service_impl : options. service_impl ,
279- escrow_accounts,
280- domain_separator,
281280 } ) ;
282281
283282 // Rate limits by allowing bursts of 10 requests and requiring 100ms of
@@ -362,13 +361,39 @@ pub async fn run(options: IndexerServiceOptions) -> Result<(), anyhow::Error> {
362361
363362 misc_routes = misc_routes. with_state ( state. clone ( ) ) ;
364363
364+ let deployment_to_allocation = deployment_to_allocation ( allocations) ;
365+ let allocation_state = AllocationState {
366+ deployment_to_allocation,
367+ } ;
368+ let sender_state = SenderState {
369+ escrow_accounts,
370+ domain_separator,
371+ } ;
372+
373+ let service_builder = ServiceBuilder :: new ( )
374+ // inject deployment id
375+ . layer ( from_fn ( deployment_middleware) )
376+ // inject receipt
377+ . layer ( from_fn ( receipt_middleware) )
378+ // inject allocation id
379+ . layer ( from_fn_with_state ( allocation_state, allocation_middleware) )
380+ // inject sender
381+ . layer ( from_fn_with_state ( sender_state, sender_middleware) )
382+ // inject metrics labels
383+ . layer ( from_fn ( labels_middleware) )
384+ // metrics for histogram and failure
385+ . layer ( MetricsMiddlewareLayer :: new (
386+ HANDLER_HISTOGRAM . clone ( ) ,
387+ HANDLER_FAILURE . clone ( ) ,
388+ ) ) ;
389+
365390 let data_routes = Router :: new ( )
366391 . route (
367392 PathBuf :: from ( & options. config . service . url_prefix )
368393 . join ( format ! ( "{}/id/:id" , options. url_namespace) )
369394 . to_str ( )
370395 . expect ( "Failed to set up `/{url_namespace}/id/:id` route" ) ,
371- post ( request_handler) ,
396+ post ( request_handler) . route_layer ( service_builder ) ,
372397 )
373398 . with_state ( state. clone ( ) ) ;
374399
0 commit comments