Only returns block time for verbosity = 2, updates snapshots

This commit is contained in:
Arya 2024-04-09 23:44:32 -04:00
parent 5da0ca6093
commit c4fe4739aa
17 changed files with 172 additions and 28 deletions

View File

@ -682,9 +682,6 @@ 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()`
// - Type second argument as an enum with variant for each verbosity level and a `Fields` variant
// for specifying exactly which fields should be included in the return value. Consider replacing
// `ReadRequest::BlockHeader` with a `PartialBlockData` request that accepts the fields?
fn get_block(
&self,
hash_or_height: String,
@ -733,7 +730,7 @@ where
}),
_ => unreachable!("unmatched response to a block request"),
}
} else if verbosity == 1 {
} else if verbosity == 1 || verbosity == 2 {
// # Performance
//
// This RPC is used in `lightwalletd`'s initial sync of 2 million blocks,
@ -754,6 +751,8 @@ where
// must be able to handle chain forks, including a hash for a block that is
// later discovered to be on a side chain.
let should_read_block_header = verbosity == 2;
let hash = match hash_or_height {
HashOrHeight::Hash(hash) => hash,
HashOrHeight::Height(height) => {
@ -790,7 +789,7 @@ where
//
// We look up by block hash so the hash, transaction IDs, and confirmations
// are consistent.
let requests = [
let mut requests = vec![
// Get transaction IDs from the transaction index by block hash
//
// # Concurrency
@ -812,10 +811,13 @@ where
// different confirmation values before or after added blocks, and switching
// between -1 and multiple different confirmation values.
zebra_state::ReadRequest::Depth(hash),
// Block header
zebra_state::ReadRequest::BlockHeader(hash.into()),
];
if should_read_block_header {
// Block header
requests.push(zebra_state::ReadRequest::BlockHeader(hash.into()))
}
let mut futs = FuturesOrdered::new();
for request in requests {
@ -848,14 +850,6 @@ where
_ => unreachable!("unmatched response to a OrchardTree request"),
};
let block_header_response = futs.next().await.expect("`futs` should not be empty");
let block_header = match block_header_response.map_server_error()? {
zebra_state::ReadResponse::BlockHeader(header) => {
header.ok_or_server_error("Block not found")?
}
_ => unreachable!("unmatched response to a BlockHeader request"),
};
// From <https://zcash.github.io/rpc/getblock.html>
const NOT_IN_BEST_CHAIN_CONFIRMATIONS: i64 = -1;
@ -868,6 +862,23 @@ where
_ => unreachable!("unmatched response to a depth request"),
};
let time = if should_read_block_header {
let block_header_response =
futs.next().await.expect("`futs` should not be empty");
match block_header_response.map_server_error()? {
zebra_state::ReadResponse::BlockHeader(header) => Some(
header
.ok_or_server_error("Block not found")?
.time
.timestamp(),
),
_ => unreachable!("unmatched response to a BlockHeader request"),
}
} else {
None
};
let sapling = SaplingTrees {
size: sapling_note_commitment_tree_count,
};
@ -882,7 +893,7 @@ where
hash: GetBlockHash(hash),
confirmations,
height,
time: Some(block_header.time.timestamp()),
time,
tx,
trees,
})

View File

@ -2,7 +2,7 @@
//!
//! To update these snapshots, run:
//! ```sh
//! cargo insta test --review
//! cargo insta test --review -p zebra-rpc --lib -- test_rpc_response_data
//! ```
use std::{collections::BTreeMap, sync::Arc};
@ -167,6 +167,25 @@ async fn test_rpc_response_data_for_network(network: &Network) {
.expect("We should have a GetBlock struct");
snapshot_rpc_getblock_verbose("hash_verbosity_1", get_block, &settings);
// `getblock`, verbosity=2, height
let get_block = rpc
.get_block(BLOCK_HEIGHT.to_string(), Some(2u8))
.await
.expect("We should have a GetBlock struct");
snapshot_rpc_getblock_verbose("height_verbosity_2", get_block, &settings);
let get_block = rpc
.get_block(EXCESSIVE_BLOCK_HEIGHT.to_string(), Some(2u8))
.await;
snapshot_rpc_getblock_invalid("excessive_height_verbosity_2", get_block, &settings);
// `getblock`, verbosity=2, hash
let get_block = rpc
.get_block(block_hash.to_string(), Some(2u8))
.await
.expect("We should have a GetBlock struct");
snapshot_rpc_getblock_verbose("hash_verbosity_2", get_block, &settings);
// `getblock`, no verbosity - defaults to 1, height
let get_block = rpc
.get_block(BLOCK_HEIGHT.to_string(), None)

View File

@ -0,0 +1,10 @@
---
source: zebra-rpc/src/methods/tests/snapshot.rs
expression: response
---
{
"Err": {
"code": -8,
"message": "block height not in best chain"
}
}

View File

@ -0,0 +1,10 @@
---
source: zebra-rpc/src/methods/tests/snapshot.rs
expression: response
---
{
"Err": {
"code": -8,
"message": "block height not in best chain"
}
}

View File

@ -5,7 +5,6 @@ expression: block
{
"hash": "0007bc227e1c57a4a70e237cad00e7b7ce565155ab49166bc57397a26d339283",
"confirmations": 10,
"time": 1477671596,
"tx": [
"851bf6fbf7a976327817c738c489d7fa657752445430922d94c983c0b9ed4609"
],

View File

@ -5,7 +5,6 @@ expression: block
{
"hash": "025579869bcf52a989337342f5f57a84f3a28b968f7d6a8307902b065a668d23",
"confirmations": 10,
"time": 1477674473,
"tx": [
"f37e9f691fffb635de0999491d906ee85ba40cd36dae9f6e5911a8277d7c5f75"
],

View File

@ -0,0 +1,13 @@
---
source: zebra-rpc/src/methods/tests/snapshot.rs
expression: block
---
{
"hash": "0007bc227e1c57a4a70e237cad00e7b7ce565155ab49166bc57397a26d339283",
"confirmations": 10,
"time": 1477671596,
"tx": [
"851bf6fbf7a976327817c738c489d7fa657752445430922d94c983c0b9ed4609"
],
"trees": {}
}

View File

@ -0,0 +1,13 @@
---
source: zebra-rpc/src/methods/tests/snapshot.rs
expression: block
---
{
"hash": "025579869bcf52a989337342f5f57a84f3a28b968f7d6a8307902b065a668d23",
"confirmations": 10,
"time": 1477674473,
"tx": [
"f37e9f691fffb635de0999491d906ee85ba40cd36dae9f6e5911a8277d7c5f75"
],
"trees": {}
}

View File

@ -5,7 +5,6 @@ expression: block
{
"hash": "0007bc227e1c57a4a70e237cad00e7b7ce565155ab49166bc57397a26d339283",
"confirmations": 10,
"time": 1477671596,
"tx": [
"851bf6fbf7a976327817c738c489d7fa657752445430922d94c983c0b9ed4609"
],

View File

@ -5,7 +5,6 @@ expression: block
{
"hash": "025579869bcf52a989337342f5f57a84f3a28b968f7d6a8307902b065a668d23",
"confirmations": 10,
"time": 1477674473,
"tx": [
"f37e9f691fffb635de0999491d906ee85ba40cd36dae9f6e5911a8277d7c5f75"
],

View File

@ -6,7 +6,6 @@ expression: block
"hash": "0007bc227e1c57a4a70e237cad00e7b7ce565155ab49166bc57397a26d339283",
"confirmations": 10,
"height": 1,
"time": 1477671596,
"tx": [
"851bf6fbf7a976327817c738c489d7fa657752445430922d94c983c0b9ed4609"
],

View File

@ -6,7 +6,6 @@ expression: block
"hash": "025579869bcf52a989337342f5f57a84f3a28b968f7d6a8307902b065a668d23",
"confirmations": 10,
"height": 1,
"time": 1477674473,
"tx": [
"f37e9f691fffb635de0999491d906ee85ba40cd36dae9f6e5911a8277d7c5f75"
],

View File

@ -0,0 +1,14 @@
---
source: zebra-rpc/src/methods/tests/snapshot.rs
expression: block
---
{
"hash": "0007bc227e1c57a4a70e237cad00e7b7ce565155ab49166bc57397a26d339283",
"confirmations": 10,
"height": 1,
"time": 1477671596,
"tx": [
"851bf6fbf7a976327817c738c489d7fa657752445430922d94c983c0b9ed4609"
],
"trees": {}
}

View File

@ -0,0 +1,14 @@
---
source: zebra-rpc/src/methods/tests/snapshot.rs
expression: block
---
{
"hash": "025579869bcf52a989337342f5f57a84f3a28b968f7d6a8307902b065a668d23",
"confirmations": 10,
"height": 1,
"time": 1477674473,
"tx": [
"f37e9f691fffb635de0999491d906ee85ba40cd36dae9f6e5911a8277d7c5f75"
],
"trees": {}
}

View File

@ -6,7 +6,6 @@ expression: block
"hash": "0007bc227e1c57a4a70e237cad00e7b7ce565155ab49166bc57397a26d339283",
"confirmations": 10,
"height": 1,
"time": 1477671596,
"tx": [
"851bf6fbf7a976327817c738c489d7fa657752445430922d94c983c0b9ed4609"
],

View File

@ -6,7 +6,6 @@ expression: block
"hash": "025579869bcf52a989337342f5f57a84f3a28b968f7d6a8307902b065a668d23",
"confirmations": 10,
"height": 1,
"time": 1477674473,
"tx": [
"f37e9f691fffb635de0999491d906ee85ba40cd36dae9f6e5911a8277d7c5f75"
],

View File

@ -140,7 +140,7 @@ async fn rpc_getblock() {
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()),
time: None,
tx: block
.transactions
.iter()
@ -158,6 +158,54 @@ async fn rpc_getblock() {
.await
.expect("We should have a GetBlock struct");
assert_eq!(
get_block,
GetBlock::Object {
hash: GetBlockHash(block.hash()),
confirmations: (blocks.len() - i).try_into().expect("valid i64"),
height: None,
time: None,
tx: block
.transactions
.iter()
.map(|tx| tx.hash().encode_hex())
.collect(),
trees,
}
);
}
// Make height calls with verbosity=2 and check response
for (i, block) in blocks.iter().enumerate() {
let get_block = rpc
.get_block(i.to_string(), Some(2u8))
.await
.expect("We should have a GetBlock struct");
assert_eq!(
get_block,
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()),
tx: block
.transactions
.iter()
.map(|tx| tx.hash().encode_hex())
.collect(),
trees,
}
);
}
// Make hash calls with verbosity=2 and check response
for (i, block) in blocks.iter().enumerate() {
let get_block = rpc
.get_block(blocks[i].hash().to_string(), Some(2u8))
.await
.expect("We should have a GetBlock struct");
assert_eq!(
get_block,
GetBlock::Object {
@ -188,7 +236,7 @@ async fn rpc_getblock() {
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()),
time: None,
tx: block
.transactions
.iter()
@ -212,7 +260,7 @@ async fn rpc_getblock() {
hash: GetBlockHash(block.hash()),
confirmations: (blocks.len() - i).try_into().expect("valid i64"),
height: None,
time: Some(block.header.time.timestamp()),
time: None,
tx: block
.transactions
.iter()