snarkvm_synthesizer_program/logic/command/
contains.rs

1// Copyright (c) 2019-2025 Provable Inc.
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
16use crate::{
17    CallOperator,
18    Opcode,
19    Operand,
20    traits::{FinalizeStoreTrait, RegistersLoad, RegistersStore, StackMatches, StackProgram},
21};
22use console::{
23    network::prelude::*,
24    program::{Literal, Register, Value},
25    types::Boolean,
26};
27
28/// A contains command, e.g. `contains accounts[r0] into r1;`.
29/// Contains is `true` if a (`key`, `value`) entry exists in `mapping`, stores the result in `destination`.
30#[derive(Clone, PartialEq, Eq, Hash)]
31pub struct Contains<N: Network> {
32    /// The mapping name.
33    mapping: CallOperator<N>,
34    /// The key to access the mapping.
35    key: Operand<N>,
36    /// The destination register.
37    destination: Register<N>,
38}
39
40impl<N: Network> Contains<N> {
41    /// Returns the opcode.
42    #[inline]
43    pub const fn opcode() -> Opcode {
44        Opcode::Command("contains")
45    }
46
47    /// Returns the operands in the operation.
48    #[inline]
49    pub fn operands(&self) -> Vec<Operand<N>> {
50        vec![self.key.clone()]
51    }
52
53    /// Returns the mapping.
54    #[inline]
55    pub const fn mapping(&self) -> &CallOperator<N> {
56        &self.mapping
57    }
58
59    /// Returns the operand containing the key.
60    #[inline]
61    pub const fn key(&self) -> &Operand<N> {
62        &self.key
63    }
64
65    /// Returns the destination register.
66    #[inline]
67    pub const fn destination(&self) -> &Register<N> {
68        &self.destination
69    }
70}
71
72impl<N: Network> Contains<N> {
73    /// Finalizes the command.
74    #[inline]
75    pub fn finalize(
76        &self,
77        stack: &(impl StackMatches<N> + StackProgram<N>),
78        store: &impl FinalizeStoreTrait<N>,
79        registers: &mut (impl RegistersLoad<N> + RegistersStore<N>),
80    ) -> Result<()> {
81        // Determine the program ID and mapping name.
82        let (program_id, mapping_name) = match self.mapping {
83            CallOperator::Locator(locator) => (*locator.program_id(), *locator.resource()),
84            CallOperator::Resource(mapping_name) => (*stack.program_id(), mapping_name),
85        };
86
87        // Ensure the mapping exists in storage.
88        if !store.contains_mapping_confirmed(&program_id, &mapping_name)? {
89            bail!("Mapping '{program_id}/{mapping_name}' does not exist in storage");
90        }
91
92        // Load the operand as a plaintext.
93        let key = registers.load_plaintext(stack, &self.key)?;
94
95        // Determine if the key exists in the mapping.
96        let contains_key = store.contains_key_speculative(program_id, mapping_name, &key)?;
97
98        // Assign the value to the destination register.
99        registers.store(stack, &self.destination, Value::from(Literal::Boolean(Boolean::new(contains_key))))?;
100
101        Ok(())
102    }
103}
104
105impl<N: Network> Parser for Contains<N> {
106    /// Parses a string into an operation.
107    #[inline]
108    fn parse(string: &str) -> ParserResult<Self> {
109        // Parse the whitespace and comments from the string.
110        let (string, _) = Sanitizer::parse(string)?;
111        // Parse the opcode from the string.
112        let (string, _) = tag(*Self::opcode())(string)?;
113        // Parse the whitespace from the string.
114        let (string, _) = Sanitizer::parse_whitespaces(string)?;
115
116        // Parse the mapping name from the string.
117        let (string, mapping) = CallOperator::parse(string)?;
118        // Parse the "[" from the string.
119        let (string, _) = tag("[")(string)?;
120        // Parse the whitespace from the string.
121        let (string, _) = Sanitizer::parse_whitespaces(string)?;
122        // Parse the key operand from the string.
123        let (string, key) = Operand::parse(string)?;
124        // Parse the whitespace from the string.
125        let (string, _) = Sanitizer::parse_whitespaces(string)?;
126        // Parse the "]" from the string.
127        let (string, _) = tag("]")(string)?;
128
129        // Parse the whitespace from the string.
130        let (string, _) = Sanitizer::parse_whitespaces(string)?;
131        // Parse the "into" keyword from the string.
132        let (string, _) = tag("into")(string)?;
133        // Parse the whitespace from the string.
134        let (string, _) = Sanitizer::parse_whitespaces(string)?;
135        // Parse the destination register from the string.
136        let (string, destination) = Register::parse(string)?;
137
138        // Parse the whitespace from the string.
139        let (string, _) = Sanitizer::parse_whitespaces(string)?;
140        // Parse the ";" from the string.
141        let (string, _) = tag(";")(string)?;
142
143        Ok((string, Self { mapping, key, destination }))
144    }
145}
146
147impl<N: Network> FromStr for Contains<N> {
148    type Err = Error;
149
150    /// Parses a string into the command.
151    #[inline]
152    fn from_str(string: &str) -> Result<Self> {
153        match Self::parse(string) {
154            Ok((remainder, object)) => {
155                // Ensure the remainder is empty.
156                ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
157                // Return the object.
158                Ok(object)
159            }
160            Err(error) => bail!("Failed to parse string. {error}"),
161        }
162    }
163}
164
165impl<N: Network> Debug for Contains<N> {
166    /// Prints the command as a string.
167    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
168        Display::fmt(self, f)
169    }
170}
171
172impl<N: Network> Display for Contains<N> {
173    /// Prints the command to a string.
174    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
175        // Print the command.
176        write!(f, "{} ", Self::opcode())?;
177        // Print the mapping and key operand.
178        write!(f, "{}[{}] into ", self.mapping, self.key)?;
179        // Print the destination register.
180        write!(f, "{};", self.destination)
181    }
182}
183
184impl<N: Network> FromBytes for Contains<N> {
185    /// Reads the command from a buffer.
186    fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
187        // Read the mapping name.
188        let mapping = CallOperator::read_le(&mut reader)?;
189        // Read the key operand.
190        let key = Operand::read_le(&mut reader)?;
191        // Read the destination register.
192        let destination = Register::read_le(&mut reader)?;
193        // Return the command.
194        Ok(Self { mapping, key, destination })
195    }
196}
197
198impl<N: Network> ToBytes for Contains<N> {
199    /// Writes the operation to a buffer.
200    fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
201        // Write the mapping name.
202        self.mapping.write_le(&mut writer)?;
203        // Write the key operand.
204        self.key.write_le(&mut writer)?;
205        // Write the destination register.
206        self.destination.write_le(&mut writer)
207    }
208}
209
210#[cfg(test)]
211mod tests {
212    use super::*;
213    use console::{network::MainnetV0, program::Register};
214
215    type CurrentNetwork = MainnetV0;
216
217    #[test]
218    fn test_parse() {
219        let (string, contains) = Contains::<CurrentNetwork>::parse("contains account[r0] into r1;").unwrap();
220        assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
221        assert_eq!(contains.mapping, CallOperator::from_str("account").unwrap());
222        assert_eq!(contains.operands().len(), 1, "The number of operands is incorrect");
223        assert_eq!(contains.key, Operand::Register(Register::Locator(0)), "The first operand is incorrect");
224        assert_eq!(contains.destination, Register::Locator(1), "The second operand is incorrect");
225
226        let (string, contains) =
227            Contains::<CurrentNetwork>::parse("contains credits.aleo/account[r0] into r1;").unwrap();
228        assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
229        assert_eq!(contains.mapping, CallOperator::from_str("credits.aleo/account").unwrap());
230        assert_eq!(contains.operands().len(), 1, "The number of operands is incorrect");
231        assert_eq!(contains.key, Operand::Register(Register::Locator(0)), "The first operand is incorrect");
232        assert_eq!(contains.destination, Register::Locator(1), "The second operand is incorrect");
233    }
234
235    #[test]
236    fn test_from_bytes() {
237        let (string, contains) = Contains::<CurrentNetwork>::parse("contains account[r0] into r1;").unwrap();
238        assert!(string.is_empty());
239        let bytes_le = contains.to_bytes_le().unwrap();
240        let result = Contains::<CurrentNetwork>::from_bytes_le(&bytes_le[..]);
241        assert!(result.is_ok())
242    }
243}