Skip to content

Commit

Permalink
Update api
Browse files Browse the repository at this point in the history
  • Loading branch information
poszu committed Nov 1, 2023
1 parent c720d44 commit 862574a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
6 changes: 4 additions & 2 deletions service/build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::{env, path::PathBuf};

fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::configure().compile(&["api/spacemesh/v1/post.proto"], &["api"])?;

let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
tonic_build::configure()
.file_descriptor_set_path(out_dir.join("post_descriptor.bin"))
.compile(&["api/spacemesh/v1/post.proto"], &["api"])?;
.file_descriptor_set_path(out_dir.join("service_descriptor.bin"))
.compile(&["api/post/v1/service.proto"], &["api"])?;

Ok(())
}
40 changes: 21 additions & 19 deletions service/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use tokio::net::TcpListener;
use tokio_stream::wrappers::TcpListenerStream;
use tonic::{transport::Server, Request, Response, Status};

use spacemesh_v1::post_service_operator_server::{PostServiceOperator, PostServiceOperatorServer};
use spacemesh_v1::{PostServiceStatusRequest, PostServiceStatusResponse};
use post_v1::operator_service_server::OperatorServiceServer;
use post_v1::{OperatorStatusRequest, OperatorStatusResponse};

pub mod spacemesh_v1 {
tonic::include_proto!("spacemesh.v1");
pub mod post_v1 {
tonic::include_proto!("post.v1");
pub(crate) const FILE_DESCRIPTOR_SET: &[u8] =
tonic::include_file_descriptor_set!("post_descriptor");
tonic::include_file_descriptor_set!("service_descriptor");
}

pub enum ServiceState {
Expand All @@ -36,19 +36,21 @@ pub struct OperatorService<S: Service> {
}

#[tonic::async_trait]
impl<S: Service + Sync + Send + 'static> PostServiceOperator for OperatorService<S> {
impl<S: Service + Sync + Send + 'static> post_v1::operator_service_server::OperatorService
for OperatorService<S>
{
async fn status(
&self,
request: Request<PostServiceStatusRequest>,
) -> Result<Response<PostServiceStatusResponse>, Status> {
request: Request<OperatorStatusRequest>,
) -> Result<Response<OperatorStatusResponse>, Status> {
log::debug!("got a request from {:?}", request.remote_addr());

let status = match self.service.status() {
ServiceState::Idle => spacemesh_v1::post_service_status_response::Status::Idle,
ServiceState::Proving => spacemesh_v1::post_service_status_response::Status::Proving,
ServiceState::Idle => post_v1::operator_status_response::Status::Idle,
ServiceState::Proving => post_v1::operator_status_response::Status::Proving,
};

Ok(Response::new(PostServiceStatusResponse {
Ok(Response::new(OperatorStatusResponse {
status: status as _,
}))
}
Expand All @@ -65,10 +67,10 @@ impl OperatorServer {
log::info!("running operator service on {}", listener.local_addr()?);

let reflection_service = tonic_reflection::server::Builder::configure()
.register_encoded_file_descriptor_set(spacemesh_v1::FILE_DESCRIPTOR_SET)
.register_encoded_file_descriptor_set(post_v1::FILE_DESCRIPTOR_SET)
.build()?;

let operator_service = PostServiceOperatorServer::new(OperatorService { service });
let operator_service = OperatorServiceServer::new(OperatorService { service });

Server::builder()
.add_service(reflection_service)
Expand All @@ -85,9 +87,9 @@ mod tests {

use tokio::net::TcpListener;

use super::spacemesh_v1::post_service_operator_client::PostServiceOperatorClient;
use super::spacemesh_v1::post_service_status_response::Status;
use super::spacemesh_v1::PostServiceStatusRequest;
use super::post_v1::operator_service_client::OperatorServiceClient;
use super::post_v1::operator_status_response::Status;
use super::post_v1::OperatorStatusRequest;

#[tokio::test]
async fn test_status() {
Expand All @@ -104,14 +106,14 @@ mod tests {

tokio::spawn(super::OperatorServer::run(listener, Arc::new(svc)));

let mut client = PostServiceOperatorClient::connect(format!("http://{addr}"))
let mut client = OperatorServiceClient::connect(format!("http://{addr}"))
.await
.unwrap();

let response = client.status(PostServiceStatusRequest {}).await.unwrap();
let response = client.status(OperatorStatusRequest {}).await.unwrap();
assert_eq!(response.into_inner().status(), Status::Idle);

let response = client.status(PostServiceStatusRequest {}).await.unwrap();
let response = client.status(OperatorStatusRequest {}).await.unwrap();
assert_eq!(response.into_inner().status(), Status::Proving);
}
}
16 changes: 7 additions & 9 deletions service/tests/test_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use post::{
use post_service::{
client::spacemesh_v1::{service_response, GenProofStatus},
operator::{
spacemesh_v1::{
post_service_operator_client::PostServiceOperatorClient,
post_service_status_response::Status, PostServiceStatusRequest,
post_v1::{
operator_service_client::OperatorServiceClient, operator_status_response::Status,
OperatorStatusRequest,
},
OperatorServer,
},
Expand Down Expand Up @@ -55,18 +55,16 @@ async fn test_gen_proof_in_progress() {
let listener = TcpListener::bind("localhost:0").await.unwrap();
let operator_addr = format!("http://{}", listener.local_addr().unwrap());
tokio::spawn(OperatorServer::run(listener, service));
let mut client = PostServiceOperatorClient::connect(operator_addr)
.await
.unwrap();
let mut client = OperatorServiceClient::connect(operator_addr).await.unwrap();

// It starts in idle state
let response = client.status(PostServiceStatusRequest {}).await.unwrap();
let response = client.status(OperatorStatusRequest {}).await.unwrap();
assert_eq!(response.into_inner().status(), Status::Idle);

// It transforms to Proving when a proof generation starts
let connected = test_server.connected.recv().await.unwrap();
TestServer::generate_proof(&connected, vec![0xCA; 32]).await;
let response = client.status(PostServiceStatusRequest {}).await.unwrap();
let response = client.status(OperatorStatusRequest {}).await.unwrap();
assert_eq!(response.into_inner().status(), Status::Proving);

loop {
Expand All @@ -89,6 +87,6 @@ async fn test_gen_proof_in_progress() {
}

// It transforms back to Idle when the proof generation finishes
let response = client.status(PostServiceStatusRequest {}).await.unwrap();
let response = client.status(OperatorStatusRequest {}).await.unwrap();
assert_eq!(response.into_inner().status(), Status::Idle);
}

0 comments on commit 862574a

Please sign in to comment.