Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
supernovahs committed Aug 14, 2024
1 parent 4628994 commit 4e2d3a2
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 55 deletions.
14 changes: 14 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 @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion operator/rust/crates/operator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -43,7 +44,6 @@ futures-util = "0.3"
eyre = "0.6.12"
#tokio
tokio = {workspace = true, features =["full"]}

[lints]
workspace = true

Expand Down
110 changes: 62 additions & 48 deletions operator/rust/crates/operator/src/start_operator.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -33,8 +29,6 @@ sol!(
"json_abi/HelloWorldServiceManager.json"
);

use eigen_utils::binding::ECDSAStakeRegistry;

static KEY: Lazy<String> =
Lazy::new(|| env::var("PRIVATE_KEY").expect("failed to retrieve private key"));

Expand All @@ -51,14 +45,15 @@ static DELEGATION_MANAGER_CONTRACT_ADDRESS: Lazy<String> = Lazy::new(|| {
});

static STAKE_REGISTRY_CONTRACT_ADDRESS: Lazy<String> = Lazy::new(|| {
env::var("STAKE_REGISTRY_ADDRESS")
env::var("HOLESKY_STAKE_REGISTRY_ADDRESS")
.expect("failed to get stake registry contract address from env")
});

static AVS_DIRECTORY_CONTRACT_ADDRESS: Lazy<String> = 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,
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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(())
Expand All @@ -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;
}
Expand Down
7 changes: 1 addition & 6 deletions operator/rust/crates/operator/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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");
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4e2d3a2

Please sign in to comment.