network_name.rs
1 // Copyright (C) 2019-2025 ADnet Contributors 2 // This file is part of the ADL library. 3 4 // The ADL library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 9 // The ADL library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 14 // You should have received a copy of the GNU General Public License 15 // along with the ADL library. If not, see <https://www.gnu.org/licenses/>. 16 17 use adl_errors::{AdlError, CliError}; 18 19 use serde::{Deserialize, Serialize}; 20 use snarkvm::prelude::{CanaryV0, MainnetV0, Network, TestnetV0}; 21 use std::{fmt, str::FromStr}; 22 23 /// Network IDs for ADnet chains 24 /// - Aleo networks: 0-2 (legacy/deprecated, aliased for compatibility) 25 /// - ALPHA chain (privacy): 10-12 26 /// - DELTA chain (transparent): 20-22 27 pub const ALPHA_MAINNET_ID: u16 = 10; 28 pub const ALPHA_TESTNET_ID: u16 = 11; 29 pub const ALPHA_CANARY_ID: u16 = 12; 30 pub const DELTA_MAINNET_ID: u16 = 20; 31 pub const DELTA_TESTNET_ID: u16 = 21; 32 pub const DELTA_CANARY_ID: u16 = 22; 33 34 // Retrievable networks for an external program 35 #[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)] 36 pub enum NetworkName { 37 // =========================================== 38 // ALPHA Chain Networks (Privacy Chain) 39 // =========================================== 40 #[serde(rename = "alpha-mainnet")] 41 AlphaMainnetV0 = 10, 42 #[default] 43 #[serde(rename = "alpha-testnet")] 44 AlphaTestnetV0 = 11, 45 #[serde(rename = "alpha-canary")] 46 AlphaCanaryV0 = 12, 47 48 // =========================================== 49 // DELTA Chain Networks (Transparent Chain) 50 // =========================================== 51 #[serde(rename = "delta-mainnet")] 52 DeltaMainnetV0 = 20, 53 #[serde(rename = "delta-testnet")] 54 DeltaTestnetV0 = 21, 55 #[serde(rename = "delta-canary")] 56 DeltaCanaryV0 = 22, 57 58 // =========================================== 59 // Legacy Aleo Networks (DEPRECATED) 60 // These are kept for backward compatibility. 61 // Use ALPHA networks for privacy features. 62 // =========================================== 63 #[deprecated(since = "0.1.0", note = "Use AlphaTestnetV0 instead")] 64 #[serde(rename = "testnet")] 65 TestnetV0 = 1, 66 #[deprecated(since = "0.1.0", note = "Use AlphaMainnetV0 instead")] 67 #[serde(rename = "mainnet")] 68 MainnetV0 = 0, 69 #[deprecated(since = "0.1.0", note = "Use AlphaCanaryV0 instead")] 70 #[serde(rename = "canary")] 71 CanaryV0 = 2, 72 } 73 74 impl NetworkName { 75 pub fn id(&self) -> u16 { 76 match self { 77 // ALPHA chain networks 78 NetworkName::AlphaMainnetV0 => ALPHA_MAINNET_ID, 79 NetworkName::AlphaTestnetV0 => ALPHA_TESTNET_ID, 80 NetworkName::AlphaCanaryV0 => ALPHA_CANARY_ID, 81 // DELTA chain networks 82 NetworkName::DeltaMainnetV0 => DELTA_MAINNET_ID, 83 NetworkName::DeltaTestnetV0 => DELTA_TESTNET_ID, 84 NetworkName::DeltaCanaryV0 => DELTA_CANARY_ID, 85 // Legacy Aleo networks (deprecated) 86 #[allow(deprecated)] 87 NetworkName::TestnetV0 => TestnetV0::ID, 88 #[allow(deprecated)] 89 NetworkName::MainnetV0 => MainnetV0::ID, 90 #[allow(deprecated)] 91 NetworkName::CanaryV0 => CanaryV0::ID, 92 } 93 } 94 95 /// Returns true if this is an ALPHA chain network (privacy-focused) 96 pub fn is_alpha(&self) -> bool { 97 matches!( 98 self, 99 NetworkName::AlphaMainnetV0 | NetworkName::AlphaTestnetV0 | NetworkName::AlphaCanaryV0 100 ) 101 } 102 103 /// Returns true if this is a DELTA chain network (transparent with exchange) 104 pub fn is_delta(&self) -> bool { 105 matches!( 106 self, 107 NetworkName::DeltaMainnetV0 | NetworkName::DeltaTestnetV0 | NetworkName::DeltaCanaryV0 108 ) 109 } 110 111 /// Returns true if this is a legacy Aleo network (deprecated) 112 #[allow(deprecated)] 113 pub fn is_legacy_aleo(&self) -> bool { 114 matches!( 115 self, 116 NetworkName::TestnetV0 | NetworkName::MainnetV0 | NetworkName::CanaryV0 117 ) 118 } 119 120 /// Returns true if this is a mainnet variant 121 #[allow(deprecated)] 122 pub fn is_mainnet(&self) -> bool { 123 matches!( 124 self, 125 NetworkName::AlphaMainnetV0 | NetworkName::DeltaMainnetV0 | NetworkName::MainnetV0 126 ) 127 } 128 129 /// Returns true if this is a testnet variant 130 #[allow(deprecated)] 131 pub fn is_testnet(&self) -> bool { 132 matches!( 133 self, 134 NetworkName::AlphaTestnetV0 | NetworkName::DeltaTestnetV0 | NetworkName::TestnetV0 135 ) 136 } 137 138 /// Returns true if this is a canary variant 139 #[allow(deprecated)] 140 pub fn is_canary(&self) -> bool { 141 matches!( 142 self, 143 NetworkName::AlphaCanaryV0 | NetworkName::DeltaCanaryV0 | NetworkName::CanaryV0 144 ) 145 } 146 } 147 148 impl FromStr for NetworkName { 149 type Err = AdlError; 150 151 #[allow(deprecated)] 152 fn from_str(s: &str) -> Result<Self, AdlError> { 153 match s { 154 // ALPHA chain networks 155 "alpha-mainnet" | "alpha_mainnet" => Ok(NetworkName::AlphaMainnetV0), 156 "alpha-testnet" | "alpha_testnet" => Ok(NetworkName::AlphaTestnetV0), 157 "alpha-canary" | "alpha_canary" => Ok(NetworkName::AlphaCanaryV0), 158 // DELTA chain networks 159 "delta-mainnet" | "delta_mainnet" => Ok(NetworkName::DeltaMainnetV0), 160 "delta-testnet" | "delta_testnet" => Ok(NetworkName::DeltaTestnetV0), 161 "delta-canary" | "delta_canary" => Ok(NetworkName::DeltaCanaryV0), 162 // Legacy Aleo networks (deprecated - aliased to ALPHA for compatibility) 163 "testnet" => Ok(NetworkName::TestnetV0), 164 "mainnet" => Ok(NetworkName::MainnetV0), 165 "canary" => Ok(NetworkName::CanaryV0), 166 _ => Err(AdlError::CliError(CliError::invalid_network_name(s))), 167 } 168 } 169 } 170 171 impl fmt::Display for NetworkName { 172 #[allow(deprecated)] 173 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 174 match self { 175 // ALPHA chain networks 176 NetworkName::AlphaMainnetV0 => write!(f, "alpha-mainnet"), 177 NetworkName::AlphaTestnetV0 => write!(f, "alpha-testnet"), 178 NetworkName::AlphaCanaryV0 => write!(f, "alpha-canary"), 179 // DELTA chain networks 180 NetworkName::DeltaMainnetV0 => write!(f, "delta-mainnet"), 181 NetworkName::DeltaTestnetV0 => write!(f, "delta-testnet"), 182 NetworkName::DeltaCanaryV0 => write!(f, "delta-canary"), 183 // Legacy Aleo networks (deprecated) 184 NetworkName::TestnetV0 => write!(f, "testnet"), 185 NetworkName::MainnetV0 => write!(f, "mainnet"), 186 NetworkName::CanaryV0 => write!(f, "canary"), 187 } 188 } 189 }