@@ -3,6 +3,7 @@ use std::{
3
3
fs:: { read_to_string, File } ,
4
4
io,
5
5
path:: Path ,
6
+ str:: FromStr ,
6
7
time:: Duration ,
7
8
} ;
8
9
@@ -30,13 +31,29 @@ mod storage;
30
31
mod tokio;
31
32
mod tracing_config;
32
33
34
+ #[ macro_use]
35
+ mod macros;
36
+
33
37
use fs:: FileSystemConfig ;
34
38
use p2p:: P2PConfig ;
35
39
use rayon:: RayonConfig ;
36
40
use storage:: StorageConfig ;
37
41
use tokio:: TokioConfig ;
38
42
use tracing_config:: TracingConfig ;
39
43
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
+
40
57
/// Reads the args & config file, returning a [`Config`].
41
58
pub fn read_config_and_args ( ) -> Config {
42
59
let args = args:: Args :: parse ( ) ;
@@ -76,32 +93,76 @@ pub fn read_config_and_args() -> Config {
76
93
args. apply_args ( config)
77
94
}
78
95
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 ,
85
105
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 ,
87
111
88
- /// [`tracing`] config.
89
- pub tracing : TracingConfig ,
112
+ #[ child = true ]
113
+ /// The tracing/log output config.
114
+ pub tracing: TracingConfig ,
90
115
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`.
92
120
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 ,
94
127
95
- /// The P2P network config.
96
- p2p : P2PConfig ,
128
+ #[ child = true ]
129
+ /// The P2P network config.
130
+ pub p2p: P2PConfig ,
97
131
98
- /// The storage config.
99
- pub storage : StorageConfig ,
132
+ #[ child = true ]
133
+ /// The storage config.
134
+ pub storage: StorageConfig ,
100
135
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
+ }
102
155
}
103
156
104
157
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
+
105
166
/// Attempts to read a config file in [`toml`] format from the given [`Path`].
106
167
///
107
168
/// # Errors
@@ -193,30 +254,13 @@ impl Config {
193
254
mod test {
194
255
use toml:: from_str;
195
256
196
- use crate :: constants:: EXAMPLE_CONFIG ;
197
-
198
257
use super :: * ;
199
258
200
- /// Tests the latest config is the `Default`.
201
259
#[ 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 ( ) ;
206
263
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 ( ) ) ;
221
265
}
222
266
}
0 commit comments