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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
pub mod error;
pub mod filter;
pub mod proof_specs;
pub mod types;
use alloc::collections::BTreeMap;
use core::{fmt, time::Duration};
use std::{fs, fs::File, io::Write, path::Path};
use serde_derive::{Deserialize, Serialize};
use tendermint_light_client_verifier::types::TrustThreshold;
use ibc::core::ics23_commitment::specs::ProofSpecs;
use ibc::core::ics24_host::identifier::{ChainId, ChannelId, PortId};
use ibc::timestamp::ZERO_DURATION;
use crate::chain::ChainType;
use crate::config::types::{MaxMsgNum, MaxTxSize, Memo};
use crate::keyring::Store;
pub use error::Error;
pub use filter::PacketFilter;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct GasPrice {
pub price: f64,
pub denom: String,
}
impl GasPrice {
pub const fn new(price: f64, denom: String) -> Self {
Self { price, denom }
}
}
impl fmt::Display for GasPrice {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}{}", self.price, self.denom)
}
}
pub mod default {
use super::*;
pub fn chain_type() -> ChainType {
ChainType::CosmosSdk
}
pub fn tx_confirmation() -> bool {
false
}
pub fn clear_packets_interval() -> u64 {
100
}
pub fn rpc_timeout() -> Duration {
Duration::from_secs(10)
}
pub fn clock_drift() -> Duration {
Duration::from_secs(5)
}
pub fn max_block_time() -> Duration {
Duration::from_secs(30)
}
pub fn connection_delay() -> Duration {
ZERO_DURATION
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
#[serde(default)]
pub global: GlobalConfig,
#[serde(default)]
pub mode: ModeConfig,
#[serde(default)]
pub rest: RestConfig,
#[serde(default)]
pub telemetry: TelemetryConfig,
#[serde(default = "Vec::new", skip_serializing_if = "Vec::is_empty")]
pub chains: Vec<ChainConfig>,
}
impl Config {
pub fn has_chain(&self, id: &ChainId) -> bool {
self.chains.iter().any(|c| c.id == *id)
}
pub fn find_chain(&self, id: &ChainId) -> Option<&ChainConfig> {
self.chains.iter().find(|c| c.id == *id)
}
pub fn find_chain_mut(&mut self, id: &ChainId) -> Option<&mut ChainConfig> {
self.chains.iter_mut().find(|c| c.id == *id)
}
pub fn packets_on_channel_allowed(
&self,
chain_id: &ChainId,
port_id: &PortId,
channel_id: &ChannelId,
) -> bool {
match self.find_chain(chain_id) {
Some(chain_config) => chain_config.packet_filter.is_allowed(port_id, channel_id),
None => false,
}
}
pub fn chains_map(&self) -> BTreeMap<&ChainId, &ChainConfig> {
self.chains.iter().map(|c| (&c.id, c)).collect()
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ModeConfig {
pub clients: Clients,
pub connections: Connections,
pub channels: Channels,
pub packets: Packets,
}
impl ModeConfig {
pub fn all_disabled(&self) -> bool {
!self.clients.enabled
&& !self.connections.enabled
&& !self.channels.enabled
&& !self.packets.enabled
}
}
impl Default for ModeConfig {
fn default() -> Self {
Self {
clients: Clients {
enabled: true,
refresh: true,
misbehaviour: false,
},
connections: Connections { enabled: false },
channels: Channels { enabled: false },
packets: Packets {
enabled: true,
clear_interval: default::clear_packets_interval(),
clear_on_start: true,
tx_confirmation: false,
},
}
}
}
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Clients {
pub enabled: bool,
#[serde(default)]
pub refresh: bool,
#[serde(default)]
pub misbehaviour: bool,
}
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Connections {
pub enabled: bool,
}
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Channels {
pub enabled: bool,
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Packets {
pub enabled: bool,
#[serde(default = "default::clear_packets_interval")]
pub clear_interval: u64,
#[serde(default)]
pub clear_on_start: bool,
#[serde(default = "default::tx_confirmation")]
pub tx_confirmation: bool,
}
impl Default for Packets {
fn default() -> Self {
Self {
enabled: false,
clear_interval: default::clear_packets_interval(),
clear_on_start: false,
tx_confirmation: default::tx_confirmation(),
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum LogLevel {
Trace,
Debug,
Info,
Warn,
Error,
}
impl Default for LogLevel {
fn default() -> Self {
Self::Info
}
}
impl fmt::Display for LogLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LogLevel::Trace => write!(f, "trace"),
LogLevel::Debug => write!(f, "debug"),
LogLevel::Info => write!(f, "info"),
LogLevel::Warn => write!(f, "warn"),
LogLevel::Error => write!(f, "error"),
}
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(default, deny_unknown_fields)]
pub struct GlobalConfig {
pub log_level: LogLevel,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct TelemetryConfig {
pub enabled: bool,
pub host: String,
pub port: u16,
}
impl Default for TelemetryConfig {
fn default() -> Self {
Self {
enabled: false,
host: "127.0.0.1".to_string(),
port: 3001,
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RestConfig {
pub enabled: bool,
pub host: String,
pub port: u16,
}
impl Default for RestConfig {
fn default() -> Self {
Self {
enabled: false,
host: "127.0.0.1".to_string(),
port: 3000,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(
rename_all = "lowercase",
tag = "derivation",
content = "proto_type",
deny_unknown_fields
)]
pub enum AddressType {
Cosmos,
Ethermint { pk_type: String },
}
impl Default for AddressType {
fn default() -> Self {
AddressType::Cosmos
}
}
impl fmt::Display for AddressType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AddressType::Cosmos => write!(f, "cosmos"),
AddressType::Ethermint { .. } => write!(f, "ethermint"),
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ChainConfig {
pub id: ChainId,
#[serde(default = "default::chain_type")]
pub r#type: ChainType,
pub rpc_addr: tendermint_rpc::Url,
pub websocket_addr: tendermint_rpc::Url,
pub grpc_addr: tendermint_rpc::Url,
#[serde(default = "default::rpc_timeout", with = "humantime_serde")]
pub rpc_timeout: Duration,
pub account_prefix: String,
pub key_name: String,
#[serde(default)]
pub key_store_type: Store,
pub store_prefix: String,
pub default_gas: Option<u64>,
pub max_gas: Option<u64>,
pub gas_adjustment: Option<f64>,
pub gas_multiplier: Option<f64>,
pub fee_granter: Option<String>,
#[serde(default)]
pub max_msg_num: MaxMsgNum,
#[serde(default)]
pub max_tx_size: MaxTxSize,
#[serde(default = "default::clock_drift", with = "humantime_serde")]
pub clock_drift: Duration,
#[serde(default = "default::max_block_time", with = "humantime_serde")]
pub max_block_time: Duration,
#[serde(default, with = "humantime_serde")]
pub trusting_period: Option<Duration>,
#[serde(default)]
pub memo_prefix: Memo,
#[serde(default, with = "self::proof_specs")]
pub proof_specs: ProofSpecs,
#[serde(default)]
pub trust_threshold: TrustThreshold,
pub gas_price: GasPrice,
#[serde(default)]
pub packet_filter: PacketFilter,
#[serde(default)]
pub address_type: AddressType,
}
pub fn load(path: impl AsRef<Path>) -> Result<Config, Error> {
let config_toml = std::fs::read_to_string(&path).map_err(Error::io)?;
let config = toml::from_str::<Config>(&config_toml[..]).map_err(Error::decode)?;
Ok(config)
}
pub fn store(config: &Config, path: impl AsRef<Path>) -> Result<(), Error> {
let mut file = if path.as_ref().exists() {
fs::OpenOptions::new().write(true).truncate(true).open(path)
} else {
File::create(path)
}
.map_err(Error::io)?;
store_writer(config, &mut file)
}
pub(crate) fn store_writer(config: &Config, mut writer: impl Write) -> Result<(), Error> {
let toml_config = toml::to_string_pretty(&config).map_err(Error::encode)?;
writeln!(writer, "{}", toml_config).map_err(Error::io)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::{load, store_writer};
use test_log::test;
#[test]
fn parse_valid_config() {
let path = concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/config/fixtures/relayer_conf_example.toml"
);
let config = load(path).expect("could not parse config");
dbg!(config);
}
#[test]
fn serialize_valid_config() {
let path = concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/config/fixtures/relayer_conf_example.toml"
);
let config = load(path).expect("could not parse config");
let mut buffer = Vec::new();
store_writer(&config, &mut buffer).unwrap();
}
}