1use crate::primitives::{Address, Timestamp};
7use serde::{Deserialize, Serialize};
8use std::net::SocketAddr;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
12pub enum NetworkRole {
13 Validator,
15 FullNode,
17 LightClient,
19 TeeProvider,
21 ModelProvider,
23 StorageProvider,
25 Archive,
27 Bootstrap,
29 MicroNode,
33}
34
35impl NetworkRole {
36 pub fn is_validator(&self) -> bool {
38 matches!(self, Self::Validator)
39 }
40
41 pub fn is_provider(&self) -> bool {
43 matches!(
44 self,
45 Self::TeeProvider | Self::ModelProvider | Self::StorageProvider
46 )
47 }
48
49 pub fn is_full_node(&self) -> bool {
51 matches!(
52 self,
53 Self::Validator | Self::FullNode | Self::Archive | Self::Bootstrap
54 )
55 }
56
57 pub fn is_micro_node(&self) -> bool {
59 matches!(self, Self::MicroNode)
60 }
61}
62
63#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
65pub struct PeerInfo {
66 pub peer_id: String,
68 pub addresses: Vec<SocketAddr>,
70 pub role: NetworkRole,
72 pub protocol_version: u32,
74 pub user_agent: String,
76 pub last_seen: Timestamp,
78 pub reputation: i64,
80 pub status: PeerStatus,
82}
83
84impl PeerInfo {
85 pub fn new(peer_id: String, addresses: Vec<SocketAddr>, role: NetworkRole) -> Self {
87 Self {
88 peer_id,
89 addresses,
90 role,
91 protocol_version: 1,
92 user_agent: "tenzro/1.0".to_string(),
93 last_seen: Timestamp::now(),
94 reputation: 0,
95 status: PeerStatus::Disconnected,
96 }
97 }
98
99 pub fn update_last_seen(&mut self) {
101 self.last_seen = Timestamp::now();
102 }
103
104 pub fn increase_reputation(&mut self, amount: i64) {
106 self.reputation = self.reputation.saturating_add(amount);
107 }
108
109 pub fn decrease_reputation(&mut self, amount: i64) {
111 self.reputation = self.reputation.saturating_sub(amount);
112 }
113}
114
115#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
117pub enum PeerStatus {
118 Disconnected,
120 Connecting,
122 Connected,
124 Failed,
126 Banned,
128}
129
130#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
132pub struct NodeInfo {
133 pub node_id: String,
135 pub address: Option<Address>,
137 pub role: NetworkRole,
139 pub listen_addresses: Vec<SocketAddr>,
141 pub public_addresses: Vec<SocketAddr>,
143 pub protocol_version: u32,
145 pub user_agent: String,
147 pub start_time: Timestamp,
149 pub config: NodeConfiguration,
151}
152
153impl NodeInfo {
154 pub fn new(node_id: String, role: NetworkRole) -> Self {
156 Self {
157 node_id,
158 address: None,
159 role,
160 listen_addresses: Vec::new(),
161 public_addresses: Vec::new(),
162 protocol_version: 1,
163 user_agent: "tenzro/1.0".to_string(),
164 start_time: Timestamp::now(),
165 config: NodeConfiguration::default(),
166 }
167 }
168
169 pub fn with_address(mut self, address: Address) -> Self {
171 self.address = Some(address);
172 self
173 }
174
175 pub fn add_listen_address(&mut self, addr: SocketAddr) {
177 if !self.listen_addresses.contains(&addr) {
178 self.listen_addresses.push(addr);
179 }
180 }
181
182 pub fn add_public_address(&mut self, addr: SocketAddr) {
184 if !self.public_addresses.contains(&addr) {
185 self.public_addresses.push(addr);
186 }
187 }
188
189 pub fn uptime(&self) -> i64 {
191 Timestamp::now().as_millis() - self.start_time.as_millis()
192 }
193}
194
195#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
197pub struct NodeConfiguration {
198 pub max_inbound_peers: u32,
200 pub max_outbound_peers: u32,
202 pub enable_discovery: bool,
204 pub enable_metrics: bool,
206 pub enable_rpc: bool,
208 pub rpc_address: Option<SocketAddr>,
210}
211
212impl Default for NodeConfiguration {
213 fn default() -> Self {
214 Self {
215 max_inbound_peers: 50,
216 max_outbound_peers: 25,
217 enable_discovery: true,
218 enable_metrics: true,
219 enable_rpc: true,
220 rpc_address: None,
221 }
222 }
223}
224
225#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
227pub struct NetworkStats {
228 pub connected_peers: u32,
230 pub bytes_sent: u64,
232 pub bytes_received: u64,
234 pub messages_sent: u64,
236 pub messages_received: u64,
238}
239
240impl NetworkStats {
241 pub fn record_sent(&mut self, bytes: u64) {
243 self.messages_sent += 1;
244 self.bytes_sent = self.bytes_sent.saturating_add(bytes);
245 }
246
247 pub fn record_received(&mut self, bytes: u64) {
249 self.messages_received += 1;
250 self.bytes_received = self.bytes_received.saturating_add(bytes);
251 }
252}
253
254#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
260pub struct MicroNodeCapabilities {
261 pub inference: bool,
263 pub payments: bool,
265 pub agent_collaboration: bool,
267 pub mcp_tools: bool,
269 pub task_execution: bool,
271 pub chain_query: bool,
273 pub smart_contracts: bool,
275 pub tee_services: bool,
277 pub bridge: bool,
279 pub governance: bool,
281}
282
283impl Default for MicroNodeCapabilities {
284 fn default() -> Self {
285 Self {
287 inference: true,
288 payments: true,
289 agent_collaboration: true,
290 mcp_tools: true,
291 task_execution: true,
292 chain_query: true,
293 smart_contracts: true,
294 tee_services: true,
295 bridge: true,
296 governance: true,
297 }
298 }
299}
300
301#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
303pub struct MicroNodeInfo {
304 pub did: String,
306 pub wallet_address: String,
308 pub display_name: String,
310 pub joined_at: Timestamp,
312 pub origin: String,
314 pub participant_type: MicroNodeParticipantType,
316 pub capabilities: MicroNodeCapabilities,
318}
319
320impl MicroNodeInfo {
321 pub fn new(
323 did: String,
324 wallet_address: String,
325 display_name: String,
326 origin: String,
327 participant_type: MicroNodeParticipantType,
328 ) -> Self {
329 Self {
330 did,
331 wallet_address,
332 display_name,
333 joined_at: Timestamp::now(),
334 origin,
335 participant_type,
336 capabilities: MicroNodeCapabilities::default(),
337 }
338 }
339}
340
341#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
343pub enum MicroNodeParticipantType {
344 Human,
346 Agent,
348 Bot,
350}