Skip to content

Commit

Permalink
fix(blueprint-test-utils): improve efficiency in handling of keys and…
Browse files Browse the repository at this point in the history
… environment in tests (#431)
  • Loading branch information
Tjemmmic authored Nov 2, 2024
1 parent 7ae3303 commit c25447c
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 128 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.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ lock_api = "0.4.12"
log = "0.4.22"
multiaddr = { version = "0.18.1", default-features = false }
nix = { version = "0.29.0", features = ["process", "signal"] }
num-bigint = "0.4.6"
parking_lot = "0.12.3"
proc-macro2 = "1.0"
prometheus = { version = "0.13.4", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions blueprint-test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ alloy-transport = { workspace = true }
# Utility
testcontainers = { workspace = true }
uuid = { workspace = true, features = ["v4"] }
tempfile = { workspace = true }

[dev-dependencies]
cargo_metadata = { workspace = true }
65 changes: 30 additions & 35 deletions blueprint-test-utils/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
use crate::test_ext::{find_open_tcp_bind_port, NAME_IDS};
use alloy_contract::{CallBuilder, CallDecoder};
use alloy_primitives::hex;
use alloy_provider::{network::Ethereum, Provider};
use alloy_rpc_types::TransactionReceipt;
use alloy_transport::{Transport, TransportError};
use gadget_io::SupportedChains;
use gadget_sdk::config::protocol::{EigenlayerContractAddresses, SymbioticContractAddresses};
use gadget_sdk::config::Protocol;
use gadget_sdk::error;
use gadget_sdk::keystore::backend::fs::FilesystemKeystore;
use gadget_sdk::keystore::Backend;
use std::collections::HashMap;
use std::net::IpAddr;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::Arc;
use thiserror::Error;
use tokio::process::Child;
use tokio::sync::Mutex;
use url::Url;

use crate::test_ext::{find_open_tcp_bind_port, NAME_IDS};
use crate::{inject_test_keys, KeyGenType};
use alloy_provider::{network::Ethereum, Provider};
use alloy_transport::{Transport, TransportError};
use gadget_io::SupportedChains;

use gadget_sdk::config::Protocol;

#[derive(Error, Debug)]
pub enum BlueprintError {
#[error("Transport error occurred: {0}")]
Expand Down Expand Up @@ -88,9 +89,10 @@ impl BlueprintProcessManager {
http_endpoint: &str,
ws_endpoint: &str,
protocol: Protocol,
keystore_path: &str,
) -> Result<BlueprintProcess, std::io::Error> {
let tmp_dir = std::env::current_dir()?;
let keystore_uri = tmp_dir.join(format!(
let keystore_path = Path::new(&keystore_path);
let keystore_uri = keystore_path.join(format!(
"keystores/{}/{}",
NAME_IDS[instance_id].to_lowercase(),
uuid::Uuid::new_v4()
Expand All @@ -105,33 +107,24 @@ impl BlueprintProcessManager {
std::path::absolute(&keystore_uri).expect("Failed to resolve keystore URI");
let keystore_uri_str = format!("file:{}", keystore_uri_normalized.display());

let filesystem_keystore = FilesystemKeystore::open(keystore_uri_str.clone())
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
// let _ = ANVIL_PRIVATE_KEYS.iter().enumerate().map(|(key, _)| inject_test_keys(&keystore_uri_normalized.clone(), KeyGenType::Anvil(key)));

for seed in [
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
"0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d",
"0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a",
"0x7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6",
"0x47e179ec197488593b187f80a00eb0da91f1b9d0b13f8733639f19c30a34926a",
"0x8b3a350cf5c34c9194ca85829a2df0ec3153be0318b5e2d3348e872092edffba",
"0x92db14e403b83dfe3df233f83dfa3a0d7096f21ca9b0d6d6b8d88b2b4ec1564e",
"0x4bbbf85ce3377467afe5d46f804f221813b2bb87f24d81f60f1fcdbf7cbf4356",
"0xdbda1821b80551c9d65939329250298aa3472ba22feea921c0cf5d620ea67b97",
"0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6",
] {
let seed_bytes = hex::decode(&seed[2..]).expect("Invalid hex seed");
filesystem_keystore
.ecdsa_generate_new(Some(&seed_bytes))
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
match protocol {
Protocol::Tangle => {
inject_test_keys(&keystore_uri.clone(), KeyGenType::Tangle(0))
.await
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
}
Protocol::Eigenlayer => {
inject_test_keys(&keystore_uri.clone(), KeyGenType::Anvil(0))
.await
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
}
Protocol::Symbiotic => {
inject_test_keys(&keystore_uri.clone(), KeyGenType::Anvil(0))
.await
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
}
}

if let Protocol::Eigenlayer = protocol {
filesystem_keystore.bls_bn254_generate_from_string("1371012690269088913462269866874713266643928125698382731338806296762673180359922".to_string())
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
};

let mut arguments = vec![
"run".to_string(),
format!("--target-addr={}", IpAddr::from_str("127.0.0.1").unwrap()),
Expand Down Expand Up @@ -223,6 +216,7 @@ impl BlueprintProcessManager {
http_endpoint: &str,
ws_endpoint: &str,
protocol: Protocol,
keystore_path: &str,
) -> Result<(), std::io::Error> {
for (index, program_path) in blueprint_paths.into_iter().enumerate() {
let process = Self::start_blueprint_process(
Expand All @@ -231,6 +225,7 @@ impl BlueprintProcessManager {
http_endpoint,
ws_endpoint,
protocol,
keystore_path,
)
.await?;
self.processes.lock().await.push(process);
Expand Down
1 change: 1 addition & 0 deletions blueprint-test-utils/src/incredible_squaring_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub async fn setup_task_spawner(
loop {
tokio::time::sleep(std::time::Duration::from_millis(10000)).await;

info!("Creating a new task...");
if get_receipt(
task_manager
.createNewTask(U256::from(2), 100u32, quorums.clone())
Expand Down
Loading

0 comments on commit c25447c

Please sign in to comment.