Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to use eventuals for monitoring network data #54

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ eip-712-derive = { git = "https://github.com/graphprotocol/eip-712-derive" }
ethereum-types = "0.14.1"
ethers = "2.0.10"
ethers-core = "2.0.10"
eventuals = "0.6.7"
faux = { version = "0.1.10", optional = true }
keccak-hash = "0.10.0"
lazy_static = "1.4.0"
log = "0.4.20"
lru = "0.11.1"
reqwest = "0.11.20"
secp256k1 = { version = "0.27.0", features = ["recovery"] }
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
tokio = { version = "1.32.0", features = ["full", "macros", "rt"] }
toolshed = { git = "https://github.com/edgeandnode/toolshed", tag = "v0.2.2", features = ["graphql"] }

[dev-dependencies]
env_logger = "0.9.0"
Expand Down
181 changes: 4 additions & 177 deletions common/src/allocations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@
// SPDX-License-Identifier: Apache-2.0

use alloy_primitives::Address;
use anyhow::Result;
use ethers::signers::coins_bip39::English;
use ethers::signers::{MnemonicBuilder, Signer, Wallet};
use ethers_core::k256::ecdsa::SigningKey;
use ethers_core::types::U256;
use serde::Deserialize;
use serde::Deserializer;
use serde::{Deserialize, Deserializer};

use crate::types::SubgraphDeploymentID;

pub mod monitor;

#[derive(Debug, Eq, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Allocation {
pub id: Address,
pub status: AllocationStatus,
Expand All @@ -31,7 +26,7 @@ pub struct Allocation {
pub query_fees_collected: Option<U256>,
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AllocationStatus {
Null,
Active,
Expand All @@ -40,7 +35,7 @@ pub enum AllocationStatus {
Claimed,
}

#[derive(Debug, Eq, PartialEq, Deserialize)]
#[derive(Clone, Debug, Eq, PartialEq, Deserialize)]
pub struct SubgraphDeployment {
pub id: SubgraphDeploymentID,
#[serde(rename = "deniedAt")]
Expand Down Expand Up @@ -94,171 +89,3 @@ impl<'d> Deserialize<'d> for Allocation {
})
}
}

pub fn derive_key_pair(
indexer_mnemonic: &str,
epoch: u64,
deployment: &SubgraphDeploymentID,
index: u64,
) -> Result<Wallet<SigningKey>> {
let mut derivation_path = format!("m/{}/", epoch);
derivation_path.push_str(
&deployment
.ipfs_hash()
.as_bytes()
.iter()
.map(|char| char.to_string())
.collect::<Vec<String>>()
.join("/"),
);
derivation_path.push_str(format!("/{}", index).as_str());

Ok(MnemonicBuilder::<English>::default()
.derivation_path(&derivation_path)
.expect("Valid derivation path")
.phrase(indexer_mnemonic)
.build()?)
}

pub fn allocation_signer(indexer_mnemonic: &str, allocation: &Allocation) -> Result<SigningKey> {
// Guess the allocation index by enumerating all indexes in the
// range [0, 100] and checking for a match
for i in 0..100 {
// The allocation was either created at the epoch it intended to or one
// epoch later. So try both both.
for created_at_epoch in [allocation.created_at_epoch, allocation.created_at_epoch - 1] {
let allocation_wallet = derive_key_pair(
indexer_mnemonic,
created_at_epoch,
&allocation.subgraph_deployment.id,
i,
)?;
if allocation_wallet.address().as_fixed_bytes() == allocation.id {
return Ok(allocation_wallet.signer().clone());
}
}
}
Err(anyhow::anyhow!(
"Could not find allocation signer for allocation {}",
allocation.id
))
}

#[cfg(test)]
mod test {
use std::str::FromStr;

use crate::prelude::SubgraphDeploymentID;

use super::*;

const INDEXER_OPERATOR_MNEMONIC: &str = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";

#[test]
fn test_derive_key_pair() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we keep these unit tests around?

assert_eq!(
derive_key_pair(
INDEXER_OPERATOR_MNEMONIC,
953,
&SubgraphDeploymentID::new(
"0xbbde25a2c85f55b53b7698b9476610c3d1202d88870e66502ab0076b7218f98a"
)
.unwrap(),
0
)
.unwrap()
.address()
.as_fixed_bytes(),
Address::from_str("0xfa44c72b753a66591f241c7dc04e8178c30e13af").unwrap()
);

assert_eq!(
derive_key_pair(
INDEXER_OPERATOR_MNEMONIC,
940,
&SubgraphDeploymentID::new(
"0xbbde25a2c85f55b53b7698b9476610c3d1202d88870e66502ab0076b7218f98a"
)
.unwrap(),
2
)
.unwrap()
.address()
.as_fixed_bytes(),
Address::from_str("0xa171cd12c3dde7eb8fe7717a0bcd06f3ffa65658").unwrap()
);
}

#[test]
fn test_allocation_signer() {
// Note that we use `derive_key_pair` to derive the private key

let allocation = Allocation {
id: Address::from_str("0xa171cd12c3dde7eb8fe7717a0bcd06f3ffa65658").unwrap(),
status: AllocationStatus::Null,
subgraph_deployment: SubgraphDeployment {
id: SubgraphDeploymentID::new(
"0xbbde25a2c85f55b53b7698b9476610c3d1202d88870e66502ab0076b7218f98a",
)
.unwrap(),
denied_at: None,
staked_tokens: U256::zero(),
signalled_tokens: U256::zero(),
query_fees_amount: U256::zero(),
},
indexer: Address::ZERO,
allocated_tokens: U256::zero(),
created_at_epoch: 940,
created_at_block_hash: "".to_string(),
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,
};
assert_eq!(
allocation_signer(INDEXER_OPERATOR_MNEMONIC, &allocation).unwrap(),
*derive_key_pair(
INDEXER_OPERATOR_MNEMONIC,
940,
&allocation.subgraph_deployment.id,
2
)
.unwrap()
.signer()
);
}

#[test]
fn test_allocation_signer_error() {
// Note that because allocation will try 200 derivations paths, this is a slow test

let allocation = Allocation {
// Purposefully wrong address
id: Address::from_str("0xdeadbeefcafebabedeadbeefcafebabedeadbeef").unwrap(),
status: AllocationStatus::Null,
subgraph_deployment: SubgraphDeployment {
id: SubgraphDeploymentID::new(
"0xbbde25a2c85f55b53b7698b9476610c3d1202d88870e66502ab0076b7218f98a",
)
.unwrap(),
denied_at: None,
staked_tokens: U256::zero(),
signalled_tokens: U256::zero(),
query_fees_amount: U256::zero(),
},
indexer: Address::ZERO,
allocated_tokens: U256::zero(),
created_at_epoch: 940,
created_at_block_hash: "".to_string(),
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,
};
assert!(allocation_signer(INDEXER_OPERATOR_MNEMONIC, &allocation).is_err());
}
}
Loading