mod.rs
  1  // Copyright (c) 2025 ADnet Contributors
  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  mod bits;
 17  mod bytes;
 18  mod serialize;
 19  mod string;
 20  
 21  use console::{
 22      network::{error, prelude::*},
 23      types::Field,
 24  };
 25  
 26  /// Enum to represent the allowed set of Merkle tree operations.
 27  #[derive(Copy, Clone, PartialEq, Eq)]
 28  pub enum FinalizeOperation<N: Network> {
 29      /// Appends a mapping to the program tree, as (`mapping ID`).
 30      InitializeMapping(Field<N>),
 31      /// Inserts a key-value leaf into the mapping tree,
 32      /// as (`mapping ID`, `key ID`, `value ID`).
 33      InsertKeyValue(Field<N>, Field<N>, Field<N>),
 34      /// Updates the key-value leaf in the mapping tree,
 35      /// as (`mapping ID`, `key ID`, `value ID`).
 36      UpdateKeyValue(Field<N>, Field<N>, Field<N>),
 37      /// Removes the key-value leaf in the mapping tree,
 38      /// as (`mapping ID`, `key ID`).
 39      RemoveKeyValue(Field<N>, Field<N>),
 40      /// Replaces a mapping from the program tree, as (`mapping ID`).
 41      ReplaceMapping(Field<N>),
 42      /// Removes a mapping from the program tree, as (`mapping ID`).
 43      RemoveMapping(Field<N>),
 44  }
 45  
 46  #[cfg(test)]
 47  pub(crate) mod test_helpers {
 48      use super::*;
 49      use console::network::MainnetV0;
 50  
 51      type CurrentNetwork = MainnetV0;
 52  
 53      /// Samples a random `InitializeMapping`.
 54      pub(crate) fn sample_initialize_mapping(rng: &mut TestRng) -> FinalizeOperation<CurrentNetwork> {
 55          FinalizeOperation::InitializeMapping(Uniform::rand(rng))
 56      }
 57  
 58      /// Samples a random `InsertKeyValue`.
 59      pub(crate) fn sample_insert_key_value(rng: &mut TestRng) -> FinalizeOperation<CurrentNetwork> {
 60          FinalizeOperation::InsertKeyValue(Uniform::rand(rng), Uniform::rand(rng), Uniform::rand(rng))
 61      }
 62  
 63      /// Samples a random `UpdateKeyValue`.
 64      pub(crate) fn sample_update_key_value(rng: &mut TestRng) -> FinalizeOperation<CurrentNetwork> {
 65          FinalizeOperation::UpdateKeyValue(Uniform::rand(rng), Uniform::rand(rng), Uniform::rand(rng))
 66      }
 67  
 68      /// Samples a random `RemoveKeyValue`.
 69      pub(crate) fn sample_remove_key_value(rng: &mut TestRng) -> FinalizeOperation<CurrentNetwork> {
 70          FinalizeOperation::RemoveKeyValue(Uniform::rand(rng), Uniform::rand(rng))
 71      }
 72  
 73      /// Samples a random `ReplaceMapping`.
 74      pub(crate) fn sample_replace_mapping(rng: &mut TestRng) -> FinalizeOperation<CurrentNetwork> {
 75          FinalizeOperation::ReplaceMapping(Uniform::rand(rng))
 76      }
 77  
 78      /// Samples a random `RemoveMapping`.
 79      pub(crate) fn sample_remove_mapping(rng: &mut TestRng) -> FinalizeOperation<CurrentNetwork> {
 80          FinalizeOperation::RemoveMapping(Uniform::rand(rng))
 81      }
 82  
 83      /// Samples a list of random `FinalizeOperation`.
 84      pub(crate) fn sample_finalize_operations() -> Vec<FinalizeOperation<CurrentNetwork>> {
 85          let rng = &mut TestRng::default();
 86  
 87          vec![
 88              sample_initialize_mapping(rng),
 89              sample_insert_key_value(rng),
 90              sample_update_key_value(rng),
 91              sample_remove_key_value(rng),
 92              sample_replace_mapping(rng),
 93              sample_remove_mapping(rng),
 94              sample_initialize_mapping(rng),
 95              sample_insert_key_value(rng),
 96              sample_update_key_value(rng),
 97              sample_remove_key_value(rng),
 98              sample_replace_mapping(rng),
 99              sample_remove_mapping(rng),
100          ]
101      }
102  }