-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grpc): Add initial
Getinfo
grpc (#8178)
* add `zebra-grpc` crate * add missing fields * convert to a lib * add zebra-scan and tonic as depenency * add a getinfo grpc * remove zebra-scanner dependency * Adds scan_service field to scanner grpc server * remove dependency * test launching the grpc server from the zebra-scan crate (not building) * fix async issue * fixes build issues * add binary for manual testing * try fix try run --------- Co-authored-by: Arya <aryasolhi@gmail.com>
- Loading branch information
1 parent
c08ad45
commit 78d33f3
Showing
15 changed files
with
256 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//! Compile proto files | ||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
tonic_build::compile_protos("proto/scanner.proto")?; | ||
Ok(()) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
syntax = "proto3"; | ||
package scanner; | ||
|
||
// Empty is for gRPCs that take no arguments, currently only GetInfo. | ||
message Empty {} | ||
|
||
service Scanner { | ||
// Get information about the scanner service. | ||
rpc GetInfo (Empty) returns (InfoReply); | ||
} | ||
|
||
// A response to a GetInfo call. | ||
message InfoReply { | ||
// The minimum sapling height allowed. | ||
uint32 min_sapling_birthday_height = 1; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
//! The gRPC server implementation | ||
use futures_util::future::TryFutureExt; | ||
use tonic::{transport::Server, Response, Status}; | ||
use tower::ServiceExt; | ||
|
||
use scanner::scanner_server::{Scanner, ScannerServer}; | ||
use scanner::{Empty, InfoReply}; | ||
|
||
use zebra_node_services::scan_service::{ | ||
request::Request as ScanServiceRequest, response::Response as ScanServiceResponse, | ||
}; | ||
|
||
/// The generated scanner proto | ||
pub mod scanner { | ||
tonic::include_proto!("scanner"); | ||
} | ||
|
||
type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>; | ||
|
||
#[derive(Debug)] | ||
/// The server implementation | ||
pub struct ScannerRPC<ScanService> | ||
where | ||
ScanService: tower::Service<ScanServiceRequest, Response = ScanServiceResponse, Error = BoxError> | ||
+ Clone | ||
+ Send | ||
+ Sync | ||
+ 'static, | ||
<ScanService as tower::Service<ScanServiceRequest>>::Future: Send, | ||
{ | ||
scan_service: ScanService, | ||
} | ||
|
||
#[tonic::async_trait] | ||
impl<ScanService> Scanner for ScannerRPC<ScanService> | ||
where | ||
ScanService: tower::Service<ScanServiceRequest, Response = ScanServiceResponse, Error = BoxError> | ||
+ Clone | ||
+ Send | ||
+ Sync | ||
+ 'static, | ||
<ScanService as tower::Service<ScanServiceRequest>>::Future: Send, | ||
{ | ||
async fn get_info( | ||
&self, | ||
_request: tonic::Request<Empty>, | ||
) -> Result<Response<InfoReply>, Status> { | ||
let ScanServiceResponse::Info { | ||
min_sapling_birthday_height, | ||
} = self | ||
.scan_service | ||
.clone() | ||
.ready() | ||
.and_then(|service| service.call(ScanServiceRequest::Info)) | ||
.await | ||
.map_err(|_| Status::unknown("scan service was unavailable"))? | ||
else { | ||
return Err(Status::unknown( | ||
"scan service returned an unexpected response", | ||
)); | ||
}; | ||
|
||
let reply = scanner::InfoReply { | ||
min_sapling_birthday_height: min_sapling_birthday_height.0, | ||
}; | ||
|
||
Ok(Response::new(reply)) | ||
} | ||
} | ||
|
||
/// Initializes the zebra-scan gRPC server | ||
pub async fn init<ScanService>(scan_service: ScanService) -> Result<(), color_eyre::Report> | ||
where | ||
ScanService: tower::Service<ScanServiceRequest, Response = ScanServiceResponse, Error = BoxError> | ||
+ Clone | ||
+ Send | ||
+ Sync | ||
+ 'static, | ||
<ScanService as tower::Service<ScanServiceRequest>>::Future: Send, | ||
{ | ||
let addr = "[::1]:50051".parse()?; | ||
let service = ScannerRPC { scan_service }; | ||
|
||
Server::builder() | ||
.add_service(ScannerServer::new(service)) | ||
.serve(addr) | ||
.await?; | ||
|
||
Ok(()) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//! Runs an RPC server with a mock ScanTask | ||
use tower::ServiceBuilder; | ||
|
||
use zebra_scan::service::ScanService; | ||
|
||
#[tokio::main] | ||
/// Runs an RPC server with a mock ScanTask | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let (config, network) = Default::default(); | ||
let scan_service = ServiceBuilder::new() | ||
.buffer(10) | ||
.service(ScanService::new_with_mock_scanner(&config, network)); | ||
|
||
// Start the gRPC server. | ||
zebra_grpc::server::init(scan_service).await?; | ||
|
||
Ok(()) | ||
} |
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
Oops, something went wrong.