unprocessed_blocks_collection.rs

Overview

This file implements a thread-safe collection and management system for unfinalized candidate blocks within a blockchain or distributed ledger context. The primary purpose is to maintain blocks that have not yet been finalized, allowing insertion, filtering, retrieval, and removal based on their state and sequence numbers. It supports notification mechanisms to alert subscribers about changes in the collection.

Key responsibilities include:

Main Structures and Their Functionality

UnfinalizedBlocksSnapshot

pub struct UnfinalizedBlocksSnapshot {
    blocks: BTreeMap<BlockIndex, (BlockState, Arc<Envelope<GoshBLS, AckiNackiBlock>>)>,
    notifications_stamp: u32,
    block_id_set: HashSet<BlockIdentifier>,
}

UnfinalizedBlocksData

struct UnfinalizedBlocksData {
    main_map: UnfinalizedBlocksSnapshot,
    identifier_to_seqno: HashMap<BlockIdentifier, BlockSeqNo>,
    filter: FilterPrehistoric,
}

Methods

FilterPrehistoric

pub struct FilterPrehistoric {
    block_seq_no: BlockSeqNo,
}

UnfinalizedCandidateBlockCollection

pub struct UnfinalizedCandidateBlockCollection {
    candidates: Arc<Mutex<UnfinalizedBlocksData>>,
    notifications: Notification,
}

Constructor

Key Methods

Important Implementation Details and Algorithms

Interactions with Other Parts of the System

Usage Examples

Creating a New Collection from Existing States

let states_iter = some_iterator_of_blocks(); // Iterator<Item = (BlockState, Arc<Envelope<GoshBLS, AckiNackiBlock>>)>
let collection = UnfinalizedCandidateBlockCollection::new(states_iter);

Inserting a New Block

collection.insert(block_state, block_envelope);

Retrieving a Block by Identifier

if let Some(block) = collection.get_block_by_id(&block_identifier) {
    // Use block envelope
}

Removing Finalized and Invalidated Blocks

collection.remove_finalized_and_invalidated_blocks();

Updating the Filter to Remove Old Blocks

let filter = FilterPrehistoric::builder()
    .block_seq_no(new_seq_no)
    .build();
collection.update_filter(filter);

Mermaid Diagram: Class Structure and Relationships

classDiagram
class UnfinalizedCandidateBlockCollection {
+new()
+get_block_by_id()
+clone_queue()
+insert()
+remove_finalized_and_invalidated_blocks()
+remove_old_blocks()
+retain()
+notifications()
+touch()
+update_filter()
}
class UnfinalizedBlocksData {
-main_map: UnfinalizedBlocksSnapshot
-identifier_to_seqno: HashMap
-filter: FilterPrehistoric
+set_filter()
+insert()
+retain()
}
class UnfinalizedBlocksSnapshot {
-blocks: BTreeMap
-notifications_stamp: u32
-block_id_set: HashSet
}
class FilterPrehistoric {
-block_seq_no: BlockSeqNo
}
UnfinalizedCandidateBlockCollection --> UnfinalizedBlocksData : manages >
UnfinalizedBlocksData --> UnfinalizedBlocksSnapshot : contains >
UnfinalizedBlocksData --> FilterPrehistoric : uses >

This diagram illustrates the containment and management hierarchy among the core structures, emphasizing the thread-safe management of unfinalized blocks and filtering behavior.