ccp_config/
config.rs

1/*
2 * Copyright 2024 Fluence DAO
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use ccp_randomx::RandomXFlags;
18use ccp_shared::types::LogicalCoreId;
19use serde::Serialize;
20use serde_with::serde_as;
21use serde_with::skip_serializing_none;
22
23use crate::defaults::default_control_queue_size;
24use crate::defaults::default_cumulative_hashrate_update_interval_secs;
25use crate::defaults::default_facade_queue_size;
26use crate::defaults::default_log_level;
27use crate::defaults::default_log_print_config;
28use crate::defaults::default_msr_enabled;
29use crate::defaults::default_report_hashrate;
30use crate::defaults::default_utility_queue_size;
31use crate::unresolved_config::UnresolvedWorkers;
32
33#[derive(Clone, Debug, PartialEq, Serialize)]
34pub struct CCPConfig {
35    pub rpc_endpoint: RpcEndpoint,
36    pub prometheus_endpoint: Option<PrometheusEndpoint>,
37    pub optimizations: Optimizations,
38    pub logs: Logs,
39    pub workers: Workers,
40    pub tokio: Tokio,
41    pub state_dir: std::path::PathBuf,
42}
43
44#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
45pub struct RpcEndpoint {
46    pub host: String,
47    pub port: u16,
48    pub utility_queue_size: usize,
49    pub facade_queue_size: usize,
50    pub control_queue_size: usize,
51}
52
53#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
54pub struct PrometheusEndpoint {
55    pub host: String,
56    pub port: u16,
57}
58
59#[serde_as]
60#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize)]
61pub struct Optimizations {
62    #[serde(serialize_with = "serialize_debug")]
63    pub randomx_flags: RandomXFlags,
64    pub threads_per_core_policy: ThreadsPerCoreAllocationPolicy,
65    pub msr_enabled: bool,
66}
67
68#[derive(Copy, Clone, Debug, PartialEq, Serialize)]
69pub struct Logs {
70    pub report_hashrate: bool,
71    pub cumulative_hashrate_update_interval_secs: f64,
72    #[serde(serialize_with = "serialize_debug")]
73    pub log_level: tracing_subscriber::filter::LevelFilter,
74    pub print_config: bool,
75}
76
77#[derive(Copy, Clone, Debug, PartialEq)]
78pub struct SimpleLogs {
79    pub report_hashrate: bool,
80    pub cumulative_hashrate_update_interval_secs: f64,
81    pub log_level: tracing_subscriber::filter::LevelFilter,
82}
83
84#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
85pub struct State {
86    pub state_dir: std::path::PathBuf,
87}
88
89#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Serialize)]
90pub enum ThreadsPerCoreAllocationPolicy {
91    /// CCP will try to run the optimal amount of threads per core,
92    /// trying to utilize all benefits of HT and SMT.
93    #[default]
94    Optimal,
95    /// CCP will try run the exact amount
96    Exact {
97        threads_per_physical_core: std::num::NonZeroUsize,
98    },
99}
100
101#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
102pub struct Workers {
103    pub hashes_per_round: usize,
104    pub async_to_sync_queue_size: usize,
105    pub sync_to_async_queue_size: usize,
106    pub dataset_chunk_size: u64,
107}
108
109#[skip_serializing_none]
110#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize)]
111pub struct Tokio {
112    pub worker_threads: Option<usize>,
113    pub max_blocking_threads: Option<usize>,
114    pub utility_cores_ids: Vec<LogicalCoreId>,
115}
116
117impl Default for RpcEndpoint {
118    fn default() -> Self {
119        Self {
120            host: "127.0.0.1".to_string(),
121            port: 9383,
122            utility_queue_size: default_utility_queue_size(),
123            facade_queue_size: default_facade_queue_size(),
124            control_queue_size: default_control_queue_size(),
125        }
126    }
127}
128
129impl Default for Optimizations {
130    fn default() -> Self {
131        Self {
132            randomx_flags: RandomXFlags::recommended_full_mem(),
133            threads_per_core_policy: <_>::default(),
134            msr_enabled: default_msr_enabled(),
135        }
136    }
137}
138
139impl Default for Logs {
140    fn default() -> Self {
141        Self {
142            report_hashrate: default_report_hashrate(),
143            cumulative_hashrate_update_interval_secs:
144                default_cumulative_hashrate_update_interval_secs(),
145            log_level: default_log_level().to_tracing_filter(),
146            print_config: default_log_print_config(),
147        }
148    }
149}
150
151impl Default for Workers {
152    fn default() -> Self {
153        UnresolvedWorkers::default().resolve()
154    }
155}
156
157fn serialize_debug<S, T>(value: &T, _s: S) -> Result<S::Ok, S::Error>
158where
159    S: serde::Serializer,
160    T: std::fmt::Debug,
161{
162    _s.serialize_str(&format!("{value:?}"))
163}