diff --git a/crates/da-clients/da-client-interface/src/lib.rs b/crates/da-clients/da-client-interface/src/lib.rs index 33e9de8b..f6158e17 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_client(&self) -> T; } diff --git a/crates/da-clients/ethereum/src/config.rs b/crates/da-clients/ethereum/src/config.rs index cb0ec9b9..c5d9b4fb 100644 --- a/crates/da-clients/ethereum/src/config.rs +++ b/crates/da-clients/ethereum/src/config.rs @@ -1,6 +1,16 @@ +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 crate::EthereumDaClient; + #[derive(Clone, Debug)] pub struct EthereumDaConfig { pub rpc_url: String, @@ -8,7 +18,8 @@ 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"), @@ -16,4 +27,14 @@ impl DaConfig for EthereumDaConfig { 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")); + 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..5c1c30e0 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,17 @@ 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 mockall::automock; use mockall::predicate::*; use reqwest::Client; -use url::Url; pub mod config; pub struct EthereumDaClient { #[allow(dead_code)] @@ -109,19 +103,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, @@ -151,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 648d36a9..a2d82858 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; @@ -54,7 +53,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,11 +134,11 @@ 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(); - Box::new(EthereumDaClient::from(config)) + 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 78e11609..e62d32ce 100644 --- a/crates/utils/src/env_utils.rs +++ b/crates/utils/src/env_utils.rs @@ -1,7 +1,7 @@ -use color_eyre::Result; +use std::env::VarError; -pub fn get_env_var(key: &str) -> Result { - std::env::var(key).map_err(|e| e.into()) +pub fn get_env_var(key: &str) -> Result { + std::env::var(key) } pub fn get_env_var_or_panic(key: &str) -> String { @@ -11,3 +11,15 @@ pub fn get_env_var_or_panic(key: &str) -> String { 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)), + 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)) +}