use std::sync::Arc;
use zakura_chain::{
block,
serialization::{BytesInDisplayOrder, ZcashDeserializeInto, ZcashSerialize},
};
#[cfg(test)]
mod tests;
pub mod methods;
pub mod server;
const BLOCK_HASH_BYTE_LEN: usize = 32;
const BLOCK_HEIGHT_BYTE_LEN: usize = std::mem::size_of::<u32>();
tonic::include_proto!("zebra.indexer.rpc");
pub(crate) const FILE_DESCRIPTOR_SET: &[u8] =
tonic::include_file_descriptor_set!("indexer_descriptor");
impl BlockHashAndHeight {
pub fn new(hash: block::Hash, block::Height(height): block::Height) -> Self {
let hash = hash.bytes_in_display_order().to_vec();
BlockHashAndHeight { hash, height }
}
pub fn try_into_hash_and_height(self) -> Option<(block::Hash, block::Height)> {
self.hash
.try_into()
.map(|bytes| block::Hash::from_bytes_in_display_order(&bytes))
.map_err(|bytes: Vec<_>| {
tracing::warn!(
"failed to convert BlockHash to Hash, unexpected len: {}",
bytes.len()
)
})
.ok()
.zip(self.height.try_into().ok())
}
}
impl BlockAndHash {
pub fn new(hash: block::Hash, block: Arc<block::Block>) -> Self {
BlockAndHash {
hash: hash.bytes_in_display_order().to_vec(),
data: block
.zcash_serialize_to_vec()
.expect("block serialization should not fail"),
}
}
pub fn decode(self) -> Option<(block::Block, block::Hash)> {
self.hash
.try_into()
.map(|bytes| block::Hash::from_bytes_in_display_order(&bytes))
.map_err(|bytes: Vec<_>| {
tracing::warn!(
"failed to convert BlockHash to Hash, unexpected len: {}",
bytes.len()
)
})
.ok()
.and_then(|hash| {
self.data
.zcash_deserialize_into()
.map_err(|err| tracing::warn!(?err, "failed to deserialize block",))
.ok()
.zip(Some(hash))
})
}
}