Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannis committed Nov 27, 2023
1 parent 43c0d8c commit 25c2087
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 88 deletions.
58 changes: 7 additions & 51 deletions common/src/tap_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl TapManager {
error!("Failed to recover receipt signer: {}", e);
anyhow!(e)
})?;

if !self
.escrow_accounts
.value_immediate()
Expand All @@ -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(),
Expand All @@ -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::<English>::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
Expand Down Expand Up @@ -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(),
Expand Down
71 changes: 34 additions & 37 deletions tap-agent/src/tap/receipt_checks_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<TransactionsResponse>(&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::<TransactionsResponse>(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!(
Expand Down

0 comments on commit 25c2087

Please sign in to comment.