Skip to content

Commit

Permalink
Support nonverifiable storage in the pcli query key interface (#4380)
Browse files Browse the repository at this point in the history
## Describe your changes

This PR adds support for querying nonverifiable storage in the `pcli
query key` interface.

It also changes the API to only generate proofs if requested.

## Issue ticket number and link

Closes #4359 and closes #2647

## Checklist before requesting a review

- [x] If this code contains consensus-breaking changes, I have added the
"consensus-breaking" label. Otherwise, I declare my belief that there
are not consensus-breaking changes, for the following reason:

  > Only changes the key value query API
  • Loading branch information
zbuc authored May 16, 2024
1 parent abfd764 commit 927312c
Show file tree
Hide file tree
Showing 12 changed files with 1,221 additions and 27 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 61 additions & 15 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 All @@ -40,7 +42,18 @@ pub enum QueryCmd {
/// Queries an arbitrary key.
Key {
/// The key to query.
///
/// When querying the JMT, keys are plain string values.
///
/// When querying nonverifiable storage, keys should be base64-encoded strings.
key: String,
/// The storage backend to query.
///
/// Valid arguments are "jmt" and "nonverifiable".
///
/// Defaults to the JMT.
#[clap(long, default_value = "jmt")]
storage_backend: String,
},
/// Queries shielded pool data.
#[clap(subcommand)]
Expand Down Expand Up @@ -145,7 +158,7 @@ impl QueryCmd {
return Ok(());
}

let key = match self {
let (key, storage_backend) = match self {
QueryCmd::Tx(_)
| QueryCmd::Chain(_)
| QueryCmd::Validator(_)
Expand All @@ -157,28 +170,61 @@ impl QueryCmd {
| QueryCmd::Ibc(_) => {
unreachable!("query handled in guard");
}
QueryCmd::ShieldedPool(p) => p.key().clone(),
QueryCmd::Key { key } => key.clone(),
QueryCmd::ShieldedPool(p) => (p.key().clone(), "jmt".to_string()),
QueryCmd::Key {
key,
storage_backend,
} => (key.clone(), storage_backend.clone()),
};

use penumbra_proto::cnidarium::v1::query_service_client::QueryServiceClient;
let mut client = QueryServiceClient::new(app.pd_channel().await?);

let req = penumbra_proto::cnidarium::v1::KeyValueRequest {
key: key.clone(),
..Default::default()
};
// Using an enum in the clap arguments was annoying; this is workable:
match storage_backend.as_str() {
"nonverifiable" => {
let key_bytes = BASE64_STANDARD
.decode(&key)
.map_err(|e| anyhow::anyhow!(format!("invalid base64: {}", e)))?;

tracing::debug!(?req);
let req = penumbra_proto::cnidarium::v1::NonVerifiableKeyValueRequest {
key: Some(NVKey { inner: key_bytes }),
..Default::default()
};

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

let value = client
.non_verifiable_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
1 change: 1 addition & 0 deletions crates/cnidarium/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rpc = ["dep:tonic", "dep:prost", "dep:serde", "dep:pbjson", "dep:ibc-proto"]
[dependencies]
anyhow = {workspace = true}
async-trait = {workspace = true}
base64 = {workspace = true}
borsh = { version = "1.3.0" , features = ["derive", "de_strict_order"]}
futures = {workspace = true}
hex = {workspace = true}
Expand Down
158 changes: 157 additions & 1 deletion 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 Down Expand Up @@ -296,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 @@ -370,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 @@ -522,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 927312c

Please sign in to comment.