Skip to main content

snarkvm_synthesizer_process/
deploy.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 super::*;
17use snarkvm_synthesizer_error::*;
18
19impl<N: Network> Process<N> {
20    /// Deploys the given program ID, if it does not exist.
21    #[inline]
22    pub fn deploy<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
23        &self,
24        program: &Program<N>,
25        rng: &mut R,
26    ) -> Result<Deployment<N>, ProcessDeployError> {
27        let timer = timer!("Process::deploy");
28
29        // Compute the stack.
30        let stack = Stack::new(self, program)?;
31        lap!(timer, "Compute the stack");
32
33        // Return the deployment.
34        let deployment = stack.deploy::<A, R>(rng)?;
35        lap!(timer, "Construct the deployment");
36
37        finish!(timer);
38
39        Ok(deployment)
40    }
41
42    /// Adds the newly-deployed program.
43    /// This method assumes the given deployment **is valid**.
44    #[inline]
45    pub fn load_deployment(&mut self, deployment: &Deployment<N>) -> Result<()> {
46        let timer = timer!("Process::load_deployment");
47
48        // Get the deployment version.
49        let version = deployment.version()?;
50
51        // Load the deployment based on its version.
52        let stack = match version {
53            DeploymentVersion::V1 | DeploymentVersion::V2 => {
54                // Compute the program stack.
55                let mut stack = Stack::new(self, deployment.program())?;
56                lap!(timer, "Compute the stack");
57
58                // Set the program owner.
59                stack.set_program_owner(deployment.program_owner());
60
61                stack
62            }
63            DeploymentVersion::V3 => {
64                // V3 is an amendment — get the existing stack.
65                let existing_stack = self.get_stack(deployment.program_id())?;
66
67                // Compute a new stack with the same program and edition.
68                // Note: `Stack::new` cannot be used here because it would increment the edition.
69                // Amendments must preserve the existing edition. Validity is verified by `initialize_and_check`.
70                let mut stack = Stack::new_raw(self, deployment.program(), *existing_stack.program_edition())?;
71                stack.initialize_and_check(self)?;
72                lap!(timer, "Compute the stack");
73
74                // Set the program owner to the existing owner.
75                stack.set_program_owner(*existing_stack.program_owner());
76
77                stack
78            }
79        };
80
81        // Insert all verifying keys (unified: functions + records).
82        for (name, (verifying_key, _)) in deployment.verifying_keys() {
83            stack.insert_verifying_key(name, verifying_key.clone())?;
84        }
85        lap!(timer, "Insert the verifying keys");
86
87        // Add the stack to the process.
88        self.add_stack(stack);
89
90        finish!(timer);
91
92        Ok(())
93    }
94}