snarkvm_synthesizer_process/verify_deployment.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::*;
17
18impl<N: Network> Process<N> {
19 /// Verifies the given deployment is ordered.
20 #[inline]
21 pub fn verify_deployment<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
22 &self,
23 consensus_version: ConsensusVersion,
24 deployment: &Deployment<N>,
25 rng: &mut R,
26 ) -> Result<()> {
27 let timer = timer!("Process::verify_deployment");
28
29 // Retrieve the program ID.
30 let program_id = deployment.program().id();
31 // Check if this deployment is an amendment.
32 let version = deployment.version()?;
33 let is_amendment = matches!(version, DeploymentVersion::V3);
34 // If the deployment is an amendment, verify that the program exists.
35 // If the edition is zero (and not an amendment), verify that the program does not exist.
36 // Otherwise, verify that the program exists.
37 if is_amendment {
38 ensure!(
39 self.contains_program(program_id),
40 "Program '{program_id}' does not exist, but amendment requires an existing program"
41 );
42 } else {
43 match deployment.edition().is_zero() {
44 true => ensure!(
45 !self.contains_program(program_id),
46 "Program '{program_id}' already exists, but the deployment edition is zero"
47 ),
48 false => ensure!(
49 self.contains_program(program_id),
50 "Program '{program_id}' does not exist, but the deployment edition is non-zero"
51 ),
52 }
53 }
54
55 // Ensure the program is well-formed, by computing the stack.
56 // Note: The program owner is intentionally not set, since `program_owner` is an operand
57 // that is only available in a finalize scope.
58 let stack = if is_amendment {
59 // For amendments, use the existing edition instead of incrementing.
60 // Note: `Stack::new` cannot be used here because it would increment the edition.
61 // Amendments must preserve the existing edition. Validity is verified by `initialize_and_check`.
62 let existing_stack = self.get_stack(program_id)?;
63 let stack = Stack::new_raw(self, deployment.program(), *existing_stack.program_edition())?;
64 stack.initialize_and_check(self)?;
65 stack
66 } else {
67 Stack::new(self, deployment.program())?
68 };
69 lap!(timer, "Compute the stack");
70
71 // Ensure the verifying keys are well-formed and the certificates are valid.
72 let verification = stack.verify_deployment::<A, R>(consensus_version, deployment, rng);
73 lap!(timer, "Verify the deployment");
74
75 finish!(timer);
76 verification
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 type CurrentAleo = circuit::network::AleoV0;
85
86 /// Use `cargo test profiler --features timer` to run this test.
87 #[ignore]
88 #[test]
89 fn test_profiler() -> Result<()> {
90 let rng = &mut TestRng::default();
91
92 // Initialize the process.
93 let process = Process::load()?;
94
95 // Fetch the large program to deploy.
96 let large_program = Program::from_str(include_str!("./resources/large_functions.aleo"))?;
97
98 // Create a deployment for the program.
99 let deployment = process.deploy::<CurrentAleo, _>(&large_program, rng)?;
100
101 // Verify the deployment.
102 assert!(process.verify_deployment::<CurrentAleo, _>(ConsensusVersion::V8, &deployment, rng).is_ok());
103
104 bail!("\n\nRemember to #[ignore] this test!\n\n")
105 }
106}