Skip to main content

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