Skip to content

Commit

Permalink
Split NV query RPC apart from JMT query RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
zbuc committed May 14, 2024
1 parent b7d03a5 commit 3257d11
Show file tree
Hide file tree
Showing 7 changed files with 1,184 additions and 350 deletions.
64 changes: 41 additions & 23 deletions crates/bin/pcli/src/command/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ mod tx;
mod validator;

use auction::AuctionCmd;
use base64::prelude::*;
use chain::ChainCmd;
use colored_json::ToColoredJson;
use community_pool::CommunityPoolCmd;
use dex::DexCmd;
use governance::GovernanceCmd;
use ibc_query::IbcCmd;
use penumbra_proto::cnidarium::v1::non_verifiable_key_value_request::Key as NVKey;
use shielded_pool::ShieldedPool;
use tx::Tx;
pub(super) use validator::ValidatorCmd;
Expand Down Expand Up @@ -179,34 +181,50 @@ impl QueryCmd {
let mut client = QueryServiceClient::new(app.pd_channel().await?);

// Using an enum in the clap arguments was annoying; this is workable:
let storage_backend = match storage_backend.as_str() {
"jmt" => penumbra_proto::cnidarium::v1::key_value_request::StorageBackend::Jmt as i32,
match storage_backend.as_str() {
"nonverifiable" => {
penumbra_proto::cnidarium::v1::key_value_request::StorageBackend::Nonverifiable
as i32
}
// Default to JMT
_ => penumbra_proto::cnidarium::v1::key_value_request::StorageBackend::Jmt as i32,
};
let key_bytes = BASE64_STANDARD
.decode(&key)
.map_err(|e| anyhow::anyhow!(format!("invalid base64: {}", e)))?;

let req = penumbra_proto::cnidarium::v1::KeyValueRequest {
key: key.clone(),
storage_backend,
// Command-line queries don't have a reason to include proofs as of now.
proof: false,
..Default::default()
};
let req = penumbra_proto::cnidarium::v1::NonVerifiableKeyValueRequest {
key: Some(NVKey { inner: key_bytes }),
..Default::default()
};

tracing::debug!(?req);

tracing::debug!(?req);
let value = client
.non_verifiable_key_value(req)
.await?
.into_inner()
.value
.context(format!("key not found! key={}", key))?;

let value = client
.key_value(req)
.await?
.into_inner()
.value
.context(format!("key not found! key={}", key))?;
self.display_value(&value.value)?;
}
// Default to JMT
"jmt" | _ => {
let req = penumbra_proto::cnidarium::v1::KeyValueRequest {
key: key.clone(),
// Command-line queries don't have a reason to include proofs as of now.
proof: false,
..Default::default()
};

tracing::debug!(?req);

let value = client
.key_value(req)
.await?
.into_inner()
.value
.context(format!("key not found! key={}", key))?;

self.display_value(&value.value)?;
}
};

self.display_value(&value.value)?;
Ok(())
}

Expand Down
204 changes: 157 additions & 47 deletions crates/cnidarium/src/gen/penumbra.cnidarium.v1.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,69 @@
/// Performs a key-value query, either by key or by key hash.
/// Performs a key-value query against the nonverifiable storage,
/// using a byte-encoded key.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct NonVerifiableKeyValueRequest {
#[prost(message, optional, tag = "1")]
pub key: ::core::option::Option<non_verifiable_key_value_request::Key>,
}
/// Nested message and enum types in `NonVerifiableKeyValueRequest`.
pub mod non_verifiable_key_value_request {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Key {
#[prost(bytes = "vec", tag = "1")]
pub inner: ::prost::alloc::vec::Vec<u8>,
}
impl ::prost::Name for Key {
const NAME: &'static str = "Key";
const PACKAGE: &'static str = "penumbra.cnidarium.v1";
fn full_name() -> ::prost::alloc::string::String {
::prost::alloc::format!(
"penumbra.cnidarium.v1.NonVerifiableKeyValueRequest.{}", Self::NAME
)
}
}
}
impl ::prost::Name for NonVerifiableKeyValueRequest {
const NAME: &'static str = "NonVerifiableKeyValueRequest";
const PACKAGE: &'static str = "penumbra.cnidarium.v1";
fn full_name() -> ::prost::alloc::string::String {
::prost::alloc::format!("penumbra.cnidarium.v1.{}", Self::NAME)
}
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct NonVerifiableKeyValueResponse {
/// The value corresponding to the specified key, if it was found.
#[prost(message, optional, tag = "1")]
pub value: ::core::option::Option<non_verifiable_key_value_response::Value>,
}
/// Nested message and enum types in `NonVerifiableKeyValueResponse`.
pub mod non_verifiable_key_value_response {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Value {
#[prost(bytes = "vec", tag = "1")]
pub value: ::prost::alloc::vec::Vec<u8>,
}
impl ::prost::Name for Value {
const NAME: &'static str = "Value";
const PACKAGE: &'static str = "penumbra.cnidarium.v1";
fn full_name() -> ::prost::alloc::string::String {
::prost::alloc::format!(
"penumbra.cnidarium.v1.NonVerifiableKeyValueResponse.{}", Self::NAME
)
}
}
}
impl ::prost::Name for NonVerifiableKeyValueResponse {
const NAME: &'static str = "NonVerifiableKeyValueResponse";
const PACKAGE: &'static str = "penumbra.cnidarium.v1";
fn full_name() -> ::prost::alloc::string::String {
::prost::alloc::format!("penumbra.cnidarium.v1.{}", Self::NAME)
}
}
/// Performs a key-value query against the JMT, either by key or by key hash.
///
/// Proofs are only supported by key.
#[allow(clippy::derive_partial_eq_without_eq)]
Expand All @@ -10,52 +75,6 @@ pub struct KeyValueRequest {
/// whether to return a proof
#[prost(bool, tag = "3")]
pub proof: bool,
/// Which storage to query.
#[prost(enumeration = "key_value_request::StorageBackend", tag = "4")]
pub storage_backend: i32,
}
/// Nested message and enum types in `KeyValueRequest`.
pub mod key_value_request {
/// The storage type to query.
#[derive(
Clone,
Copy,
Debug,
PartialEq,
Eq,
Hash,
PartialOrd,
Ord,
::prost::Enumeration
)]
#[repr(i32)]
pub enum StorageBackend {
Unspecified = 0,
Jmt = 1,
Nonverifiable = 2,
}
impl StorageBackend {
/// String value of the enum field names used in the ProtoBuf definition.
///
/// The values are not transformed in any way and thus are considered stable
/// (if the ProtoBuf definition does not change) and safe for programmatic use.
pub fn as_str_name(&self) -> &'static str {
match self {
StorageBackend::Unspecified => "STORAGE_BACKEND_UNSPECIFIED",
StorageBackend::Jmt => "STORAGE_BACKEND_JMT",
StorageBackend::Nonverifiable => "STORAGE_BACKEND_NONVERIFIABLE",
}
}
/// Creates an enum from field names used in the ProtoBuf definition.
pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
match value {
"STORAGE_BACKEND_UNSPECIFIED" => Some(Self::Unspecified),
"STORAGE_BACKEND_JMT" => Some(Self::Jmt),
"STORAGE_BACKEND_NONVERIFIABLE" => Some(Self::Nonverifiable),
_ => None,
}
}
}
}
impl ::prost::Name for KeyValueRequest {
const NAME: &'static str = "KeyValueRequest";
Expand Down Expand Up @@ -342,6 +361,38 @@ pub mod query_service_client {
);
self.inner.unary(req, path, codec).await
}
/// General-purpose key-value state query API, that can be used to query
/// arbitrary keys in the non-verifiable storage.
pub async fn non_verifiable_key_value(
&mut self,
request: impl tonic::IntoRequest<super::NonVerifiableKeyValueRequest>,
) -> std::result::Result<
tonic::Response<super::NonVerifiableKeyValueResponse>,
tonic::Status,
> {
self.inner
.ready()
.await
.map_err(|e| {
tonic::Status::new(
tonic::Code::Unknown,
format!("Service was not ready: {}", e.into()),
)
})?;
let codec = tonic::codec::ProstCodec::default();
let path = http::uri::PathAndQuery::from_static(
"/penumbra.cnidarium.v1.QueryService/NonVerifiableKeyValue",
);
let mut req = request.into_request();
req.extensions_mut()
.insert(
GrpcMethod::new(
"penumbra.cnidarium.v1.QueryService",
"NonVerifiableKeyValue",
),
);
self.inner.unary(req, path, codec).await
}
/// General-purpose prefixed key-value state query API, that can be used to query
/// arbitrary prefixes in the JMT storage.
pub async fn prefix_value(
Expand Down Expand Up @@ -416,6 +467,15 @@ pub mod query_service_server {
tonic::Response<super::KeyValueResponse>,
tonic::Status,
>;
/// General-purpose key-value state query API, that can be used to query
/// arbitrary keys in the non-verifiable storage.
async fn non_verifiable_key_value(
&self,
request: tonic::Request<super::NonVerifiableKeyValueRequest>,
) -> std::result::Result<
tonic::Response<super::NonVerifiableKeyValueResponse>,
tonic::Status,
>;
/// Server streaming response type for the PrefixValue method.
type PrefixValueStream: tonic::codegen::tokio_stream::Stream<
Item = std::result::Result<super::PrefixValueResponse, tonic::Status>,
Expand Down Expand Up @@ -568,6 +628,56 @@ pub mod query_service_server {
};
Box::pin(fut)
}
"/penumbra.cnidarium.v1.QueryService/NonVerifiableKeyValue" => {
#[allow(non_camel_case_types)]
struct NonVerifiableKeyValueSvc<T: QueryService>(pub Arc<T>);
impl<
T: QueryService,
> tonic::server::UnaryService<super::NonVerifiableKeyValueRequest>
for NonVerifiableKeyValueSvc<T> {
type Response = super::NonVerifiableKeyValueResponse;
type Future = BoxFuture<
tonic::Response<Self::Response>,
tonic::Status,
>;
fn call(
&mut self,
request: tonic::Request<super::NonVerifiableKeyValueRequest>,
) -> Self::Future {
let inner = Arc::clone(&self.0);
let fut = async move {
<T as QueryService>::non_verifiable_key_value(
&inner,
request,
)
.await
};
Box::pin(fut)
}
}
let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let max_decoding_message_size = self.max_decoding_message_size;
let max_encoding_message_size = self.max_encoding_message_size;
let inner = self.inner.clone();
let fut = async move {
let inner = inner.0;
let method = NonVerifiableKeyValueSvc(inner);
let codec = tonic::codec::ProstCodec::default();
let mut grpc = tonic::server::Grpc::new(codec)
.apply_compression_config(
accept_compression_encodings,
send_compression_encodings,
)
.apply_max_message_size_config(
max_decoding_message_size,
max_encoding_message_size,
);
let res = grpc.unary(method, req).await;
Ok(res)
};
Box::pin(fut)
}
"/penumbra.cnidarium.v1.QueryService/PrefixValue" => {
#[allow(non_camel_case_types)]
struct PrefixValueSvc<T: QueryService>(pub Arc<T>);
Expand Down
Loading

0 comments on commit 3257d11

Please sign in to comment.