From 9b7f1e2aac15557ee24895e489850fec20ebb64d Mon Sep 17 00:00:00 2001 From: Heemank Verma Date: Sat, 20 Jul 2024 12:34:34 +0530 Subject: [PATCH 1/6] update: added boilerplate build_da_client --- .../da-clients/da-client-interface/src/lib.rs | 4 ++- crates/da-clients/ethereum/src/config.rs | 7 ++++- crates/orchestrator/src/config.rs | 4 +-- crates/utils/src/env_utils.rs | 26 ++++++++++++++++--- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/crates/da-clients/da-client-interface/src/lib.rs b/crates/da-clients/da-client-interface/src/lib.rs index 33e9de8b..bca97748 100644 --- a/crates/da-clients/da-client-interface/src/lib.rs +++ b/crates/da-clients/da-client-interface/src/lib.rs @@ -29,7 +29,9 @@ pub trait DaClient: Send + Sync { } /// Trait for every new DaConfig to implement -pub trait DaConfig { +#[async_trait] +pub trait DaConfig { /// Should create a new instance of the DaConfig from the environment variables fn new_from_env() -> Self; + async fn build_da_client(&self) -> T; } diff --git a/crates/da-clients/ethereum/src/config.rs b/crates/da-clients/ethereum/src/config.rs index cb0ec9b9..8d9e5406 100644 --- a/crates/da-clients/ethereum/src/config.rs +++ b/crates/da-clients/ethereum/src/config.rs @@ -1,5 +1,6 @@ use da_client_interface::DaConfig; use utils::env_utils::get_env_var_or_panic; +use async_trait::async_trait; #[derive(Clone, Debug)] pub struct EthereumDaConfig { @@ -8,12 +9,16 @@ pub struct EthereumDaConfig { pub private_key: String, } -impl DaConfig for EthereumDaConfig { +#[async_trait] +impl DaConfig for EthereumDaConfig { fn new_from_env() -> Self { Self { rpc_url: get_env_var_or_panic("ETHEREUM_RPC_URL"), memory_pages_contract: get_env_var_or_panic("MEMORY_PAGES_CONTRACT_ADDRESS"), private_key: get_env_var_or_panic("PRIVATE_KEY"), } + } + async fn build_da_client(&self) -> String{ + "Create Ethereum Client here".to_string() } } diff --git a/crates/orchestrator/src/config.rs b/crates/orchestrator/src/config.rs index 648d36a9..c05a5c7d 100644 --- a/crates/orchestrator/src/config.rs +++ b/crates/orchestrator/src/config.rs @@ -54,7 +54,7 @@ pub async fn init_config() -> Config { // init the queue let queue = Box::new(SqsQueue {}); - let da_client = build_da_client(); + let da_client = build_da_client().await; let settings_provider = DefaultSettingsProvider {}; let settlement_client = build_settlement_client(&settings_provider).await; @@ -135,7 +135,7 @@ pub async fn config_force_init(config: Config) { } /// Builds the DA client based on the environment variable DA_LAYER -fn build_da_client() -> Box { +async fn build_da_client() -> Box { match get_env_var_or_panic("DA_LAYER").as_str() { "ethereum" => { let config = EthereumDaConfig::new_from_env(); diff --git a/crates/utils/src/env_utils.rs b/crates/utils/src/env_utils.rs index 78e11609..1a573f3a 100644 --- a/crates/utils/src/env_utils.rs +++ b/crates/utils/src/env_utils.rs @@ -1,7 +1,27 @@ -use color_eyre::Result; +use std::env::VarError; + + +pub fn get_env_var(key: &str) -> Result { + std::env::var(key) +} + +pub fn get_env_var_optional(key: &str) -> Result,VarError> { + match get_env_var(key){ + Ok(s) => { + Ok(Some(s)) + } + Err(VarError::NotPresent) => { + Ok(None) + } + Err(e) => { + Err(e) + } + } +} + +pub fn get_env_car_optional_or_panic(key: &str) -> Option { + get_env_var_optional(key).unwrap_or_else(|e| panic!("Failed to get env var {}: {}", key, e)) -pub fn get_env_var(key: &str) -> Result { - std::env::var(key).map_err(|e| e.into()) } pub fn get_env_var_or_panic(key: &str) -> String { From 82a9137f09620263de1fc304b364078f04fb37a1 Mon Sep 17 00:00:00 2001 From: Heemank Verma Date: Sat, 20 Jul 2024 12:51:22 +0530 Subject: [PATCH 2/6] build_da_client support for Ethereum --- crates/da-clients/ethereum/src/config.rs | 21 ++++++++++++++++++--- crates/da-clients/ethereum/src/lib.rs | 13 ------------- crates/orchestrator/src/config.rs | 2 +- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/crates/da-clients/ethereum/src/config.rs b/crates/da-clients/ethereum/src/config.rs index 8d9e5406..1ac9868a 100644 --- a/crates/da-clients/ethereum/src/config.rs +++ b/crates/da-clients/ethereum/src/config.rs @@ -1,6 +1,15 @@ +use std::{env, path::Path}; +use std::str::FromStr; + +use alloy::{network::Ethereum, providers::ProviderBuilder, rpc::client::RpcClient}; +use c_kzg::KzgSettings; use da_client_interface::DaConfig; +use url::Url; use utils::env_utils::get_env_var_or_panic; use async_trait::async_trait; +use alloy::signers::wallet::LocalWallet; + +use crate::EthereumDaClient; #[derive(Clone, Debug)] pub struct EthereumDaConfig { @@ -10,7 +19,7 @@ pub struct EthereumDaConfig { } #[async_trait] -impl DaConfig for EthereumDaConfig { +impl DaConfig for EthereumDaConfig { fn new_from_env() -> Self { Self { rpc_url: get_env_var_or_panic("ETHEREUM_RPC_URL"), @@ -18,7 +27,13 @@ impl DaConfig for EthereumDaConfig { private_key: get_env_var_or_panic("PRIVATE_KEY"), } } - async fn build_da_client(&self) -> String{ - "Create Ethereum Client here".to_string() + async fn build_da_client(&self) -> EthereumDaClient{ + let client = RpcClient::new_http(Url::from_str(self.rpc_url.as_str()).expect("Failed to parse ETHEREUM_RPC_URL")); + let provider = ProviderBuilder::<_, Ethereum>::new().on_client(client); + let wallet: LocalWallet = env::var("PK").expect("PK must be set").parse().expect("issue while parsing"); + // let wallet: LocalWallet = config.private_key.as_str().parse(); + let trusted_setup = KzgSettings::load_trusted_setup_file(Path::new("./trusted_setup.txt")) + .expect("issue while loading the trusted setup"); + EthereumDaClient { provider, wallet, trusted_setup } } } diff --git a/crates/da-clients/ethereum/src/lib.rs b/crates/da-clients/ethereum/src/lib.rs index b8ceb808..429d9f39 100644 --- a/crates/da-clients/ethereum/src/lib.rs +++ b/crates/da-clients/ethereum/src/lib.rs @@ -109,19 +109,6 @@ impl DaClient for EthereumDaClient { } } -impl From for EthereumDaClient { - fn from(config: EthereumDaConfig) -> Self { - let client = - RpcClient::new_http(Url::from_str(config.rpc_url.as_str()).expect("Failed to parse ETHEREUM_RPC_URL")); - let provider = ProviderBuilder::<_, Ethereum>::new().on_client(client); - let wallet: LocalWallet = env::var("PK").expect("PK must be set").parse().expect("issue while parsing"); - // let wallet: LocalWallet = config.private_key.as_str().parse(); - let trusted_setup = KzgSettings::load_trusted_setup_file(Path::new("./trusted_setup.txt")) - .expect("issue while loading the trusted setup"); - EthereumDaClient { provider, wallet, trusted_setup } - } -} - async fn prepare_sidecar( state_diff: &[Vec], trusted_setup: &KzgSettings, diff --git a/crates/orchestrator/src/config.rs b/crates/orchestrator/src/config.rs index c05a5c7d..ba18240d 100644 --- a/crates/orchestrator/src/config.rs +++ b/crates/orchestrator/src/config.rs @@ -139,7 +139,7 @@ async fn build_da_client() -> Box { match get_env_var_or_panic("DA_LAYER").as_str() { "ethereum" => { let config = EthereumDaConfig::new_from_env(); - Box::new(EthereumDaClient::from(config)) + Box::new(config.build_da_client().await) } _ => panic!("Unsupported DA layer"), } From 241f32d0198a44434ed39a085ad462ce8e4606cb Mon Sep 17 00:00:00 2001 From: Heemank Verma Date: Sat, 20 Jul 2024 12:55:31 +0530 Subject: [PATCH 3/6] update: support for Ethereum - build_da_client --- crates/da-clients/ethereum/src/lib.rs | 9 ++------- crates/orchestrator/src/config.rs | 1 - 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/crates/da-clients/ethereum/src/lib.rs b/crates/da-clients/ethereum/src/lib.rs index 429d9f39..3ad22b4e 100644 --- a/crates/da-clients/ethereum/src/lib.rs +++ b/crates/da-clients/ethereum/src/lib.rs @@ -1,8 +1,5 @@ #![allow(missing_docs)] #![allow(clippy::missing_docs_in_private_items)] -use std::env; -use std::path::Path; -use std::str::FromStr; use alloy::consensus::{ BlobTransactionSidecar, SignableTransaction, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxEnvelope, @@ -12,20 +9,18 @@ use alloy::eips::eip2930::AccessList; use alloy::eips::eip4844::BYTES_PER_BLOB; use alloy::network::{Ethereum, TxSigner}; use alloy::primitives::{bytes, Address, FixedBytes, TxHash, U256, U64}; -use alloy::providers::{Provider, ProviderBuilder, RootProvider}; -use alloy::rpc::client::RpcClient; +use alloy::providers::{Provider, RootProvider}; use alloy::signers::wallet::LocalWallet; use alloy::transports::http::Http; use async_trait::async_trait; use c_kzg::{Blob, KzgCommitment, KzgProof, KzgSettings}; use color_eyre::Result; -use config::EthereumDaConfig; use da_client_interface::{DaClient, DaVerificationStatus}; use dotenv::dotenv; +use std::path::Path; use mockall::automock; use mockall::predicate::*; use reqwest::Client; -use url::Url; pub mod config; pub struct EthereumDaClient { #[allow(dead_code)] diff --git a/crates/orchestrator/src/config.rs b/crates/orchestrator/src/config.rs index ba18240d..80e176b6 100644 --- a/crates/orchestrator/src/config.rs +++ b/crates/orchestrator/src/config.rs @@ -4,7 +4,6 @@ use arc_swap::{ArcSwap, Guard}; use da_client_interface::{DaClient, DaConfig}; use dotenvy::dotenv; use ethereum_da_client::config::EthereumDaConfig; -use ethereum_da_client::EthereumDaClient; use ethereum_settlement_client::EthereumSettlementClient; use prover_client_interface::ProverClient; use settlement_client_interface::SettlementClient; From f90dc393d0d3f2f330032b7b84fef301ae13deaf Mon Sep 17 00:00:00 2001 From: Heemank Verma Date: Sat, 20 Jul 2024 13:32:57 +0530 Subject: [PATCH 4/6] update: rename : build_da_client in DaConfig to build_client --- .../da-clients/da-client-interface/src/lib.rs | 2 +- crates/da-clients/ethereum/src/config.rs | 2 +- crates/da-clients/ethereum/src/lib.rs | 2 +- crates/orchestrator/src/config.rs | 2 +- crates/utils/src/env_utils.rs | 18 +++++------------- 5 files changed, 9 insertions(+), 17 deletions(-) diff --git a/crates/da-clients/da-client-interface/src/lib.rs b/crates/da-clients/da-client-interface/src/lib.rs index bca97748..f6158e17 100644 --- a/crates/da-clients/da-client-interface/src/lib.rs +++ b/crates/da-clients/da-client-interface/src/lib.rs @@ -33,5 +33,5 @@ pub trait DaClient: Send + Sync { pub trait DaConfig { /// Should create a new instance of the DaConfig from the environment variables fn new_from_env() -> Self; - async fn build_da_client(&self) -> T; + async fn build_client(&self) -> T; } diff --git a/crates/da-clients/ethereum/src/config.rs b/crates/da-clients/ethereum/src/config.rs index 1ac9868a..807c0492 100644 --- a/crates/da-clients/ethereum/src/config.rs +++ b/crates/da-clients/ethereum/src/config.rs @@ -27,7 +27,7 @@ impl DaConfig for EthereumDaConfig { private_key: get_env_var_or_panic("PRIVATE_KEY"), } } - async fn build_da_client(&self) -> EthereumDaClient{ + async fn build_client(&self) -> EthereumDaClient{ let client = RpcClient::new_http(Url::from_str(self.rpc_url.as_str()).expect("Failed to parse ETHEREUM_RPC_URL")); let provider = ProviderBuilder::<_, Ethereum>::new().on_client(client); let wallet: LocalWallet = env::var("PK").expect("PK must be set").parse().expect("issue while parsing"); diff --git a/crates/da-clients/ethereum/src/lib.rs b/crates/da-clients/ethereum/src/lib.rs index 3ad22b4e..5c1c30e0 100644 --- a/crates/da-clients/ethereum/src/lib.rs +++ b/crates/da-clients/ethereum/src/lib.rs @@ -17,7 +17,6 @@ use c_kzg::{Blob, KzgCommitment, KzgProof, KzgSettings}; use color_eyre::Result; use da_client_interface::{DaClient, DaVerificationStatus}; use dotenv::dotenv; -use std::path::Path; use mockall::automock; use mockall::predicate::*; use reqwest::Client; @@ -133,6 +132,7 @@ async fn prepare_sidecar( mod tests { use std::fs::File; use std::io::{self, BufRead}; + use std::path::Path; use super::*; diff --git a/crates/orchestrator/src/config.rs b/crates/orchestrator/src/config.rs index 80e176b6..a2d82858 100644 --- a/crates/orchestrator/src/config.rs +++ b/crates/orchestrator/src/config.rs @@ -138,7 +138,7 @@ async fn build_da_client() -> Box { match get_env_var_or_panic("DA_LAYER").as_str() { "ethereum" => { let config = EthereumDaConfig::new_from_env(); - Box::new(config.build_da_client().await) + Box::new(config.build_client().await) } _ => panic!("Unsupported DA layer"), } diff --git a/crates/utils/src/env_utils.rs b/crates/utils/src/env_utils.rs index 1a573f3a..327bd478 100644 --- a/crates/utils/src/env_utils.rs +++ b/crates/utils/src/env_utils.rs @@ -1,27 +1,19 @@ use std::env::VarError; - pub fn get_env_var(key: &str) -> Result { std::env::var(key) } -pub fn get_env_var_optional(key: &str) -> Result,VarError> { - match get_env_var(key){ - Ok(s) => { - Ok(Some(s)) - } - Err(VarError::NotPresent) => { - Ok(None) - } - Err(e) => { - Err(e) - } +pub fn get_env_var_optional(key: &str) -> Result, VarError> { + match get_env_var(key) { + Ok(s) => Ok(Some(s)), + Err(VarError::NotPresent) => Ok(None), + Err(e) => Err(e), } } pub fn get_env_car_optional_or_panic(key: &str) -> Option { get_env_var_optional(key).unwrap_or_else(|e| panic!("Failed to get env var {}: {}", key, e)) - } pub fn get_env_var_or_panic(key: &str) -> String { From f3aae1fedba35083e538d6c5e1ae1be4bf111e03 Mon Sep 17 00:00:00 2001 From: Heemank Verma Date: Sat, 20 Jul 2024 13:41:52 +0530 Subject: [PATCH 5/6] chore: linting fixes --- crates/da-clients/ethereum/src/config.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/da-clients/ethereum/src/config.rs b/crates/da-clients/ethereum/src/config.rs index 807c0492..c5d9b4fb 100644 --- a/crates/da-clients/ethereum/src/config.rs +++ b/crates/da-clients/ethereum/src/config.rs @@ -1,13 +1,13 @@ -use std::{env, path::Path}; use std::str::FromStr; +use std::{env, path::Path}; +use alloy::signers::wallet::LocalWallet; use alloy::{network::Ethereum, providers::ProviderBuilder, rpc::client::RpcClient}; +use async_trait::async_trait; use c_kzg::KzgSettings; use da_client_interface::DaConfig; use url::Url; use utils::env_utils::get_env_var_or_panic; -use async_trait::async_trait; -use alloy::signers::wallet::LocalWallet; use crate::EthereumDaClient; @@ -26,9 +26,10 @@ impl DaConfig for EthereumDaConfig { memory_pages_contract: get_env_var_or_panic("MEMORY_PAGES_CONTRACT_ADDRESS"), private_key: get_env_var_or_panic("PRIVATE_KEY"), } - } - async fn build_client(&self) -> EthereumDaClient{ - let client = RpcClient::new_http(Url::from_str(self.rpc_url.as_str()).expect("Failed to parse ETHEREUM_RPC_URL")); + } + async fn build_client(&self) -> EthereumDaClient { + let client = + RpcClient::new_http(Url::from_str(self.rpc_url.as_str()).expect("Failed to parse ETHEREUM_RPC_URL")); let provider = ProviderBuilder::<_, Ethereum>::new().on_client(client); let wallet: LocalWallet = env::var("PK").expect("PK must be set").parse().expect("issue while parsing"); // let wallet: LocalWallet = config.private_key.as_str().parse(); From 5d729f5c2298c90131d2bd308db011590df54c90 Mon Sep 17 00:00:00 2001 From: Heemank Verma Date: Sat, 20 Jul 2024 13:58:59 +0530 Subject: [PATCH 6/6] chore: rearrange env_utils functions --- crates/utils/src/env_utils.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/utils/src/env_utils.rs b/crates/utils/src/env_utils.rs index 327bd478..e62d32ce 100644 --- a/crates/utils/src/env_utils.rs +++ b/crates/utils/src/env_utils.rs @@ -4,6 +4,14 @@ pub fn get_env_var(key: &str) -> Result { std::env::var(key) } +pub fn get_env_var_or_panic(key: &str) -> String { + get_env_var(key).unwrap_or_else(|e| panic!("Failed to get env var {}: {}", key, e)) +} + +pub fn get_env_var_or_default(key: &str, default: &str) -> String { + get_env_var(key).unwrap_or(default.to_string()) +} + pub fn get_env_var_optional(key: &str) -> Result, VarError> { match get_env_var(key) { Ok(s) => Ok(Some(s)), @@ -15,11 +23,3 @@ pub fn get_env_var_optional(key: &str) -> Result, VarError> { pub fn get_env_car_optional_or_panic(key: &str) -> Option { get_env_var_optional(key).unwrap_or_else(|e| panic!("Failed to get env var {}: {}", key, e)) } - -pub fn get_env_var_or_panic(key: &str) -> String { - get_env_var(key).unwrap_or_else(|e| panic!("Failed to get env var {}: {}", key, e)) -} - -pub fn get_env_var_or_default(key: &str, default: &str) -> String { - get_env_var(key).unwrap_or(default.to_string()) -}