-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement non-KeyImage Functionality for Fog Ledger Router (#3084)
* Ledger enclave support for router and store (#2896) * Pulling changes in from milliec/ledger-router-dev * Run cargo fmt * Making requested changes and clarifications to Fog ledger router comments. * Additional comment fixes * Fix CI lint * PR feedback nits Co-authored-by: Andrew Wygle <andrew@mobilecoin.com> * Fog Ledger Router Admin service * Sort itertools properly in Cargo.toml * Key Image Router Server + Binary * Update router config for parameterized retries * Changes due to rebase * normalize naming - ledger router, key image store * Linting fixes * Accept code review suggestions * Updates for GRPCIO 0.12 * Pulling changes in from milliec/ledger-router-dev * Making requested changes and clarifications to Fog ledger router comments. * PR feedback nits * Key Image Router Service * Update fog/ledger/server/src/key_image_router_service.rs Co-authored-by: Nick Santana <nick@mobilecoin.com> * Address PR feedback around logging and comments. * Parameterize allowed number of retries for query loop * Fog Ledger Router Admin service * Sort itertools properly in Cargo.toml * Key Image Router Server + Binary * Key image store changes pulled in from milliec/ledger-router-dev * Cargo fmt * Run clippy * Sort itertools properly in Cargo.toml * Rebase and update to match current fog ledger router branch * Implement ShardingStrategy into DbFetcher * Add a DbFetcher to the ledger store server. * Run cargo fmt * Other clippy and fmt changes * Sort dependencies * Remove unimplemented! that snuck in from other branch * Update and sync with latest Fog ledger router * Refactor router dispatch into match statement Uses ugly names from rust-protobuf. rust-protobuf version 3.* will do the Rusty thing and put the generated enums into modules, but we're on version 2.* and they do the C thing of really_long_snake_case_names. * Add merkle proof service to router server * Add untrusted tx out service to router server * Add block service to router server * Fixups from rebase --------- Co-authored-by: Emily C <gyrocoder@gmail.com> Co-authored-by: Nick Santana <nick@mobilecoin.com>
- Loading branch information
1 parent
dfa338f
commit 49d0990
Showing
9 changed files
with
656 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,117 @@ | ||
// Copyright (c) 2018-2022 The MobileCoin Foundation | ||
|
||
use std::{ | ||
collections::HashMap, | ||
sync::{Arc, RwLock}, | ||
}; | ||
|
||
use futures::executor::block_on; | ||
use mc_common::logger::{log, Logger}; | ||
use mc_fog_api::ledger_grpc; | ||
use mc_fog_ledger_enclave::LedgerEnclaveProxy; | ||
use mc_fog_uri::{ConnectionUri, KeyImageStoreUri}; | ||
use mc_util_grpc::{ConnectionUriGrpcioServer, ReadinessIndicator}; | ||
|
||
use crate::{ | ||
config::LedgerRouterConfig, key_image_router_service::KeyImageRouterService, | ||
router_admin_service::LedgerRouterAdminService, | ||
}; | ||
|
||
pub struct KeyImageRouterServer { | ||
router_server: grpcio::Server, | ||
admin_server: grpcio::Server, | ||
logger: Logger, | ||
} | ||
|
||
impl KeyImageRouterServer { | ||
pub fn new<E>( | ||
config: LedgerRouterConfig, | ||
enclave: E, | ||
shards: Arc<RwLock<HashMap<KeyImageStoreUri, Arc<ledger_grpc::KeyImageStoreApiClient>>>>, | ||
logger: Logger, | ||
) -> KeyImageRouterServer | ||
where | ||
E: LedgerEnclaveProxy, | ||
{ | ||
let readiness_indicator = ReadinessIndicator::default(); | ||
|
||
let env = Arc::new( | ||
grpcio::EnvBuilder::new() | ||
.name_prefix("key-image-router-server".to_string()) | ||
.build(), | ||
); | ||
|
||
// Health check service - will be used in both cases | ||
let health_service = | ||
mc_util_grpc::HealthService::new(Some(readiness_indicator.into()), logger.clone()) | ||
.into_service(); | ||
|
||
// Build our router server. | ||
// Init ledger router service. | ||
let ledger_router_service = ledger_grpc::create_ledger_api(KeyImageRouterService::new( | ||
enclave, | ||
shards.clone(), | ||
logger.clone(), | ||
)); | ||
log::debug!(logger, "Constructed Key Image Router GRPC Service"); | ||
|
||
// Init ledger router admin service. | ||
let ledger_router_admin_service = ledger_grpc::create_ledger_router_admin_api( | ||
LedgerRouterAdminService::new(shards, logger.clone()), | ||
); | ||
log::debug!(logger, "Constructed Key Image Router Admin GRPC Service"); | ||
|
||
// Package service into grpc server | ||
log::info!( | ||
logger, | ||
"Starting Key Image Router server on {}", | ||
config.client_listen_uri.addr(), | ||
); | ||
|
||
let router_server_builder = grpcio::ServerBuilder::new(env.clone()) | ||
.register_service(ledger_router_service) | ||
.register_service(health_service) | ||
.bind_using_uri(&config.client_listen_uri, logger.clone()); | ||
let admin_server_builder = grpcio::ServerBuilder::new(env) | ||
.register_service(ledger_router_admin_service) | ||
.bind_using_uri(&config.admin_listen_uri, logger.clone()); | ||
|
||
let router_server = router_server_builder.build().unwrap(); | ||
let admin_server = admin_server_builder.build().unwrap(); | ||
|
||
Self { | ||
router_server, | ||
admin_server, | ||
logger, | ||
} | ||
} | ||
|
||
/// Starts the server | ||
pub fn start(&mut self) { | ||
self.router_server.start(); | ||
for (host, port) in self.router_server.bind_addrs() { | ||
log::info!(self.logger, "Router API listening on {}:{}", host, port); | ||
} | ||
self.admin_server.start(); | ||
for (host, port) in self.admin_server.bind_addrs() { | ||
log::info!( | ||
self.logger, | ||
"Router Admin API listening on {}:{}", | ||
host, | ||
port | ||
); | ||
} | ||
} | ||
|
||
/// Stops the server | ||
pub fn stop(&mut self) { | ||
block_on(self.router_server.shutdown()).expect("Could not stop router grpc server"); | ||
block_on(self.admin_server.shutdown()).expect("Could not stop router admin grpc server"); | ||
} | ||
} | ||
|
||
impl Drop for KeyImageRouterServer { | ||
fn drop(&mut self) { | ||
self.stop(); | ||
} | ||
} |
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,86 @@ | ||
// Copyright (c) 2018-2022 The MobileCoin Foundation | ||
|
||
use std::{ | ||
collections::HashMap, | ||
sync::{Arc, RwLock}, | ||
}; | ||
|
||
use futures::{FutureExt, TryFutureExt}; | ||
use grpcio::{DuplexSink, RequestStream, RpcContext}; | ||
use mc_common::logger::{log, Logger}; | ||
use mc_fog_api::{ | ||
ledger::{LedgerRequest, LedgerResponse}, | ||
ledger_grpc::{self, LedgerApi}, | ||
}; | ||
use mc_fog_ledger_enclave::LedgerEnclaveProxy; | ||
use mc_fog_uri::KeyImageStoreUri; | ||
use mc_util_grpc::rpc_logger; | ||
use mc_util_metrics::SVC_COUNTERS; | ||
|
||
use crate::router_handlers; | ||
|
||
#[derive(Clone)] | ||
pub struct KeyImageRouterService<E> | ||
where | ||
E: LedgerEnclaveProxy, | ||
{ | ||
enclave: E, | ||
shards: Arc<RwLock<HashMap<KeyImageStoreUri, Arc<ledger_grpc::KeyImageStoreApiClient>>>>, | ||
query_retries: usize, | ||
logger: Logger, | ||
} | ||
|
||
impl<E: LedgerEnclaveProxy> KeyImageRouterService<E> { | ||
/// Creates a new LedgerRouterService that can be used by a gRPC server to | ||
/// fulfill gRPC requests. | ||
#[allow(dead_code)] // FIXME | ||
pub fn new( | ||
enclave: E, | ||
shards: Arc<RwLock<HashMap<KeyImageStoreUri, Arc<ledger_grpc::KeyImageStoreApiClient>>>>, | ||
query_retries: usize, | ||
logger: Logger, | ||
) -> Self { | ||
Self { | ||
enclave, | ||
shards, | ||
query_retries, | ||
logger, | ||
} | ||
} | ||
} | ||
|
||
impl<E> LedgerApi for KeyImageRouterService<E> | ||
where | ||
E: LedgerEnclaveProxy, | ||
{ | ||
fn request( | ||
&mut self, | ||
ctx: RpcContext, | ||
requests: RequestStream<LedgerRequest>, | ||
responses: DuplexSink<LedgerResponse>, | ||
) { | ||
let _timer = SVC_COUNTERS.req(&ctx); | ||
mc_common::logger::scoped_global_logger(&rpc_logger(&ctx, &self.logger), |logger| { | ||
log::warn!( | ||
self.logger, | ||
"Streaming GRPC Ledger API only partially implemented." | ||
); | ||
let logger = logger.clone(); | ||
|
||
let shards = self.shards.read().expect("RwLock poisoned"); | ||
let future = router_handlers::handle_requests( | ||
shards.values().cloned().collect(), | ||
self.enclave.clone(), | ||
requests, | ||
responses, | ||
self.query_retries, | ||
logger.clone(), | ||
) | ||
.map_err(move |err| log::error!(&logger, "failed to reply: {}", err)) | ||
// TODO: Do more with the error than just push it to the log. | ||
.map(|_| ()); | ||
|
||
ctx.spawn(future) | ||
}); | ||
} | ||
} |
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.