drive/
config.rs

1//! Drive Configuration File
2//!
3
4use dpp::dashcore::Network;
5use dpp::fee::epoch::DEFAULT_EPOCHS_PER_ERA;
6#[cfg(feature = "serde")]
7use serde::{Deserialize, Serialize};
8
9/// Boolean if GroveDB batching consistency verification is enabled by default
10pub const DEFAULT_GROVE_BATCHING_CONSISTENCY_VERIFICATION_ENABLED: bool = false;
11/// Boolean if GroveDB has_raw in enabled by default
12pub const DEFAULT_GROVE_HAS_RAW_ENABLED: bool = true;
13/// Boolean if verification of GroveDB should be run on startup
14pub const DEFAULT_VERIFY_GROVE_ON_STARTUP: bool = false;
15/// The default default query limit
16pub const DEFAULT_QUERY_LIMIT: u16 = 100;
17/// The default max query limit
18pub const DEFAULT_MAX_QUERY_LIMIT: u16 = 100;
19/// Default maximum number of contracts in cache
20pub const DEFAULT_DATA_CONTRACTS_CACHE_SIZE: u64 = 500;
21
22#[derive(Clone, Debug)]
23#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
24/// Drive configuration struct
25pub struct DriveConfig {
26    /// Boolean if batching consistency verification is enabled
27    #[cfg_attr(
28        feature = "serde",
29        serde(default = "default_batching_consistency_verification")
30    )]
31    pub batching_consistency_verification: bool,
32
33    /// Boolean if has_raw is enabled
34    #[cfg_attr(feature = "serde", serde(default = "default_has_raw_enabled"))]
35    pub has_raw_enabled: bool,
36
37    /// Boolean if GroveDB verification should happen on startup
38    #[cfg_attr(
39        feature = "serde",
40        serde(default = "default_grove_verify_on_startup_enabled")
41    )]
42    pub grovedb_verify_on_startup: bool,
43
44    /// The default returned count if no limit is set
45    #[cfg_attr(
46        feature = "serde",
47        serde(
48            default = "default_default_query_limit",
49            deserialize_with = "from_str_or_number"
50        )
51    )]
52    pub default_query_limit: u16,
53
54    /// The default returned count if no limit is set
55    #[cfg_attr(
56        feature = "serde",
57        serde(
58            default = "default_epochs_per_era",
59            deserialize_with = "from_str_or_number"
60        )
61    )]
62    pub epochs_per_era: u16,
63
64    /// The limit for user defined queries
65    #[cfg_attr(
66        feature = "serde",
67        serde(
68            default = "default_max_query_limit",
69            deserialize_with = "from_str_or_number"
70        )
71    )]
72    pub max_query_limit: u16,
73
74    /// Default genesis time
75    #[cfg_attr(feature = "serde", serde(default))]
76    pub default_genesis_time: Option<u64>,
77
78    /// Maximum number of contracts in global cache
79    #[cfg_attr(
80        feature = "serde",
81        serde(
82            default = "default_data_contracts_cache_size",
83            deserialize_with = "from_str_or_number"
84        )
85    )]
86    pub data_contracts_global_cache_size: u64,
87
88    /// Maximum number of contracts in block candidate cache
89    #[cfg_attr(
90        feature = "serde",
91        serde(
92            default = "default_data_contracts_cache_size",
93            deserialize_with = "from_str_or_number"
94        )
95    )]
96    pub data_contracts_block_cache_size: u64,
97
98    /// GroveDB visualizer address
99    #[cfg(feature = "grovedbg")]
100    #[cfg_attr(
101        feature = "serde",
102        serde(
103            default = "default_grovedb_visualizer_address",
104            deserialize_with = "from_str_to_socket_address"
105        )
106    )]
107    pub grovedb_visualizer_address: std::net::SocketAddr,
108
109    /// Enable GroveDB visualizer
110    #[cfg(feature = "grovedbg")]
111    #[cfg_attr(
112        feature = "serde",
113        serde(default, deserialize_with = "from_str_to_bool")
114    )]
115    pub grovedb_visualizer_enabled: bool,
116
117    /// The network type
118    #[cfg_attr(
119        feature = "serde",
120        serde(skip_deserializing, default = "DriveConfig::default_network")
121    )]
122    pub network: Network,
123}
124
125// TODO: some weird envy behavior requires this to exist
126#[cfg(all(feature = "serde", feature = "grovedbg"))]
127fn from_str_to_bool<'de, D>(deserializer: D) -> Result<bool, D::Error>
128where
129    D: serde::Deserializer<'de>,
130{
131    let s = String::deserialize(deserializer)?;
132    s.parse().map_err(serde::de::Error::custom)
133}
134
135#[cfg(feature = "serde")]
136fn from_str_or_number<'de, D, T>(deserializer: D) -> Result<T, D::Error>
137where
138    D: serde::Deserializer<'de>,
139    T: serde::Deserialize<'de> + std::str::FromStr,
140    <T as std::str::FromStr>::Err: std::fmt::Display,
141{
142    use serde::de::Error;
143
144    let s = String::deserialize(deserializer)?;
145    s.parse::<T>().map_err(Error::custom)
146}
147
148#[cfg(all(feature = "serde", feature = "grovedbg"))]
149fn from_str_to_socket_address<'de, D>(deserializer: D) -> Result<std::net::SocketAddr, D::Error>
150where
151    D: serde::Deserializer<'de>,
152{
153    let s = String::deserialize(deserializer)?;
154    s.parse().map_err(serde::de::Error::custom)
155}
156
157const fn default_batching_consistency_verification() -> bool {
158    DEFAULT_GROVE_BATCHING_CONSISTENCY_VERIFICATION_ENABLED
159}
160
161const fn default_has_raw_enabled() -> bool {
162    DEFAULT_GROVE_HAS_RAW_ENABLED
163}
164
165const fn default_grove_verify_on_startup_enabled() -> bool {
166    DEFAULT_VERIFY_GROVE_ON_STARTUP
167}
168
169const fn default_default_query_limit() -> u16 {
170    DEFAULT_QUERY_LIMIT
171}
172
173const fn default_epochs_per_era() -> u16 {
174    DEFAULT_EPOCHS_PER_ERA
175}
176
177const fn default_max_query_limit() -> u16 {
178    DEFAULT_MAX_QUERY_LIMIT
179}
180
181const fn default_data_contracts_cache_size() -> u64 {
182    DEFAULT_DATA_CONTRACTS_CACHE_SIZE
183}
184
185/// The default grovedb visualizer_address
186pub fn default_grovedb_visualizer_address() -> std::net::SocketAddr {
187    "127.0.0.1:8083".parse().unwrap()
188}
189
190impl Default for DriveConfig {
191    fn default() -> Self {
192        DriveConfig {
193            batching_consistency_verification: default_batching_consistency_verification(),
194            has_raw_enabled: default_has_raw_enabled(),
195            grovedb_verify_on_startup: default_grove_verify_on_startup_enabled(),
196            default_query_limit: default_default_query_limit(),
197            epochs_per_era: default_epochs_per_era(),
198            max_query_limit: default_max_query_limit(),
199            default_genesis_time: None,
200            data_contracts_global_cache_size: default_data_contracts_cache_size(),
201            data_contracts_block_cache_size: default_data_contracts_cache_size(),
202            #[cfg(feature = "grovedbg")]
203            grovedb_visualizer_address: default_grovedb_visualizer_address(),
204            #[cfg(feature = "grovedbg")]
205            grovedb_visualizer_enabled: false,
206            network: Network::Mainnet,
207        }
208    }
209}
210
211impl DriveConfig {
212    /// The default network type for mainnet
213    pub fn default_network() -> Network {
214        Network::Mainnet
215    }
216
217    /// The default testnet configuration
218    pub fn default_testnet() -> Self {
219        Self {
220            network: Network::Testnet,
221            ..Default::default()
222        }
223    }
224}