|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use apollo_infra::component_client::{ClientError, LocalComponentClient, RemoteComponentClient}; |
| 4 | +use apollo_infra::component_definitions::{ComponentClient, PrioritizedRequest, RequestWrapper}; |
| 5 | +use apollo_infra::requests::LABEL_NAME_REQUEST_VARIANT; |
| 6 | +use apollo_infra::{impl_debug_for_infra_requests_and_responses, impl_labeled_request}; |
| 7 | +use apollo_metrics::generate_permutation_labels; |
| 8 | +use apollo_proc_macros::handle_all_response_variants; |
| 9 | +use async_trait::async_trait; |
| 10 | +#[cfg(any(feature = "testing", test))] |
| 11 | +use mockall::automock; |
| 12 | +use serde::{Deserialize, Serialize}; |
| 13 | +use starknet_api::transaction::fields::Proof; |
| 14 | +use starknet_types_core::felt::Felt; |
| 15 | +use strum::{EnumVariantNames, VariantNames}; |
| 16 | +use strum_macros::{AsRefStr, EnumDiscriminants, EnumIter, IntoStaticStr}; |
| 17 | +use thiserror::Error; |
| 18 | + |
| 19 | +pub type ProofManagerResult<T> = Result<T, ProofManagerError>; |
| 20 | +pub type ProofManagerClientResult<T> = Result<T, ProofManagerClientError>; |
| 21 | + |
| 22 | +pub type LocalProofManagerClient = LocalComponentClient<ProofManagerRequest, ProofManagerResponse>; |
| 23 | +pub type RemoteProofManagerClient = |
| 24 | + RemoteComponentClient<ProofManagerRequest, ProofManagerResponse>; |
| 25 | +pub type ProofManagerRequestWrapper = RequestWrapper<ProofManagerRequest, ProofManagerResponse>; |
| 26 | + |
| 27 | +pub type SharedProofManagerClient = Arc<dyn ProofManagerClient>; |
| 28 | + |
| 29 | +/// Serves as the proof manager's shared interface. |
| 30 | +/// Requires `Send + Sync` to allow transferring and sharing resources (inputs, futures) across |
| 31 | +/// threads. |
| 32 | +#[cfg_attr(any(feature = "testing", test), automock)] |
| 33 | +#[async_trait] |
| 34 | +pub trait ProofManagerClient: Send + Sync { |
| 35 | + async fn set_proof(&self, facts_hash: Felt, proof: Proof) -> ProofManagerClientResult<()>; |
| 36 | + |
| 37 | + async fn get_proof(&self, facts_hash: Felt) -> ProofManagerClientResult<Option<Proof>>; |
| 38 | + |
| 39 | + async fn contains_proof(&self, facts_hash: Felt) -> ProofManagerClientResult<bool>; |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(Clone, Debug, Error, Eq, PartialEq, Serialize, Deserialize)] |
| 43 | +pub enum ProofManagerError { |
| 44 | + #[error("Internal client error: {0}")] |
| 45 | + Client(String), |
| 46 | + #[error("Proof storage error: {0}")] |
| 47 | + ProofStorage(String), |
| 48 | + #[error("IO error: {0}")] |
| 49 | + Io(String), |
| 50 | +} |
| 51 | + |
| 52 | +#[derive(Clone, Debug, Error)] |
| 53 | +pub enum ProofManagerClientError { |
| 54 | + #[error(transparent)] |
| 55 | + ClientError(#[from] ClientError), |
| 56 | + #[error(transparent)] |
| 57 | + ProofManagerError(#[from] ProofManagerError), |
| 58 | +} |
| 59 | + |
| 60 | +#[derive(Clone, Serialize, Deserialize, AsRefStr, EnumDiscriminants)] |
| 61 | +#[strum_discriminants( |
| 62 | + name(ProofManagerRequestLabelValue), |
| 63 | + derive(IntoStaticStr, EnumIter, EnumVariantNames), |
| 64 | + strum(serialize_all = "snake_case") |
| 65 | +)] |
| 66 | +pub enum ProofManagerRequest { |
| 67 | + SetProof(Felt, Proof), |
| 68 | + GetProof(Felt), |
| 69 | + ContainsProof(Felt), |
| 70 | +} |
| 71 | +impl_debug_for_infra_requests_and_responses!(ProofManagerRequest); |
| 72 | +impl_labeled_request!(ProofManagerRequest, ProofManagerRequestLabelValue); |
| 73 | +impl PrioritizedRequest for ProofManagerRequest {} |
| 74 | + |
| 75 | +generate_permutation_labels! { |
| 76 | + PROOF_MANAGER_REQUEST_LABELS, |
| 77 | + (LABEL_NAME_REQUEST_VARIANT, ProofManagerRequestLabelValue), |
| 78 | +} |
| 79 | + |
| 80 | +#[derive(Clone, Serialize, Deserialize, AsRefStr)] |
| 81 | +pub enum ProofManagerResponse { |
| 82 | + SetProof(ProofManagerResult<()>), |
| 83 | + GetProof(ProofManagerResult<Option<Proof>>), |
| 84 | + ContainsProof(ProofManagerResult<bool>), |
| 85 | +} |
| 86 | +impl_debug_for_infra_requests_and_responses!(ProofManagerResponse); |
| 87 | + |
| 88 | +#[async_trait] |
| 89 | +impl<ComponentClientType> ProofManagerClient for ComponentClientType |
| 90 | +where |
| 91 | + ComponentClientType: Send + Sync + ComponentClient<ProofManagerRequest, ProofManagerResponse>, |
| 92 | +{ |
| 93 | + async fn set_proof(&self, facts_hash: Felt, proof: Proof) -> ProofManagerClientResult<()> { |
| 94 | + let request = ProofManagerRequest::SetProof(facts_hash, proof); |
| 95 | + handle_all_response_variants!( |
| 96 | + ProofManagerResponse, |
| 97 | + SetProof, |
| 98 | + ProofManagerClientError, |
| 99 | + ProofManagerError, |
| 100 | + Direct |
| 101 | + ) |
| 102 | + } |
| 103 | + |
| 104 | + async fn get_proof(&self, facts_hash: Felt) -> ProofManagerClientResult<Option<Proof>> { |
| 105 | + let request = ProofManagerRequest::GetProof(facts_hash); |
| 106 | + handle_all_response_variants!( |
| 107 | + ProofManagerResponse, |
| 108 | + GetProof, |
| 109 | + ProofManagerClientError, |
| 110 | + ProofManagerError, |
| 111 | + Direct |
| 112 | + ) |
| 113 | + } |
| 114 | + |
| 115 | + async fn contains_proof(&self, facts_hash: Felt) -> ProofManagerClientResult<bool> { |
| 116 | + let request = ProofManagerRequest::ContainsProof(facts_hash); |
| 117 | + handle_all_response_variants!( |
| 118 | + ProofManagerResponse, |
| 119 | + ContainsProof, |
| 120 | + ProofManagerClientError, |
| 121 | + ProofManagerError, |
| 122 | + Direct |
| 123 | + ) |
| 124 | + } |
| 125 | +} |
0 commit comments