snarkvm_synthesizer_program/logic/command/
get_or_use.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)]
31pub struct GetOrUse<N: Network> {
32 mapping: CallOperator<N>,
34 key: Operand<N>,
36 default: Operand<N>,
38 destination: Register<N>,
40}
41
42impl<N: Network> PartialEq for GetOrUse<N> {
43 #[inline]
44 fn eq(&self, other: &Self) -> bool {
45 self.mapping == other.mapping
46 && self.key == other.key
47 && self.default == other.default
48 && self.destination == other.destination
49 }
50}
51
52impl<N: Network> Eq for GetOrUse<N> {}
53
54impl<N: Network> std::hash::Hash for GetOrUse<N> {
55 #[inline]
56 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
57 self.mapping.hash(state);
58 self.key.hash(state);
59 self.default.hash(state);
60 self.destination.hash(state);
61 }
62}
63
64impl<N: Network> GetOrUse<N> {
65 #[inline]
67 pub const fn opcode() -> Opcode {
68 Opcode::Command("get.or_use")
69 }
70
71 #[inline]
73 pub fn operands(&self) -> Vec<Operand<N>> {
74 vec![self.key.clone(), self.default.clone()]
75 }
76
77 #[inline]
79 pub const fn mapping(&self) -> &CallOperator<N> {
80 &self.mapping
81 }
82
83 #[inline]
85 pub const fn key(&self) -> &Operand<N> {
86 &self.key
87 }
88
89 #[inline]
91 pub const fn default(&self) -> &Operand<N> {
92 &self.default
93 }
94
95 #[inline]
97 pub const fn destination(&self) -> &Register<N> {
98 &self.destination
99 }
100}
101
102impl<N: Network> GetOrUse<N> {
103 #[inline]
105 pub fn finalize(
106 &self,
107 stack: &(impl StackMatches<N> + StackProgram<N>),
108 store: &impl FinalizeStoreTrait<N>,
109 registers: &mut (impl RegistersLoad<N> + RegistersStore<N>),
110 ) -> Result<()> {
111 let (program_id, mapping_name) = match self.mapping {
113 CallOperator::Locator(locator) => (*locator.program_id(), *locator.resource()),
114 CallOperator::Resource(mapping_name) => (*stack.program_id(), mapping_name),
115 };
116
117 if !store.contains_mapping_confirmed(&program_id, &mapping_name)? {
119 bail!("Mapping '{program_id}/{mapping_name}' does not exist in storage");
120 }
121
122 let key = registers.load_plaintext(stack, &self.key)?;
124
125 let value = match store.get_value_speculative(program_id, mapping_name, &key)? {
127 Some(Value::Plaintext(plaintext)) => Value::Plaintext(plaintext),
128 Some(Value::Record(..)) => bail!("Cannot 'get.or_use' a 'record'"),
129 Some(Value::Future(..)) => bail!("Cannot 'get.or_use' a 'future'"),
130 None => Value::Plaintext(registers.load_plaintext(stack, &self.default)?),
132 };
133
134 registers.store(stack, &self.destination, value)?;
136
137 Ok(())
139 }
140}
141
142impl<N: Network> Parser for GetOrUse<N> {
143 #[inline]
145 fn parse(string: &str) -> ParserResult<Self> {
146 let (string, _) = Sanitizer::parse(string)?;
148 let (string, _) = tag(*Self::opcode())(string)?;
150 let (string, _) = Sanitizer::parse_whitespaces(string)?;
152
153 let (string, mapping) = CallOperator::parse(string)?;
155 let (string, _) = tag("[")(string)?;
157 let (string, _) = Sanitizer::parse_whitespaces(string)?;
159 let (string, key) = Operand::parse(string)?;
161 let (string, _) = Sanitizer::parse_whitespaces(string)?;
163 let (string, _) = tag("]")(string)?;
165 let (string, _) = Sanitizer::parse_whitespaces(string)?;
167 let (string, default) = Operand::parse(string)?;
169
170 let (string, _) = Sanitizer::parse_whitespaces(string)?;
172 let (string, _) = tag("into")(string)?;
174 let (string, _) = Sanitizer::parse_whitespaces(string)?;
176 let (string, destination) = Register::parse(string)?;
178
179 let (string, _) = Sanitizer::parse_whitespaces(string)?;
181 let (string, _) = tag(";")(string)?;
183
184 Ok((string, Self { mapping, key, default, destination }))
185 }
186}
187
188impl<N: Network> FromStr for GetOrUse<N> {
189 type Err = Error;
190
191 #[inline]
193 fn from_str(string: &str) -> Result<Self> {
194 match Self::parse(string) {
195 Ok((remainder, object)) => {
196 ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
198 Ok(object)
200 }
201 Err(error) => bail!("Failed to parse string. {error}"),
202 }
203 }
204}
205
206impl<N: Network> Debug for GetOrUse<N> {
207 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
209 Display::fmt(self, f)
210 }
211}
212
213impl<N: Network> Display for GetOrUse<N> {
214 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
216 write!(f, "{} ", Self::opcode())?;
218 write!(f, "{}[{}] {} into ", self.mapping, self.key, self.default)?;
220 write!(f, "{};", self.destination)
222 }
223}
224
225impl<N: Network> FromBytes for GetOrUse<N> {
226 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
228 let mapping = CallOperator::read_le(&mut reader)?;
230 let key = Operand::read_le(&mut reader)?;
232 let default = Operand::read_le(&mut reader)?;
234 let destination = Register::read_le(&mut reader)?;
236 Ok(Self { mapping, key, default, destination })
238 }
239}
240
241impl<N: Network> ToBytes for GetOrUse<N> {
242 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
244 self.mapping.write_le(&mut writer)?;
246 self.key.write_le(&mut writer)?;
248 self.default.write_le(&mut writer)?;
250 self.destination.write_le(&mut writer)
252 }
253}
254
255#[cfg(test)]
256mod tests {
257 use super::*;
258 use console::{network::MainnetV0, program::Register};
259
260 type CurrentNetwork = MainnetV0;
261
262 #[test]
263 fn test_parse() {
264 let (string, get_or_use) = GetOrUse::<CurrentNetwork>::parse("get.or_use account[r0] r1 into r2;").unwrap();
265 assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
266 assert_eq!(get_or_use.mapping, CallOperator::from_str("account").unwrap());
267 assert_eq!(get_or_use.operands().len(), 2, "The number of operands is incorrect");
268 assert_eq!(get_or_use.key, Operand::Register(Register::Locator(0)), "The first operand is incorrect");
269 assert_eq!(get_or_use.default, Operand::Register(Register::Locator(1)), "The second operand is incorrect");
270 assert_eq!(get_or_use.destination, Register::Locator(2), "The second operand is incorrect");
271
272 let (string, get_or_use) =
273 GetOrUse::<CurrentNetwork>::parse("get.or_use token.aleo/balances[r0] r1 into r2;").unwrap();
274 assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
275 assert_eq!(get_or_use.mapping, CallOperator::from_str("token.aleo/balances").unwrap());
276 assert_eq!(get_or_use.operands().len(), 2, "The number of operands is incorrect");
277 assert_eq!(get_or_use.key, Operand::Register(Register::Locator(0)), "The first operand is incorrect");
278 assert_eq!(get_or_use.default, Operand::Register(Register::Locator(1)), "The second operand is incorrect");
279 assert_eq!(get_or_use.destination, Register::Locator(2), "The second operand is incorrect");
280 }
281
282 #[test]
283 fn test_from_bytes() {
284 let (string, get_or_use) = GetOrUse::<CurrentNetwork>::parse("get.or_use account[r0] r1 into r2;").unwrap();
285 assert!(string.is_empty());
286 let bytes_le = get_or_use.to_bytes_le().unwrap();
287 let result = GetOrUse::<CurrentNetwork>::from_bytes_le(&bytes_le[..]);
288 assert!(result.is_ok());
289 }
290}