block_hash method for LightChainClient
This commit is contained in:
parent
ac057ebe93
commit
da3dd65726
@ -355,6 +355,22 @@ impl HeaderChain {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get a block's hash by ID. In the case of query by number, only canonical results
|
||||||
|
/// will be returned.
|
||||||
|
pub fn block_hash(&self, id: BlockId) -> Option<H256> {
|
||||||
|
match id {
|
||||||
|
BlockId::Earliest => Some(self.genesis_hash()),
|
||||||
|
BlockId::Hash(hash) => Some(hash),
|
||||||
|
BlockId::Number(num) => {
|
||||||
|
if self.best_block.read().number < num { return None }
|
||||||
|
self.candidates.read().get(&num).map(|entry| entry.canonical_hash)
|
||||||
|
}
|
||||||
|
BlockId::Latest | BlockId::Pending => {
|
||||||
|
Some(self.best_block.read().hash)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Get a block header. In the case of query by number, only canonical blocks
|
/// Get a block header. In the case of query by number, only canonical blocks
|
||||||
/// will be returned.
|
/// will be returned.
|
||||||
pub fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
|
pub fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
|
||||||
|
@ -65,6 +65,9 @@ pub trait LightChainClient: Send + Sync {
|
|||||||
/// parent queued prior.
|
/// parent queued prior.
|
||||||
fn queue_header(&self, header: Header) -> Result<H256, BlockImportError>;
|
fn queue_header(&self, header: Header) -> Result<H256, BlockImportError>;
|
||||||
|
|
||||||
|
/// Attempt to get a block hash by block id.
|
||||||
|
fn block_hash(&self, id: BlockId) -> Option<H256>;
|
||||||
|
|
||||||
/// Attempt to get block header by block id.
|
/// Attempt to get block header by block id.
|
||||||
fn block_header(&self, id: BlockId) -> Option<encoded::Header>;
|
fn block_header(&self, id: BlockId) -> Option<encoded::Header>;
|
||||||
|
|
||||||
@ -181,6 +184,11 @@ impl Client {
|
|||||||
self.queue.queue_info()
|
self.queue.queue_info()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Attempt to get a block hash by block id.
|
||||||
|
pub fn block_hash(&self, id: BlockId) -> Option<H256> {
|
||||||
|
self.chain.block_hash(id)
|
||||||
|
}
|
||||||
|
|
||||||
/// Get a block header by Id.
|
/// Get a block header by Id.
|
||||||
pub fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
|
pub fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
|
||||||
self.chain.block_header(id)
|
self.chain.block_header(id)
|
||||||
@ -308,6 +316,10 @@ impl LightChainClient for Client {
|
|||||||
self.import_header(header)
|
self.import_header(header)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn block_hash(&self, id: BlockId) -> Option<H256> {
|
||||||
|
Client::block_hash(self, id)
|
||||||
|
}
|
||||||
|
|
||||||
fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
|
fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
|
||||||
Client::block_header(self, id)
|
Client::block_header(self, id)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user