diff --git a/.env.test b/.env.test index 4d09e45b..498fd1dd 100644 --- a/.env.test +++ b/.env.test @@ -50,7 +50,7 @@ SHARP_PROOF_LAYOUT="small" DA_LAYER="ethereum" SETTLEMENT_LAYER="ethereum" -SETTLEMENT_RPC_URL="http://localhost:3001" +SETTLEMENT_RPC_URL="https://eth-mainnet.public.blastapi.io" MADARA_RPC_URL="http://localhost:3000" L1_CORE_CONTRACT_ADDRESS="0xc662c410C0ECf747543f5bA90660f6ABeBD9C8c4" MEMORY_PAGES_CONTRACT_ADDRESS="0x47312450B3Ac8b5b8e247a6bB6d523e7605bDb60" diff --git a/CHANGELOG.md b/CHANGELOG.md index 1eec7e27..e95daa70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ## Changed +- settings provider - refactor AWS config usage and clean .env files - GitHub's coverage CI yml file for localstack and db testing. - Orchestrator :Moved TestConfigBuilder to `config.rs` in tests folder. diff --git a/Cargo.lock b/Cargo.lock index ff73bb06..566a06a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1658,9 +1658,9 @@ dependencies = [ [[package]] name = "aws-credential-types" -version = "1.2.0" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e16838e6c9e12125face1c1eff1343c75e3ff540de98ff7ebd61874a89bcfeb9" +checksum = "60e8f6b615cb5fc60a98132268508ad104310f0cfb25a1c22eee76efdf9154da" dependencies = [ "aws-smithy-async", "aws-smithy-runtime-api", @@ -2025,9 +2025,9 @@ dependencies = [ [[package]] name = "aws-smithy-types" -version = "1.2.2" +version = "1.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6cee7cadb433c781d3299b916fbf620fea813bf38f49db282fb6858141a05cc8" +checksum = "273dcdfd762fae3e1650b8024624e7cd50e484e37abdab73a7a706188ad34543" dependencies = [ "base64-simd", "bytes", @@ -5670,7 +5670,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19" dependencies = [ "cfg-if", - "windows-targets 0.48.5", + "windows-targets 0.52.6", ] [[package]] @@ -6368,6 +6368,7 @@ dependencies = [ "async-std", "async-trait", "aws-config", + "aws-credential-types", "aws-sdk-s3", "aws-sdk-sns", "aws-sdk-sqs", @@ -8318,6 +8319,7 @@ dependencies = [ "color-eyre", "mockall", "starknet", + "utils", ] [[package]] @@ -8396,6 +8398,7 @@ dependencies = [ "async-trait", "base64 0.22.1", "cairo-vm 1.0.0-rc3", + "color-eyre", "dotenvy", "gps-fact-checker", "hex", diff --git a/crates/da-clients/ethereum/src/config.rs b/crates/da-clients/ethereum/src/config.rs index 34613486..0ac3b72e 100644 --- a/crates/da-clients/ethereum/src/config.rs +++ b/crates/da-clients/ethereum/src/config.rs @@ -1,34 +1,19 @@ -use std::str::FromStr; +use serde::{Deserialize, Serialize}; +use utils::settings::Settings; -use alloy::{network::Ethereum, providers::ProviderBuilder, rpc::client::RpcClient}; -use async_trait::async_trait; -use da_client_interface::DaConfig; -use url::Url; -use utils::env_utils::get_env_var_or_panic; - -use crate::EthereumDaClient; - -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct EthereumDaConfig { pub rpc_url: String, pub memory_pages_contract: String, pub private_key: String, } -#[async_trait] -impl DaConfig for EthereumDaConfig { - fn new_from_env() -> Self { - Self { - rpc_url: get_env_var_or_panic("SETTLEMENT_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 SETTLEMENT_RPC_URL")); - let provider = ProviderBuilder::<_, Ethereum>::new().on_client(client); - - EthereumDaClient { provider } +impl EthereumDaConfig { + pub fn new_with_settings(settings: &impl Settings) -> color_eyre::Result { + Ok(Self { + rpc_url: settings.get_settings("SETTLEMENT_RPC_URL")?, + memory_pages_contract: settings.get_settings("MEMORY_PAGES_CONTRACT_ADDRESS")?, + private_key: settings.get_settings("PRIVATE_KEY")?, + }) } } diff --git a/crates/da-clients/ethereum/src/lib.rs b/crates/da-clients/ethereum/src/lib.rs index 68c933bd..537eb640 100644 --- a/crates/da-clients/ethereum/src/lib.rs +++ b/crates/da-clients/ethereum/src/lib.rs @@ -1,8 +1,10 @@ #![allow(missing_docs)] #![allow(clippy::missing_docs_in_private_items)] +use crate::config::EthereumDaConfig; use alloy::network::Ethereum; -use alloy::providers::RootProvider; +use alloy::providers::{ProviderBuilder, RootProvider}; +use alloy::rpc::client::RpcClient; use alloy::transports::http::Http; use async_trait::async_trait; use color_eyre::Result; @@ -10,6 +12,12 @@ use da_client_interface::{DaClient, DaVerificationStatus}; use mockall::automock; use mockall::predicate::*; use reqwest::Client; +use std::str::FromStr; +use url::Url; +use utils::settings::Settings; + +pub const DA_SETTINGS_NAME: &str = "ethereum"; + pub mod config; pub struct EthereumDaClient { #[allow(dead_code)] @@ -37,3 +45,14 @@ impl DaClient for EthereumDaClient { 131072 } } + +impl EthereumDaClient { + pub fn new_with_settings(settings: &impl Settings) -> Self { + let config = EthereumDaConfig::new_with_settings(settings) + .expect("Not able to create EthereumDaClient from given settings."); + let client = + RpcClient::new_http(Url::from_str(config.rpc_url.as_str()).expect("Failed to parse SETTLEMENT_RPC_URL")); + let provider = ProviderBuilder::<_, Ethereum>::new().on_client(client); + EthereumDaClient { provider } + } +} diff --git a/crates/orchestrator/Cargo.toml b/crates/orchestrator/Cargo.toml index 14e979d6..37c9d672 100644 --- a/crates/orchestrator/Cargo.toml +++ b/crates/orchestrator/Cargo.toml @@ -18,6 +18,9 @@ assert_matches = "1.5.0" async-std = "1.12.0" async-trait = { workspace = true } aws-config = { version = "1.1.7", features = ["behavior-version-latest"] } +aws-credential-types = { version = "1.2.1", features = [ + "hardcoded-credentials", +] } aws-sdk-s3 = { version = "1.38.0", features = ["behavior-version-latest"] } aws-sdk-sns = { version = "1.40.0", features = ["behavior-version-latest"] } aws-sdk-sqs = "1.36.0" diff --git a/crates/orchestrator/src/alerts/aws_sns/config.rs b/crates/orchestrator/src/alerts/aws_sns/config.rs new file mode 100644 index 00000000..c861ba88 --- /dev/null +++ b/crates/orchestrator/src/alerts/aws_sns/config.rs @@ -0,0 +1,19 @@ +use serde::{Deserialize, Serialize}; +use utils::settings::Settings; + +#[derive(Clone, Serialize, Deserialize)] +pub struct AWSSNSConfig { + /// AWS SNS ARN + pub sns_arn: String, + /// AWS SNS region + pub sns_arn_region: String, +} + +impl AWSSNSConfig { + pub fn new_with_settings(settings: &impl Settings) -> color_eyre::Result { + Ok(Self { + sns_arn: settings.get_settings("AWS_SNS_ARN")?, + sns_arn_region: settings.get_settings("AWS_SNS_REGION")?, + }) + } +} diff --git a/crates/orchestrator/src/alerts/aws_sns/mod.rs b/crates/orchestrator/src/alerts/aws_sns/mod.rs index 9aa95f9a..2f8740c3 100644 --- a/crates/orchestrator/src/alerts/aws_sns/mod.rs +++ b/crates/orchestrator/src/alerts/aws_sns/mod.rs @@ -1,27 +1,35 @@ +mod config; + +use crate::alerts::aws_sns::config::AWSSNSConfig; use crate::alerts::Alerts; +use crate::config::ProviderConfig; use async_trait::async_trait; -use aws_sdk_sns::config::Region; use aws_sdk_sns::Client; -use utils::env_utils::get_env_var_or_panic; +use utils::settings::Settings; + +pub const AWS_SNS_SETTINGS_NAME: &str = "sns"; pub struct AWSSNS { client: Client, + topic_arn: String, } impl AWSSNS { - /// To create a new SNS client - pub async fn new() -> Self { - let sns_region = get_env_var_or_panic("AWS_SNS_REGION"); - let config = aws_config::from_env().region(Region::new(sns_region)).load().await; - AWSSNS { client: Client::new(&config) } + pub async fn new_with_settings(settings: &impl Settings, provider_config: ProviderConfig) -> Self { + match provider_config { + ProviderConfig::AWS(aws_config) => { + let sns_config = AWSSNSConfig::new_with_settings(settings) + .expect("Not able to get Aws sns config from provided settings"); + Self { client: Client::new(&aws_config), topic_arn: sns_config.sns_arn } + } + } } } #[async_trait] impl Alerts for AWSSNS { async fn send_alert_message(&self, message_body: String) -> color_eyre::Result<()> { - let topic_arn = get_env_var_or_panic("AWS_SNS_ARN"); - self.client.publish().topic_arn(topic_arn).message(message_body).send().await?; + self.client.publish().topic_arn(self.topic_arn.clone()).message(message_body).send().await?; Ok(()) } } diff --git a/crates/orchestrator/src/config.rs b/crates/orchestrator/src/config.rs index 4f29cac5..4365a95a 100644 --- a/crates/orchestrator/src/config.rs +++ b/crates/orchestrator/src/config.rs @@ -2,14 +2,15 @@ use std::sync::Arc; use crate::alerts::aws_sns::AWSSNS; use crate::alerts::Alerts; -use crate::data_storage::aws_s3::config::AWSS3Config; use crate::data_storage::aws_s3::AWSS3; -use crate::data_storage::{DataStorage, DataStorageConfig}; +use crate::data_storage::DataStorage; use arc_swap::{ArcSwap, Guard}; -use aws_config::SdkConfig; -use da_client_interface::{DaClient, DaConfig}; +use aws_config::meta::region::RegionProviderChain; +use aws_config::{Region, SdkConfig}; +use aws_credential_types::Credentials; +use da_client_interface::DaClient; 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; @@ -19,12 +20,11 @@ use starknet::providers::{JsonRpcClient, Url}; use starknet_settlement_client::StarknetSettlementClient; use tokio::sync::OnceCell; use utils::env_utils::get_env_var_or_panic; -use utils::settings::default::DefaultSettingsProvider; -use utils::settings::SettingsProvider; +use utils::settings::env::EnvSettingsProvider; +use utils::settings::Settings; -use crate::database::mongodb::config::MongoDbConfig; use crate::database::mongodb::MongoDb; -use crate::database::{Database, DatabaseConfig}; +use crate::database::Database; use crate::queue::sqs::SqsQueue; use crate::queue::QueueProvider; @@ -49,6 +49,33 @@ pub struct Config { alerts: Box, } +/// `ProviderConfig` is an enum used to represent the global config built +/// using the settings provider. More providers can be added eg : GCP, AZURE etc. +/// +/// We are using Arc because the config size is large and keeping it +/// a pointer is a better way to pass it through. +pub enum ProviderConfig { + AWS(Arc), +} + +/// To build a `SdkConfig` for AWS provider. +pub async fn get_aws_config(settings_provider: &impl Settings) -> SdkConfig { + let region = settings_provider + .get_settings("AWS_REGION") + .expect("Not able to get AWS_REGION from provided settings provider."); + let region_provider = RegionProviderChain::first_try(Region::new(region)).or_default_provider(); + let credentials = Credentials::from_keys( + settings_provider + .get_settings("AWS_ACCESS_KEY_ID") + .expect("Not able to get AWS_ACCESS_KEY_ID from provided settings provider."), + settings_provider + .get_settings("AWS_SECRET_ACCESS_KEY") + .expect("Not able to get AWS_SECRET_ACCESS_KEY from provided settings provider."), + None, + ); + aws_config::from_env().credentials_provider(credentials).region(region_provider).load().await +} + /// Initializes the app config pub async fn init_config() -> Config { dotenv().ok(); @@ -58,27 +85,22 @@ pub async fn init_config() -> Config { Url::parse(get_env_var_or_panic("MADARA_RPC_URL").as_str()).expect("Failed to parse URL"), )); - // init database - let database = build_database_client().await; + let settings_provider = EnvSettingsProvider {}; + let aws_config = Arc::new(get_aws_config(&settings_provider).await); - // init AWS - let aws_config = aws_config::load_from_env().await; + // init database + let database = build_database_client(&settings_provider).await; + let da_client = build_da_client(&settings_provider).await; + let settlement_client = build_settlement_client(&settings_provider).await; + let prover_client = build_prover_service(&settings_provider); + let storage_client = build_storage_client(&settings_provider, ProviderConfig::AWS(Arc::clone(&aws_config))).await; + let alerts_client = build_alert_client(&settings_provider, ProviderConfig::AWS(Arc::clone(&aws_config))).await; // init the queue // TODO: we use omniqueue for now which doesn't support loading AWS config // from `SdkConfig`. We can later move to using `aws_sdk_sqs`. This would require // us stop using the generic omniqueue abstractions for message ack/nack - let queue = build_queue_client(&aws_config); - - let da_client = build_da_client().await; - - let settings_provider = DefaultSettingsProvider {}; - let settlement_client = build_settlement_client(&settings_provider).await; - let prover_client = build_prover_service(&settings_provider); - - let storage_client = build_storage_client(&aws_config).await; - - let alerts_client = build_alert_client().await; + let queue = build_queue_client(); Config::new( Arc::new(provider), @@ -177,58 +199,60 @@ pub async fn config_force_init(config: Config) { } /// Builds the DA client based on the environment variable DA_LAYER -pub async fn build_da_client() -> Box { +pub async fn build_da_client(settings_provider: &impl Settings) -> Box { match get_env_var_or_panic("DA_LAYER").as_str() { - "ethereum" => { - let config = EthereumDaConfig::new_from_env(); - Box::new(config.build_client().await) - } + "ethereum" => Box::new(EthereumDaClient::new_with_settings(settings_provider)), _ => panic!("Unsupported DA layer"), } } /// Builds the prover service based on the environment variable PROVER_SERVICE -pub fn build_prover_service(settings_provider: &impl SettingsProvider) -> Box { +pub fn build_prover_service(settings_provider: &impl Settings) -> Box { match get_env_var_or_panic("PROVER_SERVICE").as_str() { - "sharp" => Box::new(SharpProverService::with_settings(settings_provider)), + "sharp" => Box::new(SharpProverService::new_with_settings(settings_provider)), _ => panic!("Unsupported prover service"), } } /// Builds the settlement client depending on the env variable SETTLEMENT_LAYER -pub async fn build_settlement_client( - settings_provider: &impl SettingsProvider, -) -> Box { +pub async fn build_settlement_client(settings_provider: &impl Settings) -> Box { match get_env_var_or_panic("SETTLEMENT_LAYER").as_str() { - "ethereum" => Box::new(EthereumSettlementClient::with_settings(settings_provider)), - "starknet" => Box::new(StarknetSettlementClient::with_settings(settings_provider).await), + "ethereum" => Box::new(EthereumSettlementClient::new_with_settings(settings_provider)), + "starknet" => Box::new(StarknetSettlementClient::new_with_settings(settings_provider).await), _ => panic!("Unsupported Settlement layer"), } } -pub async fn build_storage_client(aws_config: &SdkConfig) -> Box { +pub async fn build_storage_client( + settings_provider: &impl Settings, + provider_config: ProviderConfig, +) -> Box { match get_env_var_or_panic("DATA_STORAGE").as_str() { - "s3" => Box::new(AWSS3::new(AWSS3Config::new_from_env(), aws_config)), + "s3" => Box::new(AWSS3::new_with_settings(settings_provider, provider_config).await), _ => panic!("Unsupported Storage Client"), } } -pub async fn build_alert_client() -> Box { +pub async fn build_alert_client( + settings_provider: &impl Settings, + provider_config: ProviderConfig, +) -> Box { match get_env_var_or_panic("ALERTS").as_str() { - "sns" => Box::new(AWSSNS::new().await), + "sns" => Box::new(AWSSNS::new_with_settings(settings_provider, provider_config).await), _ => panic!("Unsupported Alert Client"), } } -pub fn build_queue_client(_aws_config: &SdkConfig) -> Box { + +pub fn build_queue_client() -> Box { match get_env_var_or_panic("QUEUE_PROVIDER").as_str() { "sqs" => Box::new(SqsQueue {}), _ => panic!("Unsupported Queue Client"), } } -pub async fn build_database_client() -> Box { +pub async fn build_database_client(settings_provider: &impl Settings) -> Box { match get_env_var_or_panic("DATABASE").as_str() { - "mongodb" => Box::new(MongoDb::new(MongoDbConfig::new_from_env()).await), + "mongodb" => Box::new(MongoDb::new_with_settings(settings_provider).await), _ => panic!("Unsupported Database Client"), } } diff --git a/crates/orchestrator/src/data_storage/aws_s3/config.rs b/crates/orchestrator/src/data_storage/aws_s3/config.rs index 665caeb4..5c542e3f 100644 --- a/crates/orchestrator/src/data_storage/aws_s3/config.rs +++ b/crates/orchestrator/src/data_storage/aws_s3/config.rs @@ -1,9 +1,10 @@ -use utils::env_utils::get_env_var_or_panic; +use serde::{Deserialize, Serialize}; +use utils::settings::Settings; use crate::data_storage::DataStorageConfig; /// Represents AWS S3 config struct with all the necessary variables. -#[derive(Clone)] +#[derive(Clone, Serialize, Deserialize)] pub struct AWSS3Config { /// S3 Bucket Name pub bucket_name: String, @@ -12,7 +13,11 @@ pub struct AWSS3Config { /// Implementation of `DataStorageConfig` for `AWSS3Config` impl DataStorageConfig for AWSS3Config { /// To return the config struct by creating it from the environment variables. - fn new_from_env() -> Self { - Self { bucket_name: get_env_var_or_panic("AWS_S3_BUCKET_NAME") } + fn new_with_settings(settings: &impl Settings) -> Self { + Self { + bucket_name: settings + .get_settings("AWS_S3_BUCKET_NAME") + .expect("Not able to get AWS_S3_BUCKET_NAME from settings provided."), + } } } diff --git a/crates/orchestrator/src/data_storage/aws_s3/mod.rs b/crates/orchestrator/src/data_storage/aws_s3/mod.rs index 0f920c7a..bb227dbc 100644 --- a/crates/orchestrator/src/data_storage/aws_s3/mod.rs +++ b/crates/orchestrator/src/data_storage/aws_s3/mod.rs @@ -1,11 +1,14 @@ +use crate::config::ProviderConfig; use crate::data_storage::aws_s3::config::AWSS3Config; -use crate::data_storage::DataStorage; +use crate::data_storage::{DataStorage, DataStorageConfig}; use async_trait::async_trait; -use aws_config::SdkConfig; use aws_sdk_s3::primitives::ByteStream; use aws_sdk_s3::Client; use bytes::Bytes; use color_eyre::Result; +use utils::settings::Settings; + +pub const S3_SETTINGS_NAME: &str = "s3"; /// Module for AWS S3 config structs and implementations pub mod config; @@ -20,16 +23,19 @@ pub struct AWSS3 { /// /// - initializing a new AWS S3 client impl AWSS3 { - /// Initializes a new AWS S3 client by passing the config - /// and returning it. - pub fn new(s3_config: AWSS3Config, aws_config: &SdkConfig) -> Self { - // Building AWS S3 config - let mut s3_config_builder = aws_sdk_s3::config::Builder::from(aws_config); - - // this is necessary for it to work with localstack in test cases - s3_config_builder.set_force_path_style(Some(true)); - let client = Client::from_conf(s3_config_builder.build()); - Self { client, bucket: s3_config.bucket_name } + /// To init the struct with main settings + pub async fn new_with_settings(settings: &impl Settings, provider_config: ProviderConfig) -> Self { + match provider_config { + ProviderConfig::AWS(aws_config) => { + let s3_config = AWSS3Config::new_with_settings(settings); + // Building AWS S3 config + let mut s3_config_builder = aws_sdk_s3::config::Builder::from(aws_config.as_ref()); + // this is necessary for it to work with localstack in test cases + s3_config_builder.set_force_path_style(Some(true)); + let client = Client::from_conf(s3_config_builder.build()); + Self { client, bucket: s3_config.bucket_name } + } + } } } diff --git a/crates/orchestrator/src/data_storage/mod.rs b/crates/orchestrator/src/data_storage/mod.rs index 598e031e..305a3dda 100644 --- a/crates/orchestrator/src/data_storage/mod.rs +++ b/crates/orchestrator/src/data_storage/mod.rs @@ -5,6 +5,7 @@ use async_trait::async_trait; use bytes::Bytes; use color_eyre::Result; use mockall::automock; +use utils::settings::Settings; /// DataStorage trait contains the functions used to store and get the data from /// the cloud provider storage. @@ -25,6 +26,5 @@ pub trait DataStorage: Send + Sync { /// initialisation of data storage client pub trait DataStorageConfig { /// Get a config file from environment vars in system or - /// env file. - fn new_from_env() -> Self; + fn new_with_settings(settings: &impl Settings) -> Self; } diff --git a/crates/orchestrator/src/database/mod.rs b/crates/orchestrator/src/database/mod.rs index a410755e..bd79f1e8 100644 --- a/crates/orchestrator/src/database/mod.rs +++ b/crates/orchestrator/src/database/mod.rs @@ -4,6 +4,7 @@ use ::mongodb::bson::doc; use async_trait::async_trait; use color_eyre::Result; use mockall::automock; +use utils::settings::Settings; use uuid::Uuid; use crate::jobs::types::{JobItem, JobStatus, JobType}; @@ -53,5 +54,5 @@ pub trait Database: Send + Sync { } pub trait DatabaseConfig { - fn new_from_env() -> Self; + fn new_with_settings(settings: &impl Settings) -> Self; } diff --git a/crates/orchestrator/src/database/mongodb/config.rs b/crates/orchestrator/src/database/mongodb/config.rs index 6ec561da..67c25b39 100644 --- a/crates/orchestrator/src/database/mongodb/config.rs +++ b/crates/orchestrator/src/database/mongodb/config.rs @@ -1,13 +1,19 @@ -use utils::env_utils::get_env_var_or_panic; +use serde::{Deserialize, Serialize}; +use utils::settings::Settings; use crate::database::DatabaseConfig; +#[derive(Debug, Serialize, Deserialize)] pub struct MongoDbConfig { pub url: String, } impl DatabaseConfig for MongoDbConfig { - fn new_from_env() -> Self { - Self { url: get_env_var_or_panic("MONGODB_CONNECTION_STRING") } + fn new_with_settings(settings: &impl Settings) -> Self { + Self { + url: settings + .get_settings("MONGODB_CONNECTION_STRING") + .expect("Not able to get MONGODB_CONNECTION_STRING form the given settings"), + } } } diff --git a/crates/orchestrator/src/database/mongodb/mod.rs b/crates/orchestrator/src/database/mongodb/mod.rs index 8e4102eb..55c3e89c 100644 --- a/crates/orchestrator/src/database/mongodb/mod.rs +++ b/crates/orchestrator/src/database/mongodb/mod.rs @@ -14,21 +14,26 @@ use mongodb::{ options::{ClientOptions, ServerApi, ServerApiVersion}, Client, Collection, }; +use utils::settings::Settings; use uuid::Uuid; use crate::database::mongodb::config::MongoDbConfig; -use crate::database::Database; +use crate::database::{Database, DatabaseConfig}; use crate::jobs::types::{JobItem, JobStatus, JobType}; pub mod config; +pub const MONGO_DB_SETTINGS: &str = "mongodb"; + pub struct MongoDb { client: Client, } impl MongoDb { - pub async fn new(config: MongoDbConfig) -> Self { - let mut client_options = ClientOptions::parse(config.url).await.expect("Failed to parse MongoDB Url"); + pub async fn new_with_settings(settings: &impl Settings) -> Self { + let mongo_db_settings = MongoDbConfig::new_with_settings(settings); + let mut client_options = + ClientOptions::parse(mongo_db_settings.url).await.expect("Failed to parse MongoDB Url"); // Set the server_api field of the client_options object to set the version of the Stable API on the // client let server_api = ServerApi::builder().version(ServerApiVersion::V1).build(); @@ -39,7 +44,7 @@ impl MongoDb { client.database("admin").run_command(doc! {"ping": 1}, None).await.expect("Failed to ping MongoDB deployment"); println!("Pinged your deployment. You successfully connected to MongoDB!"); - MongoDb { client } + Self { client } } /// Mongodb client uses Arc internally, reducing the cost of clone. diff --git a/crates/orchestrator/src/tests/common/mod.rs b/crates/orchestrator/src/tests/common/mod.rs index 8e36bf22..6e183b7d 100644 --- a/crates/orchestrator/src/tests/common/mod.rs +++ b/crates/orchestrator/src/tests/common/mod.rs @@ -1,7 +1,9 @@ pub mod constants; use std::collections::HashMap; +use std::sync::Arc; +use crate::config::{get_aws_config, ProviderConfig}; use ::uuid::Uuid; use aws_config::Region; use aws_sdk_sns::error::SdkError; @@ -11,13 +13,11 @@ use mongodb::Client; use rstest::*; use serde::Deserialize; use utils::env_utils::get_env_var_or_panic; +use utils::settings::env::EnvSettingsProvider; -use crate::data_storage::aws_s3::config::AWSS3Config; use crate::data_storage::aws_s3::AWSS3; -use crate::data_storage::{DataStorage, DataStorageConfig}; -use crate::database::mongodb::config::MongoDbConfig; +use crate::data_storage::DataStorage; use crate::database::mongodb::MongoDb; -use crate::database::DatabaseConfig; use crate::jobs::types::JobStatus::Created; use crate::jobs::types::JobType::DataSubmission; use crate::jobs::types::{ExternalId, JobItem}; @@ -59,7 +59,7 @@ pub async fn get_sns_client() -> aws_sdk_sns::client::Client { } pub async fn drop_database() -> color_eyre::Result<()> { - let db_client: Client = MongoDb::new(MongoDbConfig::new_from_env()).await.client(); + let db_client: Client = MongoDb::new_with_settings(&EnvSettingsProvider {}).await.client(); // dropping all the collection. // use .collection::("") // if only particular collection is to be dropped @@ -102,7 +102,11 @@ pub struct MessagePayloadType { } pub async fn get_storage_client() -> Box { - let aws_config = - aws_config::load_from_env().await.into_builder().endpoint_url(get_env_var_or_panic("AWS_ENDPOINT_URL")).build(); - Box::new(AWSS3::new(AWSS3Config::new_from_env(), &aws_config)) + Box::new( + AWSS3::new_with_settings( + &EnvSettingsProvider {}, + ProviderConfig::AWS(Arc::new(get_aws_config(&EnvSettingsProvider {}).await)), + ) + .await, + ) } diff --git a/crates/orchestrator/src/tests/config.rs b/crates/orchestrator/src/tests/config.rs index 8424d6d7..216fabf6 100644 --- a/crates/orchestrator/src/tests/config.rs +++ b/crates/orchestrator/src/tests/config.rs @@ -1,7 +1,8 @@ use std::sync::Arc; use crate::config::{ - build_alert_client, build_da_client, build_prover_service, build_settlement_client, config_force_init, Config, + build_alert_client, build_da_client, build_prover_service, build_settlement_client, config_force_init, + get_aws_config, Config, ProviderConfig, }; use crate::data_storage::DataStorage; use da_client_interface::DaClient; @@ -13,11 +14,10 @@ use settlement_client_interface::SettlementClient; use starknet::providers::jsonrpc::HttpTransport; use starknet::providers::{JsonRpcClient, Url}; use utils::env_utils::get_env_var_or_panic; -use utils::settings::default::DefaultSettingsProvider; +use utils::settings::env::EnvSettingsProvider; -use crate::database::mongodb::config::MongoDbConfig; use crate::database::mongodb::MongoDb; -use crate::database::{Database, DatabaseConfig}; +use crate::database::Database; use crate::queue::sqs::SqsQueue; use crate::queue::QueueProvider; use crate::tests::common::{create_sns_arn, create_sqs_queues, drop_database, get_storage_client}; @@ -111,16 +111,17 @@ impl TestConfigBuilder { dotenvy::from_filename("../.env.test").expect("Failed to load the .env file"); let server = MockServer::start(); - let settings_provider = DefaultSettingsProvider {}; + let settings_provider = EnvSettingsProvider {}; + let aws_config = get_aws_config(&settings_provider).await; // init database if self.database.is_none() { - self.database = Some(Box::new(MongoDb::new(MongoDbConfig::new_from_env()).await)); + self.database = Some(Box::new(MongoDb::new_with_settings(&settings_provider).await)); } // init the DA client if self.da_client.is_none() { - self.da_client = Some(build_da_client().await); + self.da_client = Some(build_da_client(&settings_provider).await); } // init the Settings client @@ -144,7 +145,7 @@ impl TestConfigBuilder { } if self.alerts.is_none() { - self.alerts = Some(build_alert_client().await); + self.alerts = Some(build_alert_client(&settings_provider, ProviderConfig::AWS(Arc::new(aws_config))).await); } // Deleting and Creating the queues in sqs. diff --git a/crates/orchestrator/src/tests/data_storage/mod.rs b/crates/orchestrator/src/tests/data_storage/mod.rs index a3055acb..b9890f36 100644 --- a/crates/orchestrator/src/tests/data_storage/mod.rs +++ b/crates/orchestrator/src/tests/data_storage/mod.rs @@ -1,10 +1,12 @@ -use crate::data_storage::aws_s3::config::AWSS3Config; +use crate::config::{get_aws_config, ProviderConfig}; use crate::data_storage::aws_s3::AWSS3; -use crate::data_storage::{DataStorage, DataStorageConfig}; +use crate::data_storage::DataStorage; use bytes::Bytes; use rstest::rstest; use serde_json::json; +use std::sync::Arc; use utils::env_utils::get_env_var_or_panic; +use utils::settings::env::EnvSettingsProvider; /// This test checks the ability to put and get data from AWS S3 using `AWSS3`. /// It puts JSON data into a test bucket and retrieves it, verifying the data @@ -14,11 +16,11 @@ use utils::env_utils::get_env_var_or_panic; #[tokio::test] async fn test_put_and_get_data_s3() -> color_eyre::Result<()> { dotenvy::from_filename("../.env.test")?; - - let config = AWSS3Config::new_from_env(); - let aws_config = - aws_config::load_from_env().await.into_builder().endpoint_url(get_env_var_or_panic("AWS_ENDPOINT_URL")).build(); - let s3_client = AWSS3::new(config, &aws_config); + let s3_client = AWSS3::new_with_settings( + &EnvSettingsProvider {}, + ProviderConfig::AWS(Arc::new(get_aws_config(&EnvSettingsProvider {}).await)), + ) + .await; s3_client.build_test_bucket(&get_env_var_or_panic("AWS_S3_BUCKET_NAME")).await.unwrap(); let mock_data = json!( diff --git a/crates/prover-services/sharp-service/Cargo.toml b/crates/prover-services/sharp-service/Cargo.toml index dd46b05a..ef9d987e 100644 --- a/crates/prover-services/sharp-service/Cargo.toml +++ b/crates/prover-services/sharp-service/Cargo.toml @@ -8,6 +8,7 @@ alloy.workspace = true async-trait.workspace = true base64 = "0.22.1" cairo-vm.workspace = true +color-eyre.workspace = true dotenvy.workspace = true gps-fact-checker.workspace = true hex.workspace = true diff --git a/crates/prover-services/sharp-service/src/config.rs b/crates/prover-services/sharp-service/src/config.rs index eddd4768..92ff53c4 100644 --- a/crates/prover-services/sharp-service/src/config.rs +++ b/crates/prover-services/sharp-service/src/config.rs @@ -1,8 +1,7 @@ use alloy::primitives::Address; use serde::{Deserialize, Serialize}; use url::Url; - -use crate::client::DEFAULT_SHARP_URL; +use utils::settings::Settings; /// SHARP proving service configuration #[derive(Debug, Clone, Serialize, Deserialize)] @@ -15,13 +14,12 @@ pub struct SharpConfig { pub verifier_address: Address, } -impl Default for SharpConfig { - /// Default config for Sepolia testnet - fn default() -> Self { - Self { - service_url: DEFAULT_SHARP_URL.parse().unwrap(), - rpc_node_url: "https://ethereum-sepolia-rpc.publicnode.com".parse().unwrap(), - verifier_address: "0x07ec0D28e50322Eb0C159B9090ecF3aeA8346DFe".parse().unwrap(), - } +impl SharpConfig { + pub fn new_with_settings(settings: &impl Settings) -> color_eyre::Result { + Ok(Self { + service_url: settings.get_settings("SETTLEMENT_RPC_URL")?.parse().unwrap(), + rpc_node_url: settings.get_settings("SETTLEMENT_RPC_URL")?.parse().unwrap(), + verifier_address: settings.get_settings("MEMORY_PAGES_CONTRACT_ADDRESS")?.parse().unwrap(), + }) } } diff --git a/crates/prover-services/sharp-service/src/lib.rs b/crates/prover-services/sharp-service/src/lib.rs index 1ef26004..5fa585f6 100644 --- a/crates/prover-services/sharp-service/src/lib.rs +++ b/crates/prover-services/sharp-service/src/lib.rs @@ -11,7 +11,7 @@ use gps_fact_checker::fact_info::get_fact_info; use gps_fact_checker::FactChecker; use prover_client_interface::{ProverClient, ProverClientError, Task, TaskId, TaskStatus}; use snos::sharp::CairoJobStatus; -use utils::settings::SettingsProvider; +use utils::settings::Settings; use uuid::Uuid; use crate::client::SharpClient; @@ -71,17 +71,19 @@ impl SharpProverService { Self { sharp_client, fact_checker } } - pub fn with_settings(settings: &impl SettingsProvider) -> Self { - let sharp_cfg: SharpConfig = settings.get_settings(SHARP_SETTINGS_NAME).unwrap(); - let sharp_client = SharpClient::new(sharp_cfg.service_url); - let fact_checker = FactChecker::new(sharp_cfg.rpc_node_url, sharp_cfg.verifier_address); + pub fn new_with_settings(settings: &impl Settings) -> Self { + let sharp_config = SharpConfig::new_with_settings(settings) + .expect("Not able to create SharpProverService from given settings."); + let sharp_client = SharpClient::new(sharp_config.service_url); + let fact_checker = FactChecker::new(sharp_config.rpc_node_url, sharp_config.verifier_address); Self::new(sharp_client, fact_checker) } - pub fn with_test_settings(settings: &impl SettingsProvider, port: u16) -> Self { - let sharp_cfg: SharpConfig = settings.get_settings(SHARP_SETTINGS_NAME).unwrap(); + pub fn with_test_settings(settings: &impl Settings, port: u16) -> Self { + let sharp_config = SharpConfig::new_with_settings(settings) + .expect("Not able to create SharpProverService from given settings."); let sharp_client = SharpClient::new(format!("http://127.0.0.1:{}", port).parse().unwrap()); - let fact_checker = FactChecker::new(sharp_cfg.rpc_node_url, sharp_cfg.verifier_address); + let fact_checker = FactChecker::new(sharp_config.rpc_node_url, sharp_config.verifier_address); Self::new(sharp_client, fact_checker) } } @@ -108,7 +110,7 @@ mod tests { use prover_client_interface::{ProverClient, Task, TaskStatus}; use tracing::log::log; use tracing::log::Level::{Error, Info}; - use utils::settings::default::DefaultSettingsProvider; + use utils::settings::env::EnvSettingsProvider; use crate::SharpProverService; @@ -117,7 +119,7 @@ mod tests { async fn sharp_reproduce_rate_limiting_issue() { // TODO: leaving this test to check if the issue still reproduces (504 error after 8th job status // query) - let sharp_service = SharpProverService::with_settings(&DefaultSettingsProvider {}); + let sharp_service = SharpProverService::new_with_settings(&EnvSettingsProvider {}); let cairo_pie_path: PathBuf = [env!("CARGO_MANIFEST_DIR"), "tests", "artifacts", "fibonacci.zip"].iter().collect(); let cairo_pie = CairoPie::read_zip_file(&cairo_pie_path).unwrap(); diff --git a/crates/prover-services/sharp-service/tests/lib.rs b/crates/prover-services/sharp-service/tests/lib.rs index 814b98f6..0171af93 100644 --- a/crates/prover-services/sharp-service/tests/lib.rs +++ b/crates/prover-services/sharp-service/tests/lib.rs @@ -9,7 +9,7 @@ use sharp_service::{split_task_id, SharpProverService}; use snos::sharp::CairoJobStatus; use std::str::FromStr; use utils::env_utils::get_env_var_or_panic; -use utils::settings::default::DefaultSettingsProvider; +use utils::settings::env::EnvSettingsProvider; mod constants; @@ -19,7 +19,7 @@ async fn prover_client_submit_task_works() { dotenvy::from_filename("../.env.test").expect("Failed to load the .env file"); let server = MockServer::start(); - let sharp_service = SharpProverService::with_test_settings(&DefaultSettingsProvider {}, server.port()); + let sharp_service = SharpProverService::with_test_settings(&EnvSettingsProvider {}, server.port()); let cairo_pie_path = env!("CARGO_MANIFEST_DIR").to_string() + CAIRO_PIE_PATH; let cairo_pie = CairoPie::read_zip_file(cairo_pie_path.as_ref()).unwrap(); @@ -58,7 +58,7 @@ async fn prover_client_get_task_status_works(#[case] cairo_job_status: CairoJobS dotenvy::from_filename("../.env.test").expect("Failed to load the .env file"); let server = MockServer::start(); - let sharp_service = SharpProverService::with_test_settings(&DefaultSettingsProvider {}, server.port()); + let sharp_service = SharpProverService::with_test_settings(&EnvSettingsProvider {}, server.port()); let customer_id = get_env_var_or_panic("SHARP_CUSTOMER_ID"); let sharp_add_job_call = server.mock(|when, then| { diff --git a/crates/settlement-clients/ethereum/src/config.rs b/crates/settlement-clients/ethereum/src/config.rs index 3d038390..ae7c0a31 100644 --- a/crates/settlement-clients/ethereum/src/config.rs +++ b/crates/settlement-clients/ethereum/src/config.rs @@ -3,7 +3,7 @@ use std::str::FromStr; use serde::{Deserialize, Serialize}; use settlement_client_interface::SettlementConfig; use url::Url; -use utils::env_utils::get_env_var_or_panic; +use utils::settings::Settings; pub const ENV_CORE_CONTRACT_ADDRESS: &str = "STARKNET_SOLIDITY_CORE_CONTRACT_ADDRESS"; pub const SETTLEMENT_RPC_URL: &str = "SETTLEMENT_RPC_URL"; @@ -16,19 +16,14 @@ pub struct EthereumSettlementConfig { } impl SettlementConfig for EthereumSettlementConfig { - fn new_from_env() -> Self { - let rpc_url = get_env_var_or_panic(SETTLEMENT_RPC_URL); + fn new_with_settings(settings: &impl Settings) -> Self { + let rpc_url = settings + .get_settings(SETTLEMENT_RPC_URL) + .expect("Not able to get SETTLEMENT_RPC_URL from settings provided"); let rpc_url = Url::from_str(&rpc_url).unwrap_or_else(|_| panic!("Failed to parse {}", SETTLEMENT_RPC_URL)); - let core_contract_address = get_env_var_or_panic(ENV_CORE_CONTRACT_ADDRESS); + let core_contract_address = settings + .get_settings(ENV_CORE_CONTRACT_ADDRESS) + .expect("Not able to get ENV_CORE_CONTRACT_ADDRESS from settings provided"); Self { rpc_url, core_contract_address } } } - -impl Default for EthereumSettlementConfig { - fn default() -> Self { - Self { - rpc_url: get_env_var_or_panic(SETTLEMENT_RPC_URL).parse().unwrap(), - core_contract_address: get_env_var_or_panic(L1_CORE_CONTRACT_ADDRESS), - } - } -} diff --git a/crates/settlement-clients/ethereum/src/lib.rs b/crates/settlement-clients/ethereum/src/lib.rs index 38ee8bb2..dcea53f0 100644 --- a/crates/settlement-clients/ethereum/src/lib.rs +++ b/crates/settlement-clients/ethereum/src/lib.rs @@ -27,10 +27,10 @@ use mockall::{automock, predicate::*}; use alloy::providers::ProviderBuilder; use conversion::{get_input_data_for_eip_4844, prepare_sidecar}; -use settlement_client_interface::{SettlementClient, SettlementVerificationStatus, SETTLEMENT_SETTINGS_NAME}; +use settlement_client_interface::{SettlementClient, SettlementConfig, SettlementVerificationStatus}; #[cfg(test)] use url::Url; -use utils::{env_utils::get_env_var_or_panic, settings::SettingsProvider}; +use utils::env_utils::get_env_var_or_panic; use crate::clients::interfaces::validity_interface::StarknetValidityContractTrait; use crate::clients::StarknetValidityContractClient; @@ -44,6 +44,7 @@ pub mod conversion; mod tests; pub mod types; +use utils::settings::Settings; use {alloy::providers::RootProvider, alloy::transports::http::Http, reqwest::Client}; pub const ENV_PRIVATE_KEY: &str = "ETHEREUM_PRIVATE_KEY"; @@ -66,9 +67,8 @@ pub struct EthereumSettlementClient { } impl EthereumSettlementClient { - pub fn with_settings(settings: &impl SettingsProvider) -> Self { - let settlement_cfg: EthereumSettlementConfig = settings.get_settings(SETTLEMENT_SETTINGS_NAME).unwrap(); - + pub fn new_with_settings(settings: &impl Settings) -> Self { + let settlement_cfg = EthereumSettlementConfig::new_with_settings(settings); let private_key = get_env_var_or_panic(ENV_PRIVATE_KEY); let signer: PrivateKeySigner = private_key.parse().expect("Failed to parse private key"); let wallet_address = signer.address(); diff --git a/crates/settlement-clients/settlement-client-interface/Cargo.toml b/crates/settlement-clients/settlement-client-interface/Cargo.toml index 3ef49239..d6a4cc7b 100644 --- a/crates/settlement-clients/settlement-client-interface/Cargo.toml +++ b/crates/settlement-clients/settlement-client-interface/Cargo.toml @@ -13,3 +13,4 @@ c-kzg = { workspace = true } color-eyre = { workspace = true } mockall = { workspace = true } starknet = { workspace = true } +utils = { workspace = true } diff --git a/crates/settlement-clients/settlement-client-interface/src/lib.rs b/crates/settlement-clients/settlement-client-interface/src/lib.rs index a827ad47..afe3d2e7 100644 --- a/crates/settlement-clients/settlement-client-interface/src/lib.rs +++ b/crates/settlement-clients/settlement-client-interface/src/lib.rs @@ -2,6 +2,7 @@ use async_trait::async_trait; use color_eyre::eyre::Result; use mockall::automock; use mockall::predicate::*; +use utils::settings::Settings; pub const SETTLEMENT_SETTINGS_NAME: &str = "settlement_settings"; @@ -52,5 +53,5 @@ pub trait SettlementClient: Send + Sync { /// Trait for every new SettlementConfig to implement pub trait SettlementConfig { /// Should create a new instance of the SettlementConfig from the environment variables - fn new_from_env() -> Self; + fn new_with_settings(settings: &impl Settings) -> Self; } diff --git a/crates/settlement-clients/starknet/src/config.rs b/crates/settlement-clients/starknet/src/config.rs index 60131bdc..7d4e662e 100644 --- a/crates/settlement-clients/starknet/src/config.rs +++ b/crates/settlement-clients/starknet/src/config.rs @@ -3,7 +3,8 @@ use std::str::FromStr; use serde::{Deserialize, Serialize}; use settlement_client_interface::SettlementConfig; use url::Url; -use utils::env_utils::{get_env_var_or_default, get_env_var_or_panic}; +use utils::env_utils::get_env_var_or_default; +use utils::settings::Settings; pub const ENV_STARKNET_RPC_URL: &str = "STARKNET_RPC_URL"; pub const ENV_CORE_CONTRACT_ADDRESS: &str = "STARKNET_CAIRO_CORE_CONTRACT_ADDRESS"; @@ -20,10 +21,14 @@ pub struct StarknetSettlementConfig { impl SettlementConfig for StarknetSettlementConfig { /// Should create a new instance of the DaConfig from the environment variables - fn new_from_env() -> Self { - let rpc_url = get_env_var_or_panic(ENV_STARKNET_RPC_URL); + fn new_with_settings(settings: &impl Settings) -> Self { + let rpc_url = settings + .get_settings(ENV_STARKNET_RPC_URL) + .expect("Not able to get ENV_STARKNET_RPC_URL from settings provided"); let rpc_url = Url::from_str(&rpc_url).unwrap_or_else(|_| panic!("Failed to parse {}", ENV_STARKNET_RPC_URL)); - let core_contract_address = get_env_var_or_panic(ENV_CORE_CONTRACT_ADDRESS); + let core_contract_address = settings + .get_settings(ENV_CORE_CONTRACT_ADDRESS) + .expect("Not able to get ENV_CORE_CONTRACT_ADDRESS from settings provided"); let tx_finality_retry_delay_in_seconds: u64 = get_env_var_or_default(ENV_STARKNET_FINALITY_RETRY_DELAY_IN_SECS, DEFAULT_FINALITY_RETRY_DELAY) .parse() @@ -31,13 +36,3 @@ impl SettlementConfig for StarknetSettlementConfig { StarknetSettlementConfig { rpc_url, core_contract_address, tx_finality_retry_delay_in_seconds } } } - -impl Default for StarknetSettlementConfig { - fn default() -> Self { - Self { - rpc_url: "https://free-rpc.nethermind.io/sepolia-juno".parse().unwrap(), - core_contract_address: "TODO:https://github.com/keep-starknet-strange/piltover".into(), - tx_finality_retry_delay_in_seconds: 60, - } - } -} diff --git a/crates/settlement-clients/starknet/src/lib.rs b/crates/settlement-clients/starknet/src/lib.rs index 0730309e..4ae098a0 100644 --- a/crates/settlement-clients/starknet/src/lib.rs +++ b/crates/settlement-clients/starknet/src/lib.rs @@ -22,9 +22,8 @@ use starknet::{ }; use tokio::time::{sleep, Duration}; -use settlement_client_interface::{SettlementClient, SettlementVerificationStatus, SETTLEMENT_SETTINGS_NAME}; -use utils::env_utils::get_env_var_or_panic; -use utils::settings::SettingsProvider; +use settlement_client_interface::{SettlementClient, SettlementConfig, SettlementVerificationStatus}; +use utils::settings::Settings; use crate::config::StarknetSettlementConfig; use crate::conversion::{slice_slice_u8_to_vec_field, slice_u8_to_field}; @@ -44,15 +43,17 @@ const MAX_RETRIES_VERIFY_TX_FINALITY: usize = 10; // https://github.com/keep-starknet-strange/piltover impl StarknetSettlementClient { - pub async fn with_settings(settings: &impl SettingsProvider) -> Self { - let settlement_cfg: StarknetSettlementConfig = settings.get_settings(SETTLEMENT_SETTINGS_NAME).unwrap(); + pub async fn new_with_settings(settings: &impl Settings) -> Self { + let settlement_cfg = StarknetSettlementConfig::new_with_settings(settings); let provider = Arc::new(JsonRpcClient::new(HttpTransport::new(settlement_cfg.rpc_url))); - let public_key = get_env_var_or_panic(ENV_PUBLIC_KEY); + let public_key = + settings.get_settings(ENV_PUBLIC_KEY).expect("Not able to get ENV_PUBLIC_KEY from given settings."); let signer_address = FieldElement::from_hex_be(&public_key).expect("invalid signer address"); // TODO: Very insecure way of building the signer. Needs to be adjusted. - let private_key = get_env_var_or_panic(ENV_PRIVATE_KEY); + let private_key = + settings.get_settings(ENV_PRIVATE_KEY).expect("Not able to get ENV_PRIVATE_KEY from given settings."); let signer = FieldElement::from_hex_be(&private_key).expect("Invalid private key"); let signer = LocalWallet::from(SigningKey::from_secret_scalar(signer)); diff --git a/crates/utils/src/settings/default.rs b/crates/utils/src/settings/default.rs deleted file mode 100644 index 3166ab6e..00000000 --- a/crates/utils/src/settings/default.rs +++ /dev/null @@ -1,13 +0,0 @@ -use super::SettingsProvider; - -#[derive(Debug, Clone, Default)] -pub struct DefaultSettingsProvider {} - -impl SettingsProvider for DefaultSettingsProvider { - fn get_settings( - &self, - _section: &'static str, - ) -> Result { - Ok(T::default()) - } -} diff --git a/crates/utils/src/settings/env.rs b/crates/utils/src/settings/env.rs new file mode 100644 index 00000000..8cc4ecd2 --- /dev/null +++ b/crates/utils/src/settings/env.rs @@ -0,0 +1,11 @@ +use crate::env_utils::get_env_var_or_panic; +use crate::settings::{Settings, SettingsProviderError}; + +#[derive(Debug, Clone, Default)] +pub struct EnvSettingsProvider {} + +impl Settings for EnvSettingsProvider { + fn get_settings(&self, name: &'static str) -> Result { + Ok(get_env_var_or_panic(name)) + } +} diff --git a/crates/utils/src/settings/mod.rs b/crates/utils/src/settings/mod.rs index cbf87c43..ede9511c 100644 --- a/crates/utils/src/settings/mod.rs +++ b/crates/utils/src/settings/mod.rs @@ -1,6 +1,4 @@ -pub mod default; - -use serde::de::DeserializeOwned; +pub mod env; #[derive(Debug, thiserror::Error)] pub enum SettingsProviderError { @@ -8,6 +6,6 @@ pub enum SettingsProviderError { Internal(#[source] Box), } -pub trait SettingsProvider { - fn get_settings(&self, name: &'static str) -> Result; +pub trait Settings { + fn get_settings(&self, name: &'static str) -> Result; }