Skip to content

Commit

Permalink
Make the verbosity argument optional in the getblock RPC (#6092)
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 authored Feb 3, 2023
1 parent 0793eaf commit a51bc2e
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 12 deletions.
12 changes: 10 additions & 2 deletions zebra-rpc/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub trait Rpc {
/// getting blocks by hash. (But we parse the height as a JSON string, not an integer).
/// `lightwalletd` also does not use verbosity=2, so we don't support it.
#[rpc(name = "getblock")]
fn get_block(&self, height: String, verbosity: u8) -> BoxFuture<Result<GetBlock>>;
fn get_block(&self, height: String, verbosity: Option<u8>) -> BoxFuture<Result<GetBlock>>;

/// Returns the hash of the current best blockchain tip block, as a [`GetBlockHash`] JSON string.
///
Expand Down Expand Up @@ -556,8 +556,16 @@ where
// - use `height_from_signed_int()` to handle negative heights
// (this might be better in the state request, because it needs the state height)
// - create a function that handles block hashes or heights, and use it in `z_get_treestate()`
fn get_block(&self, hash_or_height: String, verbosity: u8) -> BoxFuture<Result<GetBlock>> {
fn get_block(
&self,
hash_or_height: String,
verbosity: Option<u8>,
) -> BoxFuture<Result<GetBlock>> {
// From <https://zcash.github.io/rpc/getblock.html>
const DEFAULT_GETBLOCK_VERBOSITY: u8 = 1;

let mut state = self.state.clone();
let verbosity = verbosity.unwrap_or(DEFAULT_GETBLOCK_VERBOSITY);

async move {
let hash_or_height: HashOrHeight =
Expand Down
21 changes: 16 additions & 5 deletions zebra-rpc/src/methods/tests/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,24 @@ async fn test_rpc_response_data_for_network(network: Network) {
// `getblock`, verbosity=0
const BLOCK_HEIGHT: u32 = 1;
let get_block = rpc
.get_block(BLOCK_HEIGHT.to_string(), 0u8)
.get_block(BLOCK_HEIGHT.to_string(), Some(0u8))
.await
.expect("We should have a GetBlock struct");
snapshot_rpc_getblock(get_block, block_data.get(&BLOCK_HEIGHT).unwrap(), &settings);

// `getblock`, verbosity=1
let get_block = rpc
.get_block(BLOCK_HEIGHT.to_string(), 1u8)
.get_block(BLOCK_HEIGHT.to_string(), Some(1u8))
.await
.expect("We should have a GetBlock struct");
snapshot_rpc_getblock_verbose(get_block, &settings);
snapshot_rpc_getblock_verbose("2_args", get_block, &settings);

// `getblock`, no verbosity, defaults to 1
let get_block = rpc
.get_block(BLOCK_HEIGHT.to_string(), None)
.await
.expect("We should have a GetBlock struct");
snapshot_rpc_getblock_verbose("1_arg", get_block, &settings);

// `getbestblockhash`
let get_best_block_hash = rpc
Expand Down Expand Up @@ -251,8 +258,12 @@ fn snapshot_rpc_getblock(block: GetBlock, block_data: &[u8], settings: &insta::S
}

/// Check `getblock` response with verbosity=1, using `cargo insta` and JSON serialization.
fn snapshot_rpc_getblock_verbose(block: GetBlock, settings: &insta::Settings) {
settings.bind(|| insta::assert_json_snapshot!("get_block_verbose", block));
fn snapshot_rpc_getblock_verbose(
variant: &'static str,
block: GetBlock,
settings: &insta::Settings,
) {
settings.bind(|| insta::assert_json_snapshot!(format!("get_block_verbose_{variant}"), block));
}

/// Snapshot `getbestblockhash` response, using `cargo insta` and JSON serialization.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: zebra-rpc/src/methods/tests/snapshot.rs
expression: block
---
{
"tx": [
"851bf6fbf7a976327817c738c489d7fa657752445430922d94c983c0b9ed4609"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: zebra-rpc/src/methods/tests/snapshot.rs
expression: block
---
{
"tx": [
"f37e9f691fffb635de0999491d906ee85ba40cd36dae9f6e5911a8277d7c5f75"
]
}
38 changes: 33 additions & 5 deletions zebra-rpc/src/methods/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ async fn rpc_getblock() {
let expected_result = GetBlock::Raw(block.clone().into());

let get_block = rpc
.get_block(i.to_string(), 0u8)
.get_block(i.to_string(), Some(0u8))
.await
.expect("We should have a GetBlock struct");

assert_eq!(get_block, expected_result);

let get_block = rpc
.get_block(block.hash().to_string(), 0u8)
.get_block(block.hash().to_string(), Some(0u8))
.await
.expect("We should have a GetBlock struct");

Expand All @@ -102,7 +102,25 @@ async fn rpc_getblock() {
// Make calls with verbosity=1 and check response
for (i, block) in blocks.iter().enumerate() {
let get_block = rpc
.get_block(i.to_string(), 1u8)
.get_block(i.to_string(), Some(1u8))
.await
.expect("We should have a GetBlock struct");

assert_eq!(
get_block,
GetBlock::Object {
tx: block
.transactions
.iter()
.map(|tx| tx.hash().encode_hex())
.collect()
}
);
}

for (i, block) in blocks.iter().enumerate() {
let get_block = rpc
.get_block(i.to_string(), None)
.await
.expect("We should have a GetBlock struct");

Expand Down Expand Up @@ -144,7 +162,17 @@ async fn rpc_getblock_parse_error() {

// Make sure we get an error if Zebra can't parse the block height.
assert!(rpc
.get_block("not parsable as height".to_string(), 0u8)
.get_block("not parsable as height".to_string(), Some(0u8))
.await
.is_err());

assert!(rpc
.get_block("not parsable as height".to_string(), Some(1u8))
.await
.is_err());

assert!(rpc
.get_block("not parsable as height".to_string(), None)
.await
.is_err());

Expand Down Expand Up @@ -175,7 +203,7 @@ async fn rpc_getblock_missing_error() {

// Make sure Zebra returns the correct error code `-8` for missing blocks
// https://github.com/adityapk00/lightwalletd/blob/c1bab818a683e4de69cd952317000f9bb2932274/common/common.go#L251-L254
let block_future = tokio::spawn(rpc.get_block("0".to_string(), 0u8));
let block_future = tokio::spawn(rpc.get_block("0".to_string(), Some(0u8)));

// Make the mock service respond with no block
let response_handler = state
Expand Down

0 comments on commit a51bc2e

Please sign in to comment.