use std::net::{Ipv4Addr, SocketAddr};
use std::path::{Path, PathBuf};
use std::time::Duration;
use serde::{Deserialize, Serialize};
use tycho_types::models::StdAddr;
use tycho_util::config::PartialConfig;
use tycho_util::serde_helpers;
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, PartialConfig)]
#[serde(default)]
pub struct RpcConfig {
#[important]
pub listen_addr: SocketAddr,
pub generate_stub_keyblock: bool,
pub shard_split_depth: u8,
pub allow_huge_requests: bool,
pub max_parallel_block_downloads: usize,
pub run_get_method: RunGetMethodConfig,
pub subscriptions: SubscriptionsConfig,
#[important]
pub storage: RpcStorageConfig,
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct RunGetMethodConfig {
pub max_vms: usize,
#[serde(with = "serde_helpers::humantime")]
pub max_wait_for_vm: Duration,
pub max_response_stack_items: usize,
pub vm_getter_gas: u64,
}
impl Default for RunGetMethodConfig {
fn default() -> Self {
Self {
max_vms: 20,
max_wait_for_vm: Duration::from_millis(50),
max_response_stack_items: 32,
vm_getter_gas: 1000000,
}
}
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum RpcStorageConfig {
Full {
gc: Option<TransactionsGcConfig>,
force_reindex: bool,
blacklist_path: Option<PathBuf>,
},
StateOnly,
}
impl RpcStorageConfig {
pub fn is_full(&self) -> bool {
matches!(self, Self::Full { .. })
}
pub fn gc_is_enabled(&self) -> bool {
match self {
Self::Full { gc, .. } => gc.is_some(),
Self::StateOnly => false,
}
}
pub fn is_force_reindex(&self) -> bool {
match self {
Self::Full { force_reindex, .. } => *force_reindex,
Self::StateOnly => false,
}
}
pub fn blacklist_path(&self) -> Option<PathBuf> {
match self {
Self::Full { blacklist_path, .. } => blacklist_path.clone(),
Self::StateOnly => None,
}
}
}
impl Default for RpcConfig {
fn default() -> Self {
Self {
listen_addr: (Ipv4Addr::UNSPECIFIED, 8000).into(),
generate_stub_keyblock: false,
shard_split_depth: 4,
allow_huge_requests: false,
max_parallel_block_downloads: 10,
run_get_method: RunGetMethodConfig::default(),
subscriptions: SubscriptionsConfig::default(),
storage: RpcStorageConfig::Full {
gc: Some(Default::default()),
force_reindex: false,
blacklist_path: None,
},
}
}
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, PartialConfig)]
#[serde(default)]
pub struct SubscriptionsConfig {
pub max_clients: u32,
pub max_addrs: u32,
pub queue_depth: usize,
}
impl Default for SubscriptionsConfig {
fn default() -> Self {
Self {
max_clients: 1_000_000,
max_addrs: 1_000_000,
queue_depth: 5,
}
}
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct TransactionsGcConfig {
#[serde(with = "serde_helpers::humantime")]
pub tx_ttl: Duration,
#[serde(default)]
pub keep_tx_per_account: usize,
}
impl Default for TransactionsGcConfig {
fn default() -> Self {
Self {
tx_ttl: Duration::from_secs(60 * 60 * 24 * 7),
keep_tx_per_account: 10,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct BlackListConfig {
pub accounts: Vec<StdAddr>,
}
impl BlackListConfig {
pub fn load_from<P: AsRef<Path>>(path: P) -> anyhow::Result<Self> {
serde_helpers::load_json_from_file(path)
}
}