Skip to main content

snarkvm_ledger_block/transactions/rejected/
mod.rs

1// Copyright (c) 2019-2025 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        is_fee_private: bool,
110        rng: &mut TestRng,
111    ) -> Rejected<CurrentNetwork> {
112        // Sample a deploy transaction.
113        let deployment = match crate::transaction::test_helpers::sample_deployment_transaction(
114            version,
115            edition,
116            is_fee_private,
117            rng,
118        ) {
119            Transaction::Deploy(_, _, _, deployment, _) => (*deployment).clone(),
120            _ => unreachable!(),
121        };
122
123        // Sample a new program owner.
124        let private_key = PrivateKey::new(rng).unwrap();
125        let deployment_id = deployment.to_deployment_id().unwrap();
126        let program_owner = ProgramOwner::new(&private_key, deployment_id, rng).unwrap();
127
128        // Return the rejected deployment.
129        Rejected::new_deployment(program_owner, deployment)
130    }
131
132    /// Samples a rejected execution.
133    pub(crate) fn sample_rejected_execution(is_fee_private: bool, rng: &mut TestRng) -> Rejected<CurrentNetwork> {
134        // Sample an execute transaction.
135        let execution =
136            match crate::transaction::test_helpers::sample_execution_transaction_with_fee(is_fee_private, rng, 0) {
137                Transaction::Execute(_, _, execution, _) => execution,
138                _ => unreachable!(),
139            };
140
141        // Return the rejected execution.
142        Rejected::new_execution(*execution)
143    }
144
145    /// Sample a list of randomly rejected transactions.
146    pub(crate) fn sample_rejected_transactions() -> Vec<Rejected<CurrentNetwork>> {
147        let rng = &mut TestRng::default();
148
149        vec![
150            sample_rejected_deployment(1, 0, true, rng),
151            sample_rejected_deployment(1, 0, false, rng),
152            sample_rejected_deployment(2, 0, true, rng),
153            sample_rejected_deployment(2, 0, false, rng),
154            sample_rejected_deployment(1, 1, true, rng),
155            sample_rejected_deployment(1, 1, false, rng),
156            sample_rejected_deployment(2, 1, true, rng),
157            sample_rejected_deployment(2, 1, false, rng),
158            sample_rejected_execution(true, rng),
159            sample_rejected_execution(false, rng),
160        ]
161    }
162}