openzeppelin_monitor/repositories/
monitor.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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
//! Monitor configuration repository implementation.
//!
//! This module provides storage and retrieval of monitor configurations, including
//! validation of references to networks and triggers. The repository loads monitor
//! configurations from JSON files and ensures all referenced components exist.

#![allow(clippy::result_large_err)]

use std::{collections::HashMap, marker::PhantomData, path::Path};

use async_trait::async_trait;

use crate::{
	models::{ConfigLoader, Monitor, Network, ScriptLanguage, Trigger},
	repositories::{
		error::RepositoryError,
		network::{NetworkRepository, NetworkRepositoryTrait, NetworkService},
		trigger::{TriggerRepository, TriggerRepositoryTrait, TriggerService},
	},
};

/// Static mapping of script languages to their file extensions
const LANGUAGE_EXTENSIONS: &[(&ScriptLanguage, &str)] = &[
	(&ScriptLanguage::Python, "py"),
	(&ScriptLanguage::JavaScript, "js"),
	(&ScriptLanguage::Bash, "sh"),
];

/// Repository for storing and retrieving monitor configurations
#[derive(Clone)]
pub struct MonitorRepository<
	N: NetworkRepositoryTrait + Send + 'static,
	T: TriggerRepositoryTrait + Send + 'static,
> {
	/// Map of monitor names to their configurations
	pub monitors: HashMap<String, Monitor>,
	_network_repository: PhantomData<N>,
	_trigger_repository: PhantomData<T>,
}

impl<
		N: NetworkRepositoryTrait + Send + Sync + 'static,
		T: TriggerRepositoryTrait + Send + Sync + 'static,
	> MonitorRepository<N, T>
{
	/// Create a new monitor repository from the given path
	///
	/// Loads all monitor configurations from JSON files in the specified directory
	/// (or default config directory if None is provided).
	pub async fn new(
		path: Option<&Path>,
		network_service: Option<NetworkService<N>>,
		trigger_service: Option<TriggerService<T>>,
	) -> Result<Self, RepositoryError> {
		let monitors = Self::load_all(path, network_service, trigger_service).await?;
		Ok(MonitorRepository {
			monitors,
			_network_repository: PhantomData,
			_trigger_repository: PhantomData,
		})
	}

	/// Create a new monitor repository from a list of monitors
	pub fn new_with_monitors(monitors: HashMap<String, Monitor>) -> Self {
		MonitorRepository {
			monitors,
			_network_repository: PhantomData,
			_trigger_repository: PhantomData,
		}
	}

	/// Returns an error if any monitor references a non-existent network or trigger.
	pub fn validate_monitor_references(
		monitors: &HashMap<String, Monitor>,
		triggers: &HashMap<String, Trigger>,
		networks: &HashMap<String, Network>,
	) -> Result<(), RepositoryError> {
		let mut validation_errors = Vec::new();
		let mut metadata = HashMap::new();

		for (monitor_name, monitor) in monitors {
			// Validate trigger references
			for trigger_id in &monitor.triggers {
				if !triggers.contains_key(trigger_id) {
					validation_errors.push(format!(
						"Monitor '{}' references non-existent trigger '{}'",
						monitor_name, trigger_id
					));
					metadata.insert(
						format!("monitor_{}_invalid_trigger", monitor_name),
						trigger_id.clone(),
					);
				}
			}

			// Validate network references
			for network_slug in &monitor.networks {
				if !networks.contains_key(network_slug) {
					validation_errors.push(format!(
						"Monitor '{}' references non-existent network '{}'",
						monitor_name, network_slug
					));
					metadata.insert(
						format!("monitor_{}_invalid_network", monitor_name),
						network_slug.clone(),
					);
				}
			}

			// Validate custom trigger conditions
			for condition in &monitor.trigger_conditions {
				let script_path = Path::new(&condition.script_path);
				if !script_path.exists() {
					validation_errors.push(format!(
						"Monitor '{}' has a custom filter script that does not exist: {}",
						monitor_name, condition.script_path
					));
				}

				// Validate file extension matches the specified language
				let expected_extension = match LANGUAGE_EXTENSIONS
					.iter()
					.find(|(lang, _)| *lang == &condition.language)
					.map(|(_, ext)| *ext)
				{
					Some(ext) => ext,
					None => {
						validation_errors.push(format!(
							"Monitor '{}' uses unsupported script language {:?}",
							monitor_name, condition.language
						));
						continue;
					}
				};

				match script_path.extension().and_then(|ext| ext.to_str()) {
					Some(ext) if ext == expected_extension => (), // Valid extension
					_ => validation_errors.push(format!(
						"Monitor '{}' has a custom filter script with invalid extension - must be \
						 .{} for {:?} language: {}",
						monitor_name, expected_extension, condition.language, condition.script_path
					)),
				}

				if condition.timeout_ms == 0 {
					validation_errors.push(format!(
						"Monitor '{}' should have a custom filter timeout_ms greater than 0",
						monitor_name
					));
				}
			}
		}

		if !validation_errors.is_empty() {
			return Err(RepositoryError::validation_error(
				format!(
					"Configuration validation failed:\n{}",
					validation_errors.join("\n"),
				),
				None,
				Some(metadata),
			));
		}

		Ok(())
	}
}

/// Interface for monitor repository implementations
///
/// This trait defines the standard operations that any monitor repository must support,
/// allowing for different storage backends while maintaining a consistent interface.
#[async_trait]
pub trait MonitorRepositoryTrait<
	N: NetworkRepositoryTrait + Send + 'static,
	T: TriggerRepositoryTrait + Send + 'static,
>: Clone + Send
{
	/// Create a new monitor repository from the given path
	async fn new(
		path: Option<&Path>,
		network_service: Option<NetworkService<N>>,
		trigger_service: Option<TriggerService<T>>,
	) -> Result<Self, RepositoryError>
	where
		Self: Sized;

	/// Load all monitor configurations from the given path
	///
	/// If no path is provided, uses the default config directory.
	/// Also validates references to networks and triggers.
	/// This is a static method that doesn't require an instance.
	async fn load_all(
		path: Option<&Path>,
		network_service: Option<NetworkService<N>>,
		trigger_service: Option<TriggerService<T>>,
	) -> Result<HashMap<String, Monitor>, RepositoryError>;

	/// Load a monitor from a specific path
	///
	/// Loads a monitor configuration from a specific path and validates all network and trigger references.
	async fn load_from_path(
		&self,
		path: Option<&Path>,
		network_service: Option<NetworkService<N>>,
		trigger_service: Option<TriggerService<T>>,
	) -> Result<Monitor, RepositoryError>;

	/// Get a specific monitor by ID
	///
	/// Returns None if the monitor doesn't exist.
	fn get(&self, monitor_id: &str) -> Option<Monitor>;

	/// Get all monitors
	///
	/// Returns a copy of the monitor map to prevent external mutation.
	fn get_all(&self) -> HashMap<String, Monitor>;
}

#[async_trait]
impl<
		N: NetworkRepositoryTrait + Send + Sync + 'static,
		T: TriggerRepositoryTrait + Send + Sync + 'static,
	> MonitorRepositoryTrait<N, T> for MonitorRepository<N, T>
{
	async fn new(
		path: Option<&Path>,
		network_service: Option<NetworkService<N>>,
		trigger_service: Option<TriggerService<T>>,
	) -> Result<Self, RepositoryError> {
		MonitorRepository::new(path, network_service, trigger_service).await
	}

	async fn load_all(
		path: Option<&Path>,
		network_service: Option<NetworkService<N>>,
		trigger_service: Option<TriggerService<T>>,
	) -> Result<HashMap<String, Monitor>, RepositoryError> {
		let monitors = Monitor::load_all(path).await.map_err(|e| {
			RepositoryError::load_error(
				"Failed to load monitors",
				Some(Box::new(e)),
				Some(HashMap::from([(
					"path".to_string(),
					path.map_or_else(|| "default".to_string(), |p| p.display().to_string()),
				)])),
			)
		})?;

		let networks = match network_service {
			Some(service) => service.get_all(),
			None => {
				NetworkRepository::new(None)
					.await
					.map_err(|e| {
						RepositoryError::load_error(
							"Failed to load networks for monitor validation",
							Some(Box::new(e)),
							None,
						)
					})?
					.networks
			}
		};

		let triggers = match trigger_service {
			Some(service) => service.get_all(),
			None => {
				TriggerRepository::new(None)
					.await
					.map_err(|e| {
						RepositoryError::load_error(
							"Failed to load triggers for monitor validation",
							Some(Box::new(e)),
							None,
						)
					})?
					.triggers
			}
		};

		Self::validate_monitor_references(&monitors, &triggers, &networks)?;
		Ok(monitors)
	}

	/// Load a monitor from a specific path
	///
	/// Loads a monitor configuration from a specific path and validates all network and trigger references.
	async fn load_from_path(
		&self,
		path: Option<&Path>,
		network_service: Option<NetworkService<N>>,
		trigger_service: Option<TriggerService<T>>,
	) -> Result<Monitor, RepositoryError> {
		match path {
			Some(path) => {
				let monitor = Monitor::load_from_path(path).await.map_err(|e| {
					RepositoryError::load_error(
						"Failed to load monitors",
						Some(Box::new(e)),
						Some(HashMap::from([(
							"path".to_string(),
							path.display().to_string(),
						)])),
					)
				})?;

				let networks = match network_service {
					Some(service) => service.get_all(),
					None => NetworkRepository::new(None).await?.networks,
				};

				let triggers = match trigger_service {
					Some(service) => service.get_all(),
					None => TriggerRepository::new(None).await?.triggers,
				};
				let monitors = HashMap::from([(monitor.name.clone(), monitor)]);
				Self::validate_monitor_references(&monitors, &triggers, &networks)?;
				match monitors.values().next() {
					Some(monitor) => Ok(monitor.clone()),
					None => Err(RepositoryError::load_error("No monitors found", None, None)),
				}
			}
			None => Err(RepositoryError::load_error(
				"Failed to load monitors",
				None,
				None,
			)),
		}
	}

	fn get(&self, monitor_id: &str) -> Option<Monitor> {
		self.monitors.get(monitor_id).cloned()
	}

	fn get_all(&self) -> HashMap<String, Monitor> {
		self.monitors.clone()
	}
}

/// Service layer for monitor repository operations
///
/// This type provides a higher-level interface for working with monitor configurations,
/// handling repository initialization and access through a trait-based interface.
/// It also ensures that all monitor references to networks and triggers are valid.
#[derive(Clone)]
pub struct MonitorService<
	M: MonitorRepositoryTrait<N, T> + Send,
	N: NetworkRepositoryTrait + Send + Sync + 'static,
	T: TriggerRepositoryTrait + Send + Sync + 'static,
> {
	repository: M,
	_network_repository: PhantomData<N>,
	_trigger_repository: PhantomData<T>,
}

impl<
		M: MonitorRepositoryTrait<N, T> + Send,
		N: NetworkRepositoryTrait + Send + Sync + 'static,
		T: TriggerRepositoryTrait + Send + Sync + 'static,
	> MonitorService<M, N, T>
{
	/// Create a new monitor service with the default repository implementation
	///
	/// Loads monitor configurations from the specified path (or default config directory)
	/// and validates all network and trigger references.
	pub async fn new(
		path: Option<&Path>,
		network_service: Option<NetworkService<N>>,
		trigger_service: Option<TriggerService<T>>,
	) -> Result<MonitorService<M, N, T>, RepositoryError> {
		let repository = M::new(path, network_service, trigger_service).await?;
		Ok(MonitorService {
			repository,
			_network_repository: PhantomData,
			_trigger_repository: PhantomData,
		})
	}

	/// Create a new monitor service with a specific configuration path
	///
	/// Similar to `new()` but makes the path parameter more explicit.
	pub async fn new_with_path(
		path: Option<&Path>,
	) -> Result<MonitorService<M, N, T>, RepositoryError> {
		let repository = M::new(path, None, None).await?;
		Ok(MonitorService {
			repository,
			_network_repository: PhantomData,
			_trigger_repository: PhantomData,
		})
	}

	/// Create a new monitor service with a custom repository implementation
	///
	/// Allows for using alternative storage backends that implement the MonitorRepositoryTrait.
	pub fn new_with_repository(repository: M) -> Result<Self, RepositoryError> {
		Ok(MonitorService {
			repository,
			_network_repository: PhantomData,
			_trigger_repository: PhantomData,
		})
	}

	/// Get a specific monitor by ID
	///
	/// Returns None if the monitor doesn't exist.
	pub fn get(&self, monitor_id: &str) -> Option<Monitor> {
		self.repository.get(monitor_id)
	}

	/// Get all monitors
	///
	/// Returns a copy of the monitor map to prevent external mutation.
	pub fn get_all(&self) -> HashMap<String, Monitor> {
		self.repository.get_all()
	}

	/// Load a monitor from a specific path
	///
	/// Loads a monitor configuration from a specific path and validates all network and trigger references.
	pub async fn load_from_path(
		&self,
		path: Option<&Path>,
		network_service: Option<NetworkService<N>>,
		trigger_service: Option<TriggerService<T>>,
	) -> Result<Monitor, RepositoryError> {
		self.repository
			.load_from_path(path, network_service, trigger_service)
			.await
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::{models::ScriptLanguage, utils::tests::builders::evm::monitor::MonitorBuilder};
	use std::fs;
	use tempfile::TempDir;

	#[test]
	fn test_validate_custom_trigger_conditions() {
		let temp_dir = TempDir::new().unwrap();
		let script_path = temp_dir.path().join("test_script.py");
		fs::write(&script_path, "print('test')").unwrap();

		let mut monitors = HashMap::new();
		let triggers = HashMap::new();
		let networks = HashMap::new();

		// Test valid configuration
		let monitor = MonitorBuilder::new()
			.name("test_monitor")
			.networks(vec![])
			.trigger_condition(
				script_path.to_str().unwrap(),
				1000,
				ScriptLanguage::Python,
				None,
			)
			.build();
		monitors.insert("test_monitor".to_string(), monitor);

		let result =
			MonitorRepository::<NetworkRepository, TriggerRepository>::validate_monitor_references(
				&monitors, &triggers, &networks,
			);
		assert!(result.is_ok());

		// Test non-existent script
		let monitor_bad_path = MonitorBuilder::new()
			.name("test_monitor_bad_path")
			.trigger_condition("non_existent_script.py", 1000, ScriptLanguage::Python, None)
			.build();
		monitors.insert("test_monitor_bad_path".to_string(), monitor_bad_path);

		let err =
			MonitorRepository::<NetworkRepository, TriggerRepository>::validate_monitor_references(
				&monitors, &triggers, &networks,
			)
			.unwrap_err();
		assert!(err.to_string().contains("does not exist"));

		// Test wrong extension
		let wrong_ext_path = temp_dir.path().join("test_script.js");
		fs::write(&wrong_ext_path, "print('test')").unwrap();

		let monitor_wrong_ext = MonitorBuilder::new()
			.name("test_monitor_wrong_ext")
			.trigger_condition(
				wrong_ext_path.to_str().unwrap(),
				1000,
				ScriptLanguage::Python,
				None,
			)
			.build();
		monitors.clear();
		monitors.insert("test_monitor_wrong_ext".to_string(), monitor_wrong_ext);

		let err =
			MonitorRepository::<NetworkRepository, TriggerRepository>::validate_monitor_references(
				&monitors, &triggers, &networks,
			)
			.unwrap_err();
		assert!(err.to_string().contains(
			"Monitor 'test_monitor_wrong_ext' has a custom filter script with invalid extension - \
			 must be .py for Python language"
		));

		// Test zero timeout
		let monitor_zero_timeout = MonitorBuilder::new()
			.name("test_monitor_zero_timeout")
			.trigger_condition(
				script_path.to_str().unwrap(),
				0,
				ScriptLanguage::Python,
				None,
			)
			.build();
		monitors.clear();
		monitors.insert(
			"test_monitor_zero_timeout".to_string(),
			monitor_zero_timeout,
		);

		let err =
			MonitorRepository::<NetworkRepository, TriggerRepository>::validate_monitor_references(
				&monitors, &triggers, &networks,
			)
			.unwrap_err();
		assert!(err.to_string().contains("timeout_ms greater than 0"));
	}

	#[tokio::test]
	async fn test_load_error_messages() {
		// Test with invalid path to trigger load error
		let invalid_path = Path::new("/non/existent/path");
		let result = MonitorRepository::<NetworkRepository, TriggerRepository>::load_all(
			Some(invalid_path),
			None,
			None,
		)
		.await;

		assert!(result.is_err());
		let err = result.unwrap_err();
		match err {
			RepositoryError::LoadError(message) => {
				assert!(message.to_string().contains("Failed to load monitors"));
			}
			_ => panic!("Expected RepositoryError::LoadError"),
		}
	}

	#[test]
	fn test_network_validation_error() {
		// Create a monitor with a reference to a non-existent network
		let mut monitors = HashMap::new();
		let monitor = MonitorBuilder::new()
			.name("test_monitor")
			.networks(vec!["non_existent_network".to_string()])
			.build();
		monitors.insert("test_monitor".to_string(), monitor);

		// Empty networks and triggers
		let networks = HashMap::new();
		let triggers = HashMap::new();

		// Validate should fail due to non-existent network reference
		let result =
			MonitorRepository::<NetworkRepository, TriggerRepository>::validate_monitor_references(
				&monitors, &triggers, &networks,
			);

		assert!(result.is_err());
		let err = result.unwrap_err();
		assert!(err.to_string().contains("references non-existent network"));
	}

	#[test]
	fn test_trigger_validation_error() {
		// Create a monitor with a reference to a non-existent trigger
		let mut monitors = HashMap::new();
		let monitor = MonitorBuilder::new()
			.name("test_monitor")
			.triggers(vec!["non_existent_trigger".to_string()])
			.build();
		monitors.insert("test_monitor".to_string(), monitor);

		// Empty networks and triggers
		let networks = HashMap::new();
		let triggers = HashMap::new();

		// Validate should fail due to non-existent trigger reference
		let result =
			MonitorRepository::<NetworkRepository, TriggerRepository>::validate_monitor_references(
				&monitors, &triggers, &networks,
			);

		assert!(result.is_err());
		let err = result.unwrap_err();
		assert!(err.to_string().contains("references non-existent trigger"));
	}

	#[tokio::test]
	async fn test_load_from_path_error_handling() {
		// Create a temporary directory for testing
		let temp_dir = TempDir::new().unwrap();
		let invalid_path = temp_dir.path().join("non_existent_monitor.json");

		// Create a repository instance
		let repository =
			MonitorRepository::<NetworkRepository, TriggerRepository>::new_with_monitors(
				HashMap::new(),
			);

		// Attempt to load from non-existent path
		let result = repository
			.load_from_path(Some(&invalid_path), None, None)
			.await;

		// Verify error handling
		assert!(result.is_err());
		let err = result.unwrap_err();
		match err {
			RepositoryError::LoadError(message) => {
				assert!(message.to_string().contains("Failed to load monitors"));
				// Verify the error contains the path in its metadata
				assert!(message
					.to_string()
					.contains(&invalid_path.display().to_string()));
			}
			_ => panic!("Expected RepositoryError::LoadError"),
		}
	}
}