agentic_payments/system/
mod.rs1mod builder;
7mod health;
8mod metrics;
9mod pool;
10mod topology;
11
12pub use builder::SystemBuilder;
13pub use health::{HealthCheck, HealthStatus, SystemHealth};
14pub use metrics::{Metrics, SystemMetrics};
15pub use pool::AgentPool;
16pub use topology::{MeshTopology, Topology};
17
18use crate::agents::VerificationAgent;
19use crate::consensus::{ConsensusEngine, ConsensusResult};
20use crate::crypto::Signature;
21use crate::error::{Error, Result};
22use ed25519_dalek::VerifyingKey;
23use std::sync::Arc;
24use tokio::sync::RwLock;
25use uuid::Uuid;
26
27pub struct AgenticVerificationSystem {
29 pub(crate) id: Uuid,
30 pub(crate) pool: Arc<RwLock<AgentPool>>,
31 pub(crate) topology: Arc<dyn Topology + Send + Sync>,
32 pub(crate) consensus_engine: Arc<ConsensusEngine>,
33 pub(crate) metrics: Arc<RwLock<SystemMetrics>>,
34 pub(crate) health: Arc<RwLock<SystemHealth>>,
35}
36
37impl Clone for AgenticVerificationSystem {
38 fn clone(&self) -> Self {
39 Self {
40 id: self.id,
41 pool: Arc::clone(&self.pool),
42 topology: Arc::clone(&self.topology),
43 consensus_engine: Arc::clone(&self.consensus_engine),
44 metrics: Arc::clone(&self.metrics),
45 health: Arc::clone(&self.health),
46 }
47 }
48}
49
50impl AgenticVerificationSystem {
51 pub fn builder() -> SystemBuilder {
53 SystemBuilder::new()
54 }
55
56 pub fn id(&self) -> Uuid {
58 self.id
59 }
60
61 pub async fn pool_size(&self) -> usize {
63 self.pool.read().await.size()
64 }
65
66 pub async fn scale_pool(&self, target_size: usize) -> Result<()> {
68 self.pool.write().await.scale(target_size).await
69 }
70
71 pub async fn verify_with_consensus(
73 &self,
74 signature: Signature,
75 message: &[u8],
76 public_key: &VerifyingKey,
77 ) -> Result<ConsensusResult> {
78 let start = std::time::Instant::now();
79
80 self.metrics.read().await.record_verification();
82
83 let pool = self.pool.read().await;
85 let agents = pool.get_all_agents();
86
87 if agents.is_empty() {
88 self.metrics.read().await.record_failure();
89 return Err(Error::agent_pool("No agents available for verification"));
90 }
91
92 let result = self
94 .consensus_engine
95 .verify_with_consensus(agents, signature.clone(), message, public_key)
96 .await?;
97
98 if result.is_valid() {
100 self.metrics.read().await.record_success();
101 } else {
102 self.metrics.read().await.record_failure();
103 }
104
105 let duration_us = start.elapsed().as_micros() as u64;
106 self.metrics
107 .read()
108 .await
109 .record_verification_time(duration_us);
110
111 Ok(result)
112 }
113
114 pub async fn health_check(&self) -> Result<HealthCheck> {
116 let pool = self.pool.read().await;
117 self.health.write().await.check_agent_pool(&pool).await
118 }
119
120 pub async fn health_status(&self) -> HealthStatus {
122 self.health.read().await.status()
123 }
124
125 pub async fn metrics(&self) -> Metrics {
127 self.metrics.read().await.snapshot()
128 }
129
130 pub async fn shutdown(&self) -> Result<()> {
132 tracing::info!("Shutting down AgenticVerificationSystem {}", self.id);
133 self.pool.write().await.shutdown().await?;
134 tracing::info!("AgenticVerificationSystem {} shutdown complete", self.id);
135 Ok(())
136 }
137}
138
139#[cfg(test)]
140mod tests {
141 use super::*;
142 use crate::crypto::AgentIdentity;
143
144 #[tokio::test]
145 async fn test_system_creation() {
146 let system = AgenticVerificationSystem::builder()
147 .pool_size(5)
148 .build()
149 .await
150 .unwrap();
151
152 assert_eq!(system.pool_size().await, 5);
153 }
154
155 #[tokio::test]
156 async fn test_system_verification() {
157 let system = AgenticVerificationSystem::builder()
158 .pool_size(5)
159 .build()
160 .await
161 .unwrap();
162
163 let identity = AgentIdentity::generate().unwrap();
164 let message = b"test message";
165 let signature = identity.sign(message).unwrap();
166
167 let result = system
168 .verify_with_consensus(signature, message, identity.verifying_key())
169 .await
170 .unwrap();
171
172 assert!(result.is_valid());
173 }
174
175 #[tokio::test]
176 async fn test_system_scaling() {
177 let system = AgenticVerificationSystem::builder()
178 .pool_size(5)
179 .build()
180 .await
181 .unwrap();
182
183 system.scale_pool(10).await.unwrap();
184 assert_eq!(system.pool_size().await, 10);
185
186 system.scale_pool(3).await.unwrap();
187 assert_eq!(system.pool_size().await, 3);
188 }
189}