-
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.
refactor: Move common utilities into indexer-common crate
- Loading branch information
Showing
25 changed files
with
742 additions
and
546 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
[workspace] | ||
members = [ | ||
"native", | ||
"common", | ||
"service", | ||
# "common", | ||
] | ||
resolver = "2" | ||
|
||
|
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,25 @@ | ||
[package] | ||
name = "indexer-common" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
alloy-primitives = { version = "0.3.3", features = ["serde"] } | ||
anyhow = "1.0.75" | ||
arc-swap = "1.6.0" | ||
bs58 = "0.5.0" | ||
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" | ||
keccak-hash = "0.10.0" | ||
lazy_static = "1.4.0" | ||
log = "0.4.20" | ||
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"] } | ||
|
||
[dev-dependencies] | ||
wiremock = "0.5.19" |
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,220 @@ | ||
// Copyright 2023-, GraphOps and Semiotic Labs. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use anyhow::Result; | ||
use ethers::signers::coins_bip39::English; | ||
use ethers::signers::MnemonicBuilder; | ||
use ethers::signers::Signer; | ||
use ethers::signers::Wallet; | ||
use ethers_core::k256::ecdsa::SigningKey; | ||
|
||
use crate::prelude::{Allocation, SubgraphDeploymentID}; | ||
|
||
pub mod signer; | ||
pub mod signers; | ||
|
||
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 attestation_signer_for_allocation( | ||
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 tests { | ||
use std::str::FromStr; | ||
use test_log::test; | ||
|
||
use crate::test_vectors; | ||
use crate::types::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() { | ||
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!( | ||
attestation_signer_for_allocation(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!(attestation_signer_for_allocation(INDEXER_OPERATOR_MNEMONIC, &allocation).is_err()); | ||
} | ||
|
||
#[test(tokio::test)] | ||
async fn test_update_attestation_signers() { | ||
unsafe { | ||
let mut mock_allocation_monitor = AllocationMonitor::faux(); | ||
|
||
faux::when!(mock_allocation_monitor.get_eligible_allocations).then_unchecked(|_| { | ||
// Spawn a thread to be able to call `blocking_read` on the RwLock, which actually spins its own async | ||
// runtime. | ||
// This is needed because `faux` will also use a runtime to mock the async function. | ||
let t = std::thread::spawn(|| { | ||
let eligible_allocations = Box::leak(Box::new(Arc::new(RwLock::new( | ||
test_vectors::expected_eligible_allocations(), | ||
)))); | ||
eligible_allocations.blocking_read() | ||
}); | ||
t.join().unwrap() | ||
}); | ||
|
||
let inner = Arc::new(AttestationSignersInner { | ||
attestation_signers: Arc::new(RwLock::new(HashMap::new())), | ||
allocation_monitor: mock_allocation_monitor, | ||
indexer_mnemonic: test_vectors::INDEXER_OPERATOR_MNEMONIC.to_string(), | ||
chain_id: U256::from(1), | ||
dispute_manager: Address::from_str(test_vectors::DISPUTE_MANAGER_ADDRESS).unwrap(), | ||
}); | ||
|
||
AttestationSigners::update_attestation_signers(inner.clone()).await; | ||
|
||
// Check that the attestation signers were found for the allocations | ||
assert_eq!(inner.attestation_signers.read().await.len(), 4); | ||
} | ||
} | ||
} |
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.