From a51bc2edd5240defcb3d17a13a5e5f2333291574 Mon Sep 17 00:00:00 2001 From: teor Date: Fri, 3 Feb 2023 12:27:29 +1000 Subject: [PATCH] Make the verbosity argument optional in the getblock RPC (#6092) --- zebra-rpc/src/methods.rs | 12 +++++- zebra-rpc/src/methods/tests/snapshot.rs | 21 +++++++--- ...> get_block_verbose_1_arg@mainnet_10.snap} | 0 ...> get_block_verbose_1_arg@testnet_10.snap} | 0 .../get_block_verbose_2_args@mainnet_10.snap | 9 +++++ .../get_block_verbose_2_args@testnet_10.snap | 9 +++++ zebra-rpc/src/methods/tests/vectors.rs | 38 ++++++++++++++++--- 7 files changed, 77 insertions(+), 12 deletions(-) rename zebra-rpc/src/methods/tests/snapshots/{get_block_verbose@mainnet_10.snap => get_block_verbose_1_arg@mainnet_10.snap} (100%) rename zebra-rpc/src/methods/tests/snapshots/{get_block_verbose@testnet_10.snap => get_block_verbose_1_arg@testnet_10.snap} (100%) create mode 100644 zebra-rpc/src/methods/tests/snapshots/get_block_verbose_2_args@mainnet_10.snap create mode 100644 zebra-rpc/src/methods/tests/snapshots/get_block_verbose_2_args@testnet_10.snap diff --git a/zebra-rpc/src/methods.rs b/zebra-rpc/src/methods.rs index 454b27bc..94f8c721 100644 --- a/zebra-rpc/src/methods.rs +++ b/zebra-rpc/src/methods.rs @@ -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>; + fn get_block(&self, height: String, verbosity: Option) -> BoxFuture>; /// Returns the hash of the current best blockchain tip block, as a [`GetBlockHash`] JSON string. /// @@ -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> { + fn get_block( + &self, + hash_or_height: String, + verbosity: Option, + ) -> BoxFuture> { + // From + 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 = diff --git a/zebra-rpc/src/methods/tests/snapshot.rs b/zebra-rpc/src/methods/tests/snapshot.rs index f25593e4..6b1b26f5 100644 --- a/zebra-rpc/src/methods/tests/snapshot.rs +++ b/zebra-rpc/src/methods/tests/snapshot.rs @@ -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 @@ -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. diff --git a/zebra-rpc/src/methods/tests/snapshots/get_block_verbose@mainnet_10.snap b/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_1_arg@mainnet_10.snap similarity index 100% rename from zebra-rpc/src/methods/tests/snapshots/get_block_verbose@mainnet_10.snap rename to zebra-rpc/src/methods/tests/snapshots/get_block_verbose_1_arg@mainnet_10.snap diff --git a/zebra-rpc/src/methods/tests/snapshots/get_block_verbose@testnet_10.snap b/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_1_arg@testnet_10.snap similarity index 100% rename from zebra-rpc/src/methods/tests/snapshots/get_block_verbose@testnet_10.snap rename to zebra-rpc/src/methods/tests/snapshots/get_block_verbose_1_arg@testnet_10.snap diff --git a/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_2_args@mainnet_10.snap b/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_2_args@mainnet_10.snap new file mode 100644 index 00000000..b9fc9794 --- /dev/null +++ b/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_2_args@mainnet_10.snap @@ -0,0 +1,9 @@ +--- +source: zebra-rpc/src/methods/tests/snapshot.rs +expression: block +--- +{ + "tx": [ + "851bf6fbf7a976327817c738c489d7fa657752445430922d94c983c0b9ed4609" + ] +} diff --git a/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_2_args@testnet_10.snap b/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_2_args@testnet_10.snap new file mode 100644 index 00000000..dbc2ce43 --- /dev/null +++ b/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_2_args@testnet_10.snap @@ -0,0 +1,9 @@ +--- +source: zebra-rpc/src/methods/tests/snapshot.rs +expression: block +--- +{ + "tx": [ + "f37e9f691fffb635de0999491d906ee85ba40cd36dae9f6e5911a8277d7c5f75" + ] +} diff --git a/zebra-rpc/src/methods/tests/vectors.rs b/zebra-rpc/src/methods/tests/vectors.rs index 0b6df288..7317aaf0 100644 --- a/zebra-rpc/src/methods/tests/vectors.rs +++ b/zebra-rpc/src/methods/tests/vectors.rs @@ -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"); @@ -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"); @@ -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()); @@ -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