use super::*;
use crate::bls_setting::BlsSetting;
use crate::case_result::compare_beacon_state_results_without_caches;
use crate::decode::{ssz_decode_file, ssz_decode_file_with, ssz_decode_state, yaml_decode_file};
use serde::Deserialize;
use ssz::Decode;
use state_processing::common::update_progressive_balances_cache::initialize_progressive_balances_cache;
use state_processing::envelope_processing::verify_execution_payload_envelope;
use state_processing::epoch_cache::initialize_epoch_cache;
use state_processing::per_block_processing::process_operations::{
    process_consolidation_requests, process_deposit_requests_post_gloas,
    process_deposit_requests_pre_gloas, process_withdrawal_requests,
};
use state_processing::{
    ConsensusContext,
    envelope_processing::EnvelopeProcessingError,
    per_block_processing::{
        VerifyBlockRoot, VerifySignatures,
        errors::BlockProcessingError,
        process_block_header, process_execution_payload, process_execution_payload_bid,
        process_operations::{
            altair_deneb, base, gloas, process_attester_slashings,
            process_bls_to_execution_changes, process_deposits, process_exits,
            process_payload_attestation, process_proposer_slashings,
        },
        process_parent_execution_payload, process_sync_aggregate, withdrawals,
    },
};
use std::fmt::Debug;
use types::{
    Attestation, AttesterSlashing, BeaconBlock, BeaconBlockBody, BeaconBlockBodyBellatrix,
    BeaconBlockBodyCapella, BeaconBlockBodyDeneb, BeaconBlockBodyElectra, BeaconBlockBodyFulu,
    BeaconState, BlindedPayload, ConsolidationRequest, Deposit, DepositRequest, ExecutionPayload,
    ForkVersionDecode, FullPayload, PayloadAttestation, ProposerSlashing,
    SignedBlsToExecutionChange, SignedExecutionPayloadEnvelope, SignedVoluntaryExit, SyncAggregate,
    WithdrawalRequest,
};

#[derive(Debug, Clone, Default, Deserialize)]
struct Metadata {
    description: Option<String>,
    bls_setting: Option<BlsSetting>,
}

#[derive(Debug, Clone, Deserialize)]
struct ExecutionMetadata {
    execution_valid: bool,
}

/// Newtype for testing withdrawals.
#[derive(Debug, Clone, Deserialize)]
pub struct WithdrawalsPayload<E: EthSpec> {
    payload: Option<ExecutionPayload<E>>,
}

/// Newtype for testing voluntary exit churn (Gloas+).
///
/// The test case applies the same `process_voluntary_exit` operation as the regular
/// `voluntary_exit` test, but under the `voluntary_exit_churn` handler directory.
#[derive(Debug, Clone)]
pub struct VoluntaryExitChurn {
    exit: SignedVoluntaryExit,
}

/// Newtype for testing execution payload bids.
#[derive(Debug, Clone, Deserialize)]
pub struct ExecutionPayloadBidBlock<E: EthSpec> {
    block: BeaconBlock<E>,
}

/// Newtype for testing parent execution payload processing.
#[derive(Debug, Clone, Deserialize)]
pub struct ParentExecutionPayloadBlock<E: EthSpec> {
    block: BeaconBlock<E>,
}

#[derive(Debug, Clone)]
pub struct Operations<E: EthSpec, O: Operation<E>> {
    metadata: Metadata,
    execution_metadata: Option<ExecutionMetadata>,
    pub pre: BeaconState<E>,
    pub operation: Option<O>,
    pub post: Option<BeaconState<E>>,
}

pub trait Operation<E: EthSpec>: Debug + Sync + Sized {
    type Error: Debug;

    fn handler_name() -> String;

    fn filename() -> String {
        format!("{}.ssz_snappy", Self::handler_name())
    }

    fn is_enabled_for_fork(_fork_name: ForkName) -> bool {
        true
    }

    fn decode(path: &Path, fork_name: ForkName, spec: &ChainSpec) -> Result<Self, Error>;

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        _: &Operations<E, Self>,
    ) -> Result<(), Self::Error>;
}

impl<E: EthSpec> Operation<E> for Attestation<E> {
    type Error = BlockProcessingError;

    fn handler_name() -> String {
        "attestation".into()
    }

    fn decode(path: &Path, fork_name: ForkName, _spec: &ChainSpec) -> Result<Self, Error> {
        if fork_name < ForkName::Electra {
            Ok(Self::Base(ssz_decode_file(path)?))
        } else {
            Ok(Self::Electra(ssz_decode_file(path)?))
        }
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        _: &Operations<E, Self>,
    ) -> Result<(), BlockProcessingError> {
        initialize_epoch_cache(state, spec)?;
        initialize_progressive_balances_cache(state, spec)?;
        let mut ctxt = ConsensusContext::new(state.slot());
        if state.fork_name_unchecked().gloas_enabled() {
            gloas::process_attestation(
                state,
                self.to_ref(),
                0,
                &mut ctxt,
                VerifySignatures::True,
                spec,
            )
        } else if state.fork_name_unchecked().altair_enabled() {
            altair_deneb::process_attestation(
                state,
                self.to_ref(),
                0,
                &mut ctxt,
                VerifySignatures::True,
                spec,
            )
        } else {
            base::process_attestations(
                state,
                [self.clone().to_ref()].into_iter(),
                VerifySignatures::True,
                &mut ctxt,
                spec,
            )
        }
    }
}

impl<E: EthSpec> Operation<E> for AttesterSlashing<E> {
    type Error = BlockProcessingError;

    fn handler_name() -> String {
        "attester_slashing".into()
    }

    fn decode(path: &Path, fork_name: ForkName, _spec: &ChainSpec) -> Result<Self, Error> {
        if fork_name.electra_enabled() {
            Ok(Self::Electra(ssz_decode_file(path)?))
        } else {
            Ok(Self::Base(ssz_decode_file(path)?))
        }
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        _: &Operations<E, Self>,
    ) -> Result<(), BlockProcessingError> {
        let mut ctxt = ConsensusContext::new(state.slot());
        initialize_progressive_balances_cache(state, spec)?;
        process_attester_slashings(
            state,
            [self.clone().to_ref()].into_iter(),
            VerifySignatures::True,
            &mut ctxt,
            spec,
        )
    }
}

impl<E: EthSpec> Operation<E> for Deposit {
    type Error = BlockProcessingError;

    fn handler_name() -> String {
        "deposit".into()
    }

    fn decode(path: &Path, _fork_name: ForkName, _spec: &ChainSpec) -> Result<Self, Error> {
        ssz_decode_file(path)
    }

    fn is_enabled_for_fork(_: ForkName) -> bool {
        // Some deposit tests require signature verification but are not marked as such.
        cfg!(not(feature = "fake_crypto"))
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        _: &Operations<E, Self>,
    ) -> Result<(), BlockProcessingError> {
        process_deposits(state, std::slice::from_ref(self), spec)
    }
}

impl<E: EthSpec> Operation<E> for ProposerSlashing {
    type Error = BlockProcessingError;

    fn handler_name() -> String {
        "proposer_slashing".into()
    }

    fn decode(path: &Path, _fork_name: ForkName, _spec: &ChainSpec) -> Result<Self, Error> {
        ssz_decode_file(path)
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        _: &Operations<E, Self>,
    ) -> Result<(), BlockProcessingError> {
        let mut ctxt = ConsensusContext::new(state.slot());
        initialize_progressive_balances_cache(state, spec)?;
        process_proposer_slashings(
            state,
            std::slice::from_ref(self),
            VerifySignatures::True,
            &mut ctxt,
            spec,
        )
    }
}

impl<E: EthSpec> Operation<E> for SignedVoluntaryExit {
    type Error = BlockProcessingError;

    fn handler_name() -> String {
        "voluntary_exit".into()
    }

    fn decode(path: &Path, _fork_name: ForkName, _spec: &ChainSpec) -> Result<Self, Error> {
        ssz_decode_file(path)
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        _: &Operations<E, Self>,
    ) -> Result<(), BlockProcessingError> {
        process_exits(
            state,
            std::slice::from_ref(self),
            VerifySignatures::True,
            spec,
        )
    }
}

impl<E: EthSpec> Operation<E> for VoluntaryExitChurn {
    type Error = BlockProcessingError;

    fn handler_name() -> String {
        "voluntary_exit_churn".into()
    }

    fn filename() -> String {
        "voluntary_exit.ssz_snappy".into()
    }

    fn is_enabled_for_fork(fork_name: ForkName) -> bool {
        fork_name.gloas_enabled()
    }

    fn decode(path: &Path, _fork_name: ForkName, _spec: &ChainSpec) -> Result<Self, Error> {
        ssz_decode_file(path).map(|exit| VoluntaryExitChurn { exit })
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        _: &Operations<E, Self>,
    ) -> Result<(), BlockProcessingError> {
        process_exits(
            state,
            std::slice::from_ref(&self.exit),
            VerifySignatures::True,
            spec,
        )
    }
}

impl<E: EthSpec> Operation<E> for BeaconBlock<E> {
    type Error = BlockProcessingError;

    fn handler_name() -> String {
        "block_header".into()
    }

    fn filename() -> String {
        "block.ssz_snappy".into()
    }

    fn decode(path: &Path, _fork_name: ForkName, spec: &ChainSpec) -> Result<Self, Error> {
        ssz_decode_file_with(path, |bytes| BeaconBlock::from_ssz_bytes(bytes, spec))
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        _: &Operations<E, Self>,
    ) -> Result<(), BlockProcessingError> {
        let mut ctxt = ConsensusContext::new(state.slot());
        process_block_header(
            state,
            self.to_ref().temporary_block_header(),
            VerifyBlockRoot::True,
            &mut ctxt,
            spec,
        )?;
        Ok(())
    }
}

impl<E: EthSpec> Operation<E> for SyncAggregate<E> {
    type Error = BlockProcessingError;

    fn handler_name() -> String {
        "sync_aggregate".into()
    }

    fn filename() -> String {
        "sync_aggregate.ssz_snappy".into()
    }

    fn is_enabled_for_fork(fork_name: ForkName) -> bool {
        fork_name.altair_enabled()
    }

    fn decode(path: &Path, _fork_name: ForkName, _spec: &ChainSpec) -> Result<Self, Error> {
        ssz_decode_file(path)
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        _: &Operations<E, Self>,
    ) -> Result<(), BlockProcessingError> {
        let proposer_index = state.get_beacon_proposer_index(state.slot(), spec)? as u64;
        process_sync_aggregate(state, self, proposer_index, VerifySignatures::True, spec)
    }
}

impl<E: EthSpec> Operation<E> for BeaconBlockBody<E, FullPayload<E>> {
    type Error = BlockProcessingError;

    fn handler_name() -> String {
        "execution_payload".into()
    }

    fn filename() -> String {
        "body.ssz_snappy".into()
    }

    fn is_enabled_for_fork(fork_name: ForkName) -> bool {
        fork_name.bellatrix_enabled() && !fork_name.gloas_enabled()
    }

    fn decode(path: &Path, fork_name: ForkName, _spec: &ChainSpec) -> Result<Self, Error> {
        ssz_decode_file_with(path, |bytes| {
            Ok(match fork_name {
                ForkName::Bellatrix => BeaconBlockBody::Bellatrix(<_>::from_ssz_bytes(bytes)?),
                ForkName::Capella => BeaconBlockBody::Capella(<_>::from_ssz_bytes(bytes)?),
                ForkName::Deneb => BeaconBlockBody::Deneb(<_>::from_ssz_bytes(bytes)?),
                ForkName::Electra => BeaconBlockBody::Electra(<_>::from_ssz_bytes(bytes)?),
                ForkName::Fulu => BeaconBlockBody::Fulu(<_>::from_ssz_bytes(bytes)?),
                _ => panic!("Not supported after Gloas"),
            })
        })
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        extra: &Operations<E, Self>,
    ) -> Result<(), BlockProcessingError> {
        let valid = extra
            .execution_metadata
            .as_ref()
            .is_some_and(|e| e.execution_valid);
        if valid {
            process_execution_payload::<E, FullPayload<E>>(state, self.to_ref(), spec)
        } else {
            Err(BlockProcessingError::ExecutionInvalid)
        }
    }
}

impl<E: EthSpec> Operation<E> for BeaconBlockBody<E, BlindedPayload<E>> {
    type Error = BlockProcessingError;

    fn handler_name() -> String {
        "execution_payload".into()
    }

    fn filename() -> String {
        "body.ssz_snappy".into()
    }

    fn is_enabled_for_fork(fork_name: ForkName) -> bool {
        fork_name.bellatrix_enabled() && !fork_name.gloas_enabled()
    }

    fn decode(path: &Path, fork_name: ForkName, _spec: &ChainSpec) -> Result<Self, Error> {
        ssz_decode_file_with(path, |bytes| {
            Ok(match fork_name {
                ForkName::Bellatrix => {
                    let inner =
                        <BeaconBlockBodyBellatrix<E, FullPayload<E>>>::from_ssz_bytes(bytes)?;
                    BeaconBlockBody::Bellatrix(inner.clone_as_blinded())
                }
                ForkName::Capella => {
                    let inner = <BeaconBlockBodyCapella<E, FullPayload<E>>>::from_ssz_bytes(bytes)?;
                    BeaconBlockBody::Capella(inner.clone_as_blinded())
                }
                ForkName::Deneb => {
                    let inner = <BeaconBlockBodyDeneb<E, FullPayload<E>>>::from_ssz_bytes(bytes)?;
                    BeaconBlockBody::Deneb(inner.clone_as_blinded())
                }
                ForkName::Electra => {
                    let inner = <BeaconBlockBodyElectra<E, FullPayload<E>>>::from_ssz_bytes(bytes)?;
                    BeaconBlockBody::Electra(inner.clone_as_blinded())
                }
                ForkName::Fulu => {
                    let inner = <BeaconBlockBodyFulu<E, FullPayload<E>>>::from_ssz_bytes(bytes)?;
                    BeaconBlockBody::Fulu(inner.clone_as_blinded())
                }
                _ => panic!("Not supported after Gloas"),
            })
        })
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        extra: &Operations<E, Self>,
    ) -> Result<(), BlockProcessingError> {
        let valid = extra
            .execution_metadata
            .as_ref()
            .is_some_and(|e| e.execution_valid);
        if valid {
            process_execution_payload::<E, BlindedPayload<E>>(state, self.to_ref(), spec)
        } else {
            Err(BlockProcessingError::ExecutionInvalid)
        }
    }
}

impl<E: EthSpec> Operation<E> for SignedExecutionPayloadEnvelope<E> {
    type Error = EnvelopeProcessingError;

    fn handler_name() -> String {
        "execution_payload".into()
    }

    fn filename() -> String {
        "signed_envelope.ssz_snappy".into()
    }

    fn is_enabled_for_fork(_fork_name: ForkName) -> bool {
        // TODO(gloas): re-enable this test when enabled upstream
        // fork_name.gloas_enabled()
        false
    }

    fn decode(path: &Path, _: ForkName, _spec: &ChainSpec) -> Result<Self, Error> {
        ssz_decode_file(path)
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        extra: &Operations<E, Self>,
    ) -> Result<(), Self::Error> {
        let valid = extra
            .execution_metadata
            .as_ref()
            .is_some_and(|e| e.execution_valid);
        if valid {
            let block_state_root = state.update_tree_hash_cache()?;
            verify_execution_payload_envelope(
                state,
                self,
                VerifySignatures::True,
                block_state_root,
                spec,
            )
        } else {
            Err(EnvelopeProcessingError::ExecutionInvalid)
        }
    }
}

impl<E: EthSpec> Operation<E> for ExecutionPayloadBidBlock<E> {
    type Error = BlockProcessingError;

    fn handler_name() -> String {
        "execution_payload_bid".into()
    }

    fn filename() -> String {
        "block.ssz_snappy".into()
    }

    fn is_enabled_for_fork(fork_name: ForkName) -> bool {
        fork_name.gloas_enabled()
    }

    fn decode(path: &Path, _fork_name: ForkName, spec: &ChainSpec) -> Result<Self, Error> {
        ssz_decode_file_with(path, |bytes| BeaconBlock::from_ssz_bytes(bytes, spec))
            .map(|block| ExecutionPayloadBidBlock { block })
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        _: &Operations<E, Self>,
    ) -> Result<(), BlockProcessingError> {
        process_execution_payload_bid(state, self.block.to_ref(), VerifySignatures::True, spec)?;
        Ok(())
    }
}

impl<E: EthSpec> Operation<E> for ParentExecutionPayloadBlock<E> {
    type Error = BlockProcessingError;

    fn handler_name() -> String {
        "parent_execution_payload".into()
    }

    fn filename() -> String {
        "block.ssz_snappy".into()
    }

    fn is_enabled_for_fork(fork_name: ForkName) -> bool {
        fork_name.gloas_enabled()
    }

    fn decode(path: &Path, _fork_name: ForkName, spec: &ChainSpec) -> Result<Self, Error> {
        ssz_decode_file_with(path, |bytes| BeaconBlock::from_ssz_bytes(bytes, spec))
            .map(|block| ParentExecutionPayloadBlock { block })
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        _: &Operations<E, Self>,
    ) -> Result<(), BlockProcessingError> {
        process_parent_execution_payload(state, self.block.to_ref(), spec)
    }
}

impl<E: EthSpec> Operation<E> for WithdrawalsPayload<E> {
    type Error = BlockProcessingError;

    fn handler_name() -> String {
        "withdrawals".into()
    }

    fn filename() -> String {
        "execution_payload.ssz_snappy".into()
    }

    fn is_enabled_for_fork(fork_name: ForkName) -> bool {
        fork_name.capella_enabled()
    }

    fn decode(path: &Path, fork_name: ForkName, _spec: &ChainSpec) -> Result<Self, Error> {
        if fork_name.gloas_enabled() {
            // No payload present or required for Gloas tests.
            Ok(WithdrawalsPayload { payload: None })
        } else {
            ssz_decode_file_with(path, |bytes| {
                ExecutionPayload::from_ssz_bytes_by_fork(bytes, fork_name)
            })
            .map(|payload| WithdrawalsPayload {
                payload: Some(payload),
            })
        }
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        _: &Operations<E, Self>,
    ) -> Result<(), BlockProcessingError> {
        if state.fork_name_unchecked().gloas_enabled() {
            withdrawals::gloas::process_withdrawals(state, spec)
        } else {
            let full_payload = FullPayload::from(self.payload.clone().unwrap());
            withdrawals::capella_electra::process_withdrawals::<_, FullPayload<_>>(
                state,
                full_payload.to_ref(),
                spec,
            )
        }
    }
}

impl<E: EthSpec> Operation<E> for SignedBlsToExecutionChange {
    type Error = BlockProcessingError;

    fn handler_name() -> String {
        "bls_to_execution_change".into()
    }

    fn filename() -> String {
        "address_change.ssz_snappy".into()
    }

    fn is_enabled_for_fork(fork_name: ForkName) -> bool {
        fork_name.capella_enabled()
    }

    fn decode(path: &Path, _fork_name: ForkName, _spec: &ChainSpec) -> Result<Self, Error> {
        ssz_decode_file(path)
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        _extra: &Operations<E, Self>,
    ) -> Result<(), BlockProcessingError> {
        process_bls_to_execution_changes(
            state,
            std::slice::from_ref(self),
            VerifySignatures::True,
            spec,
        )
    }
}

impl<E: EthSpec> Operation<E> for WithdrawalRequest {
    type Error = BlockProcessingError;

    fn handler_name() -> String {
        "withdrawal_request".into()
    }

    fn is_enabled_for_fork(fork_name: ForkName) -> bool {
        fork_name.electra_enabled()
    }

    fn decode(path: &Path, _fork_name: ForkName, _spec: &ChainSpec) -> Result<Self, Error> {
        ssz_decode_file(path)
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        _extra: &Operations<E, Self>,
    ) -> Result<(), BlockProcessingError> {
        state.update_pubkey_cache()?;
        process_withdrawal_requests(state, std::slice::from_ref(self), spec)
    }
}

impl<E: EthSpec> Operation<E> for DepositRequest {
    type Error = BlockProcessingError;

    fn handler_name() -> String {
        "deposit_request".into()
    }

    fn is_enabled_for_fork(fork_name: ForkName) -> bool {
        fork_name.electra_enabled()
    }

    fn decode(path: &Path, _fork_name: ForkName, _spec: &ChainSpec) -> Result<Self, Error> {
        ssz_decode_file(path)
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        _extra: &Operations<E, Self>,
    ) -> Result<(), BlockProcessingError> {
        if state.fork_name_unchecked().gloas_enabled() {
            process_deposit_requests_post_gloas(state, std::slice::from_ref(self), spec)
        } else {
            process_deposit_requests_pre_gloas(state, std::slice::from_ref(self), spec)
        }
    }
}

impl<E: EthSpec> Operation<E> for ConsolidationRequest {
    type Error = BlockProcessingError;

    fn handler_name() -> String {
        "consolidation_request".into()
    }

    fn is_enabled_for_fork(fork_name: ForkName) -> bool {
        fork_name.electra_enabled()
    }

    fn decode(path: &Path, _fork_name: ForkName, _spec: &ChainSpec) -> Result<Self, Error> {
        ssz_decode_file(path)
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        _extra: &Operations<E, Self>,
    ) -> Result<(), BlockProcessingError> {
        state.update_pubkey_cache()?;
        process_consolidation_requests(state, std::slice::from_ref(self), spec)
    }
}

impl<E: EthSpec> Operation<E> for PayloadAttestation<E> {
    type Error = BlockProcessingError;

    fn handler_name() -> String {
        "payload_attestation".into()
    }

    fn is_enabled_for_fork(fork_name: ForkName) -> bool {
        fork_name.gloas_enabled()
    }

    fn decode(path: &Path, _fork_name: ForkName, _spec: &ChainSpec) -> Result<Self, Error> {
        ssz_decode_file(path)
    }

    fn apply_to(
        &self,
        state: &mut BeaconState<E>,
        spec: &ChainSpec,
        _extra: &Operations<E, Self>,
    ) -> Result<(), BlockProcessingError> {
        let mut ctxt = ConsensusContext::new(state.slot());
        process_payload_attestation(state, self, 0, VerifySignatures::True, &mut ctxt, spec)
    }
}

impl<E: EthSpec, O: Operation<E>> LoadCase for Operations<E, O> {
    fn load_from_dir(path: &Path, fork_name: ForkName) -> Result<Self, Error> {
        let spec = &testing_spec::<E>(fork_name);
        let metadata_path = path.join("meta.yaml");
        let metadata: Metadata = if metadata_path.is_file() {
            yaml_decode_file(&metadata_path)?
        } else {
            Metadata::default()
        };

        // For execution payloads only.
        let execution_yaml_path = path.join("execution.yaml");
        let execution_metadata = if execution_yaml_path.is_file() {
            Some(yaml_decode_file(&execution_yaml_path)?)
        } else {
            None
        };

        let pre = ssz_decode_state(&path.join("pre.ssz_snappy"), spec)?;

        // Check BLS setting here before SSZ deserialization, as most types require signatures
        // to be valid.
        let operation_path = path.join(O::filename());
        let (operation, bls_error) = if metadata.bls_setting.unwrap_or_default().check().is_ok() {
            match O::decode(&operation_path, fork_name, spec) {
                Ok(op) => (Some(op), None),
                Err(Error::InvalidBLSInput(error)) => (None, Some(error)),
                Err(e) => return Err(e),
            }
        } else {
            (None, None)
        };
        let post_filename = path.join("post.ssz_snappy");
        let post = if post_filename.is_file() {
            if let Some(bls_error) = bls_error {
                panic!("input is unexpectedly invalid: {}", bls_error);
            }
            Some(ssz_decode_state(&post_filename, spec)?)
        } else {
            None
        };

        Ok(Self {
            metadata,
            execution_metadata,
            pre,
            operation,
            post,
        })
    }
}

impl<E: EthSpec, O: Operation<E>> Case for Operations<E, O> {
    fn description(&self) -> String {
        self.metadata.description.clone().unwrap_or_default()
    }

    fn is_enabled_for_fork(fork_name: ForkName) -> bool {
        O::is_enabled_for_fork(fork_name)
    }

    fn result(&self, _case_index: usize, fork_name: ForkName) -> Result<(), Error> {
        let spec = &testing_spec::<E>(fork_name);

        let mut pre_state = self.pre.clone();
        // Processing requires the committee caches.
        // NOTE: some of the withdrawals tests have 0 active validators, do not try
        // to build the commitee cache in this case.
        if O::handler_name() != "withdrawals" {
            pre_state.build_all_committee_caches(spec).unwrap();
        }

        let mut state = pre_state.clone();
        let mut expected = self.post.clone();

        if O::handler_name() != "withdrawals"
            && let Some(post_state) = expected.as_mut()
        {
            post_state.build_all_committee_caches(spec).unwrap();
        }

        let mut result = self
            .operation
            .as_ref()
            .ok_or(Error::SkippedBls)?
            .apply_to(&mut state, spec, self)
            .map(|()| state);

        compare_beacon_state_results_without_caches(&mut result, &mut expected)
    }
}
