Skip to content

Commit

Permalink
always includes height in verbose output of getblock method
Browse files Browse the repository at this point in the history
  • Loading branch information
arya2 committed Apr 8, 2024
1 parent 4aa00fa commit c9421d0
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 30 deletions.
25 changes: 12 additions & 13 deletions zebra-rpc/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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: Height,

/// The height of the requested block.
#[serde(skip_serializing_if = "Option::is_none")]
time: Option<i64>,
time: i64,

/// List of transaction IDs in block order, hex-encoded.
//
Expand All @@ -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(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ expression: block
{
"hash": "0007bc227e1c57a4a70e237cad00e7b7ce565155ab49166bc57397a26d339283",
"confirmations": 10,
"height": 1,
"time": 1477671596,
"tx": [
"851bf6fbf7a976327817c738c489d7fa657752445430922d94c983c0b9ed4609"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ expression: block
{
"hash": "025579869bcf52a989337342f5f57a84f3a28b968f7d6a8307902b065a668d23",
"confirmations": 10,
"height": 1,
"time": 1477674473,
"tx": [
"f37e9f691fffb635de0999491d906ee85ba40cd36dae9f6e5911a8277d7c5f75"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ expression: block
{
"hash": "0007bc227e1c57a4a70e237cad00e7b7ce565155ab49166bc57397a26d339283",
"confirmations": 10,
"height": 1,
"time": 1477671596,
"tx": [
"851bf6fbf7a976327817c738c489d7fa657752445430922d94c983c0b9ed4609"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ expression: block
{
"hash": "025579869bcf52a989337342f5f57a84f3a28b968f7d6a8307902b065a668d23",
"confirmations": 10,
"height": 1,
"time": 1477674473,
"tx": [
"f37e9f691fffb635de0999491d906ee85ba40cd36dae9f6e5911a8277d7c5f75"
Expand Down
16 changes: 8 additions & 8 deletions zebra-rpc/src/methods/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions zebra-state/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub enum Response {
Block(Option<Arc<Block>>),

/// The response to a `BlockHeader` request.
BlockHeader(Option<Arc<block::Header>>),
BlockHeader(Option<(block::Height, Arc<block::Header>)>),

/// The response to a `AwaitUtxo` request, from any non-finalized chains, finalized chain,
/// pending unverified blocks, or blocks received after the request was sent.
Expand Down Expand Up @@ -135,7 +135,7 @@ pub enum ReadResponse {
Block(Option<Arc<Block>>),

/// The response to a `BlockHeader` request.
BlockHeader(Option<Arc<block::Header>>),
BlockHeader(Option<(block::Height, Arc<block::Header>)>),

/// Response to [`ReadRequest::Transaction`] with the specified transaction.
Transaction(Option<MinedTx>),
Expand Down
2 changes: 1 addition & 1 deletion zebra-state/src/service/block_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl ChainItem for block::Header {
type Type = Arc<block::Header>;

fn read(chain: Option<&Arc<Chain>>, db: &ZebraDb, height: Height) -> Option<Self::Type> {
read::block_header(chain, db, height.into())
read::block_header(chain, db, height.into()).map(|(_height, header)| header)
}
}

Expand Down
9 changes: 6 additions & 3 deletions zebra-state/src/service/finalized_state/zebra_db/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arc<block::Header>> {
pub fn block_header(
&self,
hash_or_height: HashOrHeight,
) -> Option<(block::Height, Arc<block::Header>)> {
// 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
Expand All @@ -136,7 +139,7 @@ impl ZebraDb {
pub fn block(&self, hash_or_height: HashOrHeight) -> Option<Arc<Block>> {
// 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();
Expand Down
4 changes: 2 additions & 2 deletions zebra-state/src/service/read/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn block_header<C>(
chain: Option<C>,
db: &ZebraDb,
hash_or_height: HashOrHeight,
) -> Option<Arc<block::Header>>
) -> Option<(block::Height, Arc<block::Header>)>
where
C: AsRef<Chain>,
{
Expand All @@ -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))
}

Expand Down
2 changes: 1 addition & 1 deletion zebra-state/src/service/read/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit c9421d0

Please sign in to comment.