block_keeper_system.rs
Overview
This file defines functionality related to managing block keeper epochs within a blockchain node. It extends the Node struct by implementing a method that inspects the current epoch contracts associated with block keepers and attempts to "touch" (i.e., finalize or update) them if they are outdated. This mechanism is part of the node's block production and synchronization process, ensuring that epochs are properly finalized or retried before expiration.
The file leverages generic parameters for state synchronization and randomness generation, ensuring that the implementation can work with various underlying services and RNG implementations.
Detailed Explanation
impl<TStateSyncService, TRandomGenerator> Node<TStateSyncService, TRandomGenerator>
This implementation block adds functionality to the generic Node struct where:
TStateSyncService must implement the
StateSyncServicetrait with a repository type ofRepositoryImpl.TRandomGeneratormust implement therand::Rngtrait for randomness.
This ensures that the node has capabilities for state synchronization and randomness generation required for block production and management.
Method: _check_and_touch_block_keeper_epochs
pub(crate) fn _check_and_touch_block_keeper_epochs(&mut self) -> anyhow::Result<()>
Purpose
This private method allows a block-producing (BP) node to:
Check the current epoch contracts for block keepers.
Send "touch" messages to those epochs that have expired or are close to expiration to finalize them.
This method is a part of the block keeper epoch lifecycle management, aimed at ensuring that epochs are properly closed and transitioned.
Parameters
&mut self: a mutable reference to the node instance, allowing modification of its internal state.
Return Value
Returns
anyhow::Result<()>, indicating a success or failure. Currently, the method always returnsOk(())as the core implementation is commented out.
Usage Example
let mut node = Node::new(...);
if let Err(e) = node._check_and_touch_block_keeper_epochs() {
eprintln!("Failed to check and touch block keeper epochs: {:?}", e);
}
Implementation Details and Algorithm
The method intends to retrieve the current timestamp (
now).It identifies the last block in the node's thread that it can continue producing on.
It retrieves the current block keeper set for that block.
It iterates over each block keeper's epoch data, checking if the epoch finish timestamp is earlier than
now.Depending on the block keeper's status (
Active,CalledToFinish, orExpired), it:Sends a touch message to prompt epoch finalization.
Updates the epoch finish timestamp by adding a retry delta (
_EPOCH_TOUCH_RETRY_TIME_DELTA, set to 5 seconds).Updates the status to reflect whether the epoch has been called to finish or has expired.
The method is currently disabled (commented out) with a
TODOto revise the mechanism due to upcoming changes in how the block keeper set is stored (moving to block state).
Constants
_EPOCH_TOUCH_RETRY_TIME_DELTA: u32 = 5
This constant defines a 5-second delta used to retry touching epochs if they have not yet been finalized, preventing premature expiration.
Interactions with Other System Components
StateSyncServiceTrait: The node depends on this trait for accessing blockchain state synchronization features, particularly involving the repository (RepositoryImpl) which manages blockchain state persistence.RepositoryImpl: Represents the underlying repository implementation the node uses to access blockchain data.production_process: The method references sending epoch messages viaself.production_process.send_epoch_message(...), indicating interaction with the node's block production subsystem, which handles message dispatch related to epoch finalization.Thread and Block Identification: The method references the node's current thread (
thread_id) and the last block it can continue, suggesting integration with thread scheduling and block sequencing logic.The commented out code suggests that the block keeper set will be moved to block state management, indicating interactions with block state storage and retrieval mechanisms.
Important Notes
The method
_check_and_touch_block_keeper_epochsis currently non-functional due to the commented-out implementation and aTODOnote to revise the epoch touching mechanism.This file focuses solely on the epoch touching logic within the block keeper system and does not expose public APIs for this functionality.
The method is marked as
pub(crate)indicating it is accessible within the crate but not outside it.
Mermaid Diagram: Structure of block_keeper_system.rs
classDiagram
class Node {
<<generic>>
+_check_and_touch_block_keeper_epochs() Result
-production_process
-thread_id
-find_thread_last_block_id_this_node_can_continue()
-get_block_keeper_set_for_block_id()
}
class StateSyncService {
<<trait>>
+Repository
}
class RepositoryImpl
Node ..|> StateSyncService
Node --> RepositoryImpl
This diagram illustrates the Node struct's relationship with the StateSyncService trait and RepositoryImpl, as well as its internal methods and properties relevant to the block keeper epoch management.