Struct BlockHeight

Source
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

Source

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);
Source

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

Source§

type Output = BlockHeight

The resulting type after applying the + operator.
Source§

fn add(self, other: u32) -> Self

Performs the + operation. Read more
Source§

impl Clone for BlockHeight

Source§

fn clone(&self) -> BlockHeight

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BlockHeight

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for BlockHeight

Displays the block height as a plain number

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&BlockHeight> for CBOR

Source§

fn from(value: &BlockHeight) -> Self

Converts to this type from the input type.
Source§

impl From<BlockHeight> for CBOR

Source§

fn from(value: BlockHeight) -> Self

Converts to this type from the input type.
Source§

impl From<BlockHeight> for Envelope

Source§

fn from(value: BlockHeight) -> Self

Converts to this type from the input type.
Source§

impl From<BlockHeight> for i64

Converts a BlockHeight to a signed i64 value

Source§

fn from(value: BlockHeight) -> i64

Converts to this type from the input type.
Source§

impl From<BlockHeight> for u32

Extracts the u32 value from a BlockHeight

Source§

fn from(value: BlockHeight) -> u32

Converts to this type from the input type.
Source§

impl From<BlockHeight> for u64

Converts a BlockHeight to a u64 value

Source§

fn from(value: BlockHeight) -> u64

Converts to this type from the input type.
Source§

impl From<u32> for BlockHeight

Creates a BlockHeight from a u32 value

Source§

fn from(value: u32) -> Self

Converts to this type from the input type.
Source§

impl Hash for BlockHeight

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for BlockHeight

Implements a total ordering between block heights

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for BlockHeight

Source§

fn eq(&self, other: &BlockHeight) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for BlockHeight

Implements a partial ordering between block heights

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Sub<u32> for BlockHeight

Subtracts a block count from a height, with saturation to prevent underflow

Source§

type Output = BlockHeight

The resulting type after applying the - operator.
Source§

fn sub(self, other: u32) -> Self

Performs the - operation. Read more
Source§

impl Sub for BlockHeight

Calculates the block count between two heights, with saturation

Source§

type Output = u32

The resulting type after applying the - operator.
Source§

fn sub(self, other: BlockHeight) -> u32

Performs the - operation. Read more
Source§

impl TryFrom<CBOR> for BlockHeight

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(cbor: CBOR) -> Result<Self>

Performs the conversion.
Source§

impl TryFrom<Envelope> for BlockHeight

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(envelope: Envelope) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<i32> for BlockHeight

Creates a BlockHeight from a signed i32 if it’s non-negative

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(value: i32) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<i64> for BlockHeight

Creates a BlockHeight from a signed i64 if it fits in a u32

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(value: i64) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<u64> for BlockHeight

Creates a BlockHeight from a u64 value if it fits in a u32

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(value: u64) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Copy for BlockHeight

Source§

impl Eq for BlockHeight

Source§

impl StructuralPartialEq for BlockHeight

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CBORDecodable for T
where T: TryFrom<CBOR, Error = Error>,

Source§

fn try_from_cbor(cbor: &CBOR) -> Result<Self, Error>

Source§

impl<T> CBOREncodable for T
where T: Into<CBOR> + Clone,

Source§

fn to_cbor(&self) -> CBOR

Converts this value to a CBOR object. Read more
Source§

fn to_cbor_data(&self) -> Vec<u8>

Converts this value directly to binary CBOR data. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> EnvelopeEncodable for T
where T: Into<Envelope> + Clone,

Source§

fn into_envelope(self) -> Envelope

Converts the value into an envelope by using its Into<Envelope> implementation.

Source§

fn to_envelope(&self) -> Envelope
where Self: Clone,

Converts a reference to this value into a Gordian Envelope. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

Source§

impl<T> CBORCodable for T

§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T