bytes.rs
1 // Copyright (c) 2019-2025 Alpha-Delta Network Inc. 2 // This file is part of the alphavm 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 super::*; 17 18 impl<N: Network> FromBytes for Identifier<N> { 19 /// Reads in an identifier from a buffer. 20 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> { 21 // Read the number of bytes. 22 let size = u8::read_le(&mut reader)?; 23 24 // Read the identifier bytes. 25 let mut buffer = vec![0u8; size as usize]; 26 reader.read_exact(&mut buffer)?; 27 28 // from_str the identifier. 29 // Note: `Self::from_str` ensures that the identifier string is not empty. 30 Self::from_str(&String::from_utf8(buffer).map_err(|e| error(format!("Failed to decode identifier: {e}")))?) 31 .map_err(|e| error(format!("{e}"))) 32 } 33 } 34 35 impl<N: Network> ToBytes for Identifier<N> { 36 /// Writes an identifier to a buffer. 37 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> { 38 // Convert the identifier to a string. 39 let string = self.to_string(); 40 if string.len() != self.1 as usize { 41 return Err(error("Identifier length does not match expected size")); 42 } 43 44 // Ensure identifier fits within the data capacity of the base field. 45 let max_bytes = Field::<N>::size_in_data_bits() / 8; // Note: This intentionally rounds down. 46 if string.len() > max_bytes { 47 return Err(error(format!("Identifier is too large. Identifiers must be <= {max_bytes} bytes long"))); 48 } 49 50 // Write the identifier to a buffer. 51 u8::try_from(string.len()).or_halt_with::<N>("Invalid identifier length").write_le(&mut writer)?; 52 string.as_bytes().write_le(&mut writer) 53 } 54 } 55 56 #[cfg(test)] 57 mod tests { 58 use super::*; 59 use crate::data::identifier::tests::sample_identifier; 60 use alphavm_console_network::MainnetV0; 61 62 type CurrentNetwork = MainnetV0; 63 64 const ITERATIONS: u64 = 1000; 65 66 #[test] 67 fn test_bytes() -> Result<()> { 68 let mut rng = TestRng::default(); 69 70 for _ in 0..ITERATIONS { 71 // Sample a random fixed-length alphanumeric identifier, that always starts with an alphabetic character. 72 let expected = sample_identifier::<CurrentNetwork>(&mut rng)?; 73 74 // Check the byte representation. 75 let expected_bytes = expected.to_bytes_le()?; 76 assert_eq!(expected, Identifier::read_le(&expected_bytes[..])?); 77 } 78 Ok(()) 79 } 80 81 #[test] 82 fn test_zero_identifier_fails() { 83 assert!(Identifier::<CurrentNetwork>::read_le(&[0u8; 1][..]).is_err()) 84 } 85 }