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(&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 // Increment the amendment count while preserving the existing edition.
67 let amendment_count = existing_stack
68 .program_amendment_count()
69 .checked_add(1)
70 .ok_or_else(|| anyhow!("Overflow while incrementing the program amendment count"))?;
71
72 // Compute a new stack with the same program and edition.
73 // Note: `Stack::new` cannot be used here because it would increment the edition.
74 // Amendments must preserve the existing edition. Validity is verified by `initialize_and_check`.
75 let mut stack = Stack::new_raw(self, deployment.program(), *existing_stack.program_edition())?;
76 stack.initialize_and_check(self)?;
77 lap!(timer, "Compute the stack");
78
79 // Set the amendment count for this edition.
80 stack.set_program_amendment_count(amendment_count);
81 // Set the program owner to the existing owner.
82 stack.set_program_owner(*existing_stack.program_owner());
83
84 stack
85 }
86 };
87
88 // Insert all verifying keys (unified: functions + records).
89 for (name, (verifying_key, _)) in deployment.verifying_keys() {
90 stack.insert_verifying_key(name, verifying_key.clone())?;
91 }
92 lap!(timer, "Insert the verifying keys");
93
94 // Add the stack to the process.
95 self.lock().add_stack(stack);
96
97 finish!(timer);
98
99 Ok(())
100 }
101}