snarkvm_synthesizer_program/logic/command/
get_or_use.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)]
26pub struct GetOrUse<N: Network> {
27 mapping: CallOperator<N>,
29 operands: [Operand<N>; 2],
31 destination: Register<N>,
33}
34
35impl<N: Network> GetOrUse<N> {
36 #[inline]
38 pub const fn opcode() -> Opcode {
39 Opcode::Command("get.or_use")
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 default(&self) -> &Operand<N> {
63 &self.operands[1]
64 }
65
66 #[inline]
68 pub const fn destination(&self) -> &Register<N> {
69 &self.destination
70 }
71
72 #[inline]
74 pub fn contains_external_struct(&self) -> bool {
75 false
76 }
77}
78
79impl<N: Network> GetOrUse<N> {
80 #[inline]
82 pub fn finalize(
83 &self,
84 stack: &impl StackTrait<N>,
85 store: &impl FinalizeStoreTrait<N>,
86 registers: &mut impl RegistersTrait<N>,
87 ) -> Result<()> {
88 let (program_id, mapping_name) = match self.mapping {
90 CallOperator::Locator(locator) => (*locator.program_id(), *locator.resource()),
91 CallOperator::Resource(mapping_name) => (*stack.program_id(), mapping_name),
92 };
93
94 if !store.contains_mapping_speculative(&program_id, &mapping_name)? {
96 bail!("Mapping '{program_id}/{mapping_name}' does not exist");
97 }
98
99 let key = registers.load_plaintext(stack, self.key())?;
101
102 let value = match store.get_value_speculative(program_id, mapping_name, &key)? {
104 Some(Value::Plaintext(plaintext)) => Value::Plaintext(plaintext),
105 Some(Value::Record(..)) => bail!("Cannot 'get.or_use' a 'record'"),
106 Some(Value::Future(..)) => bail!("Cannot 'get.or_use' a 'future'"),
107 None => Value::Plaintext(registers.load_plaintext(stack, self.default())?),
109 };
110
111 registers.store(stack, &self.destination, value)?;
113
114 Ok(())
116 }
117}
118
119impl<N: Network> Parser for GetOrUse<N> {
120 #[inline]
122 fn parse(string: &str) -> ParserResult<Self> {
123 let (string, _) = Sanitizer::parse(string)?;
125 let (string, _) = tag(*Self::opcode())(string)?;
127 let (string, _) = Sanitizer::parse_whitespaces(string)?;
129
130 let (string, mapping) = CallOperator::parse(string)?;
132 let (string, _) = tag("[")(string)?;
134 let (string, _) = Sanitizer::parse_whitespaces(string)?;
136 let (string, key) = Operand::parse(string)?;
138 let (string, _) = Sanitizer::parse_whitespaces(string)?;
140 let (string, _) = tag("]")(string)?;
142 let (string, _) = Sanitizer::parse_whitespaces(string)?;
144 let (string, default) = Operand::parse(string)?;
146
147 let (string, _) = Sanitizer::parse_whitespaces(string)?;
149 let (string, _) = tag("into")(string)?;
151 let (string, _) = Sanitizer::parse_whitespaces(string)?;
153 let (string, destination) = Register::parse(string)?;
155
156 let (string, _) = Sanitizer::parse_whitespaces(string)?;
158 let (string, _) = tag(";")(string)?;
160
161 Ok((string, Self { mapping, operands: [key, default], destination }))
162 }
163}
164
165impl<N: Network> FromStr for GetOrUse<N> {
166 type Err = Error;
167
168 #[inline]
170 fn from_str(string: &str) -> Result<Self> {
171 match Self::parse(string) {
172 Ok((remainder, object)) => {
173 ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
175 Ok(object)
177 }
178 Err(error) => bail!("Failed to parse string. {error}"),
179 }
180 }
181}
182
183impl<N: Network> Debug for GetOrUse<N> {
184 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
186 Display::fmt(self, f)
187 }
188}
189
190impl<N: Network> Display for GetOrUse<N> {
191 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
193 write!(f, "{} ", Self::opcode())?;
195 write!(f, "{}[{}] {} into ", self.mapping, self.key(), self.default())?;
197 write!(f, "{};", self.destination)
199 }
200}
201
202impl<N: Network> FromBytes for GetOrUse<N> {
203 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
205 let mapping = CallOperator::read_le(&mut reader)?;
207 let key = Operand::read_le(&mut reader)?;
209 let default = Operand::read_le(&mut reader)?;
211 let destination = Register::read_le(&mut reader)?;
213 Ok(Self { mapping, operands: [key, default], destination })
215 }
216}
217
218impl<N: Network> ToBytes for GetOrUse<N> {
219 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
221 self.mapping.write_le(&mut writer)?;
223 self.key().write_le(&mut writer)?;
225 self.default().write_le(&mut writer)?;
227 self.destination.write_le(&mut writer)
229 }
230}
231
232#[cfg(test)]
233mod tests {
234 use super::*;
235 use console::{network::MainnetV0, program::Register};
236
237 type CurrentNetwork = MainnetV0;
238
239 #[test]
240 fn test_parse() {
241 let (string, get_or_use) = GetOrUse::<CurrentNetwork>::parse("get.or_use account[r0] r1 into r2;").unwrap();
242 assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
243 assert_eq!(get_or_use.mapping, CallOperator::from_str("account").unwrap());
244 assert_eq!(get_or_use.operands().len(), 2, "The number of operands is incorrect");
245 assert_eq!(get_or_use.key(), &Operand::Register(Register::Locator(0)), "The first operand is incorrect");
246 assert_eq!(get_or_use.default(), &Operand::Register(Register::Locator(1)), "The second operand is incorrect");
247 assert_eq!(get_or_use.destination, Register::Locator(2), "The second operand is incorrect");
248
249 let (string, get_or_use) =
250 GetOrUse::<CurrentNetwork>::parse("get.or_use token.aleo/balances[r0] r1 into r2;").unwrap();
251 assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
252 assert_eq!(get_or_use.mapping, CallOperator::from_str("token.aleo/balances").unwrap());
253 assert_eq!(get_or_use.operands().len(), 2, "The number of operands is incorrect");
254 assert_eq!(get_or_use.key(), &Operand::Register(Register::Locator(0)), "The first operand is incorrect");
255 assert_eq!(get_or_use.default(), &Operand::Register(Register::Locator(1)), "The second operand is incorrect");
256 assert_eq!(get_or_use.destination, Register::Locator(2), "The second operand is incorrect");
257 }
258
259 #[test]
260 fn test_from_bytes() {
261 let (string, get_or_use) = GetOrUse::<CurrentNetwork>::parse("get.or_use account[r0] r1 into r2;").unwrap();
262 assert!(string.is_empty());
263 let bytes_le = get_or_use.to_bytes_le().unwrap();
264 let result = GetOrUse::<CurrentNetwork>::from_bytes_le(&bytes_le[..]);
265 assert!(result.is_ok());
266 }
267}