pub struct BlockHeight(/* private fields */);
Expand description
A block’s position in the blockchain, represented as a distance from the genesis block.
BlockHeight
represents the number of blocks between a specific block and the genesis
block (block zero). Each block increments the height by one, forming a total ordering
that defines the blockchain’s canonical sequence.
§Zcash Concept Relation
In Zcash, like Bitcoin, block height is crucial for:
- Identifying when transactions were confirmed
- Determining when network upgrades activate
- Calculating confirmation counts
- Anchoring shielded transactions to specific blockchain states
Many Zcash protocol parameters are defined in terms of block heights, including upgrade activation heights and consensus rule changes.
§Data Preservation
BlockHeight
preserves the numeric height values from wallet data, which are essential
for chronological ordering of transactions and for determining whether specific network
features were active when a transaction was created.
§Implementation Details
Internally, block heights are stored as unsigned 32-bit integers, which can represent blocks up to approximately 136 years into the future at Zcash’s target rate of one block every 75 seconds.
§Examples
// The genesis block
let genesis = BlockHeight::from(0u32);
// Block #1,000,000
let millionth = BlockHeight::from(1_000_000u32);
// Calculate difference between blocks
let blocks_between = millionth - genesis;
assert_eq!(blocks_between, 1_000_000);
Implementations§
Source§impl BlockHeight
impl BlockHeight
Sourcepub const fn from_u32(v: u32) -> BlockHeight
pub const fn from_u32(v: u32) -> BlockHeight
Creates a new BlockHeight
from a u32 value.
This constructor is a const fn
, which allows it to be used in constant expressions.
§Examples
// Create a constant block height
const CANOPY_ACTIVATION: BlockHeight = BlockHeight::from_u32(1_046_400);
Sourcepub fn saturating_sub(self, v: u32) -> BlockHeight
pub fn saturating_sub(self, v: u32) -> BlockHeight
Subtracts the provided value from this height, returning H0
if this would result in
underflow of the wrapped u32
.
This method ensures that block height calculations never underflow below the genesis block (height 0), which would be an invalid state in a blockchain.
§Examples
let height = BlockHeight::from(100u32);
// Normal subtraction
let earlier = height.saturating_sub(50);
assert_eq!(u32::from(earlier), 50);
// Saturating at genesis block (0) when underflow would occur
let genesis = height.saturating_sub(200); // Would be -100, saturates to 0
assert_eq!(u32::from(genesis), 0);
Trait Implementations§
Source§impl Add<u32> for BlockHeight
Adds a block count to a height, with saturation to prevent overflow
impl Add<u32> for BlockHeight
Adds a block count to a height, with saturation to prevent overflow
Source§impl Clone for BlockHeight
impl Clone for BlockHeight
Source§fn clone(&self) -> BlockHeight
fn clone(&self) -> BlockHeight
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for BlockHeight
impl Debug for BlockHeight
Source§impl Display for BlockHeight
Displays the block height as a plain number
impl Display for BlockHeight
Displays the block height as a plain number
Source§impl From<&BlockHeight> for CBOR
impl From<&BlockHeight> for CBOR
Source§fn from(value: &BlockHeight) -> Self
fn from(value: &BlockHeight) -> Self
Source§impl From<BlockHeight> for CBOR
impl From<BlockHeight> for CBOR
Source§fn from(value: BlockHeight) -> Self
fn from(value: BlockHeight) -> Self
Source§impl From<BlockHeight> for Envelope
impl From<BlockHeight> for Envelope
Source§fn from(value: BlockHeight) -> Self
fn from(value: BlockHeight) -> Self
Source§impl From<BlockHeight> for i64
Converts a BlockHeight to a signed i64 value
impl From<BlockHeight> for i64
Converts a BlockHeight to a signed i64 value
Source§fn from(value: BlockHeight) -> i64
fn from(value: BlockHeight) -> i64
Source§impl From<BlockHeight> for u32
Extracts the u32 value from a BlockHeight
impl From<BlockHeight> for u32
Extracts the u32 value from a BlockHeight
Source§fn from(value: BlockHeight) -> u32
fn from(value: BlockHeight) -> u32
Source§impl From<BlockHeight> for u64
Converts a BlockHeight to a u64 value
impl From<BlockHeight> for u64
Converts a BlockHeight to a u64 value
Source§fn from(value: BlockHeight) -> u64
fn from(value: BlockHeight) -> u64
Source§impl From<u32> for BlockHeight
Creates a BlockHeight from a u32 value
impl From<u32> for BlockHeight
Creates a BlockHeight from a u32 value
Source§impl Hash for BlockHeight
impl Hash for BlockHeight
Source§impl Ord for BlockHeight
Implements a total ordering between block heights
impl Ord for BlockHeight
Implements a total ordering between block heights
Source§impl PartialEq for BlockHeight
impl PartialEq for BlockHeight
Source§impl PartialOrd for BlockHeight
Implements a partial ordering between block heights
impl PartialOrd for BlockHeight
Implements a partial ordering between block heights
Source§impl Sub<u32> for BlockHeight
Subtracts a block count from a height, with saturation to prevent underflow
impl Sub<u32> for BlockHeight
Subtracts a block count from a height, with saturation to prevent underflow
Source§impl Sub for BlockHeight
Calculates the block count between two heights, with saturation
impl Sub for BlockHeight
Calculates the block count between two heights, with saturation
Source§impl TryFrom<CBOR> for BlockHeight
impl TryFrom<CBOR> for BlockHeight
Source§impl TryFrom<Envelope> for BlockHeight
impl TryFrom<Envelope> for BlockHeight
Source§impl TryFrom<i32> for BlockHeight
Creates a BlockHeight from a signed i32 if it’s non-negative
impl TryFrom<i32> for BlockHeight
Creates a BlockHeight from a signed i32 if it’s non-negative
Source§impl TryFrom<i64> for BlockHeight
Creates a BlockHeight from a signed i64 if it fits in a u32
impl TryFrom<i64> for BlockHeight
Creates a BlockHeight from a signed i64 if it fits in a u32
Source§impl TryFrom<u64> for BlockHeight
Creates a BlockHeight from a u64 value if it fits in a u32
impl TryFrom<u64> for BlockHeight
Creates a BlockHeight from a u64 value if it fits in a u32
impl Copy for BlockHeight
impl Eq for BlockHeight
impl StructuralPartialEq for BlockHeight
Auto Trait Implementations§
impl Freeze for BlockHeight
impl RefUnwindSafe for BlockHeight
impl Send for BlockHeight
impl Sync for BlockHeight
impl Unpin for BlockHeight
impl UnwindSafe for BlockHeight
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CBORDecodable for T
impl<T> CBORDecodable for T
Source§impl<T> CBOREncodable for T
impl<T> CBOREncodable for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> EnvelopeEncodable for T
impl<T> EnvelopeEncodable for T
Source§fn into_envelope(self) -> Envelope
fn into_envelope(self) -> Envelope
Converts the value into an envelope by using its Into<Envelope>
implementation.
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more