-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
92 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,76 @@ | ||
use tonic::{transport::Server, Request, Response, Status}; | ||
use async_stream::stream; | ||
use futures::{Stream, StreamExt, TryStreamExt}; | ||
use starknet::signers::SigningKey; | ||
use std::hash::{DefaultHasher, Hash, Hasher}; | ||
use std::pin::Pin; | ||
use tokio::{ | ||
select, | ||
sync::{broadcast, mpsc}, | ||
}; | ||
use tonic::{Request, Response, Status, Streaming}; | ||
use tracing::info; | ||
use zetina_common::hash; | ||
use zetina_common::{ | ||
job::{Job, JobData}, | ||
job_witness::JobWitness, | ||
}; | ||
|
||
pub mod proto { | ||
tonic::include_proto!("delegator"); | ||
} | ||
use crate::tonic::proto::delegator_service_server::DelegatorService; | ||
use proto::{DelegateRequest, DelegateResponse}; | ||
|
||
#[derive(Default)] | ||
pub struct DelegatorGRPCServer {} | ||
pub struct DelegatorGRPCServer { | ||
signing_key: SigningKey, | ||
job_topic_tx: mpsc::Sender<Vec<u8>>, | ||
job_witness_rx: broadcast::Receiver<JobWitness>, | ||
} | ||
|
||
impl DelegatorGRPCServer { | ||
pub fn new( | ||
signing_key: SigningKey, | ||
job_topic_tx: mpsc::Sender<Vec<u8>>, | ||
job_witness_rx: broadcast::Receiver<JobWitness>, | ||
) -> Self { | ||
Self { signing_key, job_topic_tx, job_witness_rx } | ||
} | ||
} | ||
|
||
#[tonic::async_trait] | ||
impl DelegatorService for DelegatorGRPCServer { | ||
async fn delegate( | ||
type DelegatorStream = Pin<Box<dyn Stream<Item = Result<DelegateResponse, Status>> + Send>>; | ||
async fn delegator( | ||
&self, | ||
request: Request<DelegateRequest>, | ||
) -> Result<Response<DelegateResponse>, Status> { | ||
request: Request<Streaming<DelegateRequest>>, | ||
) -> Result<Response<Self::DelegatorStream>, Status> { | ||
println!("Got a request from {:?}", request.remote_addr()); | ||
let mut in_stream = request.into_inner().into_stream().fuse(); | ||
let job_channel = self.job_topic_tx.clone(); | ||
let mut witness_channel = self.job_witness_rx.resubscribe(); | ||
let signing_key = self.signing_key.clone(); | ||
|
||
let out_stream = stream! { | ||
loop { | ||
select! { | ||
Ok(request) = in_stream.select_next_some() => { | ||
let job_data = JobData::new(0, request.cairo_pie); | ||
let job = Job::try_from_job_data(job_data, &signing_key); | ||
let serialized_job = serde_json::to_string(&job).unwrap(); | ||
job_channel.send(serialized_job.into()).await.unwrap(); | ||
info!("Sent a new job: {}", hash!(&job)); | ||
} | ||
Ok(rx) = witness_channel.recv() => { | ||
yield Ok(DelegateResponse { proof: rx.proof }) | ||
} | ||
else => { | ||
yield Err(Status::cancelled("")) | ||
} | ||
} | ||
} | ||
} | ||
.boxed(); | ||
|
||
let reply = DelegateResponse { message: format!("Hello {}!", request.into_inner().name) }; | ||
Ok(Response::new(reply)) | ||
Ok(Response::new(out_stream)) | ||
} | ||
} |