Skip to content

Commit

Permalink
Merge branch 'main' into release-please--branches--main
Browse files Browse the repository at this point in the history
  • Loading branch information
gusinacio authored Jan 29, 2025
2 parents fe327da + 236fefe commit 344ed95
Show file tree
Hide file tree
Showing 32 changed files with 352 additions and 388 deletions.
268 changes: 158 additions & 110 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,23 @@ 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 = "6af1add", default-features = false }
tap_aggregator = { git = "https://github.com/semiotic-ai/timeline-aggregation-protocol", rev = "6af1add", default-features = false }
tap_core = { version = "3.0.0", default-features = false }
tap_aggregator = { version = "0.4.0", default-features = false }
tap_graph = { version = "0.1.0", default-features = false }
tracing-subscriber = { version = "0.3", features = [
"json",
"env-filter",
"ansi",
], default-features = false }
thegraph-core = { version = "0.10.0", features = [
thegraph-core = { version = "0.11.0", features = [
"attestation",
"alloy-eip712",
"alloy-sol-types",
"alloy-rlp",
"alloy-signers",
"alloy-signer-local",
"alloy-signer-mnemonic",
"serde"
"serde",
] }
thegraph-graphql-http = { version = "0.3.2", features = ["reqwest"] }
graphql_client = { version = "0.14.0", features = ["reqwest-rustls"] }
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_graph.workspace = true
uuid.workspace = true
typed-builder.workspace = true
tower_governor = { version = "0.5.0", features = ["axum"] }
Expand Down
8 changes: 6 additions & 2 deletions crates/service/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pub enum IndexerServiceError {

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

#[error("Issues with provided receipt: {0}")]
Eip712Error(#[from] tap_core::signed_message::Eip712Error),

#[error("There was an error while accessing escrow account: {0}")]
EscrowAccount(#[from] EscrowAccountsError),
}
Expand All @@ -38,13 +42,13 @@ impl StatusCodeExt for IndexerServiceError {
use IndexerServiceError as E;
match &self {
E::TapCoreError(ref error) => match error {
TapError::SignatureError(_)
| TapError::ReceiptError(ReceiptError::CheckFailure(_)) => StatusCode::BAD_REQUEST,
TapError::ReceiptError(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,
E::Eip712Error(_) => StatusCode::BAD_REQUEST,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/service/src/middleware/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use axum::{
middleware::Next,
response::Response,
};
use tap_core::receipt::SignedReceipt;
use tap_graph::SignedReceipt;
use thegraph_core::{alloy::primitives::Address, DeploymentId};
use tokio::sync::watch;

Expand Down
20 changes: 9 additions & 11 deletions crates/service/src/middleware/auth/tap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ use axum::{
};
use tap_core::{
manager::{adapters::ReceiptStore, Manager},
receipt::{Context, SignedReceipt},
receipt::Context,
};
use tap_graph::{ReceiptAggregateVoucher, SignedReceipt};
use tower_http::auth::AsyncAuthorizeRequest;

use crate::{error::IndexerServiceError, middleware::prometheus_metrics::MetricLabels};
Expand All @@ -30,7 +31,7 @@ use crate::{error::IndexerServiceError, middleware::prometheus_metrics::MetricLa
///
/// Requires SignedReceipt, MetricLabels and Arc<Context> extensions
pub fn tap_receipt_authorize<T, B>(
tap_manager: Arc<Manager<T>>,
tap_manager: Arc<Manager<T, SignedReceipt, ReceiptAggregateVoucher>>,
failed_receipt_metric: &'static prometheus::CounterVec,
) -> impl AsyncAuthorizeRequest<
B,
Expand All @@ -40,7 +41,7 @@ pub fn tap_receipt_authorize<T, B>(
> + Clone
+ Send
where
T: ReceiptStore + Sync + Send + 'static,
T: ReceiptStore<SignedReceipt> + Sync + Send + 'static,
B: Send,
{
move |request: Request<B>| {
Expand Down Expand Up @@ -88,12 +89,9 @@ mod tests {
use sqlx::PgPool;
use tap_core::{
manager::Manager,
receipt::{
checks::{Check, CheckError, CheckList, CheckResult},
state::Checking,
ReceiptWithState,
},
receipt::checks::{Check, CheckError, CheckList, CheckResult},
};
use tap_graph::SignedReceipt;
use test_assets::{
assert_while_retry, create_signed_receipt, SignedReceiptRequest, TAP_EIP712_DOMAIN,
};
Expand All @@ -105,7 +103,7 @@ mod tests {
auth::tap_receipt_authorize,
prometheus_metrics::{MetricLabelProvider, MetricLabels},
},
tap::IndexerTapContext,
tap::{CheckingReceipt, IndexerTapContext},
};

#[fixture]
Expand Down Expand Up @@ -133,11 +131,11 @@ mod tests {

struct MyCheck;
#[async_trait::async_trait]
impl Check for MyCheck {
impl Check<SignedReceipt> for MyCheck {
async fn check(
&self,
_: &tap_core::receipt::Context,
receipt: &ReceiptWithState<Checking>,
receipt: &CheckingReceipt,
) -> CheckResult {
if receipt.signed_receipt().message.nonce == FAILED_NONCE {
Err(CheckError::Failed(anyhow::anyhow!("Failed")))
Expand Down
2 changes: 1 addition & 1 deletion crates/service/src/middleware/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use axum::{
response::Response,
};
use indexer_monitor::EscrowAccounts;
use tap_core::receipt::SignedReceipt;
use tap_graph::SignedReceipt;
use thegraph_core::alloy::{primitives::Address, sol_types::Eip712Domain};
use tokio::sync::watch;

Expand Down
2 changes: 1 addition & 1 deletion crates/service/src/middleware/tap_receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mod tests {
};
use axum_extra::headers::Header;
use reqwest::StatusCode;
use tap_core::receipt::SignedReceipt;
use tap_graph::SignedReceipt;
use test_assets::{create_signed_receipt, SignedReceiptRequest};
use tower::ServiceExt;

Expand Down
2 changes: 1 addition & 1 deletion crates/service/src/service/tap_receipt_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use axum_extra::headers::{self, Header, HeaderName, HeaderValue};
use lazy_static::lazy_static;
use prometheus::{register_counter, Counter};
use tap_core::receipt::SignedReceipt;
use tap_graph::SignedReceipt;

#[derive(Debug, PartialEq)]
pub struct TapReceipt(pub SignedReceipt);
Expand Down
7 changes: 5 additions & 2 deletions crates/service/src/tap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use indexer_allocation::Allocation;
use indexer_monitor::EscrowAccounts;
use receipt_store::{DatabaseReceipt, InnerContext};
use sqlx::PgPool;
use tap_core::receipt::checks::ReceiptCheck;
use tap_core::receipt::{checks::ReceiptCheck, state::Checking, ReceiptWithState};
use tap_graph::SignedReceipt;
use thegraph_core::alloy::{primitives::Address, sol_types::Eip712Domain};
use tokio::sync::{
mpsc::{self, Sender},
Expand All @@ -26,6 +27,8 @@ mod receipt_store;

pub use checks::value_check::AgoraQuery;

pub type CheckingReceipt = ReceiptWithState<Checking, SignedReceipt>;

const GRACE_PERIOD: u64 = 60;

#[derive(Clone)]
Expand All @@ -48,7 +51,7 @@ impl IndexerTapContext {
escrow_accounts: Receiver<EscrowAccounts>,
timestamp_error_tolerance: Duration,
receipt_max_value: u128,
) -> Vec<ReceiptCheck> {
) -> Vec<ReceiptCheck<SignedReceipt>> {
vec![
Arc::new(AllocationEligible::new(indexer_allocations)),
Arc::new(SenderBalanceCheck::new(escrow_accounts)),
Expand Down
13 changes: 6 additions & 7 deletions crates/service/src/tap/checks/allocation_eligible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ use std::collections::HashMap;

use anyhow::anyhow;
use indexer_allocation::Allocation;
use tap_core::receipt::{
checks::{Check, CheckError, CheckResult},
state::Checking,
ReceiptWithState,
};
use tap_core::receipt::checks::{Check, CheckError, CheckResult};
use tap_graph::SignedReceipt;
use thegraph_core::alloy::primitives::Address;
use tokio::sync::watch::Receiver;

use crate::tap::CheckingReceipt;

pub struct AllocationEligible {
indexer_allocations: Receiver<HashMap<Address, Allocation>>,
}
Expand All @@ -25,11 +24,11 @@ impl AllocationEligible {
}
}
#[async_trait::async_trait]
impl Check for AllocationEligible {
impl Check<SignedReceipt> for AllocationEligible {
async fn check(
&self,
_: &tap_core::receipt::Context,
receipt: &ReceiptWithState<Checking>,
receipt: &CheckingReceipt,
) -> CheckResult {
let allocation_id = receipt.signed_receipt().message.allocation_id;
if !self
Expand Down
23 changes: 8 additions & 15 deletions crates/service/src/tap/checks/deny_list_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ use std::{
};

use sqlx::{postgres::PgListener, PgPool};
use tap_core::receipt::{
checks::{Check, CheckError, CheckResult},
state::Checking,
ReceiptWithState,
};
use tap_core::receipt::checks::{Check, CheckError, CheckResult};
use tap_graph::SignedReceipt;
use thegraph_core::alloy::primitives::Address;

use crate::middleware::Sender;
use crate::{middleware::Sender, tap::CheckingReceipt};

pub struct DenyListCheck {
sender_denylist: Arc<RwLock<HashSet<Address>>>,
Expand Down Expand Up @@ -153,12 +150,8 @@ impl DenyListCheck {
}

#[async_trait::async_trait]
impl Check for DenyListCheck {
async fn check(
&self,
ctx: &tap_core::receipt::Context,
_: &ReceiptWithState<Checking>,
) -> CheckResult {
impl Check<SignedReceipt> for DenyListCheck {
async fn check(&self, ctx: &tap_core::receipt::Context, _: &CheckingReceipt) -> CheckResult {
let Sender(receipt_sender) = ctx
.get::<Sender>()
.ok_or(CheckError::Failed(anyhow::anyhow!("Could not find sender")))?;
Expand Down Expand Up @@ -191,7 +184,7 @@ impl Drop for DenyListCheck {
#[cfg(test)]
mod tests {
use sqlx::PgPool;
use tap_core::receipt::{checks::Check, Context, ReceiptWithState};
use tap_core::receipt::{checks::Check, Context};
use test_assets::{self, create_signed_receipt, SignedReceiptRequest, TAP_SENDER};
use thegraph_core::alloy::hex::ToHexExt;

Expand Down Expand Up @@ -220,7 +213,7 @@ mod tests {

let deny_list_check = new_deny_list_check(pgpool.clone()).await;

let checking_receipt = ReceiptWithState::new(signed_receipt);
let checking_receipt = CheckingReceipt::new(signed_receipt);

let mut ctx = Context::new();
ctx.insert(Sender(TAP_SENDER.1));
Expand All @@ -239,7 +232,7 @@ mod tests {
let deny_list_check = new_deny_list_check(pgpool.clone()).await;

// Check that the receipt is valid
let checking_receipt = ReceiptWithState::new(signed_receipt);
let checking_receipt = CheckingReceipt::new(signed_receipt);

let mut ctx = Context::new();
ctx.insert(Sender(TAP_SENDER.1));
Expand Down
26 changes: 13 additions & 13 deletions crates/service/src/tap/checks/receipt_max_val_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ pub struct ReceiptMaxValueCheck {
receipt_max_value: u128,
}

use tap_core::receipt::{
checks::{Check, CheckError, CheckResult},
state::Checking,
ReceiptWithState,
};
use tap_core::receipt::checks::{Check, CheckError, CheckResult};
use tap_graph::SignedReceipt;

use crate::tap::CheckingReceipt;

impl ReceiptMaxValueCheck {
pub fn new(receipt_max_value: u128) -> Self {
Expand All @@ -19,11 +18,11 @@ impl ReceiptMaxValueCheck {
}

#[async_trait::async_trait]
impl Check for ReceiptMaxValueCheck {
impl Check<SignedReceipt> for ReceiptMaxValueCheck {
async fn check(
&self,
_: &tap_core::receipt::Context,
receipt: &ReceiptWithState<Checking>,
receipt: &CheckingReceipt,
) -> CheckResult {
let receipt_value = receipt.signed_receipt().message.value;

Expand All @@ -42,19 +41,20 @@ mod tests {
use std::time::{Duration, SystemTime};

use tap_core::{
receipt::{checks::Check, state::Checking, Context, Receipt, ReceiptWithState},
signed_message::EIP712SignedMessage,
receipt::{checks::Check, Context},
signed_message::Eip712SignedMessage,
tap_eip712_domain,
};
use tap_graph::Receipt;
use thegraph_core::alloy::{
primitives::{address, Address},
signers::local::{coins_bip39::English, MnemonicBuilder, PrivateKeySigner},
};

use super::*;
use crate::tap::Eip712Domain;
use crate::tap::{CheckingReceipt, Eip712Domain};

fn create_signed_receipt_with_custom_value(value: u128) -> ReceiptWithState<Checking> {
fn create_signed_receipt_with_custom_value(value: u128) -> CheckingReceipt {
let index: u32 = 0;
let wallet: PrivateKeySigner = MnemonicBuilder::<English>::default()
.phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about")
Expand All @@ -75,7 +75,7 @@ mod tests {

let value: u128 = value;
let nonce: u64 = 10;
let receipt = EIP712SignedMessage::new(
let receipt = Eip712SignedMessage::new(
&eip712_domain_separator,
Receipt {
allocation_id: address!("abababababababababababababababababababab"),
Expand All @@ -86,7 +86,7 @@ mod tests {
&wallet,
)
.unwrap();
ReceiptWithState::<Checking>::new(receipt)
CheckingReceipt::new(receipt)
}

const RECEIPT_LIMIT: u128 = 10;
Expand Down
17 changes: 5 additions & 12 deletions crates/service/src/tap/checks/sender_balance_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@

use anyhow::anyhow;
use indexer_monitor::EscrowAccounts;
use tap_core::receipt::{
checks::{Check, CheckError, CheckResult},
state::Checking,
ReceiptWithState,
};
use tap_core::receipt::checks::{Check, CheckError, CheckResult};
use tap_graph::SignedReceipt;
use thegraph_core::alloy::primitives::U256;
use tokio::sync::watch::Receiver;

use crate::middleware::Sender;
use crate::{middleware::Sender, tap::CheckingReceipt};

pub struct SenderBalanceCheck {
escrow_accounts: Receiver<EscrowAccounts>,
Expand All @@ -24,12 +21,8 @@ impl SenderBalanceCheck {
}

#[async_trait::async_trait]
impl Check for SenderBalanceCheck {
async fn check(
&self,
ctx: &tap_core::receipt::Context,
_: &ReceiptWithState<Checking>,
) -> CheckResult {
impl Check<SignedReceipt> for SenderBalanceCheck {
async fn check(&self, ctx: &tap_core::receipt::Context, _: &CheckingReceipt) -> CheckResult {
let escrow_accounts_snapshot = self.escrow_accounts.borrow();

let Sender(receipt_sender) = ctx
Expand Down
Loading

0 comments on commit 344ed95

Please sign in to comment.