block_index.rs

Overview

The block_index.rs file defines the BlockIndex struct, which serves as a concise representation of a block's identity within a blockchain or distributed ledger system. It encapsulates two key attributes: the block's sequence number (block_seq_no) and its unique identifier (block_identifier). This struct is designed to enable efficient indexing, comparison, and retrieval of blocks.

The file also provides implementations to construct a BlockIndex from other blockchain data structures, as well as traits to support ordering and comparison operations between instances of BlockIndex. This functionality is essential for sorting blocks and ensuring consistent ordering in block processing workflows.

Structs and Implementations

BlockIndex

Description

BlockIndex is a data structure containing minimal identifying information about a block:

The struct derives several traits to facilitate cloning, debugging, equality checks, serialization, and deserialization. It also uses the Getters derive macro to automatically generate getter methods for its fields.

Fields

Field

Type

Description

block_seq_no

BlockSeqNo

The block's sequence number (to be updated to height).

block_identifier

BlockIdentifier

Unique identifier of the block.

Methods

Trait Implementations

Conversion from Envelope<GoshBLS, AckiNackiBlock> to BlockIndex

impl From<&Envelope<GoshBLS, AckiNackiBlock>> for BlockIndex

Usage:

let envelope: Envelope<GoshBLS, AckiNackiBlock> = ...;
let block_index: BlockIndex = BlockIndex::from(&envelope);

Ordering Traits: PartialOrd and Ord

Implementation Detail:

Usage Example:

let index1 = BlockIndex::new(seq_no1, id1);
let index2 = BlockIndex::new(seq_no2, id2);

if index1 < index2 {
    // index1 precedes index2 in blockchain order
}

Implementation Details

Interactions with Other Modules

This file thus forms a bridge between raw block data (AckiNackiBlock), its envelope representation, and the indexing and ordering logic essential for block management in the system.

Diagram: Structure of block_index.rs

classDiagram
class BlockIndex {
-block_seq_no: BlockSeqNo
-block_identifier: BlockIdentifier
+new()
+block_seq_no()
+block_identifier()
+cmp()
}
BlockIndex ..> BlockSeqNo : uses
BlockIndex ..> BlockIdentifier : uses
BlockIndex ..> Envelope : From<&Envelope<GoshBLS, AckiNackiBlock>>

This diagram illustrates the core BlockIndex struct with its fields and main constructor, as well as its relationships with other types used in this file.