node_type.rs
1 // Copyright (c) 2025 ADnet Contributors 2 // This file is part of the AlphaOS library. 3 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at: 7 8 // http://www.apache.org/licenses/LICENSE-2.0 9 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 use alphavm::prelude::{FromBytes, ToBytes, error}; 17 18 use serde::{Deserialize, Serialize}; 19 use std::io; 20 21 #[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash)] 22 #[repr(u8)] 23 pub enum NodeType { 24 /// A client node is a full node, capable of syncing with the network. 25 Client = 0, 26 /// A prover is a light node, capable of producing proofs for consensus. 27 Prover, 28 /// A validator is a full node, capable of validating blocks. 29 Validator, 30 /// A bootstrapclient is a light node dedicated to serving peer lists. 31 BootstrapClient, 32 } 33 34 impl NodeType { 35 /// Returns a string representation of the node type. 36 pub const fn description(&self) -> &str { 37 match self { 38 Self::Client => "a client node", 39 Self::Prover => "a prover node", 40 Self::Validator => "a validator node", 41 Self::BootstrapClient => "a bootstrap client node", 42 } 43 } 44 45 /// Returns `true` if the node type is a client. 46 pub const fn is_client(&self) -> bool { 47 matches!(self, Self::Client) 48 } 49 50 /// Returns `true` if the node type is a prover. 51 pub const fn is_prover(&self) -> bool { 52 matches!(self, Self::Prover) 53 } 54 55 /// Returns `true` if the node type is a validator. 56 pub const fn is_validator(&self) -> bool { 57 matches!(self, Self::Validator) 58 } 59 } 60 61 impl core::fmt::Display for NodeType { 62 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 63 write!(f, "{}", match self { 64 Self::Client => "Client", 65 Self::Prover => "Prover", 66 Self::Validator => "Validator", 67 Self::BootstrapClient => "Bootstrap Client", 68 }) 69 } 70 } 71 72 impl ToBytes for NodeType { 73 fn write_le<W: io::Write>(&self, writer: W) -> io::Result<()> { 74 (*self as u8).write_le(writer) 75 } 76 } 77 78 impl FromBytes for NodeType { 79 fn read_le<R: io::Read>(reader: R) -> io::Result<Self> { 80 match u8::read_le(reader)? { 81 0 => Ok(Self::Client), 82 1 => Ok(Self::Prover), 83 2 => Ok(Self::Validator), 84 3 => Ok(Self::BootstrapClient), 85 x => Err(error(format!("Invalid node type: expected 0..=3, got {x}."))), 86 } 87 } 88 }