snarkvm_synthesizer_program/logic/command/get/
standard.rs1use crate::{CallOperator, FinalizeStoreTrait, Opcode, Operand, RegistersTrait, StackTrait};
17use console::{
18 network::prelude::*,
19 program::{Register, Value},
20};
21
22#[derive(Clone, PartialEq, Eq, Hash)]
25pub struct Get<N: Network> {
26 mapping: CallOperator<N>,
28 operands: [Operand<N>; 1],
30 destination: Register<N>,
32}
33
34impl<N: Network> Get<N> {
35 #[inline]
37 pub const fn opcode() -> Opcode {
38 Opcode::Command("get")
39 }
40
41 #[inline]
43 pub fn operands(&self) -> &[Operand<N>] {
44 &self.operands
45 }
46
47 #[inline]
49 pub const fn mapping(&self) -> &CallOperator<N> {
50 &self.mapping
51 }
52
53 #[inline]
55 pub const fn key(&self) -> &Operand<N> {
56 &self.operands[0]
57 }
58
59 #[inline]
61 pub const fn destination(&self) -> &Register<N> {
62 &self.destination
63 }
64
65 #[inline]
67 pub fn contains_external_struct(&self) -> bool {
68 false
69 }
70}
71
72impl<N: Network> Get<N> {
73 #[inline]
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 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 value = match store.get_value_speculative(program_id, mapping_name, &key)? {
97 Some(Value::Plaintext(plaintext)) => Value::Plaintext(plaintext),
98 Some(Value::Record(..)) => bail!("Cannot 'get' a 'record'"),
99 Some(Value::Future(..)) => bail!("Cannot 'get' a 'future'",),
100 Some(Value::DynamicRecord(..)) => bail!("Cannot 'get' a 'dynamic.record'"),
101 Some(Value::DynamicFuture(..)) => bail!("Cannot 'get' a 'dynamic.future'"),
102 None => bail!("Key '{key}' does not exist in mapping '{program_id}/{mapping_name}'"),
104 };
105
106 registers.store(stack, &self.destination, value)?;
108
109 Ok(())
110 }
111}
112
113impl<N: Network> Parser for Get<N> {
114 #[inline]
116 fn parse(string: &str) -> ParserResult<Self> {
117 let (string, _) = Sanitizer::parse(string)?;
119 let (string, _) = tag(*Self::opcode())(string)?;
121 let (string, _) = Sanitizer::parse_whitespaces(string)?;
123
124 let (string, mapping) = CallOperator::parse(string)?;
126 let (string, _) = tag("[")(string)?;
128 let (string, _) = Sanitizer::parse_whitespaces(string)?;
130 let (string, key) = Operand::parse(string)?;
132 let (string, _) = Sanitizer::parse_whitespaces(string)?;
134 let (string, _) = tag("]")(string)?;
136
137 let (string, _) = Sanitizer::parse_whitespaces(string)?;
139 let (string, _) = tag("into")(string)?;
141 let (string, _) = Sanitizer::parse_whitespaces(string)?;
143 let (string, destination) = Register::parse(string)?;
145
146 let (string, _) = Sanitizer::parse_whitespaces(string)?;
148 let (string, _) = tag(";")(string)?;
150
151 Ok((string, Self { mapping, operands: [key], destination }))
152 }
153}
154
155impl<N: Network> FromStr for Get<N> {
156 type Err = Error;
157
158 #[inline]
160 fn from_str(string: &str) -> Result<Self> {
161 match Self::parse(string) {
162 Ok((remainder, object)) => {
163 ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
165 Ok(object)
167 }
168 Err(error) => bail!("Failed to parse string. {error}"),
169 }
170 }
171}
172
173impl<N: Network> Debug for Get<N> {
174 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
176 Display::fmt(self, f)
177 }
178}
179
180impl<N: Network> Display for Get<N> {
181 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
183 write!(f, "{} ", Self::opcode())?;
185 write!(f, "{}[{}] into ", self.mapping, self.key())?;
187 write!(f, "{};", self.destination)
189 }
190}
191
192impl<N: Network> FromBytes for Get<N> {
193 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
195 let mapping = CallOperator::read_le(&mut reader)?;
197 let key = Operand::read_le(&mut reader)?;
199 let destination = Register::read_le(&mut reader)?;
201 Ok(Self { mapping, operands: [key], destination })
203 }
204}
205
206impl<N: Network> ToBytes for Get<N> {
207 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
209 self.mapping.write_le(&mut writer)?;
211 self.key().write_le(&mut writer)?;
213 self.destination.write_le(&mut writer)
215 }
216}
217
218#[cfg(test)]
219mod tests {
220 use super::*;
221 use console::{network::MainnetV0, program::Register};
222
223 type CurrentNetwork = MainnetV0;
224
225 #[test]
226 fn test_parse() {
227 let (string, get) = Get::<CurrentNetwork>::parse("get account[r0] into r1;").unwrap();
228 assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
229 assert_eq!(get.mapping, CallOperator::from_str("account").unwrap());
230 assert_eq!(get.operands().len(), 1, "The number of operands is incorrect");
231 assert_eq!(get.key(), &Operand::Register(Register::Locator(0)), "The first operand is incorrect");
232 assert_eq!(get.destination, Register::Locator(1), "The second operand is incorrect");
233
234 let (string, get) = Get::<CurrentNetwork>::parse("get token.aleo/balances[r0] into r1;").unwrap();
235 assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
236 assert_eq!(get.mapping, CallOperator::from_str("token.aleo/balances").unwrap());
237 assert_eq!(get.operands().len(), 1, "The number of operands is incorrect");
238 assert_eq!(get.key(), &Operand::Register(Register::Locator(0)), "The first operand is incorrect");
239 assert_eq!(get.destination, Register::Locator(1), "The second operand is incorrect");
240 }
241
242 #[test]
243 fn test_from_bytes() {
244 let (string, get) = Get::<CurrentNetwork>::parse("get account[r0] into r1;").unwrap();
245 assert!(string.is_empty());
246 let bytes_le = get.to_bytes_le().unwrap();
247 let result = Get::<CurrentNetwork>::from_bytes_le(&bytes_le[..]);
248 assert!(result.is_ok())
249 }
250}