Skip to content

Commit 99b4dbe

Browse files
authored
fix(common): attestation for free query (#297)
1 parent 409c515 commit 99b4dbe

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

common/src/indexer_service/http/indexer_service.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ pub trait IndexerServiceResponse {
5353

5454
fn is_attestable(&self) -> bool;
5555
fn as_str(&self) -> Result<&str, Self::Error>;
56-
fn finalize(self, attestation: Option<Attestation>) -> Self::Data;
56+
fn finalize(self, attestation: AttestationOutput) -> Self::Data;
57+
}
58+
59+
pub enum AttestationOutput {
60+
Attestation(Option<Attestation>),
61+
Attestable,
5762
}
5863

5964
#[async_trait]
@@ -81,8 +86,6 @@ where
8186
ServiceNotReady,
8287
#[error("No attestation signer found for allocation `{0}`")]
8388
NoSignerForAllocation(Address),
84-
#[error("No attestation signer found for manifest `{0}`")]
85-
NoSignerForManifest(DeploymentId),
8689
#[error("Invalid request body: {0}")]
8790
InvalidRequest(anyhow::Error),
8891
#[error("Error while processing the request: {0}")]
@@ -114,9 +117,7 @@ where
114117

115118
Unauthorized => StatusCode::UNAUTHORIZED,
116119

117-
NoSignerForAllocation(_) | NoSignerForManifest(_) | FailedToSignAttestation => {
118-
StatusCode::INTERNAL_SERVER_ERROR
119-
}
120+
NoSignerForAllocation(_) | FailedToSignAttestation => StatusCode::INTERNAL_SERVER_ERROR,
120121

121122
ReceiptError(_)
122123
| InvalidRequest(_)

common/src/indexer_service/http/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ pub use config::{
1313
ServerConfig, SubgraphConfig, TapConfig,
1414
};
1515
pub use indexer_service::{
16-
IndexerService, IndexerServiceImpl, IndexerServiceOptions, IndexerServiceRelease,
17-
IndexerServiceResponse,
16+
AttestationOutput, IndexerService, IndexerServiceImpl, IndexerServiceOptions,
17+
IndexerServiceRelease, IndexerServiceResponse,
1818
};

common/src/indexer_service/http/request_handler.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ use reqwest::StatusCode;
1414
use thegraph_core::DeploymentId;
1515
use tracing::trace;
1616

17-
use crate::{indexer_service::http::IndexerServiceResponse, prelude::AttestationSigner};
17+
use crate::indexer_service::http::IndexerServiceResponse;
1818

1919
use super::{
20-
indexer_service::{IndexerServiceError, IndexerServiceState},
20+
indexer_service::{AttestationOutput, IndexerServiceError, IndexerServiceState},
2121
tap_receipt_header::TapReceipt,
2222
IndexerServiceImpl,
2323
};
@@ -44,9 +44,7 @@ where
4444
let request =
4545
serde_json::from_slice(&body).map_err(|e| IndexerServiceError::InvalidRequest(e.into()))?;
4646

47-
let mut attestation_signer: Option<AttestationSigner> = None;
48-
49-
if let Some(receipt) = receipt.into_signed_receipt() {
47+
let attestation_signer = if let Some(receipt) = receipt.into_signed_receipt() {
5048
let allocation_id = receipt.message.allocation_id;
5149

5250
// Verify the receipt and store it in the database
@@ -63,12 +61,12 @@ where
6361
.value_immediate()
6462
.ok_or_else(|| IndexerServiceError::ServiceNotReady)?;
6563

66-
attestation_signer = Some(
64+
Some(
6765
signers
6866
.get(&allocation_id)
6967
.cloned()
7068
.ok_or_else(|| (IndexerServiceError::NoSignerForAllocation(allocation_id)))?,
71-
);
69+
)
7270
} else {
7371
match headers
7472
.get("authorization")
@@ -83,25 +81,29 @@ where
8381
}
8482
}
8583
}
86-
}
84+
None
85+
};
8786

8887
let (request, response) = state
8988
.service_impl
9089
.process_request(manifest_id, request)
9190
.await
9291
.map_err(IndexerServiceError::ProcessingError)?;
9392

94-
let attestation = match (response.is_attestable(), attestation_signer) {
95-
(false, _) => None,
96-
(true, None) => return Err(IndexerServiceError::NoSignerForManifest(manifest_id)),
97-
(true, Some(signer)) => {
93+
let attestation = match attestation_signer {
94+
Some(signer) => {
9895
let req = serde_json::to_string(&request)
9996
.map_err(|_| IndexerServiceError::FailedToSignAttestation)?;
10097
let res = response
10198
.as_str()
10299
.map_err(|_| IndexerServiceError::FailedToSignAttestation)?;
103-
Some(signer.create_attestation(&req, res))
100+
AttestationOutput::Attestation(
101+
response
102+
.is_attestable()
103+
.then(|| signer.create_attestation(&req, res)),
104+
)
104105
}
106+
None => AttestationOutput::Attestable,
105107
};
106108

107109
let response = response.finalize(attestation);

service/src/service.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ use std::time::Duration;
77
use super::{config::Config, error::SubgraphServiceError, routes};
88
use anyhow::anyhow;
99
use axum::{async_trait, routing::post, Json, Router};
10-
use indexer_common::indexer_service::http::{IndexerServiceImpl, IndexerServiceResponse};
10+
use indexer_common::indexer_service::http::{
11+
AttestationOutput, IndexerServiceImpl, IndexerServiceResponse,
12+
};
1113
use indexer_config::Config as MainConfig;
1214
use reqwest::Url;
1315
use serde_json::{json, Value};
1416
use sqlx::PgPool;
15-
use thegraph_core::{Attestation, DeploymentId};
17+
use thegraph_core::DeploymentId;
1618

1719
use crate::{cli::Cli, database};
1820

@@ -46,10 +48,14 @@ impl IndexerServiceResponse for SubgraphServiceResponse {
4648
Ok(self.inner.as_str())
4749
}
4850

49-
fn finalize(self, attestation: Option<Attestation>) -> Self::Data {
51+
fn finalize(self, attestation: AttestationOutput) -> Self::Data {
52+
let (attestation_key, attestation_value) = match attestation {
53+
AttestationOutput::Attestation(attestation) => ("attestation", json!(attestation)),
54+
AttestationOutput::Attestable => ("attestable", json!(self.is_attestable())),
55+
};
5056
Json(json!({
5157
"graphQLResponse": self.inner,
52-
"attestation": attestation
58+
attestation_key: attestation_value,
5359
}))
5460
}
5561
}

0 commit comments

Comments
 (0)