Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ redis_url = "${REDIS_URL}"
[general.rpc_server]
server_port = "${SERVER_PORT}"
max_gas_burnt = "${MAX_GAS_BURNT}"
contract_code_cache_size = "${CONTRACT_CODE_CAHCE_SIZE}"
contract_code_cache_size = "${CONTRACT_CODE_CACHE_SIZE}"
block_cache_size = "${BLOCK_CACHE_SIZE}"
shadow_data_consistency_rate = "${SHADOW_DATA_CONSISTENCY_RATE}"
prefetch_state_size_limit = "${PREFETCH_STATE_SIZE_LIMIT}"
Expand Down
6 changes: 3 additions & 3 deletions configuration/src/configs/database.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::configs::deserialize_data_or_env;
use crate::configs::{deserialize_data_or_env, deserialize_url_or_env};
use near_lake_framework::near_indexer_primitives::near_primitives;

// Database connection URL
Expand All @@ -9,7 +9,7 @@ type DatabaseConnectUrl = String;
pub struct ShardDatabaseConfig {
#[serde(deserialize_with = "deserialize_data_or_env")]
pub shard_id: u64,
#[serde(deserialize_with = "deserialize_data_or_env")]
#[serde(deserialize_with = "deserialize_url_or_env")]
pub database_url: DatabaseConnectUrl,
}

Expand All @@ -35,7 +35,7 @@ impl DatabaseConfig {

#[derive(serde_derive::Deserialize, Debug, Clone, Default)]
pub struct CommonDatabaseConfig {
#[serde(deserialize_with = "deserialize_data_or_env")]
#[serde(deserialize_with = "deserialize_url_or_env")]
pub database_url: DatabaseConnectUrl,
#[serde(default)]
pub shards: Vec<ShardDatabaseConfig>,
Expand Down
28 changes: 20 additions & 8 deletions configuration/src/configs/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::str::FromStr;
use serde_derive::Deserialize;

use crate::configs::{
deserialize_data_or_env, deserialize_optional_data_or_env, required_value_or_panic,
deserialize_data_or_env, deserialize_optional_block_cache_size_or_env,
deserialize_optional_contract_code_cache_size_or_env, deserialize_optional_data_or_env,
deserialize_optional_shadow_data_consistency_rate_or_env, deserialize_optional_url_or_env,
required_value_or_panic,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -53,13 +56,13 @@ pub struct GeneralNearStateIndexerConfig {
pub struct CommonGeneralConfig {
#[serde(deserialize_with = "deserialize_data_or_env")]
pub chain_id: ChainId,
#[serde(deserialize_with = "deserialize_optional_data_or_env", default)]
#[serde(deserialize_with = "deserialize_optional_url_or_env", default)]
pub near_rpc_url: Option<String>,
#[serde(deserialize_with = "deserialize_optional_data_or_env", default)]
#[serde(deserialize_with = "deserialize_optional_url_or_env", default)]
pub near_archival_rpc_url: Option<String>,
#[serde(deserialize_with = "deserialize_optional_data_or_env", default)]
#[serde(deserialize_with = "deserialize_optional_url_or_env", default)]
pub referer_header_value: Option<String>,
#[serde(deserialize_with = "deserialize_optional_data_or_env", default)]
#[serde(deserialize_with = "deserialize_optional_url_or_env", default)]
pub redis_url: Option<String>,
#[serde(default)]
pub rpc_server: CommonGeneralRpcServerConfig,
Expand Down Expand Up @@ -101,11 +104,20 @@ pub struct CommonGeneralRpcServerConfig {
pub server_port: Option<u16>,
#[serde(deserialize_with = "deserialize_optional_data_or_env", default)]
pub max_gas_burnt: Option<u64>,
#[serde(deserialize_with = "deserialize_optional_data_or_env", default)]
#[serde(
deserialize_with = "deserialize_optional_contract_code_cache_size_or_env",
default
)]
pub contract_code_cache_size: Option<f64>,
#[serde(deserialize_with = "deserialize_optional_data_or_env", default)]
#[serde(
deserialize_with = "deserialize_optional_block_cache_size_or_env",
default
)]
pub block_cache_size: Option<f64>,
#[serde(deserialize_with = "deserialize_optional_data_or_env", default)]
#[serde(
deserialize_with = "deserialize_optional_shadow_data_consistency_rate_or_env",
default
)]
pub shadow_data_consistency_rate: Option<f64>,
#[serde(deserialize_with = "deserialize_optional_data_or_env", default)]
pub prefetch_state_size_limit: Option<u64>,
Expand Down
115 changes: 115 additions & 0 deletions configuration/src/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,35 @@ where
serde_json::from_value::<T>(value).map_err(serde::de::Error::custom)
}

fn deserialize_url_or_env<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = serde_json::Value::deserialize(deserializer)?;
if let serde_json::Value::String(value) = &value {
if let Some(caps) = RE_NAME_ENV.captures(value) {
let env_value =
get_env_var::<String>(&caps["env_name"]).map_err(serde::de::Error::custom)?;
if url::Url::parse(&env_value).is_ok() {
return Ok(env_value);
} else {
tracing::warn!("Failed to deserialize_url_or_env: Environment variable value should be a valid URL");
return Err(serde::de::Error::custom(
"Environment variable value should be a valid URL",
));
}
}
}

let value = serde_json::from_value::<String>(value).map_err(serde::de::Error::custom)?;
if url::Url::parse(&value).is_ok() {
Ok(value)
} else {
tracing::warn!("Failed to deserialize_url_or_env: Value should be a valid URL");
Err(serde::de::Error::custom("Value should be a valid URL"))
}
}

fn deserialize_optional_data_or_env<'de, D, T>(data: D) -> Result<Option<T>, D::Error>
where
D: serde::Deserializer<'de>,
Expand All @@ -74,6 +103,92 @@ where
})
}

fn deserialize_optional_url_or_env<'de, D>(data: D) -> Result<Option<String>, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(match deserialize_data_or_env::<D, String>(data) {
Ok(value) => {
if url::Url::parse(&value).is_ok() {
Some(value.to_string())
} else {
tracing::warn!(
"Failed to deserialize_optional_url_or_env: Value should be a valid URL"
);
None
}
}
Err(err) => {
tracing::warn!("Failed to deserialize_optional_url_or_env: {:?}", err);
None
}
})
}

fn deserialize_optional_contract_code_cache_size_or_env<'de, D>(
data: D,
) -> Result<Option<f64>, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(match deserialize_data_or_env(data) {
Ok(value) => {
if value >= 0.0 {
Some(value)
} else {
tracing::warn!("Failed to deserialize_optional_contract_code_cache_size_or_env: Value should be greater than or equal to 0");
None
}
}
Err(err) => {
tracing::warn!("Failed to deserialize_optional_data_or_env: {:?}", err);
None
}
})
}

fn deserialize_optional_block_cache_size_or_env<'de, D>(data: D) -> Result<Option<f64>, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(match deserialize_data_or_env(data) {
Ok(value) => {
if value >= 0.0 {
Some(value)
} else {
tracing::warn!("Failed to deserialize_optional_block_cache_size_or_env: Value should be greater than or equal to 0");
None
}
}
Err(err) => {
tracing::warn!("Failed to deserialize_optional_data_or_env: {:?}", err);
None
}
})
}

fn deserialize_optional_shadow_data_consistency_rate_or_env<'de, D>(
data: D,
) -> Result<Option<f64>, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(match deserialize_data_or_env(data) {
Ok(value) => {
if (0.0..=100.0).contains(&value) {
Some(value)
} else {
tracing::warn!("Failed to deserialize_optional_shadow_data_consistency_rate_or_env: Value should be in range 0.0..100.0");
None
}
}
Err(err) => {
tracing::warn!("Failed to deserialize_optional_data_or_env: {:?}", err);
None
}
})
}

#[derive(Deserialize, Debug, Clone, Default)]
pub struct CommonConfig {
pub general: general::CommonGeneralConfig,
Expand Down