Skip to main content

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