snarkvm_console_program/state_path/transaction_leaf/mod.rs
1// Copyright 2024 Aleo Network Foundation
2// This file is part of the snarkVM 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
16mod bytes;
17mod serialize;
18mod string;
19mod to_bits;
20
21use snarkvm_console_network::prelude::*;
22use snarkvm_console_types::Field;
23
24/// The Merkle leaf for a function or transition in the transaction.
25#[derive(Copy, Clone, PartialEq, Eq)]
26pub struct TransactionLeaf<N: Network> {
27 /// The variant of the Merkle leaf.
28 variant: u8,
29 /// The index of the Merkle leaf.
30 index: u16,
31 /// The ID.
32 id: Field<N>,
33}
34
35impl<N: Network> TransactionLeaf<N> {
36 /// Initializes a new instance of `TransactionLeaf`.
37 pub const fn new_deployment(index: u16, id: Field<N>) -> Self {
38 Self { variant: 0, index, id }
39 }
40
41 /// Initializes a new instance of `TransactionLeaf`.
42 pub const fn new_execution(index: u16, id: Field<N>) -> Self {
43 Self { variant: 1, index, id }
44 }
45
46 /// Initializes a new instance of `TransactionLeaf`.
47 pub const fn new_fee(index: u16, id: Field<N>) -> Self {
48 Self { variant: 1, index, id }
49 }
50
51 /// Initializes a new instance of `TransactionLeaf`.
52 pub const fn from(variant: u8, index: u16, id: Field<N>) -> Self {
53 Self { variant, index, id }
54 }
55
56 /// Returns the variant of the Merkle leaf.
57 pub const fn variant(&self) -> u8 {
58 self.variant
59 }
60
61 /// Returns the index of the Merkle leaf.
62 pub const fn index(&self) -> u16 {
63 self.index
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)]
73mod test_helpers {
74 use super::*;
75 use snarkvm_console_network::MainnetV0;
76
77 type CurrentNetwork = MainnetV0;
78
79 pub(super) fn sample_leaf(rng: &mut TestRng) -> TransactionLeaf<CurrentNetwork> {
80 // Construct a new leaf.
81 TransactionLeaf::from(rng.gen(), rng.gen(), Uniform::rand(rng))
82 }
83}