Skip to content

Commit

Permalink
feat(common): improve how responses are attested to and finalized
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannis committed Dec 7, 2023
1 parent 5369ccd commit 5d6b2da
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
14 changes: 10 additions & 4 deletions common/src/indexer_service/http/indexer_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

use std::{
collections::HashMap, fmt::Debug, net::SocketAddr, path::PathBuf, sync::Arc, time::Duration,
collections::HashMap, error::Error, fmt::Debug, net::SocketAddr, path::PathBuf, sync::Arc,
time::Duration,
};

use alloy_primitives::Address;
Expand All @@ -22,7 +23,7 @@ use eventuals::Eventual;
use reqwest::StatusCode;
use serde::{de::DeserializeOwned, Serialize};
use sqlx::postgres::PgPoolOptions;
use thegraph::types::DeploymentId;
use thegraph::types::{Attestation, DeploymentId};
use thiserror::Error;
use tokio::signal;
use tower::ServiceBuilder;
Expand All @@ -40,15 +41,20 @@ use crate::{

use super::{request_handler::request_handler, IndexerServiceConfig};

pub trait IsAttestable {
pub trait IndexerServiceResponse {
type Data: IntoResponse;
type Error: Error;

fn is_attestable(&self) -> bool;
fn as_str<'a>(&'a self) -> Result<&'a str, Self::Error>;
fn finalize(self, attestation: Option<Attestation>) -> Self::Data;
}

#[async_trait]
pub trait IndexerServiceImpl {
type Error: std::error::Error;
type Request: DeserializeOwned + Send + Debug + Serialize;
type Response: IntoResponse + Serialize + IsAttestable;
type Response: IndexerServiceResponse + Sized;
type State: Send + Sync;

async fn process_request(
Expand Down
3 changes: 2 additions & 1 deletion common/src/indexer_service/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ pub use config::{
SubgraphConfig,
};
pub use indexer_service::{
IndexerService, IndexerServiceImpl, IndexerServiceOptions, IndexerServiceRelease, IsAttestable,
IndexerService, IndexerServiceImpl, IndexerServiceOptions, IndexerServiceRelease,
IndexerServiceResponse,
};
18 changes: 6 additions & 12 deletions common/src/indexer_service/http/request_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use axum::{
};
use reqwest::StatusCode;
use thegraph::types::DeploymentId;
use tracing::{info, warn};
use tracing::trace;

use crate::{indexer_service::http::IndexerServiceResponse, prelude::AttestationSigner};

Expand All @@ -33,7 +33,7 @@ pub async fn request_handler<I>(
where
I: IndexerServiceImpl + Sync + Send + 'static,
{
info!("Handling request for deployment `{manifest_id}`");
trace!("Handling request for deployment `{manifest_id}`");

state
.metrics
Expand Down Expand Up @@ -96,20 +96,14 @@ where
(true, Some(signer)) => {
let req = serde_json::to_string(&request)
.map_err(|_| IndexerServiceError::FailedToSignAttestation)?;
let res = serde_json::to_string(&response)
let res = response
.as_str()
.map_err(|_| IndexerServiceError::FailedToSignAttestation)?;
Some(signer.create_attestation(&req, &res))
}
};

let mut headers = HeaderMap::new();
if let Some(attestation) = attestation {
let raw_attestation = serde_json::to_string(&attestation)
.map_err(|_| IndexerServiceError::FailedToProvideAttestation)?;
let header_value = HeaderValue::from_str(&raw_attestation)
.map_err(|_| IndexerServiceError::FailedToProvideAttestation)?;
headers.insert("graph-attestation", header_value);
}
let response = response.finalize(attestation);

Ok((StatusCode::OK, headers, response))
Ok((StatusCode::OK, response))
}

0 comments on commit 5d6b2da

Please sign in to comment.