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

68 lines
1.9 KiB
Rust
Raw Normal View History

2024-02-27 23:29:06 +00:00
use quartz_cw::{
msg::{execute::session_create::SessionCreate, instantiate::CoreInstantiate},
state::{Config, Nonce},
};
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,
2024-02-27 23:29:06 +00:00
InstantiateResponse as RawInstantiateResponse, SessionCreateRequest as RawSessionCreateRequest,
SessionCreateResponse as RawSessionCreateResponse,
2024-02-22 20:09:44 +00:00
};
2024-02-27 23:29:06 +00:00
use quartz_relayer::types::{InstantiateResponse, SessionCreateResponse};
use rand::Rng;
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());
2024-02-27 23:29:06 +00:00
2024-02-26 10:56:55 +00:00
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,
2024-02-27 23:29:06 +00:00
_request: Request<RawSessionCreateRequest>,
) -> TonicResult<Response<RawSessionCreateResponse>> {
let nonce = rand::thread_rng().gen::<Nonce>();
let session_create_msg = SessionCreate::new(nonce);
2024-02-20 11:27:10 +00:00
2024-02-27 23:29:06 +00:00
let quote = self
.attestor
.quote(session_create_msg)
.map_err(|e| Status::internal(e.to_string()))?;
2024-02-20 11:27:10 +00:00
2024-02-27 23:29:06 +00:00
let response = SessionCreateResponse::new(nonce, quote);
Ok(Response::new(response.into()))
2024-02-20 11:27:10 +00:00
}
}