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();
let mut txs = Vec::new();
for version in 1..=2 {
for edition in 0..=1 {
for has_translation_keys in [true, false] {
if version == 1 && has_translation_keys {
continue;
}
if version == 2 && edition == 0 {
continue;
}
for is_fee_private in [true, false] {
let tx =
sample_rejected_deployment(version, edition, has_translation_keys, is_fee_private, rng);
txs.push(tx);
}
}
}
}
for is_fee_private in [true, false] {
let tx = sample_rejected_execution(is_fee_private, rng);
txs.push(tx);
}
txs
}
}