newton-aggregator 0.4.18

newton prover aggregator utils
use alloy::{
    contract::Error as ContractError,
    transports::{RpcError, TransportErrorKind},
};
use eigensdk::{
    client_avsregistry::error::AvsRegistryError, crypto_bls::error::BlsError,
    services_blsaggregation::bls_aggregation_service_error::BlsAggregationServiceError as EigensdkBlsAggregationServiceError,
    services_operatorsinfo::operatorsinfo_inmemory::OperatorInfoServiceError,
};
use newton_chainio::{bls::BlsAggregationServiceError, error::ChainIoError};
use newton_core::config::error::ConfigError;
use serde_json::Error;
use thiserror::Error;

/// Error returned by chainio
#[derive(Debug, Error)]
pub enum AggregatorError {
    /// Key error
    #[error("Key error: {0}")]
    KeyError(String),

    /// Bls Aggregation Service Error
    #[error("Bls Aggregation Service Error : {0}")]
    BlsAggregationServiceError(#[from] EigensdkBlsAggregationServiceError),
    /// Task Response not found
    #[error("Task Response not found")]
    TaskResponseNotFound,
    /// parse error
    #[error("Config parse error")]
    ParseError(#[from] ConfigError),
    /// Build avs registry chain reader
    #[error("Failed to build avs registry chain reader ")]
    BuildAvsRegistryChainReader(#[from] AvsRegistryError),

    /// build avswriter
    #[error("Failed to build avs wrtier in chain io ")]
    BuildAvsWriter(#[from] ChainIoError),

    /// Bls crate error
    #[error("Bls Crate Error SDK")]
    Bls(#[from] BlsError),

    /// alloy rpc error
    #[error("Alloy rpc error")]
    AlloyRpc(#[from] RpcError<TransportErrorKind>),

    /// serde json error
    #[error("Serde json error")]
    SerdeError(#[from] Error),

    /// IO error
    #[error("IO error")]
    IOError(#[from] std::io::Error),

    /// Operator Info service error
    #[error("Operator Info Service error")]
    OperatorInfoServiceError(#[from] OperatorInfoServiceError),

    /// Alloy Contract Error
    #[error("Alloy Contract Error")]
    ContractError(#[from] ContractError),

    /// Invalid Bls Key
    #[error("Invalid Bls Key")]
    InvalidBlsKey(String),

    /// Send aggregated response error
    #[error("Send aggregated response error")]
    SendAggregatedResponseError,
}

/// Error returned by AggregatorCore operations
#[derive(Debug, Error)]
pub enum AggregatorCoreError {
    /// Invalid timeout duration (zero or negative)
    #[error("Invalid timeout duration: {0}")]
    InvalidTimeoutDuration(String),

    /// Aggregation service error
    #[error("Aggregation service error: {0}")]
    AggregationServiceError(#[from] BlsAggregationServiceError),

    /// Timeout waiting for aggregation
    #[error("Timeout waiting for aggregation after {duration_ms} ms (timeout: {timeout_ms} ms)")]
    Timeout {
        /// Duration elapsed before timeout
        duration_ms: u128,
        /// Configured timeout duration
        timeout_ms: u128,
        /// Operator errors (if any)
        operator_errors: Option<Vec<crate::rpc_server::OperatorErrorResponse>>,
    },

    /// Task not initialized
    #[error("Task {task_id} not initialized - call initialize_task first")]
    TaskNotInitialized {
        /// The task ID that was not initialized
        task_id: newton_core::TaskId,
    },

    /// Operation was cancelled
    #[error("Operation cancelled")]
    Cancelled,
}