Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build Client inside DaConfig #46

Merged
merged 6 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/da-clients/da-client-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ pub trait DaClient: Send + Sync {
}

/// Trait for every new DaConfig to implement
pub trait DaConfig {
#[async_trait]
pub trait DaConfig<T> {
/// Should create a new instance of the DaConfig from the environment variables
fn new_from_env() -> Self;
async fn build_client(&self) -> T;
}
23 changes: 22 additions & 1 deletion crates/da-clients/ethereum/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
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,
pub memory_pages_contract: String,
pub private_key: String,
}

impl DaConfig for EthereumDaConfig {
#[async_trait]
impl DaConfig<EthereumDaClient> 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_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 }
}
}
22 changes: 2 additions & 20 deletions crates/da-clients/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)]
Expand Down Expand Up @@ -109,19 +103,6 @@ impl DaClient for EthereumDaClient {
}
}

impl From<EthereumDaConfig> 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<u8>],
trusted_setup: &KzgSettings,
Expand Down Expand Up @@ -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::*;

Expand Down
7 changes: 3 additions & 4 deletions crates/orchestrator/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<dyn DaClient + Send + Sync> {
async fn build_da_client() -> Box<dyn DaClient + Send + Sync> {
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"),
}
Expand Down
18 changes: 15 additions & 3 deletions crates/utils/src/env_utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
use color_eyre::Result;
use std::env::VarError;

pub fn get_env_var(key: &str) -> Result<String> {
std::env::var(key).map_err(|e| e.into())
pub fn get_env_var(key: &str) -> Result<String, VarError> {
std::env::var(key)
}

pub fn get_env_var_optional(key: &str) -> Result<Option<String>, 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<String> {
get_env_var_optional(key).unwrap_or_else(|e| panic!("Failed to get env var {}: {}", key, e))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn get_env_car_optional_or_panic(key: &str) -> Option<String> {
pub fn get_env_var_optional_or_panic(key: &str) -> Option<String> {

}

pub fn get_env_var_or_panic(key: &str) -> String {
Expand Down
Loading