Skip to main content

snarkvm_ledger_block/transactions/rejected/
mod.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
16mod bytes;
17mod serialize;
18mod string;
19
20use super::*;
21
22use crate::{Deployment, Execution, Fee};
23
24/// A wrapper around the rejected deployment or execution.
25#[derive(Clone, PartialEq, Eq)]
26pub enum Rejected<N: Network> {
27    Deployment(ProgramOwner<N>, Box<Deployment<N>>),
28    Execution(Box<Execution<N>>),
29}
30
31impl<N: Network> Rejected<N> {
32    /// Initializes a rejected deployment.
33    pub fn new_deployment(program_owner: ProgramOwner<N>, deployment: Deployment<N>) -> Self {
34        Self::Deployment(program_owner, Box::new(deployment))
35    }
36
37    /// Initializes a rejected execution.
38    pub fn new_execution(execution: Execution<N>) -> Self {
39        Self::Execution(Box::new(execution))
40    }
41
42    /// Returns true if the rejected transaction is a deployment.
43    pub fn is_deployment(&self) -> bool {
44        matches!(self, Self::Deployment(..))
45    }
46
47    /// Returns true if the rejected transaction is an execution.
48    pub fn is_execution(&self) -> bool {
49        matches!(self, Self::Execution(..))
50    }
51
52    /// Returns the program owner of the rejected deployment.
53    pub fn program_owner(&self) -> Option<&ProgramOwner<N>> {
54        match self {
55            Self::Deployment(program_owner, ..) => Some(program_owner),
56            Self::Execution(..) => None,
57        }
58    }
59
60    /// Returns the rejected deployment.
61    pub fn deployment(&self) -> Option<&Deployment<N>> {
62        match self {
63            Self::Deployment(_, deployment) => Some(deployment),
64            Self::Execution(..) => None,
65        }
66    }
67
68    /// Returns the rejected execution.
69    pub fn execution(&self) -> Option<&Execution<N>> {
70        match self {
71            Self::Deployment(..) => None,
72            Self::Execution(execution) => Some(execution),
73        }
74    }
75
76    /// Returns the rejected ID.
77    pub fn to_id(&self) -> Result<Field<N>> {
78        match self {
79            Self::Deployment(_, deployment) => deployment.to_deployment_id(),
80            Self::Execution(execution) => execution.to_execution_id(),
81        }
82    }
83
84    /// Returns the unconfirmed transaction ID, which is defined as the transaction ID prior to confirmation.
85    /// When a transaction is rejected, its fee transition is used to construct the confirmed transaction ID,
86    /// changing the original transaction ID.
87    pub fn to_unconfirmed_id(&self, fee: &Option<Fee<N>>) -> Result<Field<N>> {
88        // Compute the deployment or execution tree.
89        let tree = match self {
90            Self::Deployment(_, deployment) => Transaction::deployment_tree(deployment)?,
91            Self::Execution(execution) => Transaction::execution_tree(execution)?,
92        };
93        // Construct the transaction tree and return the unconfirmed transaction ID.
94        Ok(*Transaction::transaction_tree(tree, fee.as_ref())?.root())
95    }
96}
97
98#[cfg(test)]
99pub mod test_helpers {
100    use super::*;
101    use console::{account::PrivateKey, network::MainnetV0};
102
103    type CurrentNetwork = MainnetV0;
104
105    /// Samples a rejected deployment.
106    pub(crate) fn sample_rejected_deployment(
107        version: u8,
108        edition: u16,
109        has_translation_keys: bool,
110        is_fee_private: bool,
111        rng: &mut TestRng,
112    ) -> Rejected<CurrentNetwork> {
113        // Sample a deploy transaction.
114        let deployment = match crate::transaction::test_helpers::sample_deployment_transaction(
115            version,
116            edition,
117            has_translation_keys,
118            is_fee_private,
119            rng,
120        ) {
121            Transaction::Deploy(_, _, _, deployment, _) => (*deployment).clone(),
122            _ => unreachable!(),
123        };
124
125        // Sample a new program owner.
126        let private_key = PrivateKey::new(rng).unwrap();
127        let deployment_id = deployment.to_deployment_id().unwrap();
128        let program_owner = ProgramOwner::new(&private_key, deployment_id, rng).unwrap();
129
130        // Return the rejected deployment.
131        Rejected::new_deployment(program_owner, deployment)
132    }
133
134    /// Samples a rejected execution.
135    pub(crate) fn sample_rejected_execution(is_fee_private: bool, rng: &mut TestRng) -> Rejected<CurrentNetwork> {
136        // Sample an execute transaction.
137        let execution =
138            match crate::transaction::test_helpers::sample_execution_transaction_with_fee(is_fee_private, rng, 0) {
139                Transaction::Execute(_, _, execution, _) => execution,
140                _ => unreachable!(),
141            };
142
143        // Return the rejected execution.
144        Rejected::new_execution(*execution)
145    }
146
147    /// Sample a list of randomly rejected transactions.
148    pub(crate) fn sample_rejected_transactions() -> Vec<Rejected<CurrentNetwork>> {
149        let rng = &mut TestRng::default();
150
151        let mut txs = Vec::new();
152
153        // Sample the deployments.
154        for version in 1..=2 {
155            for edition in 0..=1 {
156                for has_translation_keys in [true, false] {
157                    // version 1 didn't support translation keys
158                    if version == 1 && has_translation_keys {
159                        continue;
160                    }
161                    // version 2 expects edition 1
162                    if version == 2 && edition == 0 {
163                        continue;
164                    }
165                    for is_fee_private in [true, false] {
166                        let tx =
167                            sample_rejected_deployment(version, edition, has_translation_keys, is_fee_private, rng);
168                        txs.push(tx);
169                    }
170                }
171            }
172        }
173
174        // Sample the executions.
175        for is_fee_private in [true, false] {
176            let tx = sample_rejected_execution(is_fee_private, rng);
177            txs.push(tx);
178        }
179
180        txs
181    }
182}