snarkvm_ledger_block/transactions/rejected/
mod.rs1mod bytes;
17mod serialize;
18mod string;
19
20use super::*;
21
22use crate::{Deployment, Execution, Fee};
23
24#[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 pub fn new_deployment(program_owner: ProgramOwner<N>, deployment: Deployment<N>) -> Self {
34 Self::Deployment(program_owner, Box::new(deployment))
35 }
36
37 pub fn new_execution(execution: Execution<N>) -> Self {
39 Self::Execution(Box::new(execution))
40 }
41
42 pub fn is_deployment(&self) -> bool {
44 matches!(self, Self::Deployment(..))
45 }
46
47 pub fn is_execution(&self) -> bool {
49 matches!(self, Self::Execution(..))
50 }
51
52 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 pub fn deployment(&self) -> Option<&Deployment<N>> {
62 match self {
63 Self::Deployment(_, deployment) => Some(deployment),
64 Self::Execution(..) => None,
65 }
66 }
67
68 pub fn execution(&self) -> Option<&Execution<N>> {
70 match self {
71 Self::Deployment(..) => None,
72 Self::Execution(execution) => Some(execution),
73 }
74 }
75
76 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 pub fn to_unconfirmed_id(&self, fee: &Option<Fee<N>>) -> Result<Field<N>> {
88 let tree = match self {
90 Self::Deployment(_, deployment) => Transaction::deployment_tree(deployment)?,
91 Self::Execution(execution) => Transaction::execution_tree(execution)?,
92 };
93 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 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 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 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 Rejected::new_deployment(program_owner, deployment)
132 }
133
134 pub(crate) fn sample_rejected_execution(is_fee_private: bool, rng: &mut TestRng) -> Rejected<CurrentNetwork> {
136 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 Rejected::new_execution(*execution)
145 }
146
147 pub(crate) fn sample_rejected_transactions() -> Vec<Rejected<CurrentNetwork>> {
149 let rng = &mut TestRng::default();
150
151 let mut txs = Vec::new();
152
153 for version in 1..=2 {
155 for edition in 0..=1 {
156 for has_translation_keys in [true, false] {
157 if version == 1 && has_translation_keys {
159 continue;
160 }
161 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 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}