Skip to main content

snarkvm_synthesizer_program/logic/command/
set.rs

1// Copyright (c) 2019-2025 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::{FinalizeOperation, FinalizeStoreTrait, Opcode, Operand, RegistersTrait, StackTrait};
17use console::{
18    network::prelude::*,
19    program::{Identifier, Value},
20};
21
22/// A set command, e.g. `set r1 into mapping[r0];`
23/// Sets the `key` entry as `value` in `mapping`.
24#[derive(Clone, PartialEq, Eq, Hash)]
25pub struct Set<N: Network> {
26    /// The mapping name.
27    mapping: Identifier<N>,
28    /// The operands.
29    operands: [Operand<N>; 2],
30}
31
32impl<N: Network> Set<N> {
33    /// Returns the opcode.
34    #[inline]
35    pub const fn opcode() -> Opcode {
36        Opcode::Command("set")
37    }
38
39    /// Returns the operands in the operation.
40    #[inline]
41    pub fn operands(&self) -> &[Operand<N>] {
42        &self.operands
43    }
44
45    /// Returns the mapping name.
46    #[inline]
47    pub const fn mapping_name(&self) -> &Identifier<N> {
48        &self.mapping
49    }
50
51    /// Returns the operand containing the key.
52    #[inline]
53    pub const fn key(&self) -> &Operand<N> {
54        &self.operands[0]
55    }
56
57    /// Returns the operand containing the value.
58    #[inline]
59    pub const fn value(&self) -> &Operand<N> {
60        &self.operands[1]
61    }
62
63    /// Returns whether this command refers to an external struct.
64    #[inline]
65    pub fn contains_external_struct(&self) -> bool {
66        false
67    }
68}
69
70impl<N: Network> Set<N> {
71    /// Finalizes the command.
72    pub fn finalize(
73        &self,
74        stack: &impl StackTrait<N>,
75        store: &impl FinalizeStoreTrait<N>,
76        registers: &mut impl RegistersTrait<N>,
77    ) -> Result<FinalizeOperation<N>> {
78        // Ensure the mapping exists.
79        if !store.contains_mapping_speculative(stack.program_id(), &self.mapping)? {
80            bail!("Mapping '{}/{}' does not exist", stack.program_id(), self.mapping);
81        }
82
83        // Load the key operand as a plaintext.
84        let key = registers.load_plaintext(stack, self.key())?;
85        // Load the value operand as a plaintext.
86        let value = Value::Plaintext(registers.load_plaintext(stack, self.value())?);
87
88        // Update the value in storage, and return the finalize operation.
89        store.update_key_value(*stack.program_id(), self.mapping, key, value)
90    }
91}
92
93impl<N: Network> Parser for Set<N> {
94    /// Parses a string into an operation.
95    fn parse(string: &str) -> ParserResult<Self> {
96        // Parse the whitespace and comments from the string.
97        let (string, _) = Sanitizer::parse(string)?;
98        // Parse the opcode from the string.
99        let (string, _) = tag(*Self::opcode())(string)?;
100        // Parse the whitespace from the string.
101        let (string, _) = Sanitizer::parse_whitespaces(string)?;
102
103        // Parse the value operand from the string.
104        let (string, value) = Operand::parse(string)?;
105        // Parse the whitespace from the string.
106        let (string, _) = Sanitizer::parse_whitespaces(string)?;
107
108        // Parse the "into" keyword from the string.
109        let (string, _) = tag("into")(string)?;
110        // Parse the whitespace from the string.
111        let (string, _) = Sanitizer::parse_whitespaces(string)?;
112
113        // Parse the mapping name from the string.
114        let (string, mapping) = Identifier::parse(string)?;
115        // Parse the "[" from the string.
116        let (string, _) = tag("[")(string)?;
117        // Parse the whitespace from the string.
118        let (string, _) = Sanitizer::parse_whitespaces(string)?;
119        // Parse the key operand from the string.
120        let (string, key) = Operand::parse(string)?;
121        // Parse the whitespace from the string.
122        let (string, _) = Sanitizer::parse_whitespaces(string)?;
123        // Parse the "]" from the string.
124        let (string, _) = tag("]")(string)?;
125        // Parse the whitespace from the string.
126        let (string, _) = Sanitizer::parse_whitespaces(string)?;
127        // Parse the ";" from the string.
128        let (string, _) = tag(";")(string)?;
129
130        Ok((string, Self { mapping, operands: [key, value] }))
131    }
132}
133
134impl<N: Network> FromStr for Set<N> {
135    type Err = Error;
136
137    /// Parses a string into the command.
138    #[inline]
139    fn from_str(string: &str) -> Result<Self> {
140        match Self::parse(string) {
141            Ok((remainder, object)) => {
142                // Ensure the remainder is empty.
143                ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
144                // Return the object.
145                Ok(object)
146            }
147            Err(error) => bail!("Failed to parse string. {error}"),
148        }
149    }
150}
151
152impl<N: Network> Debug for Set<N> {
153    /// Prints the command as a string.
154    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
155        Display::fmt(self, f)
156    }
157}
158
159impl<N: Network> Display for Set<N> {
160    /// Prints the command to a string.
161    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
162        // Print the command.
163        write!(f, "{} ", Self::opcode())?;
164        // Print the value operand.
165        write!(f, "{} into ", self.value())?;
166        // Print the mapping and key operand.
167        write!(f, "{}[{}];", self.mapping, self.key())
168    }
169}
170
171impl<N: Network> FromBytes for Set<N> {
172    /// Reads the command from a buffer.
173    fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
174        // Read the mapping name.
175        let mapping = Identifier::read_le(&mut reader)?;
176        // Read the key operand.
177        let key = Operand::read_le(&mut reader)?;
178        // Read the value operand.
179        let value = Operand::read_le(&mut reader)?;
180        // Return the command.
181        Ok(Self { mapping, operands: [key, value] })
182    }
183}
184
185impl<N: Network> ToBytes for Set<N> {
186    /// Writes the operation to a buffer.
187    fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
188        // Write the mapping name.
189        self.mapping.write_le(&mut writer)?;
190        // Write the key operand.
191        self.key().write_le(&mut writer)?;
192        // Write the value operand.
193        self.value().write_le(&mut writer)
194    }
195}
196
197#[cfg(test)]
198mod tests {
199    use super::*;
200    use console::{network::MainnetV0, program::Register};
201
202    type CurrentNetwork = MainnetV0;
203
204    #[test]
205    fn test_parse() {
206        let (string, set) = Set::<CurrentNetwork>::parse("set r0 into account[r1];").unwrap();
207        assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
208        assert_eq!(set.mapping, Identifier::from_str("account").unwrap());
209        assert_eq!(set.operands().len(), 2, "The number of operands is incorrect");
210        assert_eq!(set.value(), &Operand::Register(Register::Locator(0)), "The first operand is incorrect");
211        assert_eq!(set.key(), &Operand::Register(Register::Locator(1)), "The second operand is incorrect");
212    }
213}