Skip to main content

tenzro_types/
config.rs

1//! Configuration types for Tenzro Network
2//!
3//! This module defines configuration structures for nodes and
4//! network parameters.
5
6use crate::primitives::ChainId;
7use serde::{Deserialize, Serialize};
8use std::net::SocketAddr;
9use std::path::PathBuf;
10
11/// Network configuration for Tenzro Network
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
13pub struct NetworkConfig {
14    /// Chain ID
15    pub chain_id: ChainId,
16    /// Network name
17    pub network_name: String,
18    /// Genesis block hash
19    pub genesis_hash: String,
20    /// Bootstrap nodes
21    pub bootstrap_nodes: Vec<String>,
22    /// Consensus parameters
23    pub consensus: ConsensusConfig,
24    /// Protocol version
25    pub protocol_version: u32,
26}
27
28impl Default for NetworkConfig {
29    fn default() -> Self {
30        Self {
31            chain_id: ChainId::mainnet(),
32            network_name: "Tenzro Network Mainnet".to_string(),
33            genesis_hash: String::new(),
34            bootstrap_nodes: Vec::new(),
35            consensus: ConsensusConfig::default(),
36            protocol_version: 1,
37        }
38    }
39}
40
41impl NetworkConfig {
42    /// Creates a mainnet configuration
43    pub fn mainnet() -> Self {
44        Self {
45            chain_id: ChainId::mainnet(),
46            network_name: "Tenzro Network Mainnet".to_string(),
47            genesis_hash: String::new(),
48            bootstrap_nodes: Vec::new(),
49            consensus: ConsensusConfig::default(),
50            protocol_version: 1,
51        }
52    }
53
54    /// Creates a testnet configuration
55    pub fn testnet() -> Self {
56        Self {
57            chain_id: ChainId::testnet(),
58            network_name: "Tenzro Network Testnet".to_string(),
59            genesis_hash: String::new(),
60            bootstrap_nodes: Vec::new(),
61            consensus: ConsensusConfig::default(),
62            protocol_version: 1,
63        }
64    }
65
66    /// Creates a local development configuration
67    pub fn devnet() -> Self {
68        Self {
69            chain_id: ChainId::new(31337),
70            network_name: "Tenzro Network Devnet".to_string(),
71            genesis_hash: String::new(),
72            bootstrap_nodes: Vec::new(),
73            consensus: ConsensusConfig::default(),
74            protocol_version: 1,
75        }
76    }
77}
78
79/// Consensus configuration
80#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
81pub struct ConsensusConfig {
82    /// Block time in milliseconds
83    pub block_time_ms: u64,
84    /// Maximum block size in bytes
85    pub max_block_size: u64,
86    /// Maximum transactions per block
87    pub max_transactions_per_block: u32,
88    /// Minimum validator stake (in smallest TNZO unit)
89    pub min_validator_stake: u64,
90    /// Maximum number of validators
91    pub max_validators: u32,
92    /// Consensus algorithm
93    pub algorithm: String,
94}
95
96impl Default for ConsensusConfig {
97    fn default() -> Self {
98        Self {
99            block_time_ms: 2000, // 2 seconds
100            max_block_size: 1024 * 1024, // 1 MB
101            max_transactions_per_block: 1000,
102            min_validator_stake: 10_000 * 10u64.pow(18), // 10,000 TNZO
103            max_validators: 100,
104            algorithm: "PoS".to_string(),
105        }
106    }
107}
108
109/// Node configuration
110#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
111pub struct NodeConfig {
112    /// Node name/identifier
113    pub node_name: String,
114    /// Data directory
115    pub data_dir: PathBuf,
116    /// Network configuration
117    pub network: NetworkConfig,
118    /// P2P configuration
119    pub p2p: P2pConfig,
120    /// RPC configuration
121    pub rpc: RpcConfig,
122    /// Storage configuration
123    pub storage: StorageConfig,
124    /// Logging configuration
125    pub logging: LoggingConfig,
126}
127
128impl Default for NodeConfig {
129    fn default() -> Self {
130        Self {
131            node_name: "tenzro-node".to_string(),
132            data_dir: PathBuf::from("./data"),
133            network: NetworkConfig::default(),
134            p2p: P2pConfig::default(),
135            rpc: RpcConfig::default(),
136            storage: StorageConfig::default(),
137            logging: LoggingConfig::default(),
138        }
139    }
140}
141
142/// P2P network configuration
143#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
144pub struct P2pConfig {
145    /// Listen address
146    pub listen_address: SocketAddr,
147    /// Public address (for NAT traversal)
148    pub public_address: Option<SocketAddr>,
149    /// Maximum inbound peers
150    pub max_inbound_peers: u32,
151    /// Maximum outbound peers
152    pub max_outbound_peers: u32,
153    /// Enable peer discovery
154    pub enable_discovery: bool,
155    /// Bootstrap nodes
156    pub bootstrap_nodes: Vec<String>,
157}
158
159impl Default for P2pConfig {
160    fn default() -> Self {
161        Self {
162            listen_address: "0.0.0.0:26656".parse().unwrap(),
163            public_address: None,
164            max_inbound_peers: 50,
165            max_outbound_peers: 25,
166            enable_discovery: true,
167            bootstrap_nodes: Vec::new(),
168        }
169    }
170}
171
172/// RPC server configuration
173#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
174pub struct RpcConfig {
175    /// Enable RPC server
176    pub enabled: bool,
177    /// RPC listen address
178    pub listen_address: SocketAddr,
179    /// Enable HTTP
180    pub enable_http: bool,
181    /// Enable WebSocket
182    pub enable_ws: bool,
183    /// CORS allowed origins
184    pub cors_allowed_origins: Vec<String>,
185    /// Maximum request size in bytes
186    pub max_request_size: usize,
187}
188
189impl Default for RpcConfig {
190    fn default() -> Self {
191        Self {
192            enabled: true,
193            listen_address: "127.0.0.1:8545".parse().unwrap(),
194            enable_http: true,
195            enable_ws: true,
196            cors_allowed_origins: vec!["*".to_string()],
197            max_request_size: 1024 * 1024, // 1 MB
198        }
199    }
200}
201
202/// Storage configuration
203#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
204pub struct StorageConfig {
205    /// Storage backend type
206    pub backend: StorageBackend,
207    /// Database path
208    pub db_path: PathBuf,
209    /// Cache size in MB
210    pub cache_size_mb: u64,
211    /// Enable state pruning
212    pub enable_pruning: bool,
213    /// Pruning interval in blocks
214    pub pruning_interval: u64,
215}
216
217impl Default for StorageConfig {
218    fn default() -> Self {
219        Self {
220            backend: StorageBackend::RocksDb,
221            db_path: PathBuf::from("./data/db"),
222            cache_size_mb: 512,
223            enable_pruning: false,
224            pruning_interval: 1000,
225        }
226    }
227}
228
229/// Storage backend types
230#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
231pub enum StorageBackend {
232    /// RocksDB backend
233    RocksDb,
234    /// In-memory backend (for testing)
235    Memory,
236    /// Custom backend
237    Custom,
238}
239
240/// Logging configuration
241#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
242pub struct LoggingConfig {
243    /// Log level
244    pub level: LogLevel,
245    /// Log format
246    pub format: LogFormat,
247    /// Log to file
248    pub log_to_file: bool,
249    /// Log file path
250    pub log_file_path: Option<PathBuf>,
251    /// Enable JSON logging
252    pub json_logs: bool,
253}
254
255impl Default for LoggingConfig {
256    fn default() -> Self {
257        Self {
258            level: LogLevel::Info,
259            format: LogFormat::Text,
260            log_to_file: false,
261            log_file_path: None,
262            json_logs: false,
263        }
264    }
265}
266
267/// Log level
268#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
269pub enum LogLevel {
270    /// Trace level
271    Trace,
272    /// Debug level
273    Debug,
274    /// Info level
275    Info,
276    /// Warning level
277    Warn,
278    /// Error level
279    Error,
280}
281
282/// Log format
283#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
284pub enum LogFormat {
285    /// Plain text format
286    Text,
287    /// JSON format
288    Json,
289    /// Compact format
290    Compact,
291}