block_identifier.rs

Overview

This file defines the BlockIdentifier struct, a core data type representing a fixed-size 32-byte identifier used primarily for blocks within a blockchain or distributed ledger context. The BlockIdentifier encapsulates a 32-byte array ([u8; 32]), providing serialization, comparison, conversion, and formatting functionalities essential for block identification and manipulation across the system.

The struct supports serialization/deserialization via serde with binary encoding optimizations, ordering comparisons crucial for fork choice algorithms, and conversions to/from other formats such as tvm_types::UInt256. It also implements string parsing from hexadecimal and Base64 formats, enabling interoperability with external representations.

Detailed Descriptions

Struct: BlockIdentifier

pub struct BlockIdentifier(pub(super) [u8; 32]);

Methods

is_zero(&self) -> bool

let id = BlockIdentifier::default();
assert!(id.is_zero());

compare(a: &BlockIdentifier, b: &BlockIdentifier) -> Ordering

as_rng_seed(&self) -> [u8; 32]

Trait Implementations

AsRef<[u8]>

From<[u8; 32]> for BlockIdentifier

From<tvm_types::UInt256> and From<BlockIdentifier> for tvm_types::UInt256

FromStr for BlockIdentifier

use std::str::FromStr;

let id = BlockIdentifier::from_str("0x1234abcd...").unwrap();

Formatting Traits

Implementation Details and Algorithms

Interactions with Other System Components

Mermaid Diagram: Class Diagram of BlockIdentifier

classDiagram
class BlockIdentifier {
-bytes: [u8; 32]
+is_zero(): bool
+compare(a: &BlockIdentifier, b: &BlockIdentifier): Ordering
+as_rng_seed(): [u8; 32]
+from_str(value: &str) -> Result<BlockIdentifier, Error>
+from(bytes: [u8; 32]) -> BlockIdentifier
}

This diagram illustrates the main structure of the BlockIdentifier type, its internal data, and key methods exposed by the struct. The private field bytes stores the 32-byte identifier, with public methods supporting zero checks, comparisons, seed extraction, and conversions.