Skip to content
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

add(scan): Implement gRPC support for registering viewing keys for scanning #8266

Merged
merged 7 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions zebra-grpc/proto/scanner.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ service Scanner {

// Get all data we have stored for the given keys.
rpc GetResults(GetResultsRequest) returns (GetResultsResponse);

// Submits scanning keys to the scanner.
rpc RegisterKeys(RegisterKeysRequest) returns (RegisterKeysResponse);
}

// A response to a GetInfo call.
Expand All @@ -45,12 +48,24 @@ message GetResultsRequest {
repeated string keys = 1;
}

// A request to register scanning keys
message RegisterKeysRequest {
// Keys to register
repeated KeyWithHeight keys = 1;
}

// A set of responses for each provided key of a GetResults call.
message GetResultsResponse {
// Results for each key.
map<string, Results> results = 1;
}

// A response to `RegisterKeysRequest` containing registered keys
message RegisterKeysResponse {
// Keys that were registered
repeated string keys = 1;
}

// A result for a single key.
message Results {
// A height, transaction id map
Expand All @@ -62,3 +77,11 @@ message TransactionHash {
// A transaction id hash
repeated string hash = 1;
}

// A scanning key with an optional birth height
message KeyWithHeight {
// Scanning key
string key = 1;
// Birth height of the key
optional uint32 height = 2;
upbqdn marked this conversation as resolved.
Show resolved Hide resolved
}
42 changes: 33 additions & 9 deletions zebra-grpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{collections::BTreeMap, net::SocketAddr};

use futures_util::future::TryFutureExt;
use tonic::{transport::Server, Response, Status};
use tonic::{transport::Server, Request, Response, Status};
use tower::ServiceExt;

use zebra_node_services::scan_service::{
Expand All @@ -13,7 +13,7 @@ use zebra_node_services::scan_service::{
use crate::scanner::{
scanner_server::{Scanner, ScannerServer},
ClearResultsRequest, DeleteKeysRequest, Empty, GetResultsRequest, GetResultsResponse,
InfoReply, Results, TransactionHash,
InfoReply, RegisterKeysRequest, RegisterKeysResponse, Results, TransactionHash,
};

type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
Expand Down Expand Up @@ -42,10 +42,7 @@ where
+ 'static,
<ScanService as tower::Service<ScanServiceRequest>>::Future: Send,
{
async fn get_info(
&self,
_request: tonic::Request<Empty>,
) -> Result<Response<InfoReply>, Status> {
async fn get_info(&self, _request: Request<Empty>) -> Result<Response<InfoReply>, Status> {
let ScanServiceResponse::Info {
min_sapling_birthday_height,
} = self
Expand All @@ -68,9 +65,36 @@ where
Ok(Response::new(reply))
}

async fn register_keys(
&self,
request: Request<RegisterKeysRequest>,
) -> Result<Response<RegisterKeysResponse>, Status> {
let keys = request
.into_inner()
.keys
.into_iter()
.map(|key_with_height| (key_with_height.key, key_with_height.height))
.collect();

let ScanServiceResponse::RegisteredKeys(keys) = self
.scan_service
.clone()
.ready()
.and_then(|service| service.call(ScanServiceRequest::RegisterKeys(keys)))
.await
.map_err(|_| Status::unknown("scan service was unavailable"))?
else {
return Err(Status::unknown(
"scan service returned an unexpected response",
));
};

Ok(Response::new(RegisterKeysResponse { keys }))
}

async fn clear_results(
&self,
request: tonic::Request<ClearResultsRequest>,
request: Request<ClearResultsRequest>,
) -> Result<Response<Empty>, Status> {
let keys = request.into_inner().keys;

Expand All @@ -97,7 +121,7 @@ where

async fn delete_keys(
&self,
request: tonic::Request<DeleteKeysRequest>,
request: Request<DeleteKeysRequest>,
) -> Result<Response<Empty>, Status> {
let keys = request.into_inner().keys;

Expand All @@ -124,7 +148,7 @@ where

async fn get_results(
&self,
request: tonic::Request<GetResultsRequest>,
request: Request<GetResultsRequest>,
) -> Result<Response<GetResultsResponse>, Status> {
let keys = request.into_inner().keys;

Expand Down
Loading