-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
grpc server scaffolding #8
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
// tonic_build::compile_protos("src/proto/blocks.proto")?; | ||
let mut builder = tonic_build::configure(); | ||
|
||
// Custom type attributes required for malachite | ||
builder = builder.type_attribute("snapchain.ShardHash", "#[derive(Eq, PartialOrd, Ord)]"); | ||
|
||
builder.compile(&["src/proto/blocks.proto"], &["src/proto"])?; | ||
// TODO: auto-discover proto files | ||
builder.compile(&[ | ||
"src/proto/blocks.proto", | ||
"src/proto/rpc.proto", | ||
"src/proto/message.proto", | ||
"src/proto/username_proof.proto", | ||
], &["src/proto"])?; | ||
|
||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,29 +4,39 @@ pub mod network; | |
pub mod connectors; | ||
mod cfg; | ||
|
||
use std::error::Error; | ||
use std::io; | ||
use std::net::SocketAddr; | ||
use clap::Parser; | ||
use futures::stream::StreamExt; | ||
use libp2p::identity::ed25519::Keypair; | ||
use malachite_config::TimeoutConfig; | ||
use malachite_metrics::{Metrics, SharedRegistry}; | ||
use std::time::Duration; | ||
use tokio::signal::ctrl_c; | ||
use tokio::sync::mpsc; | ||
use tokio::{select, time}; | ||
use tokio::time::sleep; | ||
use tonic::transport::Server; | ||
use tracing::{error, info}; | ||
use tracing_subscriber::EnvFilter; | ||
use connectors::fname::Fetcher; | ||
|
||
|
||
use crate::consensus::consensus::{Consensus, ConsensusMsg, ConsensusParams}; | ||
use crate::core::types::{proto, Address, Height, ShardId, SnapchainShard, SnapchainValidator, SnapchainValidatorContext, SnapchainValidatorSet}; | ||
use crate::network::gossip::{GossipEvent}; | ||
use crate::network::gossip::GossipEvent; | ||
use network::gossip::SnapchainGossip; | ||
use network::server::MySnapchainService; | ||
use network::server::rpc::snapchain_service_server::SnapchainServiceServer; | ||
|
||
pub enum SystemMessage { | ||
Consensus(ConsensusMsg<SnapchainValidatorContext>), | ||
} | ||
|
||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
async fn main() -> Result<(), Box<dyn Error>> { | ||
let args: Vec<String> = std::env::args().collect(); | ||
|
||
let app_config = cfg::load_and_merge_config(args)?; | ||
|
@@ -39,7 +49,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { | |
let port = base_port + app_config.id; | ||
let addr = format!("/ip4/0.0.0.0/udp/{}/quic-v1", port); | ||
|
||
println!("SnapchainService (ID: {}) listening on {}", app_config.id, addr); | ||
let base_grpc_port = 50060; | ||
let grpc_port = base_grpc_port + app_config.id; | ||
let grpc_addr = format!("0.0.0.0:{}", grpc_port); | ||
let grpc_socket_addr: SocketAddr = grpc_addr.parse()?; | ||
|
||
info!( | ||
id = app_config.id, | ||
addr = addr, | ||
grpc_addr = grpc_addr, | ||
"SnapchainService listening", | ||
); | ||
|
||
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")); | ||
match app_config.log_format.as_str() { | ||
|
@@ -60,17 +80,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { | |
|
||
let gossip_result = SnapchainGossip::create(keypair.clone(), addr, system_tx.clone()); | ||
if let Err(e) = gossip_result { | ||
println!("Failed to create SnapchainGossip: {:?}", e); | ||
error!(error = ?e, "Failed to create SnapchainGossip"); | ||
return Ok(()); | ||
} | ||
|
||
let mut gossip = gossip_result?; | ||
let gossip_tx = gossip.tx.clone(); | ||
|
||
tokio::spawn(async move { | ||
println!("Starting gossip"); | ||
info!("Starting gossip"); | ||
gossip.start().await; | ||
println!("Gossip Stopped"); | ||
info!("Gossip Stopped"); | ||
}); | ||
|
||
if !app_config.fnames.disable { | ||
|
@@ -81,6 +101,25 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { | |
}); | ||
} | ||
|
||
let (shutdown_tx, mut shutdown_rx) = mpsc::channel::<()>(1); | ||
|
||
tokio::spawn(async move { | ||
let service = MySnapchainService::default(); | ||
|
||
let resp = Server::builder() | ||
.add_service(SnapchainServiceServer::new(service)) | ||
.serve(grpc_socket_addr) | ||
.await; | ||
|
||
let msg = "grpc server stopped"; | ||
match resp { | ||
Ok(()) => error!(msg), | ||
Err(e) => error!(error = ?e, "{}", msg), | ||
} | ||
|
||
shutdown_tx.send(()).await.ok(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When will this be hit? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess for any reason the grpc server stops running. I'm not sure the full set of cases, but one common one is when the grpc server address/port is already in use. |
||
}); | ||
|
||
let registry = SharedRegistry::global(); | ||
let metrics = Metrics::register(registry); | ||
|
||
|
@@ -119,7 +158,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { | |
loop { | ||
select! { | ||
_ = ctrl_c() => { | ||
println!("Received Ctrl-C, shutting down"); | ||
info!("Received Ctrl-C, shutting down"); | ||
consensus_actor.stop(None); | ||
return Ok(()); | ||
} | ||
_ = shutdown_rx.recv() => { | ||
error!("Received shutdown signal, shutting down"); | ||
consensus_actor.stop(None); | ||
return Ok(()); | ||
} | ||
|
@@ -134,7 +178,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { | |
}), | ||
nonce: tick_count as u64, // Need the nonce to avoid the gossip duplicate message check | ||
}; | ||
println!("Registering validator with nonce: {}", register_validator.nonce); | ||
info!("Registering validator with nonce: {}", register_validator.nonce); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I'll swap the remaining printlns with tracing in my next pr |
||
gossip_tx.send(GossipEvent::RegisterValidator(register_validator)).await?; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod gossip; | ||
pub mod server; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use std::error::Error; | ||
use std::net::SocketAddr; | ||
use tonic::{transport::Server, Request, Response, Status}; | ||
use tonic::Code::Unimplemented; | ||
use tracing::{info}; | ||
use hex::ToHex; | ||
|
||
|
||
pub mod rpc { | ||
tonic::include_proto!("rpc"); | ||
} | ||
|
||
pub mod message { | ||
tonic::include_proto!("message"); | ||
} | ||
|
||
pub mod username_proof { | ||
tonic::include_proto!("username_proof"); | ||
} | ||
|
||
use rpc::snapchain_service_server::{SnapchainService, SnapchainServiceServer}; | ||
use message::{Message}; | ||
|
||
#[derive(Default)] | ||
pub struct MySnapchainService; | ||
|
||
#[tonic::async_trait] | ||
impl SnapchainService for MySnapchainService { | ||
async fn submit_message(&self, request: Request<Message>) -> Result<Response<Message>, Status> { | ||
let hash = request.get_ref().hash.encode_hex::<String>(); | ||
info!(hash, "Received a message"); | ||
Err(Status::new(Unimplemented, "not implemented")) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to autodiscover all the files, but not a priority
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a TODO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to see this implemented as proposed in issue 5 some days ago
#5