Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tap v2 receipt parser #558

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 152 additions & 64 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ uuid = { version = "1.11.0", features = ["v7"] }
tracing = { version = "0.1.40", default-features = false }
bigdecimal = "0.4.3"
build-info = "0.0.39"
tap_core = { git = "https://github.com/semiotic-ai/timeline-aggregation-protocol", rev = "1c6e29f", default-features = false }
tap_aggregator = { git = "https://github.com/semiotic-ai/timeline-aggregation-protocol", rev = "1c6e29f", default-features = false }
tap_core = { git = "https://github.com/semiotic-ai/timeline-aggregation-protocol", rev = "3c56018", default-features = false }
tap_core_v2 = { git = "https://github.com/semiotic-ai/timeline-aggregation-protocol", rev = "1dada3e", package = "tap_core" }
tap_aggregator = { git = "https://github.com/semiotic-ai/timeline-aggregation-protocol", rev = "3c56018", default-features = false }
tracing-subscriber = { version = "0.3", features = [
"json",
"env-filter",
"ansi",
], default-features = false }
thegraph-core = { version = "0.9.6", features = [
thegraph-core = { git = "https://github.com/edgeandnode/toolshed", rev= "d710e05", features = [
"attestation",
"alloy-eip712",
"alloy-sol-types",
Expand Down
1 change: 1 addition & 0 deletions crates/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ async-graphql-axum = "7.0.11"
base64.workspace = true
graphql = { git = "https://github.com/edgeandnode/toolshed", tag = "graphql-v0.3.0" }
tap_core.workspace = true
tap_core_v2.workspace = true
uuid.workspace = true
typed-builder.workspace = true
tower_governor = { version = "0.5.0", features = ["axum"] }
Expand Down
15 changes: 13 additions & 2 deletions crates/service/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ pub enum IndexerServiceError {
SerializationError(#[from] serde_json::Error),

#[error("Issues with provided receipt: {0}")]
TapCoreError(#[from] tap_core::Error),
TapCoreErrorV1(#[from] tap_core::Error),

#[error("Issues with provided receipt: {0}")]
TapCoreErrorV2(#[from] tap_core_v2::Error),

#[error("There was an error while accessing escrow account: {0}")]
EscrowAccount(#[from] EscrowAccountsError),
}
Expand All @@ -37,11 +41,18 @@ impl StatusCodeExt for IndexerServiceError {
fn status_code(&self) -> StatusCode {
use IndexerServiceError as E;
match &self {
E::TapCoreError(ref error) => match error {
E::TapCoreErrorV1(ref error) => match error {
TapError::SignatureError(_)
| TapError::ReceiptError(ReceiptError::CheckFailure(_)) => StatusCode::BAD_REQUEST,
_ => StatusCode::INTERNAL_SERVER_ERROR,
},
E::TapCoreErrorV2(ref error) => match error {
tap_core_v2::Error::SignatureError(_)
| tap_core_v2::Error::ReceiptError(
tap_core_v2::receipt::ReceiptError::CheckFailure(_),
) => StatusCode::BAD_REQUEST,
_ => StatusCode::INTERNAL_SERVER_ERROR,
},
E::EscrowAccount(_) | E::ReceiptNotFound => StatusCode::PAYMENT_REQUIRED,
E::DeploymentIdNotFound => StatusCode::INTERNAL_SERVER_ERROR,
E::AxumError(_) | E::SerializationError(_) => StatusCode::BAD_GATEWAY,
Expand Down
3 changes: 2 additions & 1 deletion crates/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ mod metrics;
mod middleware;
mod routes;
pub mod service;
mod tap;
mod tap_v1;
mod tap_v2;
mod wallet;

pub use middleware::QueryBody;
10 changes: 6 additions & 4 deletions crates/service/src/middleware/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

mod bearer;
mod or;
mod tap;
mod tap_v1;
mod tap_v2;

pub use bearer::Bearer;
pub use or::OrExt;
pub use tap::tap_receipt_authorize;
pub use tap_v1::tap_receipt_authorize as tap_receipt_authorize_v1;
pub use tap_v2::tap_receipt_authorize as tap_receipt_authorize_v2;

#[cfg(test)]
mod tests {
Expand All @@ -28,7 +30,7 @@ mod tests {

use crate::{
middleware::auth::{self, Bearer, OrExt},
tap::IndexerTapContext,
tap_v1::IndexerTapContext,
};

const BEARER_TOKEN: &str = "test";
Expand All @@ -54,7 +56,7 @@ mod tests {
.unwrap(),
));
let free_query = Bearer::new(BEARER_TOKEN);
let tap_auth = auth::tap_receipt_authorize(tap_manager, metric);
let tap_auth = auth::tap_receipt_authorize_v1(tap_manager, metric);
let authorize_requests = free_query.or(tap_auth);

let authorization_middleware = AsyncRequireAuthorizationLayer::new(authorize_requests);
Expand Down
53 changes: 41 additions & 12 deletions crates/service/src/middleware/auth/or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{future::Future, marker::PhantomData, pin::Pin, task::Poll};

use axum::http::{Request, Response};
use pin_project::pin_project;
use thegraph_core::alloy::transports::BoxFuture;
use tower_http::{auth::AsyncAuthorizeRequest, validate_request::ValidateRequest};

/// Extension that allows using a simple .or() function and return an Or struct
Expand Down Expand Up @@ -49,29 +50,57 @@ where
Self(self.0.clone(), self.1.clone(), self.2)
}
}

impl<T, E, Req, Resp, Fut> AsyncAuthorizeRequest<Req> for Or<T, E, Req, Resp>
//
//impl<T, E, Req, Resp, Fut> AsyncAuthorizeRequest<Req> for Or<T, E, Req, Resp>
//where
// Req: 'static + Send,
// Resp: 'static + Send,
// T: ValidateRequest<Req, ResponseBody = Resp>,
// E: AsyncAuthorizeRequest<Req, RequestBody = Req, ResponseBody = Resp, Future = Fut>
// + Clone
// + 'static
// + Send,
// Fut: Future<Output = Result<Request<Req>, Response<Resp>>> + Send,
//{
// type RequestBody = Req;
// type ResponseBody = Resp;
//
// type Future = OrFuture<Fut, Req, Resp>;
//
// fn authorize(&mut self, mut request: axum::http::Request<Req>) -> Self::Future {
// let mut this = self.1.clone();
// if self.0.validate(&mut request).is_ok() {
// return OrFuture::with_result(Ok(request));
// }
// OrFuture::with_future(this.authorize(request))
// }
//}

impl<T, E, Req, Resp> AsyncAuthorizeRequest<Req> for Or<T, E, Req, Resp>
where
Req: 'static + Send,
Resp: 'static + Send,
T: ValidateRequest<Req, ResponseBody = Resp>,
E: AsyncAuthorizeRequest<Req, RequestBody = Req, ResponseBody = Resp, Future = Fut>
T: AsyncAuthorizeRequest<Req, RequestBody = Req, ResponseBody = Resp, Future = impl Send>
+ Clone
+ 'static
+ Send,
E: AsyncAuthorizeRequest<Req, RequestBody = Req, ResponseBody = Resp, Future = impl Send>
+ Clone
+ 'static
+ Send,
Fut: Future<Output = Result<Request<Req>, Response<Resp>>> + Send,
{
type RequestBody = Req;
type ResponseBody = Resp;

type Future = OrFuture<Fut, Req, Resp>;
type Future = BoxFuture<'static, Result<Request<Req>, Response<Resp>>>;

fn authorize(&mut self, mut request: axum::http::Request<Req>) -> Self::Future {
let mut this = self.1.clone();
if self.0.validate(&mut request).is_ok() {
return OrFuture::with_result(Ok(request));
}
OrFuture::with_future(this.authorize(request))
fn authorize(&mut self, request: axum::http::Request<Req>) -> Self::Future {
Box::pin(async move {
match self.0.authorize(request).await {
Ok(req) => Ok(req),
Err(_) => self.1.authorize(request).await,
}
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ mod tests {

use crate::{
middleware::{
auth::tap_receipt_authorize,
auth::tap_receipt_authorize_v1,
prometheus_metrics::{MetricLabelProvider, MetricLabels},
},
tap::IndexerTapContext,
tap_v1::IndexerTapContext,
};

#[fixture]
Expand Down Expand Up @@ -152,7 +152,7 @@ mod tests {
context,
CheckList::new(vec![Arc::new(MyCheck)]),
));
let tap_auth = tap_receipt_authorize(manager, metric);
let tap_auth = tap_receipt_authorize_v1(manager, metric);
let authorization_middleware = AsyncRequireAuthorizationLayer::new(tap_auth);

let mut service = ServiceBuilder::new()
Expand Down
Loading