cat_gateway/settings/
chain_follower.rsuse std::{cmp::min, time::Duration};
use cardano_chain_follower::{turbo_downloader::DlConfig, ChainSyncConfig, Network};
use tracing::info;
use super::str_env_var::StringEnvVar;
const DEFAULT_NETWORK: Network = Network::Mainnet;
const DEFAULT_SYNC_TASKS: u16 = 16;
const MAX_SYNC_TASKS: u16 = 256;
const DEFAULT_SYNC_MAX_SLOTS: u64 = 700_000;
const MIN_SYNC_MAX_SLOTS: u64 = 10_000;
const MAX_SYNC_MAX_SLOTS: u64 = 100_000_000;
const MAX_DL_CONNECTIONS: usize = 256;
const MAX_DL_CHUNK_SIZE: usize = 256;
const MAX_DL_CHUNK_QUEUE_AHEAD: usize = 256;
const MAX_DL_TIMEOUT: u64 = 300;
const ONE_MEGABYTE: usize = 1_048_576;
#[derive(Clone)]
pub(crate) struct EnvVars {
pub(crate) chain: Network,
pub(crate) sync_tasks: u16,
pub(crate) sync_chunk_max_slots: u64,
pub(crate) dl_config: DlConfig,
}
impl EnvVars {
pub(super) fn new() -> Self {
let chain = StringEnvVar::new_as_enum("CHAIN_NETWORK", DEFAULT_NETWORK, false);
let sync_tasks: u16 = StringEnvVar::new_as(
"CHAIN_FOLLOWER_SYNC_TASKS",
DEFAULT_SYNC_TASKS,
1,
MAX_SYNC_TASKS,
);
let sync_slots: u64 = StringEnvVar::new_as(
"CHAIN_FOLLOWER_SYNC_MAX_SLOTS",
DEFAULT_SYNC_MAX_SLOTS,
MIN_SYNC_MAX_SLOTS,
MAX_SYNC_MAX_SLOTS,
);
let cfg = ChainSyncConfig::default_for(chain);
let mut dl_config = cfg.mithril_cfg.dl_config.clone().unwrap_or_default();
let workers = StringEnvVar::new_as(
"CHAIN_FOLLOWER_DL_CONNECTIONS",
dl_config.workers,
1,
MAX_DL_CONNECTIONS,
);
dl_config = dl_config.with_workers(workers);
let default_dl_chunk_size = min(1, dl_config.chunk_size / ONE_MEGABYTE);
let chunk_size = StringEnvVar::new_as(
"CHAIN_FOLLOWER_DL_CHUNK_SIZE",
default_dl_chunk_size,
1,
MAX_DL_CHUNK_SIZE,
) * ONE_MEGABYTE;
dl_config = dl_config.with_chunk_size(chunk_size);
let queue_ahead = StringEnvVar::new_as(
"CHAIN_FOLLOWER_DL_QUEUE_AHEAD",
dl_config.queue_ahead,
1,
MAX_DL_CHUNK_QUEUE_AHEAD,
);
dl_config = dl_config.with_queue_ahead(queue_ahead);
let default_dl_connect_timeout = match dl_config.connection_timeout {
Some(timeout) => timeout.as_secs(),
None => 0,
};
let dl_connect_timeout = StringEnvVar::new_as(
"CHAIN_FOLLOWER_DL_CONNECT_TIMEOUT",
default_dl_connect_timeout,
0,
MAX_DL_TIMEOUT,
);
if dl_connect_timeout == 0 {
dl_config.connection_timeout = None;
} else {
dl_config = dl_config.with_connection_timeout(Duration::from_secs(dl_connect_timeout));
}
let default_dl_data_timeout = match dl_config.data_read_timeout {
Some(timeout) => timeout.as_secs(),
None => 0,
};
let dl_data_timeout = StringEnvVar::new_as(
"CHAIN_FOLLOWER_DL_DATA_TIMEOUT",
default_dl_data_timeout,
0,
MAX_DL_TIMEOUT,
);
if dl_connect_timeout == 0 {
dl_config.data_read_timeout = None;
} else {
dl_config = dl_config.with_data_read_timeout(Duration::from_secs(dl_data_timeout));
}
Self {
chain,
sync_tasks,
sync_chunk_max_slots: sync_slots,
dl_config,
}
}
pub(crate) fn log(&self) {
info!(
chain = self.chain.to_string(),
sync_tasks = self.sync_tasks,
dl_config = ?self.dl_config,
"Chain Follower Configuration"
);
}
}