Skip to content

Commit

Permalink
Enhancement : Refactor Settings (#106)
Browse files Browse the repository at this point in the history
* feat : added settings

* changelog

* feat : added env settings fucntion

* feat : updated settings module implementation

* feat : updated provider config

* feat : added arc for aws config

* chore : refactoring
  • Loading branch information
ocdbytes authored Sep 9, 2024
1 parent 30116b2 commit 6d6ca02
Show file tree
Hide file tree
Showing 31 changed files with 296 additions and 214 deletions.
2 changes: 1 addition & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 8 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 10 additions & 25 deletions crates/da-clients/ethereum/src/config.rs
Original file line number Diff line number Diff line change
@@ -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<EthereumDaClient> 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<Self> {
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")?,
})
}
}
21 changes: 20 additions & 1 deletion crates/da-clients/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
#![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;
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)]
Expand Down Expand Up @@ -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 }
}
}
3 changes: 3 additions & 0 deletions crates/orchestrator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 19 additions & 0 deletions crates/orchestrator/src/alerts/aws_sns/config.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
Ok(Self {
sns_arn: settings.get_settings("AWS_SNS_ARN")?,
sns_arn_region: settings.get_settings("AWS_SNS_REGION")?,
})
}
}
26 changes: 17 additions & 9 deletions crates/orchestrator/src/alerts/aws_sns/mod.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
Loading

0 comments on commit 6d6ca02

Please sign in to comment.