Skip to content

Commit bbcf7b0

Browse files
heemankvocdbytes
andauthored
Feature: Drilled Config (#107)
* update: drilled config * update: TestConfigBuilder with configurations * chore: lint fixes * chore: lint fixes #2 * update: Non-Arc Impl for TestConfigBuilder * update: New TestConfigBuilder accomodating changed on TestCases * update: uncomment fft tests * update: PR review changes #1 * update: optimised init_<service> functions * Improvement/better alert (#114) * update: better alert impl * ignore: empty comment to trigger CI * update: TestConfigBuilder object name changes * update: removed new_from_env() from AWSSNS * ignore: empty comment to trigger CI * update: starknet dummy provider optimisiation * update: starknet dummy provider + Conversion optimisiation * update: added default for ConfigType * update: mod implement_client added * update: adaptation to provider-config * fix : e2e test * update: fix integration tests * fix: PR reviews fixed * update: reworking Provider config to be an ARC --------- Co-authored-by: Arun Jangra <arunjangra1001@gmail.com>
1 parent 369ccbe commit bbcf7b0

File tree

48 files changed

+920
-714
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+920
-714
lines changed

.env.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ PORT=3000
88
AWS_ACCESS_KEY_ID="AWS_ACCESS_KEY_ID"
99
AWS_SECRET_ACCESS_KEY="AWS_SECRET_ACCESS_KEY"
1010
AWS_REGION="us-east-1"
11+
AWS_SNS_REGION="us-east-1"
1112
AWS_ENDPOINT_URL="http://localhost.localstack.cloud:4566"
1213
AWS_DEFAULT_REGION="localhost"
1314

@@ -27,7 +28,6 @@ SQS_WORKER_TRIGGER_QUEUE_URL="http://sqs.us-east-1.localhost.localstack.cloud:45
2728
##### SNS #####
2829

2930
ALERTS="sns"
30-
AWS_SNS_REGION="us-east-1"
3131
AWS_SNS_ARN="arn:aws:sns:us-east-1:000000000000:madara-orchestrator-arn"
3232
AWS_SNS_ARN_NAME="madara-orchestrator-arn"
3333

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
3333

3434
## Changed
3535

36+
- AWS config built from TestConfigBuilder.
37+
- Better TestConfigBuilder, with sync config clients.
38+
- Drilled Config, removing dirty global reads.
3639
- settings provider
3740
- refactor AWS config usage and clean .env files
3841
- GitHub's coverage CI yml file for localstack and db testing.

Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ url = { version = "2.5.0", features = ["serde"] }
7070
uuid = { version = "1.7.0", features = ["v4", "serde"] }
7171
httpmock = { version = "0.7.0", features = ["remote"] }
7272
num-bigint = { version = "0.4.4" }
73-
arc-swap = { version = "1.7.0" }
7473
num-traits = "0.2"
7574
lazy_static = "1.4.0"
7675
stark_evm_adapter = "0.1.1"

crates/da-clients/ethereum/src/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ pub struct EthereumDaConfig {
1111
impl EthereumDaConfig {
1212
pub fn new_with_settings(settings: &impl Settings) -> color_eyre::Result<Self> {
1313
Ok(Self {
14-
rpc_url: settings.get_settings("SETTLEMENT_RPC_URL")?,
15-
memory_pages_contract: settings.get_settings("MEMORY_PAGES_CONTRACT_ADDRESS")?,
16-
private_key: settings.get_settings("PRIVATE_KEY")?,
14+
rpc_url: settings.get_settings_or_panic("SETTLEMENT_RPC_URL"),
15+
memory_pages_contract: settings.get_settings_or_panic("MEMORY_PAGES_CONTRACT_ADDRESS"),
16+
private_key: settings.get_settings_or_panic("PRIVATE_KEY"),
1717
})
1818
}
1919
}

crates/orchestrator/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ path = "src/main.rs"
1313

1414
[dependencies]
1515
alloy = { workspace = true }
16-
arc-swap = { workspace = true }
1716
assert_matches = "1.5.0"
1817
async-std = "1.12.0"
1918
async-trait = { workspace = true }

crates/orchestrator/src/alerts/aws_sns/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ pub struct AWSSNSConfig {
1212
impl AWSSNSConfig {
1313
pub fn new_with_settings(settings: &impl Settings) -> color_eyre::Result<Self> {
1414
Ok(Self {
15-
sns_arn: settings.get_settings("AWS_SNS_ARN")?,
16-
sns_arn_region: settings.get_settings("AWS_SNS_REGION")?,
15+
sns_arn: settings.get_settings_or_panic("AWS_SNS_ARN"),
16+
sns_arn_region: settings.get_settings_or_panic("AWS_SNS_REGION"),
1717
})
1818
}
1919
}

crates/orchestrator/src/alerts/aws_sns/mod.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
mod config;
22

3+
use std::sync::Arc;
4+
35
use crate::alerts::aws_sns::config::AWSSNSConfig;
46
use crate::alerts::Alerts;
57
use crate::config::ProviderConfig;
@@ -15,14 +17,11 @@ pub struct AWSSNS {
1517
}
1618

1719
impl AWSSNS {
18-
pub async fn new_with_settings(settings: &impl Settings, provider_config: ProviderConfig) -> Self {
19-
match provider_config {
20-
ProviderConfig::AWS(aws_config) => {
21-
let sns_config = AWSSNSConfig::new_with_settings(settings)
22-
.expect("Not able to get Aws sns config from provided settings");
23-
Self { client: Client::new(&aws_config), topic_arn: sns_config.sns_arn }
24-
}
25-
}
20+
pub async fn new_with_settings(settings: &impl Settings, provider_config: Arc<ProviderConfig>) -> Self {
21+
let sns_config =
22+
AWSSNSConfig::new_with_settings(settings).expect("Not able to get Aws sns config from provided settings");
23+
let config = provider_config.get_aws_client_or_panic();
24+
Self { client: Client::new(config), topic_arn: sns_config.sns_arn }
2625
}
2726
}
2827

crates/orchestrator/src/config.rs

Lines changed: 38 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,27 @@ use std::str::FromStr;
77

88
use std::sync::Arc;
99

10-
use crate::alerts::aws_sns::AWSSNS;
11-
use crate::alerts::Alerts;
12-
use crate::data_storage::aws_s3::AWSS3;
13-
use crate::data_storage::DataStorage;
14-
use arc_swap::{ArcSwap, Guard};
15-
use aws_config::meta::region::RegionProviderChain;
16-
use aws_config::{Region, SdkConfig};
17-
use aws_credential_types::Credentials;
18-
use da_client_interface::DaClient;
10+
use aws_config::SdkConfig;
1911
use dotenvy::dotenv;
20-
use ethereum_da_client::EthereumDaClient;
12+
use starknet::providers::jsonrpc::HttpTransport;
13+
use starknet::providers::{JsonRpcClient, Url};
14+
15+
use da_client_interface::DaClient;
2116
use ethereum_settlement_client::EthereumSettlementClient;
2217
use prover_client_interface::ProverClient;
2318
use settlement_client_interface::SettlementClient;
2419
use sharp_service::SharpProverService;
25-
use starknet::providers::jsonrpc::HttpTransport;
26-
use starknet::providers::{JsonRpcClient, Url};
2720
use starknet_settlement_client::StarknetSettlementClient;
28-
use tokio::sync::OnceCell;
2921
use utils::env_utils::get_env_var_or_panic;
22+
23+
use crate::alerts::aws_sns::AWSSNS;
24+
use crate::alerts::Alerts;
25+
use crate::data_storage::aws_s3::AWSS3;
26+
use crate::data_storage::DataStorage;
27+
use aws_config::meta::region::RegionProviderChain;
28+
use aws_config::Region;
29+
use aws_credential_types::Credentials;
30+
use ethereum_da_client::EthereumDaClient;
3031
use utils::settings::env::EnvSettingsProvider;
3132
use utils::settings::Settings;
3233

@@ -61,55 +62,58 @@ pub struct Config {
6162
///
6263
/// We are using Arc<SdkConfig> because the config size is large and keeping it
6364
/// a pointer is a better way to pass it through.
65+
#[derive(Clone)]
6466
pub enum ProviderConfig {
65-
AWS(Arc<SdkConfig>),
67+
AWS(Box<SdkConfig>),
68+
}
69+
70+
impl ProviderConfig {
71+
pub fn get_aws_client_or_panic(&self) -> &SdkConfig {
72+
match self {
73+
ProviderConfig::AWS(config) => config.as_ref(),
74+
}
75+
}
6676
}
6777

6878
/// To build a `SdkConfig` for AWS provider.
6979
pub async fn get_aws_config(settings_provider: &impl Settings) -> SdkConfig {
70-
let region = settings_provider
71-
.get_settings("AWS_REGION")
72-
.expect("Not able to get AWS_REGION from provided settings provider.");
80+
let region = settings_provider.get_settings_or_panic("AWS_REGION");
7381
let region_provider = RegionProviderChain::first_try(Region::new(region)).or_default_provider();
7482
let credentials = Credentials::from_keys(
75-
settings_provider
76-
.get_settings("AWS_ACCESS_KEY_ID")
77-
.expect("Not able to get AWS_ACCESS_KEY_ID from provided settings provider."),
78-
settings_provider
79-
.get_settings("AWS_SECRET_ACCESS_KEY")
80-
.expect("Not able to get AWS_SECRET_ACCESS_KEY from provided settings provider."),
83+
settings_provider.get_settings_or_panic("AWS_ACCESS_KEY_ID"),
84+
settings_provider.get_settings_or_panic("AWS_SECRET_ACCESS_KEY"),
8185
None,
8286
);
8387
aws_config::from_env().credentials_provider(credentials).region(region_provider).load().await
8488
}
8589

8690
/// Initializes the app config
87-
pub async fn init_config() -> Config {
91+
pub async fn init_config() -> Arc<Config> {
8892
dotenv().ok();
8993

94+
let settings_provider = EnvSettingsProvider {};
95+
let provider_config = Arc::new(ProviderConfig::AWS(Box::new(get_aws_config(&settings_provider).await)));
96+
9097
// init starknet client
9198
let provider = JsonRpcClient::new(HttpTransport::new(
92-
Url::parse(get_env_var_or_panic("MADARA_RPC_URL").as_str()).expect("Failed to parse URL"),
99+
Url::parse(settings_provider.get_settings_or_panic("MADARA_RPC_URL").as_str()).expect("Failed to parse URL"),
93100
));
94101

95-
let settings_provider = EnvSettingsProvider {};
96-
let aws_config = Arc::new(get_aws_config(&settings_provider).await);
97-
98102
// init database
99103
let database = build_database_client(&settings_provider).await;
100104
let da_client = build_da_client(&settings_provider).await;
101105
let settlement_client = build_settlement_client(&settings_provider).await;
102106
let prover_client = build_prover_service(&settings_provider);
103-
let storage_client = build_storage_client(&settings_provider, ProviderConfig::AWS(Arc::clone(&aws_config))).await;
104-
let alerts_client = build_alert_client(&settings_provider, ProviderConfig::AWS(Arc::clone(&aws_config))).await;
107+
let storage_client = build_storage_client(&settings_provider, provider_config.clone()).await;
108+
let alerts_client = build_alert_client(&settings_provider, provider_config.clone()).await;
105109

106110
// init the queue
107111
// TODO: we use omniqueue for now which doesn't support loading AWS config
108112
// from `SdkConfig`. We can later move to using `aws_sdk_sqs`. This would require
109113
// us stop using the generic omniqueue abstractions for message ack/nack
110114
let queue = build_queue_client();
111115

112-
Config::new(
116+
Arc::new(Config::new(
113117
Arc::new(provider),
114118
da_client,
115119
prover_client,
@@ -118,7 +122,7 @@ pub async fn init_config() -> Config {
118122
queue,
119123
storage_client,
120124
alerts_client,
121-
)
125+
))
122126
}
123127

124128
impl Config {
@@ -178,33 +182,6 @@ impl Config {
178182
}
179183
}
180184

181-
/// The app config. It can be accessed from anywhere inside the service.
182-
/// It's initialized only once.
183-
/// We are using `ArcSwap` as it allow us to replace the new `Config` with
184-
/// a new one which is required when running test cases. This approach was
185-
/// inspired from here - https://github.com/matklad/once_cell/issues/127
186-
pub static CONFIG: OnceCell<ArcSwap<Config>> = OnceCell::const_new();
187-
188-
/// Returns the app config. Initializes if not already done.
189-
pub async fn config() -> Guard<Arc<Config>> {
190-
let cfg = CONFIG.get_or_init(|| async { ArcSwap::from_pointee(init_config().await) }).await;
191-
cfg.load()
192-
}
193-
194-
/// OnceCell only allows us to initialize the config once and that's how it should be on production.
195-
/// However, when running tests, we often want to reinitialize because we want to clear the DB and
196-
/// set it up again for reuse in new tests. By calling `config_force_init` we replace the already
197-
/// stored config inside `ArcSwap` with the new configuration and pool settings.
198-
#[cfg(test)]
199-
pub async fn config_force_init(config: Config) {
200-
match CONFIG.get() {
201-
Some(arc) => arc.store(Arc::new(config)),
202-
None => {
203-
CONFIG.get_or_init(|| async { ArcSwap::from_pointee(config) }).await;
204-
}
205-
}
206-
}
207-
208185
/// Builds the DA client based on the environment variable DA_LAYER
209186
pub async fn build_da_client(settings_provider: &impl Settings) -> Box<dyn DaClient + Send + Sync> {
210187
match get_env_var_or_panic("DA_LAYER").as_str() {
@@ -246,7 +223,7 @@ pub async fn build_settlement_client(settings_provider: &impl Settings) -> Box<d
246223

247224
pub async fn build_storage_client(
248225
settings_provider: &impl Settings,
249-
provider_config: ProviderConfig,
226+
provider_config: Arc<ProviderConfig>,
250227
) -> Box<dyn DataStorage + Send + Sync> {
251228
match get_env_var_or_panic("DATA_STORAGE").as_str() {
252229
"s3" => Box::new(AWSS3::new_with_settings(settings_provider, provider_config).await),
@@ -256,7 +233,7 @@ pub async fn build_storage_client(
256233

257234
pub async fn build_alert_client(
258235
settings_provider: &impl Settings,
259-
provider_config: ProviderConfig,
236+
provider_config: Arc<ProviderConfig>,
260237
) -> Box<dyn Alerts + Send + Sync> {
261238
match get_env_var_or_panic("ALERTS").as_str() {
262239
"sns" => Box::new(AWSSNS::new_with_settings(settings_provider, provider_config).await),

crates/orchestrator/src/data_storage/aws_s3/config.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ pub struct AWSS3Config {
1414
impl DataStorageConfig for AWSS3Config {
1515
/// To return the config struct by creating it from the environment variables.
1616
fn new_with_settings(settings: &impl Settings) -> Self {
17-
Self {
18-
bucket_name: settings
19-
.get_settings("AWS_S3_BUCKET_NAME")
20-
.expect("Not able to get AWS_S3_BUCKET_NAME from settings provided."),
21-
}
17+
Self { bucket_name: settings.get_settings_or_panic("AWS_S3_BUCKET_NAME") }
2218
}
2319
}

crates/orchestrator/src/data_storage/aws_s3/mod.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::Arc;
2+
13
use crate::config::ProviderConfig;
24
use crate::data_storage::aws_s3::config::AWSS3Config;
35
use crate::data_storage::{DataStorage, DataStorageConfig};
@@ -24,18 +26,15 @@ pub struct AWSS3 {
2426
/// - initializing a new AWS S3 client
2527
impl AWSS3 {
2628
/// To init the struct with main settings
27-
pub async fn new_with_settings(settings: &impl Settings, provider_config: ProviderConfig) -> Self {
28-
match provider_config {
29-
ProviderConfig::AWS(aws_config) => {
30-
let s3_config = AWSS3Config::new_with_settings(settings);
31-
// Building AWS S3 config
32-
let mut s3_config_builder = aws_sdk_s3::config::Builder::from(aws_config.as_ref());
33-
// this is necessary for it to work with localstack in test cases
34-
s3_config_builder.set_force_path_style(Some(true));
35-
let client = Client::from_conf(s3_config_builder.build());
36-
Self { client, bucket: s3_config.bucket_name }
37-
}
38-
}
29+
pub async fn new_with_settings(settings: &impl Settings, provider_config: Arc<ProviderConfig>) -> Self {
30+
let s3_config = AWSS3Config::new_with_settings(settings);
31+
let aws_config = provider_config.get_aws_client_or_panic();
32+
// Building AWS S3 config
33+
let mut s3_config_builder = aws_sdk_s3::config::Builder::from(aws_config);
34+
// this is necessary for it to work with localstack in test cases
35+
s3_config_builder.set_force_path_style(Some(true));
36+
let client = Client::from_conf(s3_config_builder.build());
37+
Self { client, bucket: s3_config.bucket_name }
3938
}
4039
}
4140

crates/orchestrator/src/database/mongodb/config.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ pub struct MongoDbConfig {
1010

1111
impl DatabaseConfig for MongoDbConfig {
1212
fn new_with_settings(settings: &impl Settings) -> Self {
13-
Self {
14-
url: settings
15-
.get_settings("MONGODB_CONNECTION_STRING")
16-
.expect("Not able to get MONGODB_CONNECTION_STRING form the given settings"),
17-
}
13+
Self { url: settings.get_settings_or_panic("MONGODB_CONNECTION_STRING") }
1814
}
1915
}

0 commit comments

Comments
 (0)