bytes.rs
1 // Copyright (c) 2025-2026 ACDC Network 2 // This file is part of the alphavm library. 3 // 4 // Alpha Chain | Delta Chain Protocol 5 // International Monetary Graphite. 6 // 7 // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com). 8 // They built world-class ZK infrastructure. We installed the EASY button. 9 // Their cryptography: elegant. Our modifications: bureaucracy-compatible. 10 // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours. 11 // 12 // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0 13 // All modifications and new work: CC0 1.0 Universal Public Domain Dedication. 14 // No rights reserved. No permission required. No warranty. No refunds. 15 // 16 // https://creativecommons.org/publicdomain/zero/1.0/ 17 // SPDX-License-Identifier: CC0-1.0 18 19 use super::*; 20 21 impl<N: Network> FromBytes for StatePath<N> { 22 /// Reads the path from a buffer. 23 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> { 24 // Read the version. 25 let version = u8::read_le(&mut reader)?; 26 // Ensure the version is valid. 27 if version != 1 { 28 return Err(error("Invalid state path version")); 29 } 30 31 // Read the state path. 32 let global_state_root = N::StateRoot::read_le(&mut reader)?; 33 34 let block_path = BlockPath::read_le(&mut reader)?; 35 let block_hash = N::BlockHash::read_le(&mut reader)?; 36 let previous_block_hash = N::BlockHash::read_le(&mut reader)?; 37 let header_root = Field::read_le(&mut reader)?; 38 let header_path = HeaderPath::read_le(&mut reader)?; 39 let header_leaf = HeaderLeaf::read_le(&mut reader)?; 40 let transactions_path = TransactionsPath::read_le(&mut reader)?; 41 42 let transaction_id = FromBytes::read_le(&mut reader)?; 43 let transaction_path = FromBytes::read_le(&mut reader)?; 44 let transaction_leaf = FromBytes::read_le(&mut reader)?; 45 let transition_root = Field::read_le(&mut reader)?; 46 let tcm = FromBytes::read_le(&mut reader)?; 47 let transition_path = FromBytes::read_le(&mut reader)?; 48 let transition_leaf = FromBytes::read_le(&mut reader)?; 49 50 // Construct the state path. 51 Ok(Self::from( 52 global_state_root, 53 block_path, 54 block_hash, 55 previous_block_hash, 56 header_root, 57 header_path, 58 header_leaf, 59 transactions_path, 60 transaction_id, 61 transaction_path, 62 transaction_leaf, 63 transition_root, 64 tcm, 65 transition_path, 66 transition_leaf, 67 )) 68 } 69 } 70 71 impl<N: Network> ToBytes for StatePath<N> { 72 /// Writes the path to a buffer. 73 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> { 74 // Write the version. 75 1u8.write_le(&mut writer)?; 76 77 // Write the state path. 78 self.global_state_root.write_le(&mut writer)?; 79 80 self.block_path.write_le(&mut writer)?; 81 self.block_hash.write_le(&mut writer)?; 82 self.previous_block_hash.write_le(&mut writer)?; 83 self.header_root.write_le(&mut writer)?; 84 self.header_path.write_le(&mut writer)?; 85 self.header_leaf.write_le(&mut writer)?; 86 self.transactions_path.write_le(&mut writer)?; 87 88 self.transaction_id.write_le(&mut writer)?; 89 self.transaction_path.write_le(&mut writer)?; 90 self.transaction_leaf.write_le(&mut writer)?; 91 self.transition_root.write_le(&mut writer)?; 92 self.tcm.write_le(&mut writer)?; 93 self.transition_path.write_le(&mut writer)?; 94 self.transition_leaf.write_le(&mut writer) 95 } 96 } 97 98 #[cfg(test)] 99 mod tests { 100 use super::*; 101 use alphavm_console_network::MainnetV0; 102 103 type CurrentNetwork = MainnetV0; 104 105 const ITERATIONS: usize = 100; 106 107 #[test] 108 fn test_bytes() { 109 let mut rng = TestRng::default(); 110 111 for _ in 0..ITERATIONS { 112 // Sample the state path. 113 let expected = 114 crate::state_path::test_helpers::sample_global_state_path::<CurrentNetwork>(None, &mut rng).unwrap(); 115 116 // Check the byte representation. 117 let expected_bytes = expected.to_bytes_le().unwrap(); 118 assert_eq!(expected, StatePath::read_le(&expected_bytes[..]).unwrap()); 119 assert!(StatePath::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err()); 120 } 121 } 122 }