Skip to main content

snarkvm_synthesizer_program/logic/command/get/
standard.rs

1// Copyright (c) 2019-2026 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use crate::{CallOperator, FinalizeStoreTrait, Opcode, Operand, RegistersTrait, StackTrait};
17use console::{
18    network::prelude::*,
19    program::{Register, Value},
20};
21
22/// A get command, e.g. `get accounts[r0] into r1;`.
23/// Gets the value stored at `operand` in `mapping` and stores the result in `destination`.
24#[derive(Clone, PartialEq, Eq, Hash)]
25pub struct Get<N: Network> {
26    /// The mapping.
27    mapping: CallOperator<N>,
28    /// The operands.
29    operands: [Operand<N>; 1],
30    /// The destination register.
31    destination: Register<N>,
32}
33
34impl<N: Network> Get<N> {
35    /// Returns the opcode.
36    #[inline]
37    pub const fn opcode() -> Opcode {
38        Opcode::Command("get")
39    }
40
41    /// Returns the operands in the operation.
42    #[inline]
43    pub fn operands(&self) -> &[Operand<N>] {
44        &self.operands
45    }
46
47    /// Returns the mapping.
48    #[inline]
49    pub const fn mapping(&self) -> &CallOperator<N> {
50        &self.mapping
51    }
52
53    /// Returns the operand containing the key.
54    #[inline]
55    pub const fn key(&self) -> &Operand<N> {
56        &self.operands[0]
57    }
58
59    /// Returns the destination register.
60    #[inline]
61    pub const fn destination(&self) -> &Register<N> {
62        &self.destination
63    }
64
65    /// Returns whether this command refers to an external struct.
66    #[inline]
67    pub fn contains_external_struct(&self) -> bool {
68        false
69    }
70}
71
72impl<N: Network> Get<N> {
73    /// Finalizes the command.
74    #[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        // Determine the program ID and mapping name.
82        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        // Ensure the mapping exists.
88        if !store.contains_mapping_speculative(&program_id, &mapping_name)? {
89            bail!("Mapping '{program_id}/{mapping_name}' does not exist");
90        }
91
92        // Load the operand as a plaintext.
93        let key = registers.load_plaintext(stack, self.key())?;
94
95        // Retrieve the value from storage as a literal.
96        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            // If a key does not exist, then bail.
103            None => bail!("Key '{key}' does not exist in mapping '{program_id}/{mapping_name}'"),
104        };
105
106        // Assign the value to the destination register.
107        registers.store(stack, &self.destination, value)?;
108
109        Ok(())
110    }
111}
112
113impl<N: Network> Parser for Get<N> {
114    /// Parses a string into an operation.
115    #[inline]
116    fn parse(string: &str) -> ParserResult<Self> {
117        // Parse the whitespace and comments from the string.
118        let (string, _) = Sanitizer::parse(string)?;
119        // Parse the opcode from the string.
120        let (string, _) = tag(*Self::opcode())(string)?;
121        // Parse the whitespace from the string.
122        let (string, _) = Sanitizer::parse_whitespaces(string)?;
123
124        // Parse the mapping name from the string.
125        let (string, mapping) = CallOperator::parse(string)?;
126        // Parse the "[" from the string.
127        let (string, _) = tag("[")(string)?;
128        // Parse the whitespace from the string.
129        let (string, _) = Sanitizer::parse_whitespaces(string)?;
130        // Parse the key operand from the string.
131        let (string, key) = Operand::parse(string)?;
132        // Parse the whitespace from the string.
133        let (string, _) = Sanitizer::parse_whitespaces(string)?;
134        // Parse the "]" from the string.
135        let (string, _) = tag("]")(string)?;
136
137        // Parse the whitespace from the string.
138        let (string, _) = Sanitizer::parse_whitespaces(string)?;
139        // Parse the "into" keyword from the string.
140        let (string, _) = tag("into")(string)?;
141        // Parse the whitespace from the string.
142        let (string, _) = Sanitizer::parse_whitespaces(string)?;
143        // Parse the destination register from the string.
144        let (string, destination) = Register::parse(string)?;
145
146        // Parse the whitespace from the string.
147        let (string, _) = Sanitizer::parse_whitespaces(string)?;
148        // Parse the ";" from the string.
149        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    /// Parses a string into the command.
159    #[inline]
160    fn from_str(string: &str) -> Result<Self> {
161        match Self::parse(string) {
162            Ok((remainder, object)) => {
163                // Ensure the remainder is empty.
164                ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
165                // Return the object.
166                Ok(object)
167            }
168            Err(error) => bail!("Failed to parse string. {error}"),
169        }
170    }
171}
172
173impl<N: Network> Debug for Get<N> {
174    /// Prints the command as a string.
175    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
176        Display::fmt(self, f)
177    }
178}
179
180impl<N: Network> Display for Get<N> {
181    /// Prints the command to a string.
182    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
183        // Print the command.
184        write!(f, "{} ", Self::opcode())?;
185        // Print the mapping and key operand.
186        write!(f, "{}[{}] into ", self.mapping, self.key())?;
187        // Print the destination register.
188        write!(f, "{};", self.destination)
189    }
190}
191
192impl<N: Network> FromBytes for Get<N> {
193    /// Reads the command from a buffer.
194    fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
195        // Read the mapping name.
196        let mapping = CallOperator::read_le(&mut reader)?;
197        // Read the key operand.
198        let key = Operand::read_le(&mut reader)?;
199        // Read the destination register.
200        let destination = Register::read_le(&mut reader)?;
201        // Return the command.
202        Ok(Self { mapping, operands: [key], destination })
203    }
204}
205
206impl<N: Network> ToBytes for Get<N> {
207    /// Writes the command to a buffer.
208    fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
209        // Write the mapping name.
210        self.mapping.write_le(&mut writer)?;
211        // Write the key operand.
212        self.key().write_le(&mut writer)?;
213        // Write the destination register.
214        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}