Skip to content

Commit 9907d9d

Browse files
apollo_proof_manager_types: create proof manager client
1 parent b465e91 commit 9907d9d

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

Cargo.lock

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ members = [
6868
"crates/apollo_proc_macros_tests",
6969
"crates/apollo_proof_manager",
7070
"crates/apollo_proof_manager_config",
71+
"crates/apollo_proof_manager_types",
7172
"crates/apollo_propeller",
7273
"crates/apollo_protobuf",
7374
"crates/apollo_reverts",
@@ -187,6 +188,7 @@ apollo_proc_macros = { path = "crates/apollo_proc_macros", version = "0.0.0" }
187188
apollo_proc_macros_tests.path = "crates/apollo_proc_macros_tests"
188189
apollo_proof_manager.path = "crates/apollo_proof_manager"
189190
apollo_proof_manager_config.path = "crates/apollo_proof_manager_config"
191+
apollo_proof_manager_types.path = "crates/apollo_proof_manager_types"
190192
apollo_propeller.path = "crates/apollo_propeller"
191193
apollo_protobuf.path = "crates/apollo_protobuf"
192194
apollo_reverts.path = "crates/apollo_reverts"

commitlint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const AllowedScopes = ['apollo_base_layer_tests',
6060
'apollo_p2p_sync_config',
6161
'apollo_proc_macros',
6262
'apollo_proof_manager',
63+
'apollo_proof_manager_types',
6364
'apollo_protobuf',
6465
'apollo_propeller',
6566
'apollo_reverts',
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "apollo_proof_manager_types"
3+
version.workspace = true
4+
edition.workspace = true
5+
repository.workspace = true
6+
license.workspace = true
7+
8+
[features]
9+
testing = []
10+
11+
[dependencies]
12+
apollo_infra.workspace = true
13+
apollo_metrics.workspace = true
14+
apollo_proc_macros.workspace = true
15+
async-trait.workspace = true
16+
mockall.workspace = true
17+
serde.workspace = true
18+
starknet-types-core.workspace = true
19+
starknet_api.workspace = true
20+
strum.workspace = true
21+
strum_macros.workspace = true
22+
thiserror.workspace = true
23+
24+
[lints]
25+
workspace = true
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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

Comments
 (0)