snarkvm_synthesizer_debug/vm/
authorize.rs

1// Copyright (C) 2019-2023 Aleo Systems 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// http://www.apache.org/licenses/LICENSE-2.0
8
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use super::*;
16
17impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
18    /// Authorizes a call to the program function for the given inputs.
19    #[inline]
20    pub fn authorize<R: Rng + CryptoRng>(
21        &self,
22        private_key: &PrivateKey<N>,
23        program_id: impl TryInto<ProgramID<N>>,
24        function_name: impl TryInto<Identifier<N>>,
25        inputs: impl IntoIterator<IntoIter = impl ExactSizeIterator<Item = impl TryInto<Value<N>>>>,
26        rng: &mut R,
27    ) -> Result<Authorization<N>> {
28        let timer = timer!("VM::authorize");
29
30        // Prepare the program ID.
31        let program_id = program_id.try_into().map_err(|_| anyhow!("Invalid program ID"))?;
32        // Prepare the function name.
33        let function_name = function_name.try_into().map_err(|_| anyhow!("Invalid function name"))?;
34        // Prepare the inputs.
35        let inputs = inputs
36            .into_iter()
37            .enumerate()
38            .map(|(index, input)| {
39                input
40                    .try_into()
41                    .map_err(|_| anyhow!("Failed to parse input #{index} for '{program_id}/{function_name}'"))
42            })
43            .collect::<Result<Vec<_>>>()?;
44        lap!(timer, "Prepare inputs");
45
46        // Authorize the call.
47        let result = self.authorize_raw(private_key, program_id, function_name, inputs, rng);
48        finish!(timer, "Authorize the call");
49        result
50    }
51
52    /// Authorizes the fee given the credits record, the fee amount (in microcredits),
53    /// and the deployment or execution ID.
54    #[inline]
55    pub fn authorize_fee_private<R: Rng + CryptoRng>(
56        &self,
57        private_key: &PrivateKey<N>,
58        credits: Record<N, Plaintext<N>>,
59        base_fee_in_microcredits: u64,
60        priority_fee_in_microcredits: u64,
61        deployment_or_execution_id: Field<N>,
62        rng: &mut R,
63    ) -> Result<Authorization<N>> {
64        macro_rules! logic {
65            ($process:expr, $network:path, $aleo:path) => {{
66                // Compute the authorization.
67                let authorization = $process.authorize_fee_private::<$aleo, _>(
68                    cast_ref!(&private_key as PrivateKey<$network>),
69                    cast_ref!(credits as Record<$network, Plaintext<$network>>).clone(),
70                    base_fee_in_microcredits,
71                    priority_fee_in_microcredits,
72                    *cast_ref!(deployment_or_execution_id as Field<$network>),
73                    rng,
74                )?;
75                // Prepare the authorization.
76                Ok(cast_ref!(authorization as Authorization<N>).clone())
77            }};
78        }
79
80        // Compute the authorization.
81        let timer = timer!("VM::authorize_fee_private");
82        let result = process!(self, logic);
83        finish!(timer, "Compute the authorization");
84        result
85    }
86
87    /// Authorizes the fee given the the fee amount (in microcredits) and the deployment or execution ID.
88    #[inline]
89    pub fn authorize_fee_public<R: Rng + CryptoRng>(
90        &self,
91        private_key: &PrivateKey<N>,
92        base_fee_in_microcredits: u64,
93        priority_fee_in_microcredits: u64,
94        deployment_or_execution_id: Field<N>,
95        rng: &mut R,
96    ) -> Result<Authorization<N>> {
97        macro_rules! logic {
98            ($process:expr, $network:path, $aleo:path) => {{
99                // Compute the authorization.
100                let authorization = $process.authorize_fee_public::<$aleo, _>(
101                    cast_ref!(&private_key as PrivateKey<$network>),
102                    base_fee_in_microcredits,
103                    priority_fee_in_microcredits,
104                    *cast_ref!(deployment_or_execution_id as Field<$network>),
105                    rng,
106                )?;
107                // Prepare the authorization.
108                Ok(cast_ref!(authorization as Authorization<N>).clone())
109            }};
110        }
111
112        // Compute the authorization.
113        let timer = timer!("VM::authorize_fee_public");
114        let result = process!(self, logic);
115        finish!(timer, "Compute the authorization");
116        result
117    }
118}
119
120impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
121    /// Authorizes a call to the program function for the given inputs.
122    #[inline]
123    fn authorize_raw<R: Rng + CryptoRng>(
124        &self,
125        private_key: &PrivateKey<N>,
126        program_id: ProgramID<N>,
127        function_name: Identifier<N>,
128        inputs: Vec<Value<N>>,
129        rng: &mut R,
130    ) -> Result<Authorization<N>> {
131        macro_rules! logic {
132            ($process:expr, $network:path, $aleo:path) => {{
133                // Compute the authorization.
134                let authorization = $process.authorize::<$aleo, _>(
135                    cast_ref!(&private_key as PrivateKey<$network>),
136                    cast_ref!(program_id as ProgramID<$network>),
137                    cast_ref!(function_name as Identifier<$network>),
138                    cast_ref!(inputs as Vec<Value<$network>>).iter(),
139                    rng,
140                )?;
141                // Prepare the authorization.
142                Ok(cast_ref!(authorization as Authorization<N>).clone())
143            }};
144        }
145
146        // Compute the authorization.
147        let timer = timer!("VM::authorize_raw");
148        let result = process!(self, logic);
149        finish!(timer, "Compute the authorization");
150        result
151    }
152}