-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: update progress on allocation query response server
- Loading branch information
1 parent
0a595fd
commit 308ac05
Showing
12 changed files
with
244 additions
and
85 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use indexer_common::prelude::SubgraphDeployment; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{bootstrap::CREATED_AT_BLOCK_HASH, keys::Signer}; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub(crate) struct Allocations { | ||
meta: Meta, | ||
allocations: Vec<AllocationFragment>, | ||
} | ||
|
||
impl Allocations { | ||
pub fn new(indexer: Signer) -> Self { | ||
Allocations { | ||
meta: Meta { | ||
block: Block { | ||
number: 123, | ||
hash: CREATED_AT_BLOCK_HASH.into(), | ||
timestamp: "2021-01-01T00:00:00Z".into(), | ||
}, | ||
}, | ||
allocations: vec![AllocationFragment { | ||
id: indexer.allocation.id.to_string(), | ||
indexer: Indexer { | ||
id: indexer.allocation.indexer.to_string(), | ||
}, | ||
allocated_tokens: indexer.allocation.allocated_tokens.to_string(), | ||
created_at_block_hash: indexer.allocation.created_at_block_hash, | ||
created_at_epoch: indexer.allocation.created_at_epoch.to_string(), | ||
closed_at_epoch: indexer | ||
.allocation | ||
.closed_at_epoch | ||
.map(|epoch| epoch.to_string()), | ||
subgraph_deployment: indexer.allocation.subgraph_deployment, | ||
}], | ||
} | ||
} | ||
} | ||
|
||
#[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<String>, | ||
subgraph_deployment: SubgraphDeployment, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
struct Indexer { | ||
id: String, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
struct Meta { | ||
block: Block, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
struct Block { | ||
number: u128, | ||
hash: String, | ||
timestamp: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,98 @@ | ||
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use alloy::primitives::B256; | ||
use axum::{ | ||
http::StatusCode, | ||
response::IntoResponse, | ||
routing::{get, post}, | ||
Router, | ||
}; | ||
use indexer_common::prelude::{ | ||
Allocation, AllocationStatus, AttestationSigner, SubgraphDeployment, | ||
}; | ||
use thegraph_core::{address, Address, DeploymentId}; | ||
use tokio::net::TcpListener; | ||
use tracing::info; | ||
|
||
use crate::subgraph::{network_subgraph, NETWORK_SUBGRAPH_ROUTE}; | ||
use crate::{ | ||
keys::{get_indexer_address_from_toml, get_mnemonic_from_toml, Signer}, | ||
subgraph::{network_subgraph, NETWORK_SUBGRAPH_ROUTE}, | ||
}; | ||
|
||
const HOST: &str = "0.0.0.0"; | ||
const PORT: &str = "8000"; | ||
pub const CREATED_AT_BLOCK_HASH: &str = | ||
"0x0000000000000000000000000000000000000000000000000000000000000000"; | ||
|
||
pub async fn start_server() -> anyhow::Result<()> { | ||
let signer = Config::signer()?; | ||
info!("Starting server on {HOST}:{PORT}"); | ||
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(signer)), | ||
); | ||
|
||
Ok(axum::serve(listener, router).await?) | ||
} | ||
|
||
async fn health_check() -> impl IntoResponse { | ||
StatusCode::OK | ||
} | ||
|
||
struct Config; | ||
|
||
impl Config { | ||
fn signer() -> anyhow::Result<Signer> { | ||
let mnemonic = get_mnemonic_from_toml("indexer", "operator_mnemonic")?; | ||
let indexer_address = get_indexer_address_from_toml("indexer", "indexer_address")?; | ||
|
||
let subgraph_deployment_id = | ||
Self::create_deployment_id("QmUhiH6Z5xo6o3GNzsSvqpGKLmCt6w5A")?; | ||
|
||
let subgraph_deployment = SubgraphDeployment { | ||
id: DeploymentId::new(B256::from(subgraph_deployment_id)), | ||
denied_at: None, | ||
}; | ||
|
||
let allocation = Self::create_allocation(subgraph_deployment, indexer_address)?; | ||
|
||
let dispute_address = address!("33f9E93266ce0E108fc85DdE2f71dab555A0F05a"); | ||
|
||
let signer = Signer::new( | ||
AttestationSigner::new(&mnemonic, &allocation, 42161, dispute_address)?, | ||
allocation, | ||
); | ||
|
||
Ok(signer) | ||
} | ||
|
||
fn create_deployment_id(deployment_str: &str) -> anyhow::Result<[u8; 32]> { | ||
let id: [u8; 32] = deployment_str.as_bytes().try_into()?; | ||
Ok(id) | ||
} | ||
|
||
fn create_allocation( | ||
subgraph_deployment: SubgraphDeployment, | ||
indexer_address: Address, | ||
) -> anyhow::Result<Allocation> { | ||
Ok(Allocation { | ||
id: address!("5BcFE6215cbeB2D75cc3b09e01243cd7Ac55B3a7"), | ||
status: AllocationStatus::Active, | ||
subgraph_deployment, | ||
indexer: indexer_address, | ||
allocated_tokens: Default::default(), | ||
created_at_epoch: 1, | ||
created_at_block_hash: CREATED_AT_BLOCK_HASH.into(), | ||
closed_at_epoch: None, | ||
closed_at_epoch_start_block_hash: None, | ||
previous_epoch_start_block_hash: None, | ||
poi: None, | ||
query_fee_rebates: None, | ||
query_fees_collected: None, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#![allow(dead_code)] | ||
|
||
use std::{io, path::PathBuf}; | ||
|
||
use alloy::primitives::Address; | ||
use indexer_common::prelude::{Allocation, AttestationSigner}; | ||
use toml::Value; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Signer { | ||
pub signer: AttestationSigner, | ||
pub allocation: Allocation, | ||
} | ||
|
||
impl Signer { | ||
pub fn new(signer: AttestationSigner, allocation: Allocation) -> Self { | ||
Signer { signer, allocation } | ||
} | ||
} | ||
|
||
pub fn get_mnemonic_from_toml(index: &str, key: &str) -> io::Result<String> { | ||
Ok(get_from_toml(index, key)?.to_string()) | ||
} | ||
|
||
pub fn get_indexer_address_from_toml(index: &str, key: &str) -> io::Result<Address> { | ||
Ok(get_from_toml(index, key)?.parse().unwrap()) | ||
} | ||
|
||
fn get_from_toml(index: &str, key: &str) -> io::Result<String> { | ||
let toml = get_value_from_toml().unwrap(); | ||
|
||
if let Some(item) = toml | ||
.get(index) | ||
.and_then(|index| index.get(key)) | ||
.and_then(|value| value.as_str()) | ||
{ | ||
Ok(item.to_string()) | ||
} else { | ||
Err(io::Error::new( | ||
io::ErrorKind::Other, | ||
"Config item not found in TOML file", | ||
)) | ||
} | ||
} | ||
|
||
fn get_value_from_toml() -> io::Result<Value> { | ||
let config_path = get_config_path(); | ||
let content = std::fs::read_to_string(config_path)?; | ||
let toml: Value = content.parse().unwrap(); | ||
Ok(toml) | ||
} | ||
|
||
fn get_config_path() -> PathBuf { | ||
// CARGO_MANIFEST_DIR points to the root of the current crate | ||
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); | ||
path.push("config.toml"); // Add the file name | ||
path | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
mod allocations; | ||
pub mod bootstrap; | ||
mod keys; | ||
mod subgraph; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.