Skip to content

Commit 03363e3

Browse files
Boog900hinto-janai
andauthored
cuprated: auto config docs (#418)
* auto config docs * fmt & change arg * fix clippy * add comment_out functionality * remove config files + gen in mdbook build * review fixes * Update books/user/src/config.md Co-authored-by: hinto-janai <hinto.janai@protonmail.com> --------- Co-authored-by: hinto-janai <hinto.janai@protonmail.com>
1 parent 57cd96e commit 03363e3

22 files changed

+444
-242
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ target/
33
monerod
44
books/*/book
55
fast_sync_hashes.bin
6+
/books/user/Cuprated.toml

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ tokio-stream = { version = "0.1", default-features = false }
140140
tokio = { version = "1", default-features = false }
141141
tower = { git = "https://github.com/Cuprate/tower.git", rev = "6c7faf0", default-features = false } # <https://github.com/tower-rs/tower/pull/796>
142142
toml = { version = "0.8", default-features = false }
143+
toml_edit = { version = "0.22", default-features = false }
143144
tracing-appender = { version = "0.2", default-features = false }
144145
tracing-subscriber = { version = "0.3", default-features = false }
145146
tracing = { version = "0.1", default-features = false }

binaries/cuprated/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ tokio-util = { workspace = true, features = ["rt"] }
7373
tokio-stream = { workspace = true }
7474
tokio = { workspace = true }
7575
toml = { workspace = true, features = ["parse", "display"]}
76+
toml_edit = { workspace = true }
7677
tower = { workspace = true }
7778
tracing-appender = { workspace = true }
7879
tracing-subscriber = { workspace = true, features = ["std", "fmt", "default"] }

binaries/cuprated/config/0.0.1.toml

Lines changed: 0 additions & 64 deletions
This file was deleted.

binaries/cuprated/config/Cuprated.toml

Lines changed: 0 additions & 1 deletion
This file was deleted.

binaries/cuprated/config/README.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

binaries/cuprated/src/config.rs

Lines changed: 81 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::{
33
fs::{read_to_string, File},
44
io,
55
path::Path,
6+
str::FromStr,
67
time::Duration,
78
};
89

@@ -30,13 +31,29 @@ mod storage;
3031
mod tokio;
3132
mod tracing_config;
3233

34+
#[macro_use]
35+
mod macros;
36+
3337
use fs::FileSystemConfig;
3438
use p2p::P2PConfig;
3539
use rayon::RayonConfig;
3640
use storage::StorageConfig;
3741
use tokio::TokioConfig;
3842
use tracing_config::TracingConfig;
3943

44+
/// Header to put at the start of the generated config file.
45+
const HEADER: &str = r"## ____ _
46+
## / ___| _ _ __ _ __ __ _| |_ ___
47+
## | | | | | | '_ \| '__/ _` | __/ _ \
48+
## | |__| |_| | |_) | | | (_| | || __/
49+
## \____\__,_| .__/|_| \__,_|\__\___|
50+
## |_|
51+
##
52+
## All these config values can be set to their default by commenting them out with #.
53+
## Some values are already commented out, to set the value remove the # at the start of the line.
54+
55+
";
56+
4057
/// Reads the args & config file, returning a [`Config`].
4158
pub fn read_config_and_args() -> Config {
4259
let args = args::Args::parse();
@@ -76,32 +93,76 @@ pub fn read_config_and_args() -> Config {
7693
args.apply_args(config)
7794
}
7895

79-
/// The config for all of Cuprate.
80-
#[derive(Debug, Default, Deserialize, Serialize, PartialEq)]
81-
#[serde(deny_unknown_fields, default)]
82-
pub struct Config {
83-
/// The network we should run on.
84-
network: Network,
96+
config_struct! {
97+
/// The config for all of Cuprate.
98+
#[derive(Debug, Deserialize, Serialize, PartialEq)]
99+
#[serde(deny_unknown_fields, default)]
100+
pub struct Config {
101+
/// The network we should run on.
102+
///
103+
/// Valid values: ["Mainnet", "Testnet" and "Stagenet"]
104+
pub network: Network,
85105

86-
pub no_fast_sync: bool,
106+
/// Enable/disable fast sync.
107+
///
108+
/// Fast sync skips verification of old blocks by comparing block hashes to a built-in hash file,
109+
/// disabling this will significantly increase sync time. New blocks are still fully validated.
110+
pub fast_sync: bool,
87111

88-
/// [`tracing`] config.
89-
pub tracing: TracingConfig,
112+
#[child = true]
113+
/// The tracing/log output config.
114+
pub tracing: TracingConfig,
90115

91-
pub tokio: TokioConfig,
116+
#[child = true]
117+
/// The tokio config.
118+
///
119+
/// Tokio is the async threadpool, used for network operations and the major service inside `cuprated`.
92120
93-
pub rayon: RayonConfig,
121+
pub tokio: TokioConfig,
122+
#[child = true]
123+
/// The rayon config.
124+
///
125+
/// Rayon is the CPU threadpool, used for CPU intensive tasks.
126+
pub rayon: RayonConfig,
94127

95-
/// The P2P network config.
96-
p2p: P2PConfig,
128+
#[child = true]
129+
/// The P2P network config.
130+
pub p2p: P2PConfig,
97131

98-
/// The storage config.
99-
pub storage: StorageConfig,
132+
#[child = true]
133+
/// The storage config.
134+
pub storage: StorageConfig,
100135

101-
pub fs: FileSystemConfig,
136+
#[child = true]
137+
/// The filesystem config.
138+
pub fs: FileSystemConfig,
139+
}
140+
}
141+
142+
impl Default for Config {
143+
fn default() -> Self {
144+
Self {
145+
network: Default::default(),
146+
fast_sync: true,
147+
tracing: Default::default(),
148+
tokio: Default::default(),
149+
rayon: Default::default(),
150+
p2p: Default::default(),
151+
storage: Default::default(),
152+
fs: Default::default(),
153+
}
154+
}
102155
}
103156

104157
impl Config {
158+
/// Returns a default [`Config`], with doc comments.
159+
pub fn documented_config() -> String {
160+
let str = toml::ser::to_string_pretty(&Self::default()).unwrap();
161+
let mut doc = toml_edit::DocumentMut::from_str(&str).unwrap();
162+
Self::write_docs(doc.as_table_mut());
163+
format!("{HEADER}{doc}")
164+
}
165+
105166
/// Attempts to read a config file in [`toml`] format from the given [`Path`].
106167
///
107168
/// # Errors
@@ -193,30 +254,13 @@ impl Config {
193254
mod test {
194255
use toml::from_str;
195256

196-
use crate::constants::EXAMPLE_CONFIG;
197-
198257
use super::*;
199258

200-
/// Tests the latest config is the `Default`.
201259
#[test]
202-
fn config_latest() {
203-
let config: Config = from_str(EXAMPLE_CONFIG).unwrap();
204-
assert_eq!(config, Config::default());
205-
}
260+
fn documented_config() {
261+
let str = Config::documented_config();
262+
let conf: Config = from_str(&str).unwrap();
206263

207-
/// Tests backwards compatibility.
208-
#[test]
209-
fn config_backwards_compat() {
210-
// (De)serialization tests.
211-
#[expect(
212-
clippy::single_element_loop,
213-
reason = "Remove after adding other versions"
214-
)]
215-
for version in ["0.0.1"] {
216-
let path = format!("config/{version}.toml");
217-
println!("Testing config serde backwards compat: {path}");
218-
let string = read_to_string(path).unwrap();
219-
from_str::<Config>(&string).unwrap();
220-
}
264+
assert_eq!(conf, Config::default());
221265
}
222266
}

binaries/cuprated/src/config/args.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde_json::Value;
55

66
use cuprate_helper::network::Network;
77

8-
use crate::{config::Config, constants::EXAMPLE_CONFIG, version::CupratedVersionInfo};
8+
use crate::{config::Config, version::CupratedVersionInfo};
99

1010
/// Cuprate Args.
1111
#[derive(clap::Parser, Debug)]
@@ -61,7 +61,7 @@ impl Args {
6161
}
6262

6363
if self.generate_config {
64-
println!("{EXAMPLE_CONFIG}");
64+
println!("{}", Config::documented_config());
6565
exit(0);
6666
}
6767
}
@@ -71,7 +71,7 @@ impl Args {
7171
/// This may exit the program if a config value was set that requires an early exit.
7272
pub const fn apply_args(&self, mut config: Config) -> Config {
7373
config.network = self.network;
74-
config.no_fast_sync = config.no_fast_sync || self.no_fast_sync;
74+
config.fast_sync = config.fast_sync && !self.no_fast_sync;
7575

7676
if let Some(outbound_connections) = self.outbound_connections {
7777
config.p2p.clear_net.general.outbound_connections = outbound_connections;

binaries/cuprated/src/config/fs.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@ use serde::{Deserialize, Serialize};
44

55
use cuprate_helper::fs::{CUPRATE_CACHE_DIR, CUPRATE_DATA_DIR};
66

7-
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
8-
#[serde(deny_unknown_fields, default)]
9-
pub struct FileSystemConfig {
10-
pub data_directory: PathBuf,
11-
pub cache_directory: PathBuf,
7+
use super::macros::config_struct;
8+
9+
config_struct! {
10+
/// The file system config.
11+
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
12+
#[serde(deny_unknown_fields, default)]
13+
pub struct FileSystemConfig {
14+
#[comment_out = true]
15+
/// The data directory.
16+
pub data_directory: PathBuf,
17+
18+
#[comment_out = true]
19+
/// The cache directory.
20+
pub cache_directory: PathBuf,
21+
}
1222
}
1323

1424
impl Default for FileSystemConfig {

0 commit comments

Comments
 (0)