Skip to content

Commit

Permalink
refactor: use config crate for service (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyasmohd authored Oct 28, 2024
1 parent 470bf5a commit ff06fa6
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 278 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "1.1.0"
edition = "2021"

[dependencies]
indexer-config = { path = "../config" }
thiserror.workspace = true
async-trait.workspace = true
alloy.workspace = true
Expand Down Expand Up @@ -37,6 +38,7 @@ tower-http = { version = "0.5.2", features = [
"trace",
] }
tokio-util = "0.7.10"
bip39 = "2.0.0"

[dev-dependencies]
env_logger = { version = "0.11.0", default-features = false }
Expand Down
5 changes: 3 additions & 2 deletions common/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use alloy::signers::local::{
coins_bip39::English, LocalSignerError, MnemonicBuilder, PrivateKeySigner,
};
use bip39::Mnemonic;

/// Build Wallet from Private key or Mnemonic
pub fn build_wallet(value: &str) -> Result<PrivateKeySigner, LocalSignerError> {
Expand All @@ -13,8 +14,8 @@ pub fn build_wallet(value: &str) -> Result<PrivateKeySigner, LocalSignerError> {
}

// Format public key to a String
pub fn public_key(value: &str) -> Result<String, LocalSignerError> {
let wallet = build_wallet(value)?;
pub fn public_key(value: Mnemonic) -> Result<String, LocalSignerError> {
let wallet = build_wallet(&value.to_string())?;
let addr = format!("{:?}", wallet.address());
Ok(addr)
}
29 changes: 16 additions & 13 deletions common/src/attestations/signers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
// SPDX-License-Identifier: Apache-2.0

use bip39::Mnemonic;
use std::collections::HashMap;
use std::sync::Arc;
use thegraph_core::{Address, ChainId};
Expand All @@ -18,12 +19,13 @@ use crate::prelude::{Allocation, AttestationSigner};
/// An always up-to-date list of attestation signers, one for each of the indexer's allocations.
pub async fn attestation_signers(
mut indexer_allocations_rx: Receiver<HashMap<Address, Allocation>>,
indexer_mnemonic: String,
indexer_mnemonic: Mnemonic,
chain_id: ChainId,
mut dispute_manager_rx: Receiver<Option<Address>>,
) -> Receiver<HashMap<Address, AttestationSigner>> {
let attestation_signers_map: &'static Mutex<HashMap<Address, AttestationSigner>> =
Box::leak(Box::new(Mutex::new(HashMap::new())));
let indexer_mnemonic = indexer_mnemonic.to_string();

let starter_signers_map = modify_sigers(
Arc::new(indexer_mnemonic.clone()),
Expand Down Expand Up @@ -91,14 +93,17 @@ async fn modify_sigers(
if !signers.contains_key(id) {
let signer =
AttestationSigner::new(&indexer_mnemonic, allocation, chain_id, dispute_manager);
if let Err(e) = signer {
warn!(
"Failed to establish signer for allocation {}, deployment {}, createdAtEpoch {}: {}",
allocation.id, allocation.subgraph_deployment.id,
allocation.created_at_epoch, e
);
} else {
signers.insert(*id, signer.unwrap());
match signer {
Ok(signer) => {
signers.insert(*id, signer);
}
Err(e) => {
warn!(
"Failed to establish signer for allocation {}, deployment {}, createdAtEpoch {}: {}",
allocation.id, allocation.subgraph_deployment.id,
allocation.created_at_epoch, e
);
}
}
}
}
Expand All @@ -108,9 +113,7 @@ async fn modify_sigers(

#[cfg(test)]
mod tests {
use crate::test_vectors::{
DISPUTE_MANAGER_ADDRESS, INDEXER_ALLOCATIONS, INDEXER_OPERATOR_MNEMONIC,
};
use crate::test_vectors::{DISPUTE_MANAGER_ADDRESS, INDEXER_ALLOCATIONS, INDEXER_MNEMONIC};

use super::*;

Expand All @@ -123,7 +126,7 @@ mod tests {
.unwrap();
let mut signers = attestation_signers(
allocations_rx,
(*INDEXER_OPERATOR_MNEMONIC).to_string(),
INDEXER_MNEMONIC.clone(),
1,
dispute_manager_rx,
)
Expand Down
70 changes: 0 additions & 70 deletions common/src/indexer_service/http/config.rs

This file was deleted.

Loading

0 comments on commit ff06fa6

Please sign in to comment.