bytes.rs
  1  // Copyright (c) 2019-2025 Alpha-Delta Network Inc.
  2  // This file is part of the deltavm 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 FinalizeOperation<N> {
 19      /// Reads the finalize operation from buffer.
 20      fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
 21          // Read the variant.
 22          let variant = u8::read_le(&mut reader)?;
 23          // Read the finalize operation.
 24          match variant {
 25              0 => {
 26                  // Read the mapping ID.
 27                  let mapping_id = Field::read_le(&mut reader)?;
 28                  // Return the finalize operation.
 29                  Ok(Self::InitializeMapping(mapping_id))
 30              }
 31              1 => {
 32                  // Read the mapping ID.
 33                  let mapping_id = Field::read_le(&mut reader)?;
 34                  // Read the key ID.
 35                  let key_id = Field::read_le(&mut reader)?;
 36                  // Read the value ID.
 37                  let value_id = Field::read_le(&mut reader)?;
 38                  // Return the finalize operation.
 39                  Ok(Self::InsertKeyValue(mapping_id, key_id, value_id))
 40              }
 41              2 => {
 42                  // Read the mapping ID.
 43                  let mapping_id = Field::read_le(&mut reader)?;
 44                  // Read the key ID.
 45                  let key_id = Field::read_le(&mut reader)?;
 46                  // Read the value ID.
 47                  let value_id = Field::read_le(&mut reader)?;
 48                  // Return the finalize operation.
 49                  Ok(Self::UpdateKeyValue(mapping_id, key_id, value_id))
 50              }
 51              3 => {
 52                  // Read the mapping ID.
 53                  let mapping_id = Field::read_le(&mut reader)?;
 54                  // Read the key ID.
 55                  let key_id = Field::read_le(&mut reader)?;
 56                  // Return the finalize operation.
 57                  Ok(Self::RemoveKeyValue(mapping_id, key_id))
 58              }
 59              4 => {
 60                  // Read the mapping ID.
 61                  let mapping_id = Field::read_le(&mut reader)?;
 62                  // Return the finalize operation.
 63                  Ok(Self::ReplaceMapping(mapping_id))
 64              }
 65              5 => {
 66                  // Read the mapping ID.
 67                  let mapping_id = Field::read_le(&mut reader)?;
 68                  // Return the finalize operation.
 69                  Ok(Self::RemoveMapping(mapping_id))
 70              }
 71              6.. => Err(error(format!("Failed to decode finalize operation variant {variant}"))),
 72          }
 73      }
 74  }
 75  
 76  impl<N: Network> ToBytes for FinalizeOperation<N> {
 77      /// Writes the finalize operation to buffer.
 78      fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
 79          match self {
 80              Self::InitializeMapping(mapping_id) => {
 81                  // Write the variant.
 82                  0u8.write_le(&mut writer)?;
 83                  // Write the mapping ID.
 84                  mapping_id.write_le(&mut writer)?;
 85              }
 86              Self::InsertKeyValue(mapping_id, key_id, value_id) => {
 87                  // Write the variant.
 88                  1u8.write_le(&mut writer)?;
 89                  // Write the mapping ID.
 90                  mapping_id.write_le(&mut writer)?;
 91                  // Write the key ID.
 92                  key_id.write_le(&mut writer)?;
 93                  // Write the value ID.
 94                  value_id.write_le(&mut writer)?;
 95              }
 96              Self::UpdateKeyValue(mapping_id, key_id, value_id) => {
 97                  // Write the variant.
 98                  2u8.write_le(&mut writer)?;
 99                  // Write the mapping ID.
100                  mapping_id.write_le(&mut writer)?;
101                  // Write the key ID.
102                  key_id.write_le(&mut writer)?;
103                  // Write the value ID.
104                  value_id.write_le(&mut writer)?;
105              }
106              Self::RemoveKeyValue(mapping_id, key_id) => {
107                  // Write the variant.
108                  3u8.write_le(&mut writer)?;
109                  // Write the mapping ID.
110                  mapping_id.write_le(&mut writer)?;
111                  // Write the key ID.
112                  key_id.write_le(&mut writer)?;
113              }
114              Self::ReplaceMapping(mapping_id) => {
115                  // Write the variant.
116                  4u8.write_le(&mut writer)?;
117                  // Write the mapping ID.
118                  mapping_id.write_le(&mut writer)?;
119              }
120              Self::RemoveMapping(mapping_id) => {
121                  // Write the variant.
122                  5u8.write_le(&mut writer)?;
123                  // Write the mapping ID.
124                  mapping_id.write_le(&mut writer)?;
125              }
126          }
127          Ok(())
128      }
129  }
130  
131  #[cfg(test)]
132  mod tests {
133      use super::*;
134  
135      #[test]
136      fn test_bytes() {
137          for expected in crate::logic::finalize_operation::test_helpers::sample_finalize_operations() {
138              // Check the byte representation.
139              let expected_bytes = expected.to_bytes_le().unwrap();
140              assert_eq!(expected, FinalizeOperation::read_le(&expected_bytes[..]).unwrap());
141          }
142      }
143  }