From 9116886942dd446328530c2741a1641b18c50fe7 Mon Sep 17 00:00:00 2001 From: Joseph Livesey Date: Thu, 24 Oct 2024 19:46:50 -0400 Subject: [PATCH] wip: update progress on allocation query response server --- Cargo.lock | 2 + local-service/Cargo.toml | 2 + local-service/src/allocations.rs | 74 ++++++++++++++++++++++++++++++ local-service/src/bootstrap.rs | 14 ++++-- local-service/src/keys.rs | 43 ++++++++++++++++++ local-service/src/lib.rs | 2 + local-service/src/main.rs | 1 + local-service/src/subgraph.rs | 77 +++----------------------------- tap-agent/src/tap/mod.rs | 2 +- 9 files changed, 140 insertions(+), 77 deletions(-) create mode 100644 local-service/src/allocations.rs create mode 100644 local-service/src/keys.rs diff --git a/Cargo.lock b/Cargo.lock index 635e3ce31..b5625d87e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3728,9 +3728,11 @@ checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" name = "local-service" version = "0.1.0" dependencies = [ + "alloy", "anyhow", "axum 0.7.7", "dotenvy", + "indexer-tap-agent", "serde", "serde_json", "tokio", diff --git a/local-service/Cargo.toml b/local-service/Cargo.toml index 7df4edcc9..2e69530c5 100644 --- a/local-service/Cargo.toml +++ b/local-service/Cargo.toml @@ -11,11 +11,13 @@ path = "src/main.rs" path = "src/lib.rs" [dependencies] +alloy = { workspace = true, features = ["k256"] } anyhow.workspace = true axum = { version = "0.7.5", features = ["macros"] } dotenvy = "0.15.7" serde = { workspace = true, features = ["derive"] } serde_json.workspace = true +indexer-tap-agent = { path = "../tap-agent" } tokio = { workspace = true, features = ["full"] } tracing.workspace = true tracing-subscriber.workspace = true diff --git a/local-service/src/allocations.rs b/local-service/src/allocations.rs new file mode 100644 index 000000000..9e6b3d830 --- /dev/null +++ b/local-service/src/allocations.rs @@ -0,0 +1,74 @@ +use serde::{Deserialize, Serialize}; + +use crate::keys; + +#[derive(Debug, Serialize, Deserialize)] +pub(crate) struct Allocations { + meta: Meta, + allocations: Vec, +} + +impl Allocations { + pub fn new(indexer: keys::Indexer) -> Self { + Allocations { + meta: Meta { + block: Block { + number: 123, + hash: "0x0000000000000000000000000000000000000000000000000000000000000000" + .into(), + timestamp: "2021-01-01T00:00:00Z".into(), + }, + }, + allocations: vec![AllocationFragment { + id: "0x123".into(), // TODO: "" + indexer: Indexer { + id: indexer.address.to_string(), + }, + allocated_tokens: "0".into(), + created_at_block_hash: + "0x0000000000000000000000000000000000000000000000000000000000000000".into(), + created_at_epoch: "1".into(), + closed_at_epoch: None, + subgraph_deployment: SubgraphDeployment { + id: "QmUhiH6Z5xo6o3GNzsSvqpGKLmCt6w5WzKQ1yHk6C8AA8S".into(), + denied_at: "0".into(), + }, + }], + } + } +} + +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +struct AllocationFragment { + id: String, + indexer: Indexer, + allocated_tokens: String, + created_at_block_hash: String, + created_at_epoch: String, + closed_at_epoch: Option, + subgraph_deployment: SubgraphDeployment, +} + +#[derive(Debug, Serialize, Deserialize)] +struct Indexer { + id: String, +} + +#[derive(Debug, Serialize, Deserialize)] +struct SubgraphDeployment { + id: String, + denied_at: String, +} + +#[derive(Debug, Serialize, Deserialize)] +struct Meta { + block: Block, +} + +#[derive(Debug, Serialize, Deserialize)] +struct Block { + number: u128, + hash: String, + timestamp: String, +} diff --git a/local-service/src/bootstrap.rs b/local-service/src/bootstrap.rs index c34df9fed..3ac61d490 100644 --- a/local-service/src/bootstrap.rs +++ b/local-service/src/bootstrap.rs @@ -6,18 +6,24 @@ use axum::{ }; use tokio::net::TcpListener; -use crate::subgraph::{network_subgraph, NETWORK_SUBGRAPH_ROUTE}; +use crate::{ + keys::manage_keys, + subgraph::{network_subgraph, NETWORK_SUBGRAPH_ROUTE}, +}; const HOST: &str = "0.0.0.0"; const PORT: &str = "8000"; pub async fn start_server() -> anyhow::Result<()> { + let (indexer, _sender) = manage_keys()?; + let port = dotenvy::var("API_PORT").unwrap_or(PORT.into()); let listener = TcpListener::bind(&format!("{HOST}:{port}")).await?; - let router = Router::new() - .route("/health", get(health_check)) - .route(NETWORK_SUBGRAPH_ROUTE, post(network_subgraph)); + let router = Router::new().route("/health", get(health_check)).route( + NETWORK_SUBGRAPH_ROUTE, + post(move || network_subgraph(indexer)), + ); Ok(axum::serve(listener, router).await?) } diff --git a/local-service/src/keys.rs b/local-service/src/keys.rs new file mode 100644 index 000000000..8e2225ef3 --- /dev/null +++ b/local-service/src/keys.rs @@ -0,0 +1,43 @@ +#![allow(dead_code)] + +use std::{ + fs::File, + io::{self, Write}, +}; + +use alloy::{ + primitives::Address, + signers::{k256::ecdsa::SigningKey, local::LocalSigner}, +}; +use indexer_tap_agent::tap::test_utils::wallet; + +#[derive(Clone, Debug)] +pub struct Indexer { + pub signer: LocalSigner, + pub address: Address, +} + +#[derive(Clone, Debug)] +pub struct QuerySender(pub Address); + +pub fn manage_keys() -> io::Result<(Indexer, QuerySender)> { + let (signer1, address1) = wallet(0); + let (signer2, address2) = wallet(1); + + write_to_file("indexer_wallet", &signer1)?; + write_to_file("sender_wallet", &signer2)?; + + let indexer = Indexer { + signer: signer1, + address: address1, + }; + let sender = QuerySender(address2); + + Ok((indexer, sender)) +} + +fn write_to_file(path: &str, signer: &LocalSigner) -> io::Result<()> { + let mut file = File::create(path)?; + file.write_all(signer.to_bytes().as_slice())?; + Ok(()) +} diff --git a/local-service/src/lib.rs b/local-service/src/lib.rs index aa17c731b..37d889ba8 100644 --- a/local-service/src/lib.rs +++ b/local-service/src/lib.rs @@ -1,2 +1,4 @@ +mod allocations; pub mod bootstrap; +mod keys; mod subgraph; diff --git a/local-service/src/main.rs b/local-service/src/main.rs index 0e07c51aa..3630269a7 100644 --- a/local-service/src/main.rs +++ b/local-service/src/main.rs @@ -5,6 +5,7 @@ use local_service::bootstrap::start_server; #[tokio::main] async fn main() -> ExitCode { tracing_subscriber::fmt::init(); + if let Err(e) = start_server().await { tracing::error!("Local Service error: {e}"); return ExitCode::from(1); diff --git a/local-service/src/subgraph.rs b/local-service/src/subgraph.rs index 493b665f6..4f3eec62d 100644 --- a/local-service/src/subgraph.rs +++ b/local-service/src/subgraph.rs @@ -1,82 +1,15 @@ use axum::{response::IntoResponse, Json}; use serde::{Deserialize, Serialize}; +use crate::{allocations::Allocations, keys::Indexer}; + pub(crate) const NETWORK_SUBGRAPH_ROUTE: &str = "/network_subgraph"; -pub(crate) async fn network_subgraph() -> impl IntoResponse { - Json(GraphqlResponse::Allocations(Allocations { - ..Default::default() - })) +pub(crate) async fn network_subgraph(indexer: Indexer) -> impl IntoResponse { + Json(GraphqlResponse::Allocations(Allocations::new(indexer))) } #[derive(Debug, Serialize, Deserialize)] -enum GraphqlResponse { +pub(crate) enum GraphqlResponse { Allocations(Allocations), } - -#[derive(Debug, Serialize, Deserialize)] -struct Allocations { - meta: Meta, - allocations: Vec, -} - -impl Default for Allocations { - fn default() -> Self { - Allocations { - meta: Meta { - block: Block { - number: 1, - hash: "0x123".into(), - timestamp: "2021-01-01T00:00:00Z".into(), - }, - }, - allocations: vec![AllocationFragment { - id: "0x123".into(), - indexer: Indexer { id: "0x456".into() }, - allocated_tokens: "100".into(), - created_at_block_hash: "0x789".into(), - created_at_epoch: "1".into(), - closed_at_epoch: None, - subgraph_deployment: SubgraphDeployment { - id: "0xabc".into(), - denied_at: "0".into(), - }, - }], - } - } -} - -#[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -struct AllocationFragment { - id: String, - indexer: Indexer, - allocated_tokens: String, - created_at_block_hash: String, - created_at_epoch: String, - closed_at_epoch: Option, - subgraph_deployment: SubgraphDeployment, -} - -#[derive(Debug, Serialize, Deserialize)] -struct Indexer { - id: String, -} - -#[derive(Debug, Serialize, Deserialize)] -struct SubgraphDeployment { - id: String, - denied_at: String, -} - -#[derive(Debug, Serialize, Deserialize)] -struct Meta { - block: Block, -} - -#[derive(Debug, Serialize, Deserialize)] -struct Block { - number: u128, - hash: String, - timestamp: String, -} diff --git a/tap-agent/src/tap/mod.rs b/tap-agent/src/tap/mod.rs index 76356ca92..003e949b5 100644 --- a/tap-agent/src/tap/mod.rs +++ b/tap-agent/src/tap/mod.rs @@ -10,7 +10,7 @@ use indexer_common::escrow_accounts::EscrowAccounts; pub mod context; pub mod escrow_adapter; -#[cfg(test)] +// #[cfg(test)] pub mod test_utils; pub async fn signers_trimmed(