snarkvm_synthesizer_program/logic/command/
contains.rs1use crate::{CallOperator, FinalizeStoreTrait, Opcode, Operand, RegistersTrait, StackTrait};
17use console::{
18 network::prelude::*,
19 program::{Literal, Register, Value},
20 types::Boolean,
21};
22
23#[derive(Clone, PartialEq, Eq, Hash)]
26pub struct Contains<N: Network> {
27 mapping: CallOperator<N>,
29 operands: [Operand<N>; 1],
31 destination: Register<N>,
33}
34
35impl<N: Network> Contains<N> {
36 #[inline]
38 pub const fn opcode() -> Opcode {
39 Opcode::Command("contains")
40 }
41
42 #[inline]
44 pub fn operands(&self) -> &[Operand<N>] {
45 &self.operands
46 }
47
48 #[inline]
50 pub const fn mapping(&self) -> &CallOperator<N> {
51 &self.mapping
52 }
53
54 #[inline]
56 pub const fn key(&self) -> &Operand<N> {
57 &self.operands[0]
58 }
59
60 #[inline]
62 pub const fn destination(&self) -> &Register<N> {
63 &self.destination
64 }
65
66 #[inline]
68 pub fn contains_external_struct(&self) -> bool {
69 false
70 }
71}
72
73impl<N: Network> Contains<N> {
74 pub fn finalize(
76 &self,
77 stack: &impl StackTrait<N>,
78 store: &impl FinalizeStoreTrait<N>,
79 registers: &mut impl RegistersTrait<N>,
80 ) -> Result<()> {
81 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 if !store.contains_mapping_speculative(&program_id, &mapping_name)? {
89 bail!("Mapping '{program_id}/{mapping_name}' does not exist");
90 }
91
92 let key = registers.load_plaintext(stack, self.key())?;
94
95 let contains_key = store.contains_key_speculative(program_id, mapping_name, &key)?;
97
98 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 fn parse(string: &str) -> ParserResult<Self> {
108 let (string, _) = Sanitizer::parse(string)?;
110 let (string, _) = tag(*Self::opcode())(string)?;
112 let (string, _) = Sanitizer::parse_whitespaces(string)?;
114
115 let (string, mapping) = CallOperator::parse(string)?;
117 let (string, _) = tag("[")(string)?;
119 let (string, _) = Sanitizer::parse_whitespaces(string)?;
121 let (string, key) = Operand::parse(string)?;
123 let (string, _) = Sanitizer::parse_whitespaces(string)?;
125 let (string, _) = tag("]")(string)?;
127
128 let (string, _) = Sanitizer::parse_whitespaces(string)?;
130 let (string, _) = tag("into")(string)?;
132 let (string, _) = Sanitizer::parse_whitespaces(string)?;
134 let (string, destination) = Register::parse(string)?;
136
137 let (string, _) = Sanitizer::parse_whitespaces(string)?;
139 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 #[inline]
151 fn from_str(string: &str) -> Result<Self> {
152 match Self::parse(string) {
153 Ok((remainder, object)) => {
154 ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
156 Ok(object)
158 }
159 Err(error) => bail!("Failed to parse string. {error}"),
160 }
161 }
162}
163
164impl<N: Network> Debug for Contains<N> {
165 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
167 Display::fmt(self, f)
168 }
169}
170
171impl<N: Network> Display for Contains<N> {
172 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
174 write!(f, "{} ", Self::opcode())?;
176 write!(f, "{}[{}] into ", self.mapping, self.key())?;
178 write!(f, "{};", self.destination)
180 }
181}
182
183impl<N: Network> FromBytes for Contains<N> {
184 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
186 let mapping = CallOperator::read_le(&mut reader)?;
188 let key = Operand::read_le(&mut reader)?;
190 let destination = Register::read_le(&mut reader)?;
192 Ok(Self { mapping, operands: [key], destination })
194 }
195}
196
197impl<N: Network> ToBytes for Contains<N> {
198 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
200 self.mapping.write_le(&mut writer)?;
202 self.key().write_le(&mut writer)?;
204 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}