openzeppelin_monitor/models/config/
network_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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! Network configuration loading and validation.
//!
//! This module implements the ConfigLoader trait for Network configurations,
//! allowing network definitions to be loaded from JSON files.

use async_trait::async_trait;
use std::{collections::HashMap, path::Path, str::FromStr};

use crate::{
	models::{config::error::ConfigError, BlockChainType, ConfigLoader, Network, SecretValue},
	utils::get_cron_interval_ms,
};

impl Network {
	/// Calculates the recommended minimum number of past blocks to maintain for this network.
	///
	/// This function computes a safe minimum value based on three factors:
	/// 1. The number of blocks that occur during one cron interval (`blocks_per_cron`)
	/// 2. The required confirmation blocks for the network
	/// 3. An additional buffer block (+1)
	///
	/// The formula used is: `(cron_interval_ms / block_time_ms) + confirmation_blocks + 1`
	///
	/// # Returns
	/// * `u64` - The recommended minimum number of past blocks to maintain
	///
	/// # Note
	/// If the cron schedule parsing fails, the blocks_per_cron component will be 0,
	/// resulting in a minimum recommendation of `confirmation_blocks + 1`
	pub fn get_recommended_past_blocks(&self) -> u64 {
		let cron_interval_ms = get_cron_interval_ms(&self.cron_schedule).unwrap_or(0) as u64;
		let blocks_per_cron = cron_interval_ms / self.block_time_ms;
		blocks_per_cron + self.confirmation_blocks + 1
	}
}

#[async_trait]
impl ConfigLoader for Network {
	/// Resolve all secrets in the network configuration
	async fn resolve_secrets(&self) -> Result<Self, ConfigError> {
		dotenvy::dotenv().ok();
		let mut network = self.clone();

		for rpc_url in &mut network.rpc_urls {
			let resolved_url = rpc_url.url.resolve().await.map_err(|e| {
				ConfigError::parse_error(
					format!("failed to resolve RPC URL: {}", e),
					Some(Box::new(e)),
					None,
				)
			})?;
			rpc_url.url = SecretValue::Plain(resolved_url);
		}
		Ok(network)
	}

	/// Load all network configurations from a directory
	///
	/// Reads and parses all JSON files in the specified directory (or default
	/// config directory) as network configurations.
	async fn load_all<T>(path: Option<&Path>) -> Result<T, ConfigError>
	where
		T: FromIterator<(String, Self)>,
	{
		let network_dir = path.unwrap_or(Path::new("config/networks"));
		let mut pairs = Vec::new();

		if !network_dir.exists() {
			return Err(ConfigError::file_error(
				"networks directory not found",
				None,
				Some(HashMap::from([(
					"path".to_string(),
					network_dir.display().to_string(),
				)])),
			));
		}

		for entry in std::fs::read_dir(network_dir).map_err(|e| {
			ConfigError::file_error(
				format!("failed to read networks directory: {}", e),
				Some(Box::new(e)),
				Some(HashMap::from([(
					"path".to_string(),
					network_dir.display().to_string(),
				)])),
			)
		})? {
			let entry = entry.map_err(|e| {
				ConfigError::file_error(
					format!("failed to read directory entry: {}", e),
					Some(Box::new(e)),
					Some(HashMap::from([(
						"path".to_string(),
						network_dir.display().to_string(),
					)])),
				)
			})?;
			let path = entry.path();

			if !Self::is_json_file(&path) {
				continue;
			}

			let name = path
				.file_stem()
				.and_then(|s| s.to_str())
				.unwrap_or("unknown")
				.to_string();

			let network = Self::load_from_path(&path).await?;
			pairs.push((name, network));
		}

		Ok(T::from_iter(pairs))
	}

	/// Load a network configuration from a specific file
	///
	/// Reads and parses a single JSON file as a network configuration.
	async fn load_from_path(path: &std::path::Path) -> Result<Self, ConfigError> {
		let file = std::fs::File::open(path).map_err(|e| {
			ConfigError::file_error(
				format!("failed to open network config file: {}", e),
				Some(Box::new(e)),
				Some(HashMap::from([(
					"path".to_string(),
					path.display().to_string(),
				)])),
			)
		})?;
		let mut config: Network = serde_json::from_reader(file).map_err(|e| {
			ConfigError::parse_error(
				format!("failed to parse network config: {}", e),
				Some(Box::new(e)),
				Some(HashMap::from([(
					"path".to_string(),
					path.display().to_string(),
				)])),
			)
		})?;

		// Resolve secrets before validating
		config = config.resolve_secrets().await?;

		// Validate the config after loading
		config.validate()?;

		Ok(config)
	}

	/// Validate the network configuration
	///
	/// Ensures that:
	/// - The network has a valid name and slug
	/// - At least one RPC URL is specified
	/// - Required chain-specific parameters are present
	/// - Block time and confirmation values are reasonable
	fn validate(&self) -> Result<(), ConfigError> {
		// Validate network name
		if self.name.is_empty() {
			return Err(ConfigError::validation_error(
				"Network name is required",
				None,
				None,
			));
		}

		// Validate network_type
		match self.network_type {
			BlockChainType::EVM | BlockChainType::Stellar => {}
			_ => {
				return Err(ConfigError::validation_error(
					"Invalid network_type",
					None,
					None,
				));
			}
		}

		// Validate slug
		if !self
			.slug
			.chars()
			.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')
		{
			return Err(ConfigError::validation_error(
				"Slug must contain only lowercase letters, numbers, and underscores",
				None,
				None,
			));
		}

		// Validate RPC URL types
		let supported_types = ["rpc"];
		if !self
			.rpc_urls
			.iter()
			.all(|rpc_url| supported_types.contains(&rpc_url.type_.as_str()))
		{
			return Err(ConfigError::validation_error(
				format!(
					"RPC URL type must be one of: {}",
					supported_types.join(", ")
				),
				None,
				None,
			));
		}

		// Validate RPC URLs format
		if !self.rpc_urls.iter().all(|rpc_url| {
			rpc_url.url.starts_with("http://") || rpc_url.url.starts_with("https://")
		}) {
			return Err(ConfigError::validation_error(
				"All RPC URLs must start with http:// or https://",
				None,
				None,
			));
		}

		// Validate RPC URL weights
		if !self.rpc_urls.iter().all(|rpc_url| rpc_url.weight <= 100) {
			return Err(ConfigError::validation_error(
				"All RPC URL weights must be between 0 and 100",
				None,
				None,
			));
		}

		// Validate block time
		if self.block_time_ms < 100 {
			return Err(ConfigError::validation_error(
				"Block time must be at least 100ms",
				None,
				None,
			));
		}

		// Validate confirmation blocks
		if self.confirmation_blocks == 0 {
			return Err(ConfigError::validation_error(
				"Confirmation blocks must be greater than 0",
				None,
				None,
			));
		}

		// Validate cron_schedule
		if self.cron_schedule.is_empty() {
			return Err(ConfigError::validation_error(
				"Cron schedule must be provided",
				None,
				None,
			));
		}

		// Add cron schedule format validation
		if let Err(e) = cron::Schedule::from_str(&self.cron_schedule) {
			return Err(ConfigError::validation_error(e.to_string(), None, None));
		}

		// Validate max_past_blocks
		if let Some(max_blocks) = self.max_past_blocks {
			if max_blocks == 0 {
				return Err(ConfigError::validation_error(
					"max_past_blocks must be greater than 0",
					None,
					None,
				));
			}

			let recommended_blocks = self.get_recommended_past_blocks();

			if max_blocks < recommended_blocks {
				tracing::warn!(
					"Network '{}' max_past_blocks ({}) below recommended {} \
					 (cron_interval/block_time + confirmations + 1)",
					self.slug,
					max_blocks,
					recommended_blocks
				);
			}
		}

		// Log a warning if the network uses an insecure protocol
		self.validate_protocol();

		Ok(())
	}

	/// Validate the safety of the protocol used in the network
	///
	/// Returns if safe, or logs a warning message if unsafe.
	fn validate_protocol(&self) {
		for rpc_url in &self.rpc_urls {
			if rpc_url.url.starts_with("http://") {
				tracing::warn!(
					"Network '{}' uses an insecure RPC URL: {}",
					self.slug,
					rpc_url.url.as_str()
				);
			}
			// Additional check for websocket connections
			if rpc_url.url.starts_with("ws://") {
				tracing::warn!(
					"Network '{}' uses an insecure WebSocket URL: {}",
					self.slug,
					rpc_url.url.as_str()
				);
			}
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::utils::tests::builders::network::NetworkBuilder;
	use tracing_test::traced_test;

	// Replace create_valid_network() with NetworkBuilder usage
	fn create_valid_network() -> Network {
		NetworkBuilder::new()
			.name("Test Network")
			.slug("test_network")
			.network_type(BlockChainType::EVM)
			.chain_id(1)
			.store_blocks(true)
			.rpc_url("https://test.network")
			.block_time_ms(1000)
			.confirmation_blocks(1)
			.cron_schedule("0 */5 * * * *")
			.max_past_blocks(10)
			.build()
	}

	#[test]
	fn test_get_recommended_past_blocks() {
		let network = NetworkBuilder::new()
			.block_time_ms(1000) // 1 second
			.confirmation_blocks(2)
			.cron_schedule("0 */5 * * * *") // every 5 minutes
			.build();

		let cron_interval_ms = get_cron_interval_ms(&network.cron_schedule).unwrap() as u64; // 300.000 (5 minutes in ms)
		let blocks_per_cron = cron_interval_ms / network.block_time_ms; // 300.000 / 1000 = 300
		let recommended_past_blocks = blocks_per_cron + network.confirmation_blocks + 1; // 300 + 2 + 1 = 303

		assert_eq!(
			network.get_recommended_past_blocks(),
			recommended_past_blocks
		);
	}

	#[test]
	fn test_validate_valid_network() {
		let network = create_valid_network();
		assert!(network.validate().is_ok());
	}

	#[test]
	fn test_validate_empty_name() {
		let network = NetworkBuilder::new().name("").build();
		assert!(matches!(
			network.validate(),
			Err(ConfigError::ValidationError(_))
		));
	}

	#[test]
	fn test_validate_invalid_slug() {
		let network = NetworkBuilder::new().slug("Invalid-Slug").build();
		assert!(matches!(
			network.validate(),
			Err(ConfigError::ValidationError(_))
		));
	}

	#[test]
	fn test_validate_invalid_rpc_url_type() {
		let mut network = create_valid_network();
		network.rpc_urls[0].type_ = "invalid".to_string();
		assert!(matches!(
			network.validate(),
			Err(ConfigError::ValidationError(_))
		));
	}

	#[test]
	fn test_validate_invalid_rpc_url_format() {
		let network = NetworkBuilder::new().rpc_url("invalid-url").build();
		assert!(matches!(
			network.validate(),
			Err(ConfigError::ValidationError(_))
		));
	}

	#[test]
	fn test_validate_invalid_rpc_weight() {
		let mut network = create_valid_network();
		network.rpc_urls[0].weight = 101;
		assert!(matches!(
			network.validate(),
			Err(ConfigError::ValidationError(_))
		));
	}

	#[test]
	fn test_validate_invalid_block_time() {
		let network = NetworkBuilder::new().block_time_ms(50).build();
		assert!(matches!(
			network.validate(),
			Err(ConfigError::ValidationError(_))
		));
	}

	#[test]
	fn test_validate_zero_confirmation_blocks() {
		let network = NetworkBuilder::new().confirmation_blocks(0).build();
		assert!(matches!(
			network.validate(),
			Err(ConfigError::ValidationError(_))
		));
	}

	#[test]
	fn test_validate_invalid_cron_schedule() {
		let network = NetworkBuilder::new().cron_schedule("invalid cron").build();
		assert!(matches!(
			network.validate(),
			Err(ConfigError::ValidationError(_))
		));
	}

	#[test]
	fn test_validate_zero_max_past_blocks() {
		let network = NetworkBuilder::new().max_past_blocks(0).build();
		assert!(matches!(
			network.validate(),
			Err(ConfigError::ValidationError(_))
		));
	}

	#[test]
	fn test_validate_empty_cron_schedule() {
		let network = NetworkBuilder::new().cron_schedule("").build();
		assert!(matches!(
			network.validate(),
			Err(ConfigError::ValidationError(_))
		));
	}

	#[tokio::test]
	async fn test_invalid_load_from_path() {
		let path = Path::new("config/networks/invalid.json");
		assert!(matches!(
			Network::load_from_path(path).await,
			Err(ConfigError::FileError(_))
		));
	}

	#[tokio::test]
	async fn test_invalid_config_from_load_from_path() {
		use std::io::Write;
		use tempfile::NamedTempFile;

		let mut temp_file = NamedTempFile::new().unwrap();
		write!(temp_file, "{{\"invalid\": \"json").unwrap();

		let path = temp_file.path();

		assert!(matches!(
			Network::load_from_path(path).await,
			Err(ConfigError::ParseError(_))
		));
	}

	#[tokio::test]
	async fn test_load_all_directory_not_found() {
		let non_existent_path = Path::new("non_existent_directory");

		let result: Result<HashMap<String, Network>, ConfigError> =
			Network::load_all(Some(non_existent_path)).await;
		assert!(matches!(result, Err(ConfigError::FileError(_))));

		if let Err(ConfigError::FileError(err)) = result {
			assert!(err.message.contains("networks directory not found"));
		}
	}

	#[test]
	#[traced_test]
	fn test_validate_protocol_insecure_rpc() {
		let network = NetworkBuilder::new()
			.name("Test Network")
			.slug("test_network")
			.network_type(BlockChainType::EVM)
			.chain_id(1)
			.store_blocks(true)
			.add_rpc_url("http://test.network", "rpc", 100)
			.add_rpc_url("ws://test.network", "rpc", 100)
			.build();

		network.validate_protocol();
		assert!(logs_contain(
			"uses an insecure RPC URL: http://test.network"
		));
		assert!(logs_contain(
			"uses an insecure WebSocket URL: ws://test.network"
		));
	}

	#[test]
	#[traced_test]
	fn test_validate_protocol_secure_rpc() {
		let network = NetworkBuilder::new()
			.name("Test Network")
			.slug("test_network")
			.network_type(BlockChainType::EVM)
			.chain_id(1)
			.store_blocks(true)
			.add_rpc_url("https://test.network", "rpc", 100)
			.add_rpc_url("wss://test.network", "rpc", 100)
			.build();

		network.validate_protocol();
		assert!(!logs_contain("uses an insecure RPC URL"));
		assert!(!logs_contain("uses an insecure WebSocket URL"));
	}

	#[test]
	#[traced_test]
	fn test_validate_protocol_mixed_security() {
		let network = NetworkBuilder::new()
			.name("Test Network")
			.slug("test_network")
			.network_type(BlockChainType::EVM)
			.chain_id(1)
			.store_blocks(true)
			.add_rpc_url("https://secure.network", "rpc", 100)
			.add_rpc_url("http://insecure.network", "rpc", 50)
			.add_rpc_url("wss://secure.ws.network", "rpc", 25)
			.add_rpc_url("ws://insecure.ws.network", "rpc", 25)
			.build();

		network.validate_protocol();
		assert!(logs_contain(
			"uses an insecure RPC URL: http://insecure.network"
		));
		assert!(logs_contain(
			"uses an insecure WebSocket URL: ws://insecure.ws.network"
		));
		assert!(!logs_contain("https://secure.network"));
		assert!(!logs_contain("wss://secure.ws.network"));
	}
}