From 4e2d3a28ef0b95f268202d4870574dc76e38d2d0 Mon Sep 17 00:00:00 2001 From: supernovahs Date: Wed, 14 Aug 2024 15:34:42 +0530 Subject: [PATCH] nits --- Cargo.lock | 14 +++ Cargo.toml | 1 + operator/rust/crates/operator/Cargo.toml | 2 +- .../crates/operator/src/start_operator.rs | 110 ++++++++++-------- .../rust/crates/operator/src/test_utils.rs | 7 +- 5 files changed, 79 insertions(+), 55 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index df3b463f..429f2f87 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1428,6 +1428,19 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "eigen-testing-utils" +version = "0.0.1-alpha" +source = "git+https://github.com/Layr-labs/eigensdk-rs?rev=a84983e#a84983ea26f403a5a47d155ee485f73038ebde0d" +dependencies = [ + "alloy-network", + "alloy-primitives", + "alloy-provider", + "alloy-transport-http", + "eigen-utils", + "once_cell", +] + [[package]] name = "eigen-types" version = "0.0.1-alpha" @@ -2236,6 +2249,7 @@ dependencies = [ "dotenv", "eigen-client-elcontracts", "eigen-logging", + "eigen-testing-utils", "eigen-types", "eigen-utils", "eyre", diff --git a/Cargo.toml b/Cargo.toml index 5ed37d69..2a58aa48 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,6 +34,7 @@ eigen-client-elcontracts = {git = "https://github.com/Layr-labs/eigensdk-rs", re eigen-types = {git = "https://github.com/Layr-labs/eigensdk-rs", rev = "a84983e"} eigen-utils = {git = "https://github.com/Layr-labs/eigensdk-rs", rev = "a84983e"} eigen-logging = {git = "https://github.com/Layr-labs/eigensdk-rs", rev = "a84983e"} +eigen-testing-utils = {git = "https://github.com/Layr-labs/eigensdk-rs", rev = "a84983e"} #alloy alloy-chains = "0.1.15" diff --git a/operator/rust/crates/operator/Cargo.toml b/operator/rust/crates/operator/Cargo.toml index 6b1f8ba0..5effd763 100644 --- a/operator/rust/crates/operator/Cargo.toml +++ b/operator/rust/crates/operator/Cargo.toml @@ -33,6 +33,7 @@ eigen-client-elcontracts.workspace = true eigen-types.workspace = true eigen-utils.workspace = true eigen-logging.workspace = true +eigen-testing-utils.workspace = true #misc dotenv = "0.15.0" @@ -43,7 +44,6 @@ futures-util = "0.3" eyre = "0.6.12" #tokio tokio = {workspace = true, features =["full"]} - [lints] workspace = true diff --git a/operator/rust/crates/operator/src/start_operator.rs b/operator/rust/crates/operator/src/start_operator.rs index ac2f515d..ea817ae5 100644 --- a/operator/rust/crates/operator/src/start_operator.rs +++ b/operator/rust/crates/operator/src/start_operator.rs @@ -1,28 +1,24 @@ #![allow(missing_docs)] -use alloy_network::Ethereum; -use alloy_provider::RootProvider; -use alloy_provider::{Provider, ProviderBuilder}; +use alloy_primitives::{eip191_hash_message, Address, FixedBytes, U256}; +use alloy_provider::Provider; use alloy_rpc_types::{BlockNumberOrTag, Filter}; +use alloy_signer::SignerSync; +use alloy_signer_local::PrivateKeySigner; use alloy_sol_types::{sol, SolEvent}; -use alloy_transport_http::Client; use chrono::Utc; use dotenv::dotenv; -use eigen_logging::{get_logger, init_logger, log_level::LogLevel, logger::Logger}; -use eigen_utils::get_signer; -use once_cell::sync::Lazy; -use rand::RngCore; -use reqwest::Url; -use HelloWorldServiceManager::Task; - -use alloy_primitives::{eip191_hash_message, Address, FixedBytes, U256}; -use alloy_signer::Signer; -use alloy_signer_local::PrivateKeySigner; use eigen_client_elcontracts::{ reader::ELChainReader, writer::{ELChainWriter, Operator}, }; +use eigen_logging::{get_logger, init_logger, log_level::LogLevel, logger::Logger, EigenLogger}; +use eigen_utils::get_signer; use eyre::Result; +use once_cell::sync::Lazy; +use rand::RngCore; +use HelloWorldServiceManager::Task; +use eigen_utils::binding::ECDSAStakeRegistry; use std::{env, str::FromStr}; use ECDSAStakeRegistry::SignatureWithSaltAndExpiry; @@ -33,8 +29,6 @@ sol!( "json_abi/HelloWorldServiceManager.json" ); -use eigen_utils::binding::ECDSAStakeRegistry; - static KEY: Lazy = Lazy::new(|| env::var("PRIVATE_KEY").expect("failed to retrieve private key")); @@ -51,7 +45,7 @@ static DELEGATION_MANAGER_CONTRACT_ADDRESS: Lazy = Lazy::new(|| { }); static STAKE_REGISTRY_CONTRACT_ADDRESS: Lazy = Lazy::new(|| { - env::var("STAKE_REGISTRY_ADDRESS") + env::var("HOLESKY_STAKE_REGISTRY_ADDRESS") .expect("failed to get stake registry contract address from env") }); @@ -59,6 +53,7 @@ static AVS_DIRECTORY_CONTRACT_ADDRESS: Lazy = Lazy::new(|| { env::var("AVS_DIRECTORY_ADDRESS") .expect("failed to get delegation manager contract address from env") }); + async fn sign_and_response_to_task( task_index: u32, task_created_block: u32, @@ -70,7 +65,7 @@ async fn sign_and_response_to_task( let msg_hash = eip191_hash_message(message); let wallet = PrivateKeySigner::from_str(&KEY.clone()).expect("failed to generate wallet "); - let signature = wallet.sign_hash(&msg_hash).await?; + let signature = wallet.sign_hash_sync(&msg_hash)?; println!("Signing and responding to task : {:?}", task_index); let hello_world_contract_address = Address::from_str(&HELLO_WORLD_CONTRACT_ADDRESS) @@ -101,13 +96,9 @@ async fn monitor_new_tasks() -> Result<()> { let hello_world_contract_address = Address::from_str(&HELLO_WORLD_CONTRACT_ADDRESS) .expect("wrong hello world contract address"); - println!( - "hello world contrat address:{:?}", - hello_world_contract_address - ); + let hello_world_contract = HelloWorldServiceManager::new(hello_world_contract_address, &provider); - println!("hello contract :{:?}", hello_world_contract); let word: &str = "EigenWorld"; // If you want to send this tx to holesky , please uncomment the gas price and gas limit @@ -154,25 +145,33 @@ async fn monitor_new_tasks() -> Result<()> { } } -async fn register_operator() -> Result<()> { +async fn register_operator(logger: EigenLogger) -> Result<()> { let wallet = PrivateKeySigner::from_str(&KEY).expect("failed to generate wallet "); let provider = get_signer(KEY.clone(), &RPC_URL); + let chain_id = provider + .get_chain_id() + .await + .expect("failed to get chain id "); + let hello_world_contract_address = Address::from_str(&HELLO_WORLD_CONTRACT_ADDRESS) .expect("wrong hello world contract address"); - let delegation_manager_contract_address = - Address::from_str(&DELEGATION_MANAGER_CONTRACT_ADDRESS) - .expect("wrong delegation manager contract address"); + let delegation_manager_contract_address: Address; + let avs_directory_contract_address: Address; + delegation_manager_contract_address = Address::from_str(&DELEGATION_MANAGER_CONTRACT_ADDRESS) + .expect("wrong stake registry contract address"); + println!( + "delegation manager :{}", + delegation_manager_contract_address + ); + avs_directory_contract_address = Address::from_str(&AVS_DIRECTORY_CONTRACT_ADDRESS) + .expect("wrong stake registry contract address"); let stake_registry_contract_address = Address::from_str(&STAKE_REGISTRY_CONTRACT_ADDRESS) .expect("wrong stake registry contract address"); - let avs_directory_contract_address = Address::from_str(&AVS_DIRECTORY_CONTRACT_ADDRESS) - .expect("wrong delegation manager contract address"); let default_slasher = Address::ZERO; // We don't need slasher for our example. let default_strategy = Address::ZERO; // We don't need strategy for our example. - // initialize logger at Debug tracing - // init_logger(LogLevel::Debug); - let logger = get_logger(); + let elcontracts_reader_instance = ELChainReader::new( logger.clone(), default_slasher, @@ -195,15 +194,30 @@ async fn register_operator() -> Result<()> { 0u32, None, ); + + let is_registered = elcontracts_reader_instance + .is_operator_registered(wallet.address()) + .await + .unwrap(); + logger + .tracing_logger + .as_ref() + .unwrap() + .info(&format!("is registered {}", is_registered), &[""]); #[allow(unused_doc_comments)] ///In case you are running holesky. Comment the below register_as_operator call after the first /// call . Since we can register only once per operator. let _tx_hash = elcontracts_writer_instance .register_as_operator(operator) .await; - println!("Operator registered on EL successfully"); + logger + .tracing_logger + .as_ref() + .unwrap() + .info("Operator registered on EL successfully", &[""]); let mut salt = [0u8; 32]; rand::rngs::OsRng.fill_bytes(&mut salt); + let salt = FixedBytes::from_slice(&salt); let now = Utc::now().timestamp(); let expiry: U256 = U256::from(now + 3600); @@ -217,8 +231,7 @@ async fn register_operator() -> Result<()> { .await .expect("not able to calculate operator "); - let signature = wallet.sign_hash(&digest_hash).await?; - + let signature = wallet.sign_hash_sync(&digest_hash)?; let operator_signature = SignatureWithSaltAndExpiry { signature: signature.as_bytes().into(), salt, @@ -227,22 +240,21 @@ async fn register_operator() -> Result<()> { let contract_ecdsa_stake_registry = ECDSAStakeRegistry::new(stake_registry_contract_address, provider); - println!("initialize new ecdsa "); - - // If you wish to run on holesky, please deploy the stake registry contract(it's not deployed right now) - // and uncomment the gas and gas_price - let registeroperator_details = contract_ecdsa_stake_registry - .registerOperatorWithSignature(wallet.clone().address(), operator_signature); - let _tx = registeroperator_details - // .gas(300000) - // .gas_price(20000000000) + let registeroperator_details_call = contract_ecdsa_stake_registry + .registerOperatorWithSignature(wallet.clone().address(), operator_signature) + .gas(200000); + registeroperator_details_call .send() .await? .get_receipt() .await?; - println!( - "Operator registered on AVS successfully :{:?}", - wallet.address() + + logger.tracing_logger.unwrap().info( + &format!( + "Operator registered on AVS successfully :{}", + wallet.address() + ), + &[""], ); Ok(()) @@ -251,7 +263,9 @@ async fn register_operator() -> Result<()> { #[tokio::main] pub async fn main() { dotenv().ok(); - if let Err(e) = register_operator().await { + init_logger(LogLevel::Info); + let logger = get_logger(); + if let Err(e) = register_operator(logger).await { eprintln!("Failed to register operator: {:?}", e); return; } diff --git a/operator/rust/crates/operator/src/test_utils.rs b/operator/rust/crates/operator/src/test_utils.rs index e3fd93ce..713e521f 100644 --- a/operator/rust/crates/operator/src/test_utils.rs +++ b/operator/rust/crates/operator/src/test_utils.rs @@ -1,11 +1,10 @@ #![allow(missing_docs)] use alloy_primitives::{eip191_hash_message, Address, FixedBytes, U256}; -use alloy_provider::{Provider, ProviderBuilder, RootProvider}; +use alloy_provider::Provider; use alloy_rpc_types::{BlockNumberOrTag, Filter}; use alloy_signer::Signer; use alloy_signer_local::PrivateKeySigner; use alloy_sol_types::{sol, SolEvent}; -use alloy_transport_http::Client; use chrono::Utc; use eigen_client_elcontracts::{ reader::ELChainReader, @@ -70,7 +69,6 @@ async fn sign_and_response_to_task( let signature = wallet.sign_hash(&msg_hash).await?; println!("Signing and responding to task : {:?}", task_index); - let url = Url::parse(&RPC_URL.clone()).expect("Wrong rpc url"); let provider = get_provider(&RPC_URL); let hello_world_contract_address = Address::from_str(&HELLO_WORLD_CONTRACT_ADDRESS) .expect("wrong hello world contract address"); @@ -96,8 +94,6 @@ async fn sign_and_response_to_task( /// Monitor new tasks pub async fn monitor_new_tasks() -> Result<()> { - let wallet = PrivateKeySigner::from_str(&KEY).expect("failed to generate wallet "); - let url = Url::parse(&RPC_URL).expect("Wrong rpc url"); let provider = get_provider(&RPC_URL); let hello_world_contract_address = Address::from_str(&HELLO_WORLD_CONTRACT_ADDRESS) @@ -208,7 +204,6 @@ pub async fn register_operator() -> Result<()> { .expect("not able to calculate operator "); let signature = wallet.sign_hash(&digest_hash).await?; - let url = Url::parse(&RPC_URL).expect("Wrong rpc url"); let provider = get_signer(KEY.clone(), &RPC_URL); let _operator_signature = SignatureWithSaltAndExpiry {