Skip to content

Commit

Permalink
fix: update runner protocol to automatically register (#30)
Browse files Browse the repository at this point in the history
* fix: utilize temporary directory for keystore in test

* fix: switch runner protocol for registration
  • Loading branch information
Tjemmmic authored Nov 1, 2024
1 parent 65879eb commit e045b41
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 138 deletions.
18 changes: 10 additions & 8 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 @@ -34,6 +34,7 @@ hex = "0.4.3"
reqwest = "0.12.8"
serde_json = "1.0.132"
num-bigint = "0.4.6"
tempfile = "3.10.1"

[dependencies.gadget-sdk]
# version = "0.1.2"
Expand Down
49 changes: 20 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub use crate::utils::eigenlayer::*;
use crate::utils::tangle::{bond_balance, update_session_key};
pub use crate::utils::tangle::{run_tangle_validator, BalanceTransferContext};
use color_eyre::eyre::Result;
Expand Down Expand Up @@ -61,7 +60,11 @@ pub async fn tangle_avs_registration(
let env = context.env.clone();

// Run Tangle Validator
run_tangle_validator().await.unwrap();
run_tangle_validator(context.env.keystore_uri.as_str())
.await
.map_err(|e| gadget_sdk::Error::Job {
reason: e.to_string(),
})?;

bond_balance(&env.clone())
.await
Expand All @@ -84,26 +87,26 @@ pub async fn tangle_avs_registration(
#[cfg(test)]
mod tests {
use super::*;
pub use crate::utils::constants::*;
use crate::utils::sol_imports::*;
use alloy_primitives::U256;
use alloy_provider::network::TransactionBuilder;
use alloy_provider::Provider;
use blueprint_test_utils::test_ext::NAME_IDS;
use blueprint_test_utils::{inject_test_keys, KeyGenType};
use gadget_sdk::config::protocol::TangleInstanceSettings;
use gadget_sdk::config::{ContextConfig, GadgetCLICoreSettings, Protocol};
use gadget_sdk::ext::sp_core;
use gadget_sdk::ext::sp_core::Pair;
use gadget_sdk::ext::subxt::tx::Signer;
use gadget_sdk::keystore::backend::fs::FilesystemKeystore;
use gadget_sdk::keystore::backend::GenericKeyStore;
use gadget_sdk::keystore::{Backend, BackendExt};
use gadget_sdk::runners::tangle::TangleConfig;
use gadget_sdk::runners::eigenlayer::EigenlayerConfig;
use gadget_sdk::runners::BlueprintRunner;
use gadget_sdk::utils::evm::get_provider_http;
use gadget_sdk::{alloy_rpc_types, error, info};
use std::net::IpAddr;
use std::path::PathBuf;
use std::path::Path;
use std::str::FromStr;
use std::time::Duration;
use url::Url;
Expand Down Expand Up @@ -191,7 +194,8 @@ mod tests {

// Setup Keystores for test
set_tangle_env_vars();
let keystore_paths = generate_tangle_avs_keys().await;
let tmp_dir = tempfile::TempDir::new().unwrap(); // Create a temporary directory for the keystores
let keystore_paths = generate_tangle_avs_keys(tmp_dir.path()).await;

// Get the operator's keys
let operator_keystore_uri = keystore_paths[5].clone();
Expand Down Expand Up @@ -232,7 +236,6 @@ mod tests {
);

// Tangle node url/port
let http_tangle_url = Url::parse("http://127.0.0.1:9948").unwrap();
let ws_tangle_url = Url::parse("ws://127.0.0.1:9948").unwrap();
let bind_port = ws_tangle_url.clone().port().unwrap();

Expand All @@ -243,7 +246,7 @@ mod tests {
bind_port,
test_mode: false,
log_id: None,
http_rpc_url: http_tangle_url,
http_rpc_url: Url::parse(&http_endpoint).unwrap(),
bootnodes: None,
keystore_uri: operator_keystore_uri,
chain: gadget_io::SupportedChains::LocalTestnet,
Expand All @@ -253,11 +256,11 @@ mod tests {
blueprint_id: Some(0),
service_id: Some(0),
skip_registration: Some(true),
protocol: Protocol::Tangle,
protocol: Protocol::Eigenlayer,
registry_coordinator: Some(REGISTRY_COORDINATOR_ADDR),
operator_state_retriever: Some(OPERATOR_STATE_RETRIEVER_ADDR),
delegation_manager: Some(DELEGATION_MANAGER_ADDR),
ws_rpc_url: ws_tangle_url,
ws_rpc_url: Url::parse(&ws_endpoint).unwrap(),
strategy_manager: Some(STRATEGY_MANAGER_ADDR),
avs_directory: Some(AVS_DIRECTORY_ADDR),
operator_registry: None,
Expand All @@ -275,10 +278,6 @@ mod tests {
let signer = env.first_sr25519_signer().unwrap();
let signer_id = signer.clone().account_id();

// Register Operator on EigenLayer
info!("Registering to EigenLayer");
register_to_eigenlayer(&env.clone()).await.unwrap();

// Spawn task to transfer balance into Operator's account on Tangle
let transfer_task = async move {
tokio::time::sleep(Duration::from_secs(4)).await;
Expand All @@ -305,24 +304,19 @@ mod tests {
// Create Instance of the Event Handler
let context = BalanceTransferContext {
client: client.clone(),
address: Default::default(),
env: env.clone(),
};
let tangle_settings = env.protocol_specific.tangle().unwrap();
let TangleInstanceSettings { service_id, .. } = tangle_settings;
let tangle_avs = RegisterToTangleEventHandler {
service_id: *service_id,
service_id: 0,
context: context.clone(),
client,
signer,
};

// Start the Runner
info!("~~~ Executing the Tangle AVS ~~~");
let tangle_config = TangleConfig {
price_targets: Default::default(),
};
BlueprintRunner::new(tangle_config, env.clone())
let eigen_config = EigenlayerConfig {};
BlueprintRunner::new(eigen_config, env.clone())
.job(tangle_avs)
.run()
.await
Expand Down Expand Up @@ -387,17 +381,15 @@ mod tests {
///
/// # Warning
/// This function is specifically for testing. It will panic upon any errors and utilizes keys that are publicly visible.
pub(crate) async fn generate_tangle_avs_keys() -> Vec<String> {
pub(crate) async fn generate_tangle_avs_keys(keystore_base_path: &Path) -> Vec<String> {
// Set up the Keys required for Tangle AVS
let mut keystore_paths = Vec::new();

// First we inject the premade Tangle Account keys
for (item, name) in NAME_IDS.iter().enumerate() {
let tmp_store = Uuid::new_v4().to_string();
let keystore_uri = PathBuf::from(format!(
"./target/keystores/{}/{tmp_store}/",
name.to_lowercase()
));
let keystore_uri =
keystore_base_path.join(format!("keystores/{}/{tmp_store}/", name.to_lowercase()));
assert!(
!keystore_uri.exists(),
"Keystore URI cannot exist: {}",
Expand All @@ -414,7 +406,7 @@ mod tests {

// Now we create a new Tangle Account for the Test
let tmp_store = Uuid::new_v4().to_string();
let keystore_uri = PathBuf::from(format!("./target/keystores/{}/{tmp_store}/", "testnode"));
let keystore_uri = keystore_base_path.join(format!("keystores/testnode/{tmp_store}/"));
assert!(
!keystore_uri.exists(),
"Keystore URI cannot exist: {}",
Expand All @@ -433,7 +425,6 @@ mod tests {
);

let acco_suri = std::env::var("ACCO_SURI").expect("ACCO_SURI not set");
// let suri = format!("//{acco_suri}");
let (_acco, acco_seed) = sp_core::sr25519::Pair::from_phrase(&acco_suri, None)
.expect("Should be valid SR keypair");
info!("Found SR_SEED: {:?}", acco_seed);
Expand Down
20 changes: 5 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use color_eyre::eyre::eyre;
use color_eyre::Result;
use gadget_sdk::config::protocol::TangleInstanceSettings;
use gadget_sdk::info;
use gadget_sdk::runners::tangle::TangleConfig;
use gadget_sdk::runners::eigenlayer::EigenlayerConfig;
use gadget_sdk::runners::BlueprintRunner;
use gadget_sdk::subxt_core::tx::signer::Signer;
use tangle_avs as blueprint;
use tangle_avs::{register_to_eigenlayer, RegisterToTangleEventHandler};
use tangle_avs::RegisterToTangleEventHandler;

#[gadget_sdk::main(env)]
async fn main() {
Expand All @@ -17,28 +16,19 @@ async fn main() {

let context = blueprint::BalanceTransferContext {
client: client.clone(),
address: Default::default(),
env: env.clone(),
};

info!("Registering to EigenLayer");
register_to_eigenlayer(&env.clone()).await?;

let tangle_settings = env.protocol_specific.tangle().unwrap();
let TangleInstanceSettings { service_id, .. } = tangle_settings;

let tangle_avs = RegisterToTangleEventHandler {
service_id: *service_id,
service_id: 0,
context: context.clone(),
client,
signer,
};

info!("~~~ Executing the Tangle AVS ~~~");
let tangle_config = TangleConfig {
price_targets: Default::default(),
};
BlueprintRunner::new(tangle_config, env.clone())
let eigen_config = EigenlayerConfig {};
BlueprintRunner::new(eigen_config, env.clone())
.job(tangle_avs)
.run()
.await
Expand Down
11 changes: 11 additions & 0 deletions src/utils/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use alloy_primitives::{address, Address};

pub const AVS_DIRECTORY_ADDR: Address = address!("0000000000000000000000000000000000000000");
pub const DELEGATION_MANAGER_ADDR: Address = address!("dc64a140aa3e981100a9beca4e685f962f0cf6c9");
pub const ERC20_MOCK_ADDR: Address = address!("7969c5ed335650692bc04293b07f5bf2e7a673c0");
pub const MAILBOX_ADDR: Address = address!("0000000000000000000000000000000000000000");
pub const OPERATOR_STATE_RETRIEVER_ADDR: Address =
address!("1613beb3b2c4f22ee086b2b38c1476a3ce7f78e8");
pub const REGISTRY_COORDINATOR_ADDR: Address = address!("c3e53f4d16ae77db1c982e75a937b9f60fe63690");
pub const SERVICE_MANAGER_ADDR: Address = address!("67d269191c92caf3cd7723f116c85e6e9bf55933");
pub const STRATEGY_MANAGER_ADDR: Address = address!("5fc8d32690cc91d4c39d9d3abcbd16989f875707");
63 changes: 0 additions & 63 deletions src/utils/eigenlayer.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod eigenlayer;
pub mod constants;
pub mod sol_imports;
pub mod tangle;
Loading

0 comments on commit e045b41

Please sign in to comment.