snarkvm_synthesizer_program/logic/command/
get.rs1use crate::{
17 CallOperator,
18 Opcode,
19 Operand,
20 traits::{FinalizeStoreTrait, RegistersLoad, RegistersStore, StackMatches, StackProgram},
21};
22use console::{
23 network::prelude::*,
24 program::{Register, Value},
25};
26
27#[derive(Clone)]
30pub struct Get<N: Network> {
31 mapping: CallOperator<N>,
33 key: Operand<N>,
35 destination: Register<N>,
37}
38
39impl<N: Network> PartialEq for Get<N> {
40 #[inline]
42 fn eq(&self, other: &Self) -> bool {
43 self.mapping == other.mapping && self.key == other.key && self.destination == other.destination
44 }
45}
46
47impl<N: Network> Eq for Get<N> {}
48
49impl<N: Network> std::hash::Hash for Get<N> {
50 #[inline]
52 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
53 self.mapping.hash(state);
54 self.key.hash(state);
55 self.destination.hash(state);
56 }
57}
58
59impl<N: Network> Get<N> {
60 #[inline]
62 pub const fn opcode() -> Opcode {
63 Opcode::Command("get")
64 }
65
66 #[inline]
68 pub fn operands(&self) -> Vec<Operand<N>> {
69 vec![self.key.clone()]
70 }
71
72 #[inline]
74 pub const fn mapping(&self) -> &CallOperator<N> {
75 &self.mapping
76 }
77
78 #[inline]
80 pub const fn key(&self) -> &Operand<N> {
81 &self.key
82 }
83
84 #[inline]
86 pub const fn destination(&self) -> &Register<N> {
87 &self.destination
88 }
89}
90
91impl<N: Network> Get<N> {
92 #[inline]
94 pub fn finalize(
95 &self,
96 stack: &(impl StackMatches<N> + StackProgram<N>),
97 store: &impl FinalizeStoreTrait<N>,
98 registers: &mut (impl RegistersLoad<N> + RegistersStore<N>),
99 ) -> Result<()> {
100 let (program_id, mapping_name) = match self.mapping {
102 CallOperator::Locator(locator) => (*locator.program_id(), *locator.resource()),
103 CallOperator::Resource(mapping_name) => (*stack.program_id(), mapping_name),
104 };
105
106 if !store.contains_mapping_confirmed(&program_id, &mapping_name)? {
108 bail!("Mapping '{program_id}/{mapping_name}' does not exist in storage");
109 }
110
111 let key = registers.load_plaintext(stack, &self.key)?;
113
114 let value = match store.get_value_speculative(program_id, mapping_name, &key)? {
116 Some(Value::Plaintext(plaintext)) => Value::Plaintext(plaintext),
117 Some(Value::Record(..)) => bail!("Cannot 'get' a 'record'"),
118 Some(Value::Future(..)) => bail!("Cannot 'get' a 'future'",),
119 None => bail!("Key '{key}' does not exist in mapping '{program_id}/{mapping_name}'"),
121 };
122
123 registers.store(stack, &self.destination, value)?;
125
126 Ok(())
127 }
128}
129
130impl<N: Network> Parser for Get<N> {
131 #[inline]
133 fn parse(string: &str) -> ParserResult<Self> {
134 let (string, _) = Sanitizer::parse(string)?;
136 let (string, _) = tag(*Self::opcode())(string)?;
138 let (string, _) = Sanitizer::parse_whitespaces(string)?;
140
141 let (string, mapping) = CallOperator::parse(string)?;
143 let (string, _) = tag("[")(string)?;
145 let (string, _) = Sanitizer::parse_whitespaces(string)?;
147 let (string, key) = Operand::parse(string)?;
149 let (string, _) = Sanitizer::parse_whitespaces(string)?;
151 let (string, _) = tag("]")(string)?;
153
154 let (string, _) = Sanitizer::parse_whitespaces(string)?;
156 let (string, _) = tag("into")(string)?;
158 let (string, _) = Sanitizer::parse_whitespaces(string)?;
160 let (string, destination) = Register::parse(string)?;
162
163 let (string, _) = Sanitizer::parse_whitespaces(string)?;
165 let (string, _) = tag(";")(string)?;
167
168 Ok((string, Self { mapping, key, destination }))
169 }
170}
171
172impl<N: Network> FromStr for Get<N> {
173 type Err = Error;
174
175 #[inline]
177 fn from_str(string: &str) -> Result<Self> {
178 match Self::parse(string) {
179 Ok((remainder, object)) => {
180 ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
182 Ok(object)
184 }
185 Err(error) => bail!("Failed to parse string. {error}"),
186 }
187 }
188}
189
190impl<N: Network> Debug for Get<N> {
191 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
193 Display::fmt(self, f)
194 }
195}
196
197impl<N: Network> Display for Get<N> {
198 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
200 write!(f, "{} ", Self::opcode())?;
202 write!(f, "{}[{}] into ", self.mapping, self.key)?;
204 write!(f, "{};", self.destination)
206 }
207}
208
209impl<N: Network> FromBytes for Get<N> {
210 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
212 let mapping = CallOperator::read_le(&mut reader)?;
214 let key = Operand::read_le(&mut reader)?;
216 let destination = Register::read_le(&mut reader)?;
218 Ok(Self { mapping, key, destination })
220 }
221}
222
223impl<N: Network> ToBytes for Get<N> {
224 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
226 self.mapping.write_le(&mut writer)?;
228 self.key.write_le(&mut writer)?;
230 self.destination.write_le(&mut writer)
232 }
233}
234
235#[cfg(test)]
236mod tests {
237 use super::*;
238 use console::{network::MainnetV0, program::Register};
239
240 type CurrentNetwork = MainnetV0;
241
242 #[test]
243 fn test_parse() {
244 let (string, get) = Get::<CurrentNetwork>::parse("get account[r0] into r1;").unwrap();
245 assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
246 assert_eq!(get.mapping, CallOperator::from_str("account").unwrap());
247 assert_eq!(get.operands().len(), 1, "The number of operands is incorrect");
248 assert_eq!(get.key, Operand::Register(Register::Locator(0)), "The first operand is incorrect");
249 assert_eq!(get.destination, Register::Locator(1), "The second operand is incorrect");
250
251 let (string, get) = Get::<CurrentNetwork>::parse("get token.aleo/balances[r0] into r1;").unwrap();
252 assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
253 assert_eq!(get.mapping, CallOperator::from_str("token.aleo/balances").unwrap());
254 assert_eq!(get.operands().len(), 1, "The number of operands is incorrect");
255 assert_eq!(get.key, Operand::Register(Register::Locator(0)), "The first operand is incorrect");
256 assert_eq!(get.destination, Register::Locator(1), "The second operand is incorrect");
257 }
258
259 #[test]
260 fn test_from_bytes() {
261 let (string, get) = Get::<CurrentNetwork>::parse("get account[r0] into r1;").unwrap();
262 assert!(string.is_empty());
263 let bytes_le = get.to_bytes_le().unwrap();
264 let result = Get::<CurrentNetwork>::from_bytes_le(&bytes_le[..]);
265 assert!(result.is_ok())
266 }
267}