mod bytes;
mod serialize;
mod string;
use super::*;
use crate::{Deployment, Execution, Fee};
#[derive(Clone, PartialEq, Eq)]
pub enum Rejected<N: Network> {
Deployment(ProgramOwner<N>, Box<Deployment<N>>),
Execution(Box<Execution<N>>),
}
impl<N: Network> Rejected<N> {
pub fn new_deployment(program_owner: ProgramOwner<N>, deployment: Deployment<N>) -> Self {
Self::Deployment(program_owner, Box::new(deployment))
}
pub fn new_execution(execution: Execution<N>) -> Self {
Self::Execution(Box::new(execution))
}
pub fn is_deployment(&self) -> bool {
matches!(self, Self::Deployment(..))
}
pub fn is_execution(&self) -> bool {
matches!(self, Self::Execution(..))
}
pub fn program_owner(&self) -> Option<&ProgramOwner<N>> {
match self {
Self::Deployment(program_owner, _) => Some(program_owner),
Self::Execution(_) => None,
}
}
pub fn deployment(&self) -> Option<&Deployment<N>> {
match self {
Self::Deployment(_, deployment) => Some(deployment),
Self::Execution(_) => None,
}
}
pub fn execution(&self) -> Option<&Execution<N>> {
match self {
Self::Deployment(_, _) => None,
Self::Execution(execution) => Some(execution),
}
}
pub fn to_id(&self) -> Result<Field<N>> {
match self {
Self::Deployment(_, deployment) => deployment.to_deployment_id(),
Self::Execution(execution) => execution.to_execution_id(),
}
}
pub fn to_unconfirmed_id(&self, fee: &Option<Fee<N>>) -> Result<Field<N>> {
let tree = match self {
Self::Deployment(_, deployment) => Transaction::deployment_tree(deployment)?,
Self::Execution(execution) => Transaction::execution_tree(execution)?,
};
Ok(*Transaction::transaction_tree(tree, fee.as_ref())?.root())
}
}
#[cfg(test)]
pub mod test_helpers {
use super::*;
use console::{account::PrivateKey, network::MainnetV0};
type CurrentNetwork = MainnetV0;
pub(crate) fn sample_rejected_deployment(
version: u8,
edition: u16,
has_translation_keys: bool,
is_fee_private: bool,
rng: &mut TestRng,
) -> Rejected<CurrentNetwork> {
let deployment = match crate::transaction::test_helpers::sample_deployment_transaction(
version,
edition,
has_translation_keys,
is_fee_private,
rng,
) {
Transaction::Deploy(_, _, _, deployment, _) => (*deployment).clone(),
_ => unreachable!(),
};
let private_key = PrivateKey::new(rng).unwrap();
let deployment_id = deployment.to_deployment_id().unwrap();
let program_owner = ProgramOwner::new(&private_key, deployment_id, rng).unwrap();
Rejected::new_deployment(program_owner, deployment)
}
pub(crate) fn sample_rejected_execution(is_fee_private: bool, rng: &mut TestRng) -> Rejected<CurrentNetwork> {
let execution =
match crate::transaction::test_helpers::sample_execution_transaction_with_fee(is_fee_private, rng, 0) {
Transaction::Execute(_, _, execution, _) => execution,
_ => unreachable!(),
};
Rejected::new_execution(*execution)
}
pub(crate) fn sample_rejected_transactions() -> Vec<Rejected<CurrentNetwork>> {
let rng = &mut TestRng::default();
vec![
sample_rejected_deployment(1, 0, false, false, rng),
sample_rejected_deployment(1, 0, false, true, rng),
sample_rejected_deployment(2, 0, false, false, rng),
sample_rejected_deployment(2, 0, false, true, rng),
sample_rejected_deployment(1, 1, false, false, rng),
sample_rejected_deployment(1, 1, false, true, rng),
sample_rejected_deployment(2, 1, true, false, rng),
sample_rejected_deployment(2, 1, true, true, rng),
sample_rejected_execution(true, rng),
sample_rejected_execution(false, rng),
]
}
}