From c9421d0ecca0e947dadc3a67fd706fbfe542dd05 Mon Sep 17 00:00:00 2001 From: Arya Date: Mon, 8 Apr 2024 16:16:11 -0400 Subject: [PATCH] always includes height in verbose output of getblock method --- zebra-rpc/src/methods.rs | 25 +++++++++---------- ...k_verbose_hash_verbosity_1@mainnet_10.snap | 1 + ...k_verbose_hash_verbosity_1@testnet_10.snap | 1 + ...ose_hash_verbosity_default@mainnet_10.snap | 1 + ...ose_hash_verbosity_default@testnet_10.snap | 1 + zebra-rpc/src/methods/tests/vectors.rs | 16 ++++++------ zebra-state/src/response.rs | 4 +-- zebra-state/src/service/block_iter.rs | 2 +- .../service/finalized_state/zebra_db/block.rs | 9 ++++--- zebra-state/src/service/read/block.rs | 4 +-- zebra-state/src/service/read/find.rs | 2 +- 11 files changed, 36 insertions(+), 30 deletions(-) diff --git a/zebra-rpc/src/methods.rs b/zebra-rpc/src/methods.rs index 1b33a8abf4d..32200aa803c 100644 --- a/zebra-rpc/src/methods.rs +++ b/zebra-rpc/src/methods.rs @@ -909,22 +909,23 @@ where _ => unreachable!("unmatched response to a SaplingTree request"), }; - // TODO: look up the height if we only have a hash, - // this needs a new state request for the height -> hash index - let height = hash_or_height.height(); - let orchard_note_commitment_tree_count = match orchard_tree_response { zebra_state::ReadResponse::OrchardTree(Some(nct)) => nct.count(), zebra_state::ReadResponse::OrchardTree(None) => 0, _ => unreachable!("unmatched response to a OrchardTree request"), }; - let block_header = match block_header_response { - zebra_state::ReadResponse::BlockHeader(header) => header, + let (height, block_header) = match block_header_response { + zebra_state::ReadResponse::BlockHeader(Some(header)) => header, + zebra_state::ReadResponse::BlockHeader(None) => Err(Error { + code: MISSING_BLOCK_ERROR_CODE, + message: "Block not found".to_string(), + data: None, + })?, _ => unreachable!("unmatched response to a BlockHeader request"), }; - let time = block_header.map(|header| header.time.timestamp()); + let time = block_header.time.timestamp(); let confirmations = match depth_response { // Confirmations are one more than the depth. @@ -1664,12 +1665,10 @@ pub enum GetBlock { confirmations: i64, /// The height of the requested block. - #[serde(skip_serializing_if = "Option::is_none")] - height: Option, + height: Height, /// The height of the requested block. - #[serde(skip_serializing_if = "Option::is_none")] - time: Option, + time: i64, /// List of transaction IDs in block order, hex-encoded. // @@ -1686,8 +1685,8 @@ impl Default for GetBlock { GetBlock::Object { hash: GetBlockHash::default(), confirmations: 0, - height: None, - time: None, + height: Height::MIN, + time: 0, tx: Vec::new(), trees: GetBlockTrees::default(), } diff --git a/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_hash_verbosity_1@mainnet_10.snap b/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_hash_verbosity_1@mainnet_10.snap index 3bcf968bed5..f18b879f6b3 100644 --- a/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_hash_verbosity_1@mainnet_10.snap +++ b/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_hash_verbosity_1@mainnet_10.snap @@ -5,6 +5,7 @@ expression: block { "hash": "0007bc227e1c57a4a70e237cad00e7b7ce565155ab49166bc57397a26d339283", "confirmations": 10, + "height": 1, "time": 1477671596, "tx": [ "851bf6fbf7a976327817c738c489d7fa657752445430922d94c983c0b9ed4609" diff --git a/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_hash_verbosity_1@testnet_10.snap b/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_hash_verbosity_1@testnet_10.snap index 7ea021d3382..013a4c09b23 100644 --- a/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_hash_verbosity_1@testnet_10.snap +++ b/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_hash_verbosity_1@testnet_10.snap @@ -5,6 +5,7 @@ expression: block { "hash": "025579869bcf52a989337342f5f57a84f3a28b968f7d6a8307902b065a668d23", "confirmations": 10, + "height": 1, "time": 1477674473, "tx": [ "f37e9f691fffb635de0999491d906ee85ba40cd36dae9f6e5911a8277d7c5f75" diff --git a/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_hash_verbosity_default@mainnet_10.snap b/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_hash_verbosity_default@mainnet_10.snap index 3bcf968bed5..f18b879f6b3 100644 --- a/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_hash_verbosity_default@mainnet_10.snap +++ b/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_hash_verbosity_default@mainnet_10.snap @@ -5,6 +5,7 @@ expression: block { "hash": "0007bc227e1c57a4a70e237cad00e7b7ce565155ab49166bc57397a26d339283", "confirmations": 10, + "height": 1, "time": 1477671596, "tx": [ "851bf6fbf7a976327817c738c489d7fa657752445430922d94c983c0b9ed4609" diff --git a/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_hash_verbosity_default@testnet_10.snap b/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_hash_verbosity_default@testnet_10.snap index 7ea021d3382..013a4c09b23 100644 --- a/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_hash_verbosity_default@testnet_10.snap +++ b/zebra-rpc/src/methods/tests/snapshots/get_block_verbose_hash_verbosity_default@testnet_10.snap @@ -5,6 +5,7 @@ expression: block { "hash": "025579869bcf52a989337342f5f57a84f3a28b968f7d6a8307902b065a668d23", "confirmations": 10, + "height": 1, "time": 1477674473, "tx": [ "f37e9f691fffb635de0999491d906ee85ba40cd36dae9f6e5911a8277d7c5f75" diff --git a/zebra-rpc/src/methods/tests/vectors.rs b/zebra-rpc/src/methods/tests/vectors.rs index c6cbb4289f2..3d353d1ab43 100644 --- a/zebra-rpc/src/methods/tests/vectors.rs +++ b/zebra-rpc/src/methods/tests/vectors.rs @@ -139,8 +139,8 @@ async fn rpc_getblock() { GetBlock::Object { hash: GetBlockHash(block.hash()), confirmations: (blocks.len() - i).try_into().expect("valid i64"), - height: Some(Height(i.try_into().expect("valid u32"))), - time: Some(block.header.time.timestamp()), + height: Height(i.try_into().expect("valid u32")), + time: block.header.time.timestamp(), tx: block .transactions .iter() @@ -163,8 +163,8 @@ async fn rpc_getblock() { GetBlock::Object { hash: GetBlockHash(block.hash()), confirmations: (blocks.len() - i).try_into().expect("valid i64"), - height: None, - time: Some(block.header.time.timestamp()), + height: Height(i.try_into().expect("valid u32")), + time: block.header.time.timestamp(), tx: block .transactions .iter() @@ -187,8 +187,8 @@ async fn rpc_getblock() { GetBlock::Object { hash: GetBlockHash(block.hash()), confirmations: (blocks.len() - i).try_into().expect("valid i64"), - height: Some(Height(i.try_into().expect("valid u32"))), - time: Some(block.header.time.timestamp()), + height: Height(i.try_into().expect("valid u32")), + time: block.header.time.timestamp(), tx: block .transactions .iter() @@ -211,8 +211,8 @@ async fn rpc_getblock() { GetBlock::Object { hash: GetBlockHash(block.hash()), confirmations: (blocks.len() - i).try_into().expect("valid i64"), - height: None, - time: Some(block.header.time.timestamp()), + height: Height(i.try_into().expect("valid u32")), + time: block.header.time.timestamp(), tx: block .transactions .iter() diff --git a/zebra-state/src/response.rs b/zebra-state/src/response.rs index 242191f8259..1c11f1000cf 100644 --- a/zebra-state/src/response.rs +++ b/zebra-state/src/response.rs @@ -51,7 +51,7 @@ pub enum Response { Block(Option>), /// The response to a `BlockHeader` request. - BlockHeader(Option>), + BlockHeader(Option<(block::Height, Arc)>), /// The response to a `AwaitUtxo` request, from any non-finalized chains, finalized chain, /// pending unverified blocks, or blocks received after the request was sent. @@ -135,7 +135,7 @@ pub enum ReadResponse { Block(Option>), /// The response to a `BlockHeader` request. - BlockHeader(Option>), + BlockHeader(Option<(block::Height, Arc)>), /// Response to [`ReadRequest::Transaction`] with the specified transaction. Transaction(Option), diff --git a/zebra-state/src/service/block_iter.rs b/zebra-state/src/service/block_iter.rs index 6cb0df903ba..64491b79f87 100644 --- a/zebra-state/src/service/block_iter.rs +++ b/zebra-state/src/service/block_iter.rs @@ -138,7 +138,7 @@ impl ChainItem for block::Header { type Type = Arc; fn read(chain: Option<&Arc>, db: &ZebraDb, height: Height) -> Option { - read::block_header(chain, db, height.into()) + read::block_header(chain, db, height.into()).map(|(_height, header)| header) } } diff --git a/zebra-state/src/service/finalized_state/zebra_db/block.rs b/zebra-state/src/service/finalized_state/zebra_db/block.rs index e10fd3b43b4..af25f7d89c5 100644 --- a/zebra-state/src/service/finalized_state/zebra_db/block.rs +++ b/zebra-state/src/service/finalized_state/zebra_db/block.rs @@ -118,14 +118,17 @@ impl ZebraDb { // // TODO: move this method to the start of the section #[allow(clippy::unwrap_in_result)] - pub fn block_header(&self, hash_or_height: HashOrHeight) -> Option> { + pub fn block_header( + &self, + hash_or_height: HashOrHeight, + ) -> Option<(block::Height, Arc)> { // Block Header let block_header_by_height = self.db.cf_handle("block_header_by_height").unwrap(); let height = hash_or_height.height_or_else(|hash| self.height(hash))?; let header = self.db.zs_get(&block_header_by_height, &height)?; - Some(header) + Some((height, header)) } /// Returns the [`Block`] with [`block::Hash`] or @@ -136,7 +139,7 @@ impl ZebraDb { pub fn block(&self, hash_or_height: HashOrHeight) -> Option> { // Block let height = hash_or_height.height_or_else(|hash| self.height(hash))?; - let header = self.block_header(height.into())?; + let (_height, header) = self.block_header(height.into())?; // Transactions let tx_by_loc = self.db.cf_handle("tx_by_loc").unwrap(); diff --git a/zebra-state/src/service/read/block.rs b/zebra-state/src/service/read/block.rs index 283fe9ddc4f..3890b5b702e 100644 --- a/zebra-state/src/service/read/block.rs +++ b/zebra-state/src/service/read/block.rs @@ -54,7 +54,7 @@ pub fn block_header( chain: Option, db: &ZebraDb, hash_or_height: HashOrHeight, -) -> Option> +) -> Option<(block::Height, Arc)> where C: AsRef, { @@ -66,7 +66,7 @@ where chain .as_ref() .and_then(|chain| chain.as_ref().block(hash_or_height)) - .map(|contextual| contextual.block.header.clone()) + .map(|contextual| (contextual.height, contextual.block.header.clone())) .or_else(|| db.block_header(hash_or_height)) } diff --git a/zebra-state/src/service/read/find.rs b/zebra-state/src/service/read/find.rs index 44491876ec9..c3d5a45a239 100644 --- a/zebra-state/src/service/read/find.rs +++ b/zebra-state/src/service/read/find.rs @@ -471,7 +471,7 @@ where // All the headers should be in the chain. // If they are not, we don't want to return them. height_range.into_iter().map_while(|height| { - let header = block_header(chain, db, Height(height).into()); + let header = block_header(chain, db, Height(height).into()).map(|(_height, header)| header); // A recently committed block dropped the intersection we previously found if header.is_none() {