Skip to content

Commit

Permalink
router: add with_options for handlers that use request/response
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Oct 24, 2022
1 parent 3835cd5 commit 276845c
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 103 deletions.
32 changes: 19 additions & 13 deletions apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ pub async fn query_epoch(args: args::Query) -> Epoch {
/// Query the raw bytes of given storage key
pub async fn query_raw_bytes(_ctx: Context, args: args::QueryRawBytes) {
let client = HttpClient::new(args.query.ledger_address).unwrap();
let bytes = unwrap_client_response(
RPC.shell().storage_value(&client, &args.storage_key).await,
let response = unwrap_client_response(
RPC.shell()
.storage_value(&client, None, None, false, &args.storage_key)
.await,
);
match bytes {
match response.data {
Some(bytes) => println!("Found data: 0x{}", HEXLOWER.encode(&bytes)),
None => println!("No data found for key {}", args.storage_key),
}
Expand Down Expand Up @@ -1032,9 +1034,7 @@ pub async fn dry_run_tx(ledger_address: &TendermintAddress, tx_bytes: Vec<u8>) {
let client = HttpClient::new(ledger_address.clone()).unwrap();
let (data, height, prove) = (Some(tx_bytes), None, false);
let result = unwrap_client_response(
RPC.shell()
.dry_run_tx_with_options(&client, data, height, prove)
.await,
RPC.shell().dry_run_tx(&client, data, height, prove).await,
)
.data;
println!("Dry-run result: {}", result);
Expand Down Expand Up @@ -1249,9 +1249,12 @@ pub async fn query_storage_value<T>(
where
T: BorshDeserialize,
{
let bytes =
unwrap_client_response(RPC.shell().storage_value(client, key).await);
bytes.map(|bytes| {
let response = unwrap_client_response(
RPC.shell()
.storage_value(client, None, None, false, key)
.await,
);
response.data.map(|bytes| {
T::try_from_slice(&bytes[..]).unwrap_or_else(|err| {
eprintln!("Error decoding the value: {}", err);
cli::safe_exit(1)
Expand All @@ -1269,8 +1272,11 @@ pub async fn query_storage_prefix<T>(
where
T: BorshDeserialize,
{
let values =
unwrap_client_response(RPC.shell().storage_prefix(client, key).await);
let values = unwrap_client_response(
RPC.shell()
.storage_prefix(client, None, None, false, key)
.await,
);
let decode =
|PrefixValue { key, value }: PrefixValue| match T::try_from_slice(
&value[..],
Expand All @@ -1284,10 +1290,10 @@ where
}
Ok(value) => Some((key, value)),
};
if values.is_empty() {
if values.data.is_empty() {
None
} else {
Some(values.into_iter().filter_map(decode))
Some(values.data.into_iter().filter_map(decode))
}
}

Expand Down
14 changes: 12 additions & 2 deletions shared/src/ledger/queries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ where
Ok(())
}

/// For queries that only support latest height, check that the given height is
/// not different from latest height, otherwise return an error.
/// For queries that don't support proofs, require that they are not requested.
pub fn require_no_proof(request: &RequestQuery) -> storage_api::Result<()> {
if request.prove {
return Err(storage_api::Error::new_const(
Expand All @@ -68,6 +67,17 @@ pub fn require_no_proof(request: &RequestQuery) -> storage_api::Result<()> {
Ok(())
}

/// For queries that don't use request data, require that there are no data
/// attached.
pub fn require_no_data(request: &RequestQuery) -> storage_api::Result<()> {
if !request.data.is_empty() {
return Err(storage_api::Error::new_const(
"This query doesn't accept request data",
));
}
Ok(())
}

#[cfg(any(test, feature = "tendermint-rpc"))]
/// Provides [`Client`] implementation for Tendermint RPC client
pub mod tm {
Expand Down
Loading

0 comments on commit 276845c

Please sign in to comment.