mod.rs

Overview

This file defines the ThreadIdentifier struct, which provides a mechanism for uniquely identifying threads within the system. The uniqueness is guaranteed by combining a BlockIdentifier (representing a specific block in the blockchain) with a local u16 index. This design allows multiple threads to be spawned simultaneously from the same block without collisions in thread identifiers.

The file includes serialization and deserialization support, string formatting, and hashing implementations for ThreadIdentifier. It also defines conversions to and from byte arrays and hexadecimal strings, facilitating interoperability and storage.

ThreadIdentifier Struct

Definition

pub struct ThreadIdentifier([u8; 34]);

Purpose

ThreadIdentifier acts as a unique handle for threads spawned from a specific blockchain block, ensuring no collisions by encoding both the block and a local thread index.

Derives and Traits

Methods

new(block_id: &BlockIdentifier, id: u16) -> Self

Creates a new ThreadIdentifier by combining the given block_id and thread index id.

Usage example:

let block_id = BlockIdentifier::from(...);
let thread_id = ThreadIdentifier::new(&block_id, 1);

is_spawning_block(&self, block_id: &BlockIdentifier) -> bool

Checks if the thread was spawned by the given block.

spawning_block_id(&self) -> BlockIdentifier

Retrieves the BlockIdentifier from which this thread was spawned.

Trait Implementations

Implementation Details and Notes

Interaction with Other Parts of the System

Diagram

classDiagram
class ThreadIdentifier {
-[u8; 34] data
+new(block_id: &BlockIdentifier, id: u16): Self
+is_spawning_block(block_id: &BlockIdentifier): bool
+spawning_block_id(): BlockIdentifier
+as_ref(): &[u8]
<<trait>> Serialize
<<trait>> Deserialize
<<trait>> Copy
<<trait>> Clone
<<trait>> Default
<<trait>> From<[u8;34]>
<<trait>> TryFrom<String>
<<trait>> LowerHex
<<trait>> Display
<<trait>> Debug
<<trait>> PartialEq
<<trait>> Eq
<<trait>> Hash
}
ThreadIdentifier --> BlockIdentifier : contains

This diagram shows the ThreadIdentifier struct, its core data, primary methods, and the traits implemented, as well as its composition relationship with BlockIdentifier.