Skip to content

Commit

Permalink
refactor: move EscrowMonitor, GraphNode to common
Browse files Browse the repository at this point in the history
Signed-off-by: Alexis Asseman <[email protected]>
  • Loading branch information
aasseman committed Sep 27, 2023
1 parent 99930bb commit c599b80
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 96 deletions.
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ reqwest = "0.11.20"
secp256k1 = { version = "0.27.0", features = ["recovery"] }
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
thiserror = "1.0.48"
tokio = { version = "1.32.0", features = ["full", "macros", "rt"] }

[dev-dependencies]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;

use indexer_common::prelude::GraphQLQuery;

use crate::graph_node::GraphNodeInstance;
use crate::prelude::GraphQLQuery;

#[derive(Debug)]
struct EscrowMonitorInner {
Expand All @@ -23,14 +22,14 @@ struct EscrowMonitorInner {
sender_accounts: Arc<RwLock<HashMap<Address, U256>>>,
}

#[cfg_attr(test, faux::create)]
#[cfg_attr(any(test, feature = "mock"), faux::create)]
#[derive(Debug, Clone)]
pub struct EscrowMonitor {
_monitor_handle: Arc<tokio::task::JoinHandle<()>>,
inner: Arc<EscrowMonitorInner>,
}

#[cfg_attr(test, faux::methods)]
#[cfg_attr(any(test, feature = "mock"), faux::methods)]
impl EscrowMonitor {
pub async fn new(
graph_node: GraphNodeInstance,
Expand Down
30 changes: 28 additions & 2 deletions service/src/graph_node.rs → common/src/graph_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
use std::sync::Arc;

use anyhow::anyhow;
use ethers::types::Signature;
use reqwest::{header, Client, Url};

use crate::query_processor::{QueryError, UnattestedQueryResult};
use serde::{Deserialize, Serialize};

/// Graph node query wrapper.
///
Expand All @@ -17,6 +17,32 @@ pub struct GraphNodeInstance {
subgraphs_base_url: Arc<Url>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryResult {
#[serde(rename = "graphQLResponse")]
pub graphql_response: String,
pub attestation: Option<Signature>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnattestedQueryResult {
#[serde(rename = "graphQLResponse")]
pub graphql_response: String,
pub attestable: bool,
}

#[derive(Debug, thiserror::Error)]
pub enum QueryError {
#[error(transparent)]
Transport(#[from] reqwest::Error),
#[error("The subgraph is in a failed state")]
IndexingError,
#[error("Bad or invalid entity data found in the subgraph: {}", .0.to_string())]
BadData(anyhow::Error),
#[error("Unknown error: {0}")]
Other(anyhow::Error),
}

impl GraphNodeInstance {
pub fn new(endpoint: &str) -> GraphNodeInstance {
let subgraphs_base_url = Url::parse(endpoint)
Expand Down
4 changes: 4 additions & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

pub mod allocations;
pub mod attestations;
pub mod escrow_monitor;
pub mod graph_node;
pub mod network_subgraph;
pub mod signature_verification;
pub mod types;
Expand All @@ -18,6 +20,8 @@ pub mod prelude {
signer::{create_attestation_signer, AttestationSigner},
signers::AttestationSigners,
};
pub use super::escrow_monitor::EscrowMonitor;
pub use super::graph_node::*;
pub use super::network_subgraph::NetworkSubgraph;
pub use super::types::*;
}
36 changes: 36 additions & 0 deletions common/src/test_vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,39 @@ pub fn expected_eligible_allocations() -> HashMap<Address, Allocation> {
),
])
}

pub const ESCROW_QUERY_RESPONSE: &str = r#"
{
"data": {
"escrowAccounts": [
{
"balance": "34",
"totalAmountThawing": "10",
"sender": {
"id": "0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1"
}
},
{
"balance": "42",
"totalAmountThawing": "0",
"sender": {
"id": "0x22d491bde2303f2f43325b2108d26f1eaba1e32b"
}
}
]
}
}
"#;

pub fn expected_escrow_accounts() -> HashMap<Address, U256> {
HashMap::from([
(
Address::from_str("0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1").unwrap(),
U256::from(24),
),
(
Address::from_str("0x22d491bde2303f2f43325b2108d26f1eaba1e32b").unwrap(),
U256::from(42),
),
])
}
4 changes: 3 additions & 1 deletion service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use clap::{command, Args, Parser, ValueEnum};
use alloy_primitives::Address;
use serde::{Deserialize, Serialize};

use crate::{query_processor::QueryError, util::init_tracing};
use indexer_common::prelude::QueryError;

use crate::util::init_tracing;

#[derive(Clone, Debug, Parser, Serialize, Deserialize, Default)]
#[clap(
Expand Down
13 changes: 6 additions & 7 deletions service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use ethereum_types::U256;
use std::{net::SocketAddr, str::FromStr};
use tracing::info;

use indexer_common::prelude::{AllocationMonitor, AttestationSigners, NetworkSubgraph};
use indexer_common::prelude::{
AllocationMonitor, AttestationSigners, EscrowMonitor, GraphNodeInstance, NetworkSubgraph,
};

use util::{package_version, shutdown_signal};

Expand All @@ -22,8 +24,6 @@ use server::ServerOptions;

mod common;
mod config;
mod escrow_monitor;
mod graph_node;
mod metrics;
mod query_processor;
mod server;
Expand Down Expand Up @@ -54,9 +54,8 @@ async fn main() -> Result<(), std::io::Error> {
let release = package_version().expect("Failed to resolve for release version");

// Initialize graph-node client
let graph_node = graph_node::GraphNodeInstance::new(
&config.indexer_infrastructure.graph_node_query_endpoint,
);
let graph_node =
GraphNodeInstance::new(&config.indexer_infrastructure.graph_node_query_endpoint);

// Make an instance of network subgraph at either
// graph_node_query_endpoint/subgraphs/id/network_subgraph_deployment
Expand Down Expand Up @@ -97,7 +96,7 @@ async fn main() -> Result<(), std::io::Error> {
// assume the models are up to date in the service.
let indexer_management_db = database::connect(&config.postgres).await;

let escrow_monitor = escrow_monitor::EscrowMonitor::new(
let escrow_monitor = EscrowMonitor::new(
graph_node.clone(),
config.escrow_subgraph.escrow_subgraph_deployment,
config.ethereum.indexer_address,
Expand Down
28 changes: 1 addition & 27 deletions service/src/query_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,8 @@ use tap_core::tap_manager::SignedReceipt;

use indexer_common::prelude::{AttestationSigner, AttestationSigners, SubgraphDeploymentID};

use crate::graph_node::GraphNodeInstance;
use crate::tap_manager::TapManager;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryResult {
#[serde(rename = "graphQLResponse")]
pub graphql_response: String,
pub attestation: Option<Signature>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnattestedQueryResult {
#[serde(rename = "graphQLResponse")]
pub graphql_response: String,
pub attestable: bool,
}
use indexer_common::prelude::{GraphNodeInstance, QueryError, QueryResult, UnattestedQueryResult};

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct Response<T> {
Expand All @@ -47,18 +33,6 @@ pub struct PaidQuery {
pub receipt: String,
}

#[derive(Debug, thiserror::Error)]
pub enum QueryError {
#[error(transparent)]
Transport(#[from] reqwest::Error),
#[error("The subgraph is in a failed state")]
IndexingError,
#[error("Bad or invalid entity data found in the subgraph: {}", .0.to_string())]
BadData(anyhow::Error),
#[error("Unknown error: {0}")]
Other(anyhow::Error),
}

#[derive(Debug, Clone)]
pub struct QueryProcessor {
graph_node: GraphNodeInstance,
Expand Down
10 changes: 4 additions & 6 deletions service/src/tap_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ use sqlx::{types::BigDecimal, PgPool};
use std::sync::Arc;
use tap_core::tap_manager::SignedReceipt;

use indexer_common::prelude::AllocationMonitor;

use crate::{escrow_monitor, query_processor::QueryError};
use indexer_common::prelude::{AllocationMonitor, EscrowMonitor, QueryError};

#[derive(Clone, Debug)]
pub struct TapManager {
allocation_monitor: AllocationMonitor,
escrow_monitor: escrow_monitor::EscrowMonitor,
escrow_monitor: EscrowMonitor,
pgpool: PgPool,
domain_separator: Arc<Eip712Domain>,
}
Expand All @@ -23,7 +21,7 @@ impl TapManager {
pub fn new(
pgpool: PgPool,
allocation_monitor: AllocationMonitor,
escrow_monitor: escrow_monitor::EscrowMonitor,
escrow_monitor: EscrowMonitor,
domain_separator: Eip712Domain,
) -> Self {
Self {
Expand Down Expand Up @@ -175,7 +173,7 @@ mod test {
faux::when!(mock_allocation_monitor.is_allocation_eligible).then_return(true);

// Mock escrow monitor
let mut mock_escrow_monitor = escrow_monitor::EscrowMonitor::faux();
let mut mock_escrow_monitor = EscrowMonitor::faux();
faux::when!(mock_escrow_monitor.is_sender_eligible).then_return(true);

let tap_manager = TapManager::new(
Expand Down
45 changes: 0 additions & 45 deletions service/src/test_vectors.rs

This file was deleted.

0 comments on commit c599b80

Please sign in to comment.