Skip to content

Commit 797d094

Browse files
authored
feat: add CARDANO_DATA_SOURCE env var to determine data source type (#934)
1 parent b4440fc commit 797d094

File tree

21 files changed

+132
-32
lines changed

21 files changed

+132
-32
lines changed

.github/actions/deploy/argocd/manifest.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ spec:
2929
memory: "4096Mi"
3030
cpu: "1000m"
3131
env:
32+
- name: CARDANO_DATA_SOURCE
33+
value: "db-sync"
3234
- name: DB_SYNC_POSTGRES_CONNECTION_STRING
3335
value: "postgres://postgres:[email protected]:5432/cexplorer"
3436
- name: CARDANO_SECURITY_PARAMETER

changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ In SCALE encoding of inherent data custom implementations of `Encode` and `Decod
4949
### Other additions
5050

5151
* `delete_metadata` extrinsic in `pallet-block-producer-metadata`
52-
* Added litep2p networking stack support to pc demo node as default. libp2p can be set with explicitly `--network-backend` parameter.
52+
* Added litep2p networking stack support to pc demo node as default. libp2p can be set with explicitly `--network-backend` parameter
53+
* Added `CARDANO_DATA_SOURCE` env variable to determine data source for partner-chains. `db-sync` value is used by default
5354

5455
## Fixed
5556

demo/node/src/data_sources.rs

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,49 @@ use sp_governed_map::GovernedMapDataSource;
1717
use sp_native_token_management::NativeTokenManagementDataSource;
1818
use std::{error::Error, sync::Arc};
1919

20+
pub const DATA_SOURCE_VAR: &str = "CARDANO_DATA_SOURCE";
21+
22+
#[derive(Clone, Debug, PartialEq)]
23+
pub enum DataSourceType {
24+
DbSync,
25+
Mock,
26+
Dolos,
27+
}
28+
29+
impl DataSourceType {
30+
pub fn from_env() -> Result<Self, Box<dyn Error + Send + Sync + 'static>> {
31+
let env_value =
32+
std::env::var(DATA_SOURCE_VAR).map_err(|_| format!("{DATA_SOURCE_VAR} is not set"))?;
33+
34+
env_value.parse().map_err(|err: String| err.into())
35+
}
36+
}
37+
38+
impl std::str::FromStr for DataSourceType {
39+
type Err = String;
40+
41+
fn from_str(s: &str) -> Result<Self, Self::Err> {
42+
match s.to_lowercase().as_str() {
43+
"db-sync" => Ok(DataSourceType::DbSync),
44+
"mock" => Ok(DataSourceType::Mock),
45+
"dolos" => Ok(DataSourceType::Dolos),
46+
_ => {
47+
Err(format!("Invalid data source type: {}. Valid options: db-sync, mock, dolos", s))
48+
},
49+
}
50+
}
51+
}
52+
53+
impl std::fmt::Display for DataSourceType {
54+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55+
match self {
56+
DataSourceType::DbSync => write!(f, "db-sync"),
57+
DataSourceType::Mock => write!(f, "mock"),
58+
DataSourceType::Dolos => write!(f, "dolos"),
59+
}
60+
}
61+
}
62+
2063
#[derive(Clone)]
2164
pub struct DataSources {
2265
pub mc_hash: Arc<dyn McHashDataSource + Send + Sync>,
@@ -30,26 +73,26 @@ pub struct DataSources {
3073
pub(crate) async fn create_cached_data_sources(
3174
metrics_opt: Option<McFollowerMetrics>,
3275
) -> std::result::Result<DataSources, ServiceError> {
33-
if use_mock_follower() {
34-
create_mock_data_sources().map_err(|err| {
35-
ServiceError::Application(
36-
format!("Failed to create mock data sources: {err}. Check configuration.").into(),
37-
)
38-
})
39-
} else {
40-
create_cached_db_sync_data_sources(metrics_opt).await.map_err(|err| {
41-
ServiceError::Application(
42-
format!("Failed to create db-sync data sources: {err}").into(),
43-
)
44-
})
45-
}
46-
}
76+
let data_source_type = DataSourceType::from_env()
77+
.map_err(|err| ServiceError::Application(err.to_string().into()))?;
78+
79+
match data_source_type {
80+
DataSourceType::DbSync => {
81+
create_cached_db_sync_data_sources(metrics_opt).await.map_err(|err| {
82+
ServiceError::Application(
83+
format!("Failed to create db-sync data sources: {err}").into(),
84+
)
85+
})
86+
},
87+
88+
DataSourceType::Mock => create_mock_data_sources().map_err(|err| {
89+
ServiceError::Application(format!("Failed to create mock data sources: {err}").into())
90+
}),
4791

48-
fn use_mock_follower() -> bool {
49-
std::env::var("USE_MOCK_DATA_SOURCES")
50-
.ok()
51-
.and_then(|v| v.parse::<bool>().ok())
52-
.unwrap_or(false)
92+
DataSourceType::Dolos => {
93+
Err(ServiceError::Application("Dolos data source is not implemented yet.".into()))
94+
},
95+
}
5396
}
5497

5598
pub fn create_mock_data_sources()
@@ -68,6 +111,7 @@ pub fn create_mock_data_sources()
68111
pub const CANDIDATES_FOR_EPOCH_CACHE_SIZE: usize = 64;
69112
pub const STAKE_CACHE_SIZE: usize = 100;
70113
pub const GOVERNED_MAP_CACHE_SIZE: u16 = 100;
114+
71115
pub async fn create_cached_db_sync_data_sources(
72116
metrics_opt: Option<McFollowerMetrics>,
73117
) -> Result<DataSources, Box<dyn Error + Send + Sync + 'static>> {

dev/envs/ci-preview/.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ export NATIVE_TOKEN_ASSET_NAME='5043546f6b656e44656d6f'
1616

1717
# Operational parameters allowing to run node after sourcing this file
1818
export DB_SYNC_POSTGRES_CONNECTION_STRING="postgres://postgres:password123@localhost/cexplorer"
19+
export CARDANO_DATA_SOURCE="db-sync"
1920
export BLOCK_STABILITY_MARGIN=0

dev/envs/devnet/.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ export NATIVE_TOKEN_ASSET_NAME='5043546f6b656e44656d6f'
1616

1717
# Operational parameters allowing to run node after sourcing this file
1818
export DB_SYNC_POSTGRES_CONNECTION_STRING="postgres://postgres:password123@localhost/cexplorer"
19+
export CARDANO_DATA_SOURCE="db-sync"
1920
export BLOCK_STABILITY_MARGIN=0

dev/envs/staging-preview/.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ export NATIVE_TOKEN_ASSET_NAME='5043546f6b656e44656d6f'
1616

1717
# Operational parameters allowing to run node after sourcing this file
1818
export DB_SYNC_POSTGRES_CONNECTION_STRING="postgres://postgres:password123@localhost/cexplorer"
19+
export CARDANO_DATA_SOURCE="db-sync"
1920
export BLOCK_STABILITY_MARGIN=0

dev/local-environment-dynamic/envrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ else
1515
source "$DOT_ENV"
1616

1717
export DB_SYNC_POSTGRES_CONNECTION_STRING="postgres://postgres:$POSTGRES_PASSWORD@localhost:$POSTGRES_PORT/cexplorer"
18-
export USE_MOCK_DATA_SOURCES=false
18+
export CARDANO_DATA_SOURCE="db-sync"
1919

2020
export BLOCK_STABILITY_MARGIN=0
2121
export MC__FIRST_EPOCH_NUMBER=0

dev/local-environment-dynamic/setup.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ EOF
581581
cat >> docker-compose.yml <<EOF
582582
environment:
583583
DB_SYNC_POSTGRES_CONNECTION_STRING: "postgres://postgres:\${POSTGRES_PASSWORD}@postgres-${db_sync_instance}:5432/cexplorer"
584+
CARDANO_DATA_SOURCE: "db-sync"
584585
CARDANO_SECURITY_PARAMETER: "5"
585586
CARDANO_ACTIVE_SLOTS_COEFF: "0.4"
586587
MC__FIRST_EPOCH_NUMBER: "0"
@@ -644,6 +645,7 @@ EOF
644645
cat >> docker-compose.yml <<EOF
645646
environment:
646647
DB_SYNC_POSTGRES_CONNECTION_STRING: "postgres://postgres:\${POSTGRES_PASSWORD}@postgres-${db_sync_instance}:5432/cexplorer"
648+
CARDANO_DATA_SOURCE: "db-sync"
647649
CARDANO_SECURITY_PARAMETER: "5"
648650
CARDANO_ACTIVE_SLOTS_COEFF: "0.4"
649651
MC__FIRST_EPOCH_NUMBER: "0"

dev/local-environment/envrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ else
1515
source "$DOT_ENV"
1616

1717
export DB_SYNC_POSTGRES_CONNECTION_STRING="postgres://postgres:$POSTGRES_PASSWORD@localhost:$POSTGRES_PORT/cexplorer"
18-
export USE_MOCK_DATA_SOURCES=false
18+
export CARDANO_DATA_SOURCE="db-sync"
1919

2020
export BLOCK_STABILITY_MARGIN=0
2121
export MC__FIRST_EPOCH_NUMBER=0

dev/local-environment/modules/partner-chains-external-node.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- shared-volume:/shared
99
- partner-chains-node-1-data:/data
1010
environment:
11+
CARDANO_DATA_SOURCE: "db-sync"
1112
DB_SYNC_POSTGRES_CONNECTION_STRING: "postgres://postgres:${POSTGRES_PASSWORD}@127.0.0.1:${POSTGRES_PORT}/cexplorer"
1213
CARDANO_SECURITY_PARAMETER: "5"
1314
CARDANO_ACTIVE_SLOTS_COEFF: "0.4"

0 commit comments

Comments
 (0)