From 25c208777e60b5de29c898919b57cc17e91d01f4 Mon Sep 17 00:00:00 2001 From: Jannis Pohlmann Date: Mon, 27 Nov 2023 23:03:45 +0100 Subject: [PATCH] WIP --- common/src/tap_manager.rs | 58 ++--------------- tap-agent/src/tap/receipt_checks_adapter.rs | 71 ++++++++++----------- 2 files changed, 41 insertions(+), 88 deletions(-) diff --git a/common/src/tap_manager.rs b/common/src/tap_manager.rs index 156a1dd8e..5c69c1b09 100644 --- a/common/src/tap_manager.rs +++ b/common/src/tap_manager.rs @@ -66,6 +66,7 @@ impl TapManager { error!("Failed to recover receipt signer: {}", e); anyhow!(e) })?; + if !self .escrow_accounts .value_immediate() @@ -88,7 +89,7 @@ impl TapManager { format!("{:?}", allocation_id) .trim_start_matches("0x") .to_owned(), - receipt_signer + receipt_signer .to_string() .trim_start_matches("0x") .to_owned(), @@ -111,63 +112,18 @@ impl TapManager { mod test { use std::str::FromStr; - use crate::prelude::{AllocationStatus, SubgraphDeployment}; + use crate::{ + prelude::{AllocationStatus, SubgraphDeployment}, + test_vectors::TAP_SENDER, + }; use alloy_primitives::Address; use keccak_hash::H256; use sqlx::postgres::PgListener; - use tap_core::tap_manager::SignedReceipt; - use tap_core::{eip_712_signed_message::EIP712SignedMessage, tap_receipt::Receipt}; - use toolshed::thegraph::DeploymentId; - use crate::test_vectors::{self, create_signed_receipt}; use super::*; - /// Fixture to generate a wallet and address - pub fn keys() -> (LocalWallet, Address) { - let wallet: LocalWallet = MnemonicBuilder::::default() - .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") - .build() - .unwrap(); - let address = wallet.address(); - - (wallet, Address::from_slice(address.as_bytes())) - } - - pub fn domain() -> Eip712Domain { - eip712_domain! { - name: "TAP", - version: "1", - chain_id: 1, - verifying_contract: Address::from([0x11u8; 20]), - } - } - - /// Fixture to generate a signed receipt using the wallet from `keys()` - /// and the given `query_id` and `value` - pub async fn create_signed_receipt( - allocation_id: Address, - nonce: u64, - timestamp_ns: u64, - value: u128, - ) -> SignedReceipt { - let (wallet, _) = keys(); - - EIP712SignedMessage::new( - &domain(), - Receipt { - allocation_id, - nonce, - timestamp_ns, - value, - }, - &wallet, - ) - .await - .unwrap() - } - #[sqlx::test(migrations = "../migrations")] async fn test_verify_and_store_receipt(pgpool: PgPool) { // Listen to pg_notify events @@ -207,7 +163,7 @@ mod test { // Mock escrow accounts let escrow_accounts = - Eventual::from_value(HashMap::from_iter(vec![(keys().1, U256::from(123))])); + Eventual::from_value(HashMap::from_iter(vec![(TAP_SENDER.1, U256::from(123))])); let tap_manager = TapManager::new( pgpool.clone(), diff --git a/tap-agent/src/tap/receipt_checks_adapter.rs b/tap-agent/src/tap/receipt_checks_adapter.rs index 0c0b09662..2da4d5ea4 100644 --- a/tap-agent/src/tap/receipt_checks_adapter.rs +++ b/tap-agent/src/tap/receipt_checks_adapter.rs @@ -7,7 +7,7 @@ use alloy_primitives::Address; use async_trait::async_trait; use ethereum_types::U256; use eventuals::{timer, Eventual, EventualExt}; -use indexer_common::subgraph_client::SubgraphClient; +use indexer_common::subgraph_client::{Query, SubgraphClient}; use serde_json::json; use sqlx::PgPool; use tap_core::adapters::receipt_checks_adapter::ReceiptChecksAdapter as ReceiptChecksAdapterTrait; @@ -153,45 +153,42 @@ impl ReceiptChecksAdapter { timer(Duration::from_millis(escrow_subgraph_polling_interval_ms)).map_with_retry( move |_| async move { let response = escrow_subgraph - .query::(&json!({ - "query": r#" - query ( - $sender_id: ID!, - $receiver_id: ID!, - $allocation_id: String! - ) { - transactions( - where: { - and: [ - { type: "redeem" } - { sender_: { id: $sender_id } } - { receiver_: { id: $receiver_id } } - { allocationID: $allocation_id } - ] - } - ) { - allocationID - sender { - id - } - } + .query::(Query::new_with_variables( + r#" + query ( + $sender_id: ID!, + $receiver_id: ID!, + $allocation_id: String! + ) { + transactions( + where: { + and: [ + { type: "redeem" } + { sender_: { id: $sender_id } } + { receiver_: { id: $receiver_id } } + { allocationID: $allocation_id } + ] } - "#, - "variables": { - "sender_id": sender_address.to_string(), - "receiver_id": indexer_address.to_string(), - "allocation_id": allocation_id.to_string(), - } - })) + ) { + allocationID + sender { + id + } + } + } + "#, + [ + ("sender_id", sender_address.to_string().into()), + ("receiver_id", indexer_address.to_string().into()), + ("allocation_id", allocation_id.to_string().into()), + ], + )) .await .map_err(|e| e.to_string())?; - let response = response.data.ok_or_else(|| { - format!( - "No data found in escrow subgraph response for allocation {} and sender {}", - allocation_id, sender_address - ) - })?; - Ok(!response.transactions.is_empty()) + + response + .map_err(|e| e.to_string()) + .map(|data| !data.transactions.is_empty()) }, move |error: String| { error!(