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 is_fee_private: bool,
110 rng: &mut TestRng,
111 ) -> Rejected<CurrentNetwork> {
112 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 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 Rejected::new_deployment(program_owner, deployment)
130 }
131
132 pub(crate) fn sample_rejected_execution(is_fee_private: bool, rng: &mut TestRng) -> Rejected<CurrentNetwork> {
134 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 Rejected::new_execution(*execution)
143 }
144
145 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}