mod.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  mod bytes;
17  mod serialize;
18  mod string;
19  mod to_bits;
20  
21  use alphavm_console_network::prelude::*;
22  use alphavm_console_types::Field;
23  
24  /// The transition leaf version.
25  const TRANSITION_LEAF_VERSION: u8 = 1u8;
26  
27  /// The Merkle leaf for an input or output ID in the transition.
28  #[derive(Copy, Clone, PartialEq, Eq)]
29  pub struct TransitionLeaf<N: Network> {
30      /// The version of the Merkle leaf.
31      version: u8,
32      /// The index of the Merkle leaf.
33      index: u8,
34      /// The variant of the Merkle leaf.
35      variant: u8,
36      /// The ID.
37      id: Field<N>,
38  }
39  
40  impl<N: Network> TransitionLeaf<N> {
41      /// Initializes a new instance of `TransitionLeaf`.
42      pub const fn new_with_version(index: u8, variant: u8, id: Field<N>) -> Self {
43          Self { version: TRANSITION_LEAF_VERSION, index, variant, id }
44      }
45  
46      /// Initializes a new instance of `TransitionLeaf`.
47      pub const fn from(version: u8, index: u8, variant: u8, id: Field<N>) -> Self {
48          Self { version, index, variant, id }
49      }
50  
51      /// Returns the version of the Merkle leaf.
52      pub const fn version(&self) -> u8 {
53          self.version
54      }
55  
56      /// Returns the index of the Merkle leaf.
57      pub const fn index(&self) -> u8 {
58          self.index
59      }
60  
61      /// Returns the variant of the Merkle leaf.
62      pub const fn variant(&self) -> u8 {
63          self.variant
64      }
65  
66      /// Returns the ID in the Merkle leaf.
67      pub const fn id(&self) -> Field<N> {
68          self.id
69      }
70  }
71  
72  #[cfg(test)]
73  mod test_helpers {
74      use super::*;
75      use alphavm_console_network::MainnetV0;
76  
77      type CurrentNetwork = MainnetV0;
78  
79      pub(super) fn sample_leaf(rng: &mut TestRng) -> TransitionLeaf<CurrentNetwork> {
80          // Construct a new leaf.
81          TransitionLeaf::new_with_version(rng.r#gen(), rng.r#gen(), Uniform::rand(rng))
82      }
83  }