Skip to content

Commit

Permalink
refactor: use more types from toolshed repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannis committed Sep 29, 2023
1 parent b171261 commit e9f4fab
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 321 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

60 changes: 20 additions & 40 deletions common/src/allocations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use ethers_core::k256::ecdsa::SigningKey;
use ethers_core::types::U256;
use serde::Deserialize;
use serde::Deserializer;

use crate::types::SubgraphDeploymentID;
use toolshed::thegraph::DeploymentId;

pub mod monitor;

Expand Down Expand Up @@ -42,7 +41,7 @@ pub enum AllocationStatus {

#[derive(Debug, Eq, PartialEq, Deserialize)]
pub struct SubgraphDeployment {
pub id: SubgraphDeploymentID,
pub id: DeploymentId,
#[serde(rename = "deniedAt")]
pub denied_at: Option<u64>,
#[serde(rename = "stakedTokens")]
Expand Down Expand Up @@ -98,14 +97,13 @@ impl<'d> Deserialize<'d> for Allocation {
pub fn derive_key_pair(
indexer_mnemonic: &str,
epoch: u64,
deployment: &SubgraphDeploymentID,
deployment: &DeploymentId,
index: u64,
) -> Result<Wallet<SigningKey>> {
let mut derivation_path = format!("m/{}/", epoch);
derivation_path.push_str(
&deployment
.ipfs_hash()
.as_bytes()
.0
.iter()
.map(|char| char.to_string())
.collect::<Vec<String>>()
Expand Down Expand Up @@ -147,44 +145,32 @@ pub fn allocation_signer(indexer_mnemonic: &str, allocation: &Allocation) -> Res
#[cfg(test)]
mod test {
use std::str::FromStr;

use crate::prelude::SubgraphDeploymentID;
use toolshed::thegraph::DeploymentId;

use super::*;

const INDEXER_OPERATOR_MNEMONIC: &str = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
const DEPLOYMENT_ID: DeploymentId = DeploymentId(
"0xbbde25a2c85f55b53b7698b9476610c3d1202d88870e66502ab0076b7218f98a"
.parse()
.unwrap(),
);

#[test]
fn test_derive_key_pair() {
assert_eq!(
derive_key_pair(
INDEXER_OPERATOR_MNEMONIC,
953,
&SubgraphDeploymentID::new(
"0xbbde25a2c85f55b53b7698b9476610c3d1202d88870e66502ab0076b7218f98a"
)
.unwrap(),
0
)
.unwrap()
.address()
.as_fixed_bytes(),
derive_key_pair(INDEXER_OPERATOR_MNEMONIC, 953, &DEPLOYMENT_ID, 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(),
derive_key_pair(INDEXER_OPERATOR_MNEMONIC, 940, &DEPLOYMENT_ID, 2)
.unwrap()
.address()
.as_fixed_bytes(),
Address::from_str("0xa171cd12c3dde7eb8fe7717a0bcd06f3ffa65658").unwrap()
);
}
Expand All @@ -197,10 +183,7 @@ mod test {
id: Address::from_str("0xa171cd12c3dde7eb8fe7717a0bcd06f3ffa65658").unwrap(),
status: AllocationStatus::Null,
subgraph_deployment: SubgraphDeployment {
id: SubgraphDeploymentID::new(
"0xbbde25a2c85f55b53b7698b9476610c3d1202d88870e66502ab0076b7218f98a",
)
.unwrap(),
id: DEPLOYMENT_ID,
denied_at: None,
staked_tokens: U256::zero(),
signalled_tokens: U256::zero(),
Expand Down Expand Up @@ -239,10 +222,7 @@ mod test {
id: Address::from_str("0xdeadbeefcafebabedeadbeefcafebabedeadbeef").unwrap(),
status: AllocationStatus::Null,
subgraph_deployment: SubgraphDeployment {
id: SubgraphDeploymentID::new(
"0xbbde25a2c85f55b53b7698b9476610c3d1202d88870e66502ab0076b7218f98a",
)
.unwrap(),
id: DEPLOYMENT_ID,
denied_at: None,
staked_tokens: U256::zero(),
signalled_tokens: U256::zero(),
Expand Down
2 changes: 1 addition & 1 deletion common/src/allocations/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl AllocationMonitor {
format!(
"{{allocation: {:?}, deployment: {}, closedAtEpoch: {:?})}}",
e.id,
e.subgraph_deployment.id.ipfs_hash(),
e.subgraph_deployment.id,
e.closed_at_epoch
)
})
Expand Down
8 changes: 4 additions & 4 deletions common/src/attestations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ use ethers::signers::MnemonicBuilder;
use ethers::signers::Signer;
use ethers::signers::Wallet;
use ethers_core::k256::ecdsa::SigningKey;
use toolshed::thegraph::DeploymentId;

use crate::prelude::{Allocation, SubgraphDeploymentID};
use crate::prelude::Allocation;

pub mod signer;
pub mod signers;

pub fn derive_key_pair(
indexer_mnemonic: &str,
epoch: u64,
deployment: &SubgraphDeploymentID,
deployment: &DeploymentId,
index: u64,
) -> Result<Wallet<SigningKey>> {
let mut derivation_path = format!("m/{}/", epoch);
derivation_path.push_str(
&deployment
.ipfs_hash()
.as_bytes()
.0
.iter()
.map(|char| char.to_string())
.collect::<Vec<String>>()
Expand Down
13 changes: 7 additions & 6 deletions common/src/attestations/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ use ethers_core::types::U256;
use keccak_hash::keccak;
use secp256k1::SecretKey;
use std::convert::TryInto;
use toolshed::thegraph::DeploymentId;

/// An attestation signer tied to a specific allocation via its signer key
#[derive(Debug, Clone)]
pub struct AttestationSigner {
subgraph_deployment_id: Bytes32,
subgraph_deployment_id: DeploymentId,
domain_separator: DomainSeparator,
signer: SecretKey,
}
Expand All @@ -25,7 +26,7 @@ impl AttestationSigner {
chain_id: eip_712_derive::U256,
dispute_manager: Address,
signer: SecretKey,
subgraph_deployment_id: Bytes32,
deployment_id: DeploymentId,
) -> Self {
let bytes = hex::decode("a070ffb1cd7409649bf77822cce74495468e06dbfaef09556838bf188679b9c2")
.unwrap();
Expand All @@ -44,7 +45,7 @@ impl AttestationSigner {
Self {
domain_separator,
signer,
subgraph_deployment_id,
subgraph_deployment_id: deployment_id,
}
}

Expand All @@ -55,7 +56,7 @@ impl AttestationSigner {
let receipt = Receipt {
request_cid,
response_cid,
subgraph_deployment_id: self.subgraph_deployment_id,
subgraph_deployment_id: *self.subgraph_deployment_id.0,
};

// Unwrap: This can only fail if the SecretKey is invalid.
Expand All @@ -69,7 +70,7 @@ impl AttestationSigner {
v,
r,
s,
subgraph_deployment_id: self.subgraph_deployment_id,
subgraph_deployment_id: *self.subgraph_deployment_id.0,
request_cid,
response_cid,
}
Expand Down Expand Up @@ -106,7 +107,7 @@ pub fn create_attestation_signer(
chain_id: U256,
dispute_manager_address: Address,
signer: SigningKey,
deployment_id: [u8; 32],
deployment_id: DeploymentId,
) -> anyhow::Result<AttestationSigner> {
// Tedious conversions to the "indexer_native" types
let mut chain_id_bytes = [0u8; 32];
Expand Down
7 changes: 3 additions & 4 deletions common/src/attestations/signers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,20 @@ impl AttestationSigners {
inner.chain_id,
inner.dispute_manager,
signer,
allocation.subgraph_deployment.id.bytes32(),
allocation.subgraph_deployment.id,
)
}) {
Ok(signer) => {
e.insert(signer);
info!(
"Found attestation signer for {{allocation: {}, deployment: {}}}",
allocation.id,
allocation.subgraph_deployment.id.ipfs_hash()
allocation.id, allocation.subgraph_deployment.id,
);
}
Err(e) => {
warn!(
"Failed to find the attestation signer for {{allocation: {}, deployment: {}, createdAtEpoch: {}, err: {}}}",
allocation.id, allocation.subgraph_deployment.id.ipfs_hash(), allocation.created_at_epoch, e
allocation.id, allocation.subgraph_deployment.id, allocation.created_at_epoch, e
)
}
}
Expand Down
2 changes: 0 additions & 2 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub mod attestations;
pub mod graphql;
pub mod network_subgraph;
pub mod signature_verification;
pub mod types;

#[cfg(test)]
mod test_vectors;
Expand All @@ -20,5 +19,4 @@ pub mod prelude {
signers::AttestationSigners,
};
pub use super::network_subgraph::NetworkSubgraph;
pub use super::types::*;
}
Loading

0 comments on commit e9f4fab

Please sign in to comment.