Skip to content

Commit 308ac05

Browse files
committed
wip: update progress on allocation query response server
1 parent 0a595fd commit 308ac05

File tree

12 files changed

+244
-85
lines changed

12 files changed

+244
-85
lines changed

Cargo.lock

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ sqlx = { version = "0.8.2", features = [
3535
"runtime-tokio",
3636
"rust_decimal",
3737
], default-features = false }
38+
toml = { version = "0.8.19", default-features = false }
3839
tracing = { version = "0.1.40", default-features = false }
3940
bigdecimal = "0.4.3"
4041
build-info = "0.0.39"

common/src/allocations/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use alloy::primitives::U256;
5-
use serde::{Deserialize, Deserializer};
5+
use serde::{Deserialize, Deserializer, Serialize};
66
use thegraph_core::{Address, DeploymentId};
77

88
pub mod monitor;
@@ -33,7 +33,7 @@ pub enum AllocationStatus {
3333
Claimed,
3434
}
3535

36-
#[derive(Clone, Debug, Eq, PartialEq, Deserialize)]
36+
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
3737
pub struct SubgraphDeployment {
3838
pub id: DeploymentId,
3939
#[serde(rename = "deniedAt")]

common/src/attestations/signers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn attestation_signers(
2929
dispute_manager_rx,
3030
move |(allocation, dispute)| {
3131
let indexer_mnemonic = indexer_mnemonic.clone();
32-
modify_sigers(
32+
modify_signers(
3333
&indexer_mnemonic,
3434
chain_id,
3535
attestation_signers_map,
@@ -39,7 +39,7 @@ pub fn attestation_signers(
3939
},
4040
)
4141
}
42-
fn modify_sigers(
42+
fn modify_signers(
4343
indexer_mnemonic: &str,
4444
chain_id: ChainId,
4545
attestation_signers_map: &'static Mutex<HashMap<Address, AttestationSigner>>,

config/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ regex = "1.11.0"
2121
sealed_test = "1.1.0"
2222
serde_test = "1.0.177"
2323
tempfile = "3.10.1"
24-
toml = { version = "0.8.12", default-features = false }
24+
toml.workspace = true
2525
tracing-test = "0.2.5"

local-service/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
2+
# SPDX-License-Identifier: Apache-2.0
3+
14
[package]
25
name = "local-service"
36
version = "0.1.0"
@@ -11,11 +14,15 @@ path = "src/main.rs"
1114
path = "src/lib.rs"
1215

1316
[dependencies]
17+
alloy = { workspace = true, features = ["k256"] }
1418
anyhow.workspace = true
1519
axum = { version = "0.7.5", features = ["macros"] }
20+
indexer-common = { path = "../common" }
1621
dotenvy = "0.15.7"
1722
serde = { workspace = true, features = ["derive"] }
1823
serde_json.workspace = true
24+
thegraph-core.workspace = true
1925
tokio = { workspace = true, features = ["full"] }
26+
toml.workspace = true
2027
tracing.workspace = true
2128
tracing-subscriber.workspace = true

local-service/src/allocations.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use indexer_common::prelude::SubgraphDeployment;
5+
use serde::{Deserialize, Serialize};
6+
7+
use crate::{bootstrap::CREATED_AT_BLOCK_HASH, keys::Signer};
8+
9+
#[derive(Debug, Serialize, Deserialize)]
10+
pub(crate) struct Allocations {
11+
meta: Meta,
12+
allocations: Vec<AllocationFragment>,
13+
}
14+
15+
impl Allocations {
16+
pub fn new(indexer: Signer) -> Self {
17+
Allocations {
18+
meta: Meta {
19+
block: Block {
20+
number: 123,
21+
hash: CREATED_AT_BLOCK_HASH.into(),
22+
timestamp: "2021-01-01T00:00:00Z".into(),
23+
},
24+
},
25+
allocations: vec![AllocationFragment {
26+
id: indexer.allocation.id.to_string(),
27+
indexer: Indexer {
28+
id: indexer.allocation.indexer.to_string(),
29+
},
30+
allocated_tokens: indexer.allocation.allocated_tokens.to_string(),
31+
created_at_block_hash: indexer.allocation.created_at_block_hash,
32+
created_at_epoch: indexer.allocation.created_at_epoch.to_string(),
33+
closed_at_epoch: indexer
34+
.allocation
35+
.closed_at_epoch
36+
.map(|epoch| epoch.to_string()),
37+
subgraph_deployment: indexer.allocation.subgraph_deployment,
38+
}],
39+
}
40+
}
41+
}
42+
43+
#[derive(Debug, Serialize, Deserialize)]
44+
#[serde(rename_all = "camelCase")]
45+
struct AllocationFragment {
46+
id: String,
47+
indexer: Indexer,
48+
allocated_tokens: String,
49+
created_at_block_hash: String,
50+
created_at_epoch: String,
51+
closed_at_epoch: Option<String>,
52+
subgraph_deployment: SubgraphDeployment,
53+
}
54+
55+
#[derive(Debug, Serialize, Deserialize)]
56+
struct Indexer {
57+
id: String,
58+
}
59+
60+
#[derive(Debug, Serialize, Deserialize)]
61+
struct Meta {
62+
block: Block,
63+
}
64+
65+
#[derive(Debug, Serialize, Deserialize)]
66+
struct Block {
67+
number: u128,
68+
hash: String,
69+
timestamp: String,
70+
}

local-service/src/bootstrap.rs

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,98 @@
1+
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use alloy::primitives::B256;
15
use axum::{
26
http::StatusCode,
37
response::IntoResponse,
48
routing::{get, post},
59
Router,
610
};
11+
use indexer_common::prelude::{
12+
Allocation, AllocationStatus, AttestationSigner, SubgraphDeployment,
13+
};
14+
use thegraph_core::{address, Address, DeploymentId};
715
use tokio::net::TcpListener;
16+
use tracing::info;
817

9-
use crate::subgraph::{network_subgraph, NETWORK_SUBGRAPH_ROUTE};
18+
use crate::{
19+
keys::{get_indexer_address_from_toml, get_mnemonic_from_toml, Signer},
20+
subgraph::{network_subgraph, NETWORK_SUBGRAPH_ROUTE},
21+
};
1022

1123
const HOST: &str = "0.0.0.0";
1224
const PORT: &str = "8000";
25+
pub const CREATED_AT_BLOCK_HASH: &str =
26+
"0x0000000000000000000000000000000000000000000000000000000000000000";
1327

1428
pub async fn start_server() -> anyhow::Result<()> {
29+
let signer = Config::signer()?;
30+
info!("Starting server on {HOST}:{PORT}");
1531
let port = dotenvy::var("API_PORT").unwrap_or(PORT.into());
1632
let listener = TcpListener::bind(&format!("{HOST}:{port}")).await?;
1733

18-
let router = Router::new()
19-
.route("/health", get(health_check))
20-
.route(NETWORK_SUBGRAPH_ROUTE, post(network_subgraph));
34+
let router = Router::new().route("/health", get(health_check)).route(
35+
NETWORK_SUBGRAPH_ROUTE,
36+
post(move || network_subgraph(signer)),
37+
);
2138

2239
Ok(axum::serve(listener, router).await?)
2340
}
2441

2542
async fn health_check() -> impl IntoResponse {
2643
StatusCode::OK
2744
}
45+
46+
struct Config;
47+
48+
impl Config {
49+
fn signer() -> anyhow::Result<Signer> {
50+
let mnemonic = get_mnemonic_from_toml("indexer", "operator_mnemonic")?;
51+
let indexer_address = get_indexer_address_from_toml("indexer", "indexer_address")?;
52+
53+
let subgraph_deployment_id =
54+
Self::create_deployment_id("QmUhiH6Z5xo6o3GNzsSvqpGKLmCt6w5A")?;
55+
56+
let subgraph_deployment = SubgraphDeployment {
57+
id: DeploymentId::new(B256::from(subgraph_deployment_id)),
58+
denied_at: None,
59+
};
60+
61+
let allocation = Self::create_allocation(subgraph_deployment, indexer_address)?;
62+
63+
let dispute_address = address!("33f9E93266ce0E108fc85DdE2f71dab555A0F05a");
64+
65+
let signer = Signer::new(
66+
AttestationSigner::new(&mnemonic, &allocation, 42161, dispute_address)?,
67+
allocation,
68+
);
69+
70+
Ok(signer)
71+
}
72+
73+
fn create_deployment_id(deployment_str: &str) -> anyhow::Result<[u8; 32]> {
74+
let id: [u8; 32] = deployment_str.as_bytes().try_into()?;
75+
Ok(id)
76+
}
77+
78+
fn create_allocation(
79+
subgraph_deployment: SubgraphDeployment,
80+
indexer_address: Address,
81+
) -> anyhow::Result<Allocation> {
82+
Ok(Allocation {
83+
id: address!("5BcFE6215cbeB2D75cc3b09e01243cd7Ac55B3a7"),
84+
status: AllocationStatus::Active,
85+
subgraph_deployment,
86+
indexer: indexer_address,
87+
allocated_tokens: Default::default(),
88+
created_at_epoch: 1,
89+
created_at_block_hash: CREATED_AT_BLOCK_HASH.into(),
90+
closed_at_epoch: None,
91+
closed_at_epoch_start_block_hash: None,
92+
previous_epoch_start_block_hash: None,
93+
poi: None,
94+
query_fee_rebates: None,
95+
query_fees_collected: None,
96+
})
97+
}
98+
}

local-service/src/keys.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#![allow(dead_code)]
5+
6+
use std::{io, path::PathBuf};
7+
8+
use alloy::primitives::Address;
9+
use indexer_common::prelude::{Allocation, AttestationSigner};
10+
use toml::Value;
11+
12+
#[derive(Clone, Debug)]
13+
pub struct Signer {
14+
pub signer: AttestationSigner,
15+
pub allocation: Allocation,
16+
}
17+
18+
impl Signer {
19+
pub fn new(signer: AttestationSigner, allocation: Allocation) -> Self {
20+
Signer { signer, allocation }
21+
}
22+
}
23+
24+
pub fn get_mnemonic_from_toml(index: &str, key: &str) -> io::Result<String> {
25+
Ok(get_from_toml(index, key)?.to_string())
26+
}
27+
28+
pub fn get_indexer_address_from_toml(index: &str, key: &str) -> io::Result<Address> {
29+
Ok(get_from_toml(index, key)?.parse().unwrap())
30+
}
31+
32+
fn get_from_toml(index: &str, key: &str) -> io::Result<String> {
33+
let toml = get_value_from_toml().unwrap();
34+
35+
if let Some(item) = toml
36+
.get(index)
37+
.and_then(|index| index.get(key))
38+
.and_then(|value| value.as_str())
39+
{
40+
Ok(item.to_string())
41+
} else {
42+
Err(io::Error::new(
43+
io::ErrorKind::Other,
44+
"Config item not found in TOML file",
45+
))
46+
}
47+
}
48+
49+
fn get_value_from_toml() -> io::Result<Value> {
50+
let config_path = get_config_path();
51+
let content = std::fs::read_to_string(config_path)?;
52+
let toml: Value = content.parse().unwrap();
53+
Ok(toml)
54+
}
55+
56+
fn get_config_path() -> PathBuf {
57+
// CARGO_MANIFEST_DIR points to the root of the current crate
58+
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
59+
path.push("config.toml"); // Add the file name
60+
path
61+
}

local-service/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
mod allocations;
15
pub mod bootstrap;
6+
mod keys;
27
mod subgraph;

0 commit comments

Comments
 (0)