Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add best_tip_hash field to BlockStatus #52

Open
wants to merge 1 commit into
base: new-index
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/new_index/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,24 +889,26 @@ impl ChainQuery {
.map(BlockId::from)
}

pub fn get_block_status(&self, hash: &BlockHash) -> BlockStatus {
pub fn get_block_status(&self, hash: &BlockHash, best_tip_hash: &BlockHash) -> BlockStatus {
// TODO differentiate orphaned and non-existing blocks? telling them apart requires
// an additional db read.

let headers = self.store.indexed_headers.read().unwrap();

// header_by_blockhash only returns blocks that are part of the best chain,
// or None for orphaned blocks.
headers
.header_by_blockhash(hash)
.map_or_else(BlockStatus::orphaned, |header| {
headers.header_by_blockhash(hash).map_or_else(
|| BlockStatus::orphaned(*best_tip_hash),
|header| {
BlockStatus::confirmed(
header.height(),
headers
.header_by_height(header.height() + 1)
.map(|h| *h.hash()),
*best_tip_hash,
)
})
},
)
}

#[cfg(not(feature = "liquid"))]
Expand Down
3 changes: 2 additions & 1 deletion src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,8 @@ fn handle_request(
}
(&Method::GET, Some(&"block"), Some(hash), Some(&"status"), None, None) => {
let hash = BlockHash::from_hex(hash)?;
let status = query.chain().get_block_status(&hash);
let best_tip_hash = query.chain().best_hash();
let status = query.chain().get_block_status(&hash, &best_tip_hash);
let ttl = ttl_by_depth(status.height, query);
json_response(status, ttl)
}
Expand Down
11 changes: 9 additions & 2 deletions src/util/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,22 +256,29 @@ pub struct BlockStatus {
pub in_best_chain: bool,
pub height: Option<usize>,
pub next_best: Option<BlockHash>,
pub best_tip_hash: BlockHash,
}

impl BlockStatus {
pub fn confirmed(height: usize, next_best: Option<BlockHash>) -> BlockStatus {
pub fn confirmed(
height: usize,
next_best: Option<BlockHash>,
best_tip_hash: BlockHash,
) -> BlockStatus {
BlockStatus {
in_best_chain: true,
height: Some(height),
next_best,
best_tip_hash,
}
}

pub fn orphaned() -> BlockStatus {
pub fn orphaned(best_tip_hash: BlockHash) -> BlockStatus {
BlockStatus {
in_best_chain: false,
height: None,
next_best: None,
best_tip_hash,
}
}
}
Expand Down