Browse Source
This enables basic verification of chain validity when CompactBlocks are received without the full header.master
Jack Grigg
5 years ago
3 changed files with 69 additions and 3 deletions
@ -1,3 +1,54 @@
|
||||
//! Generated code for handling light client protobuf structs.
|
||||
|
||||
use zcash_primitives::block::{BlockHash, BlockHeader}; |
||||
|
||||
pub mod compact_formats; |
||||
|
||||
impl compact_formats::CompactBlock { |
||||
/// Returns the [`BlockHash`] for this block.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function will panic if [`CompactBlock.header`] is not set and
|
||||
/// [`CompactBlock.hash`] is not exactly 32 bytes.
|
||||
///
|
||||
/// [`CompactBlock.header`]: #structfield.header
|
||||
/// [`CompactBlock.hash`]: #structfield.hash
|
||||
pub fn hash(&self) -> BlockHash { |
||||
if let Some(header) = self.header() { |
||||
header.hash() |
||||
} else { |
||||
BlockHash::from_slice(&self.hash) |
||||
} |
||||
} |
||||
|
||||
/// Returns the [`BlockHash`] for this block's parent.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function will panic if [`CompactBlock.header`] is not set and
|
||||
/// [`CompactBlock.prevHash`] is not exactly 32 bytes.
|
||||
///
|
||||
/// [`CompactBlock.header`]: #structfield.header
|
||||
/// [`CompactBlock.prevHash`]: #structfield.prevHash
|
||||
pub fn prev_hash(&self) -> BlockHash { |
||||
if let Some(header) = self.header() { |
||||
header.prev_block |
||||
} else { |
||||
BlockHash::from_slice(&self.prevHash) |
||||
} |
||||
} |
||||
|
||||
/// Returns the [`BlockHeader`] for this block if present.
|
||||
///
|
||||
/// A convenience method that parses [`CompactBlock.header`] if present.
|
||||
///
|
||||
/// [`CompactBlock.header`]: #structfield.header
|
||||
pub fn header(&self) -> Option<BlockHeader> { |
||||
if self.header.is_empty() { |
||||
None |
||||
} else { |
||||
BlockHeader::read(&self.header[..]).ok() |
||||
} |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue