hopr_lib/
config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
use validator::{Validate, ValidationError};

pub use hopr_strategy::StrategyConfig;
pub use hopr_transport::config::{
    validate_external_host, HeartbeatConfig, HostConfig, HostType, NetworkConfig, ProtocolConfig, TransportConfig,
};

use hopr_primitive_types::prelude::*;
use hopr_transport::config::SessionGlobalConfig;

pub const DEFAULT_SAFE_TRANSACTION_SERVICE_PROVIDER: &str = "https://safe-transaction.prod.hoprtech.net/";
pub const DEFAULT_HOST: &str = "0.0.0.0";
pub const DEFAULT_PORT: u16 = 9091;

fn validate_announced(v: &bool) -> Result<(), ValidationError> {
    if *v {
        Ok(())
    } else {
        Err(ValidationError::new(
            "Announce option should be turned ON in 2.*, only public nodes are supported",
        ))
    }
}

#[inline]
fn default_network() -> String {
    "anvil-localhost".to_owned()
}

#[inline]
fn just_true() -> bool {
    true
}

#[derive(Debug, Clone, PartialEq, smart_default::SmartDefault, Serialize, Deserialize, Validate)]
#[serde(deny_unknown_fields)]
pub struct Chain {
    #[validate(custom(function = "validate_announced"))]
    #[serde(default = "just_true")]
    #[default = true]
    pub announce: bool,
    #[serde(default = "default_network")]
    #[default(default_network())]
    pub network: String,
    #[serde(default)]
    pub provider: Option<String>,
    #[serde(default)]
    pub max_rpc_requests_per_sec: Option<u32>,
    #[serde(default)]
    pub protocols: hopr_chain_api::config::ProtocolsConfig,
    #[serde(default = "just_true")]
    #[default = true]
    pub check_unrealized_balance: bool,
    #[serde(default = "just_true")]
    #[default = true]
    pub keep_logs: bool,
    #[serde(default = "just_true")]
    #[default = true]
    pub fast_sync: bool,
}

#[inline]
fn default_invalid_address() -> Address {
    Address::default()
}

#[inline]
fn default_safe_transaction_service_provider() -> String {
    DEFAULT_SAFE_TRANSACTION_SERVICE_PROVIDER.to_owned()
}

#[serde_as]
#[derive(Debug, Clone, PartialEq, smart_default::SmartDefault, Serialize, Deserialize, Validate)]
#[serde(deny_unknown_fields)]
pub struct SafeModule {
    #[validate(url)]
    #[serde(default = "default_safe_transaction_service_provider")]
    #[default(default_safe_transaction_service_provider())]
    pub safe_transaction_service_provider: String,
    #[serde_as(as = "DisplayFromStr")]
    #[serde(default = "default_invalid_address")]
    #[default(default_invalid_address())]
    pub safe_address: Address,
    #[serde_as(as = "DisplayFromStr")]
    #[serde(default = "default_invalid_address")]
    #[default(default_invalid_address())]
    pub module_address: Address,
}

#[allow(dead_code)]
fn validate_directory_exists(s: &str) -> Result<(), ValidationError> {
    if std::path::Path::new(s).is_dir() {
        Ok(())
    } else {
        Err(ValidationError::new("Invalid directory path specified"))
    }
}

#[derive(Debug, Clone, PartialEq, smart_default::SmartDefault, Serialize, Deserialize, Validate)]
#[serde(deny_unknown_fields)]
pub struct Db {
    /// Path to the directory containing the database
    #[serde(default)]
    pub data: String,
    #[serde(default = "just_true")]
    #[default = true]
    pub initialize: bool,
    #[serde(default)]
    pub force_initialize: bool,
}

#[derive(Debug, Clone, PartialEq, smart_default::SmartDefault, Serialize, Deserialize, Validate)]
pub struct HoprLibConfig {
    /// Configuration related to host specifics
    #[validate(nested)]
    #[validate(custom(function = "validate_external_host"))]
    #[serde(default = "default_host")]
    #[default(default_host())]
    pub host: HostConfig,
    /// Configuration of the underlying database engine
    #[validate(nested)]
    #[serde(default)]
    pub db: Db,
    /// Configuration of underlying node behavior in the form strategies
    ///
    /// Strategies represent automatically executable behavior performed by
    /// the node given pre-configured triggers.
    #[validate(nested)]
    #[serde(default = "hopr_strategy::hopr_default_strategies")]
    #[default(hopr_strategy::hopr_default_strategies())]
    pub strategy: StrategyConfig,
    /// Configuration of the protocol heartbeat mechanism
    #[validate(nested)]
    #[serde(default)]
    pub heartbeat: HeartbeatConfig,
    /// Configuration of network properties
    #[validate(nested)]
    #[serde(default)]
    pub network_options: NetworkConfig,
    /// Configuration specific to transport mechanics
    #[validate(nested)]
    #[serde(default)]
    pub transport: TransportConfig,
    /// Configuration specific to protocol execution on the p2p layer
    #[validate(nested)]
    #[serde(default)]
    pub protocol: ProtocolConfig,
    /// Configuration specific to Session management.
    #[validate(nested)]
    #[serde(default)]
    pub session: SessionGlobalConfig,
    /// Blockchain-specific configuration
    #[validate(nested)]
    #[serde(default)]
    pub chain: Chain,
    /// Configuration of the `Safe` mechanism
    #[validate(nested)]
    #[serde(default)]
    pub safe_module: SafeModule,
}

// NOTE: this intentionally does not validate (0.0.0.0) to force user to specify
// their external IP.
#[inline]
fn default_host() -> HostConfig {
    HostConfig {
        address: HostType::IPv4(DEFAULT_HOST.to_owned()),
        port: DEFAULT_PORT,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_config_should_be_serializable_using_serde() -> Result<(), Box<dyn std::error::Error>> {
        let cfg = HoprLibConfig::default();

        let yaml = serde_yaml::to_string(&cfg)?;
        let cfg_after_serde: HoprLibConfig = serde_yaml::from_str(&yaml)?;
        assert_eq!(cfg, cfg_after_serde);

        Ok(())
    }
}