cycles-quartz/enclaves/quartz/src/server.rs

59 lines
1.6 KiB
Rust
Raw Normal View History

2024-02-26 10:56:55 +00:00
use quartz_cw::{msg::instantiate::CoreInstantiate, state::Config};
2024-02-22 20:09:44 +00:00
use quartz_proto::quartz::{
2024-02-23 20:51:55 +00:00
core_server::Core, InstantiateRequest as RawInstantiateRequest,
InstantiateResponse as RawInstantiateResponse, SessionCreateRequest, SessionCreateResponse,
2024-02-22 20:09:44 +00:00
};
2024-02-23 22:27:29 +00:00
use quartz_relayer::types::InstantiateResponse;
2024-02-20 11:27:10 +00:00
use tonic::{Request, Response, Status};
2024-02-26 10:56:55 +00:00
use crate::attestor::Attestor;
2024-02-23 20:51:55 +00:00
type TonicResult<T> = Result<T, Status>;
2024-02-26 10:56:55 +00:00
#[derive(Clone, PartialEq, Debug)]
pub struct CoreService<A> {
2024-02-23 20:51:20 +00:00
config: Config,
2024-02-26 10:56:55 +00:00
attestor: A,
2024-02-23 20:51:20 +00:00
}
2024-02-26 10:56:55 +00:00
impl<A> CoreService<A>
where
A: Attestor,
{
pub fn new(config: Config, attestor: A) -> Self {
Self { config, attestor }
2024-02-23 20:51:20 +00:00
}
}
2024-02-20 11:27:10 +00:00
#[tonic::async_trait]
2024-02-26 10:56:55 +00:00
impl<A> Core for CoreService<A>
where
A: Attestor + Send + Sync + 'static,
{
2024-02-22 20:09:44 +00:00
async fn instantiate(
&self,
2024-02-23 20:51:55 +00:00
_request: Request<RawInstantiateRequest>,
) -> TonicResult<Response<RawInstantiateResponse>> {
2024-02-26 10:56:55 +00:00
let core_instantiate_msg = CoreInstantiate::new(self.config.clone());
let quote = self
.attestor
.quote(core_instantiate_msg)
.map_err(|e| Status::internal(e.to_string()))?;
2024-02-22 20:09:44 +00:00
2024-02-23 20:51:55 +00:00
let response = InstantiateResponse::new(self.config.clone(), quote);
Ok(Response::new(response.into()))
2024-02-22 20:09:44 +00:00
}
2024-02-20 11:27:10 +00:00
async fn session_create(
&self,
request: Request<SessionCreateRequest>,
2024-02-23 20:51:55 +00:00
) -> TonicResult<Response<SessionCreateResponse>> {
2024-02-20 11:27:10 +00:00
println!("Got a request: {:?}", request);
let reply = SessionCreateResponse {
message: "Hello!".to_string(),
};
Ok(Response::new(reply))
}
}