Skip to content

Commit

Permalink
Squashme: Fix all test build issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannis committed Sep 18, 2023
1 parent 4f12cbb commit 7a4faaf
Show file tree
Hide file tree
Showing 13 changed files with 314 additions and 278 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

9 changes: 8 additions & 1 deletion common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ 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"
faux = { version = "0.1.10", optional = true }
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"] }
tokio = { version = "1.32.0", features = ["full", "macros", "rt"] }

[dev-dependencies]
env_logger = "0.9.0"
faux = "0.1.10"
test-log = "0.2.12"
wiremock = "0.5.19"

[features]
mock = ["dep:faux"]
3 changes: 2 additions & 1 deletion common/src/allocations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ pub fn allocation_signer(indexer_mnemonic: &str, allocation: &Allocation) -> Res
mod test {
use std::str::FromStr;

use crate::prelude::SubgraphDeploymentID;

use super::*;
use crate::common::types::SubgraphDeploymentID;

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

Expand Down
6 changes: 3 additions & 3 deletions common/src/allocations/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ struct AllocationMonitorInner {
watch_receiver: Receiver<()>,
}

#[cfg_attr(test, faux::create)]
#[cfg_attr(any(test, feature = "mock"), faux::create)]
#[derive(Debug, Clone)]
pub struct AllocationMonitor {
_monitor_handle: Arc<tokio::task::JoinHandle<()>>,
inner: Arc<AllocationMonitorInner>,
}

#[cfg_attr(test, faux::methods)]
#[cfg_attr(any(test, feature = "mock"), faux::methods)]
impl AllocationMonitor {
pub async fn new(
network_subgraph: NetworkSubgraph,
Expand Down Expand Up @@ -281,7 +281,7 @@ mod tests {
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

use crate::common::network_subgraph::NetworkSubgraph;
use crate::prelude::NetworkSubgraph;
use crate::test_vectors;

use super::*;
Expand Down
38 changes: 3 additions & 35 deletions common/src/attestations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ pub fn attestation_signer_for_allocation(

#[cfg(test)]
mod tests {
use alloy_primitives::Address;
use ethers_core::types::U256;
use std::str::FromStr;
use test_log::test;

use crate::test_vectors;
use crate::types::SubgraphDeploymentID;
use crate::prelude::{Allocation, AllocationStatus, SubgraphDeployment, SubgraphDeploymentID};

use super::*;

Expand Down Expand Up @@ -184,37 +185,4 @@ mod tests {
};
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);
}
}
}
50 changes: 48 additions & 2 deletions common/src/attestations/signers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct AttestationSigners {
}

#[derive(Debug)]
struct AttestationSignersInner {
pub(crate) struct AttestationSignersInner {
attestation_signers: Arc<RwLock<HashMap<Address, AttestationSigner>>>,
allocation_monitor: AllocationMonitor,
indexer_mnemonic: String,
Expand Down Expand Up @@ -53,7 +53,7 @@ impl AttestationSigners {
}
}

async fn update_attestation_signers(inner: Arc<AttestationSignersInner>) {
pub(crate) async fn update_attestation_signers(inner: Arc<AttestationSignersInner>) {
let mut attestation_signers_write = inner.attestation_signers.write().await;
for allocation in inner
.allocation_monitor
Expand Down Expand Up @@ -116,3 +116,49 @@ impl AttestationSigners {
self.inner.attestation_signers.read().await
}
}

#[cfg(test)]
mod tests {
use alloy_primitives::Address;
use ethers_core::types::U256;
use std::str::FromStr;
use std::sync::Arc;

use crate::prelude::AllocationMonitor;
use crate::test_vectors;

use super::*;

#[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);
}
}
}
11 changes: 9 additions & 2 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ pub mod network_subgraph;
pub mod signature_verification;
pub mod types;

#[cfg(test)]
mod test_vectors;

pub mod prelude {
pub use super::allocations::monitor::AllocationMonitor;
pub use super::allocations::{Allocation, AllocationStatus};
pub use super::attestations::{signer::AttestationSigner, signers::AttestationSigners};
pub use super::allocations::{Allocation, AllocationStatus, SubgraphDeployment};
pub use super::attestations::{
attestation_signer_for_allocation,
signer::{create_attestation_signer, AttestationSigner},
signers::AttestationSigners,
};
pub use super::network_subgraph::NetworkSubgraph;
pub use super::types::*;
}
12 changes: 4 additions & 8 deletions common/src/network_subgraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,11 @@ mod test {

let query = r#""{\"data\":{\"graphNetwork\":{\"currentEpoch\":960}}}""#;

let response = network_subgraph
// Check that the response is valid JSON
network_subgraph
.network_query(query.to_string(), None)
.await
.unwrap();

// Check that the response is valid JSON
let _json: serde_json::Value = serde_json::from_str(&response.graphql_response).unwrap();
}

#[tokio::test]
Expand All @@ -159,12 +157,10 @@ mod test {
}
"#;

let response = network_subgraph
// Check that the response is valid JSON
network_subgraph
.network_query(query.to_string(), None)
.await
.unwrap();

// Check that the response is valid JSON
let _json: serde_json::Value = serde_json::from_str(&response.graphql_response).unwrap();
}
}
Loading

0 comments on commit 7a4faaf

Please sign in to comment.