thread_synchrinization_service.rs
Overview
This file implements the ThreadSyncService, a service responsible for tracking and managing synchronization information between multiple threads in a blockchain system. Its primary function is to maintain records of the last finalized blocks per thread and to provide information about blocks from other threads that may send messages to a given thread. The service is designed around the concept of finalized blocks, ignoring forks and focusing on finalized states to ensure consistency and reduce verification delays.
The service facilitates cross-thread communication by referencing finalized blocks from other threads, which helps in verifying blocks in a timely manner and maintaining system integrity. It is structured to be extendable for more optimistic synchronization strategies in the future.
ThreadSyncService Struct
Description
ThreadSyncService maintains a mapping between thread identifiers and the last finalized block identifiers associated with those threads. This mapping supports queries about which blocks from other threads may be sending messages to a specific thread.
Fields
last_finalized_blocks: HashMap<ThreadIdentifier, BlockIdentifier>A hash map storing the latest finalized block for each thread.
Finalized blocks are guaranteed to be free from forks since forks are resolved before finalization.
The map may grow in size in scenarios involving thread collapsing or splitting, though this is not a concern for the current implementation.
Implementation Details
Methods
pub fn on_block_finalized(&mut self, block_identifier: &BlockIdentifier, thread_identifier: &ThreadIdentifier) -> anyhow::Result<()>
Purpose: Updates the service with a newly finalized block for a given thread.
Parameters:
block_identifier: Reference to the identifier of the finalized block.
thread_identifier: Reference to the identifier of the thread to which the block belongs.
Returns:
Result<(), anyhow::Error>— Returns an Ok result if insertion succeeds.Functionality: Inserts or updates the
last_finalized_blocksmap with the provided block for the specified thread.Usage Example:
thread_sync_service.on_block_finalized(&block_id, &thread_id)?;
pub fn list_blocks_sending_messages_to_thread(&mut self, thread_identifier: &ThreadIdentifier) -> anyhow::Result<Vec<(ThreadIdentifier, BlockIdentifier)>>
Purpose: Retrieves a list of blocks from other threads that may be sending messages to the specified thread.
Parameters:
thread_identifier: Reference to the thread for which incoming blocks are queried.
Returns:
Result<Vec<(ThreadIdentifier, BlockIdentifier)>, anyhow::Error>A vector of tuples where each tuple contains a
ThreadIdentifierand a correspondingBlockIdentifier.The vector excludes the queried thread's own blocks.
Functionality: Filters the
last_finalized_blocksmap to collect all entries except the one corresponding to the specified thread.Usage Example:
let incoming_blocks = thread_sync_service.list_blocks_sending_messages_to_thread(&target_thread)?; for (sender_thread, block_id) in incoming_blocks { // process incoming blocks }
pub fn start(_: ()) -> Self
Purpose: Constructs and returns a new instance of
ThreadSyncService.Parameters: None (unit type
()).Returns: A new
ThreadSyncServiceinstance with an emptylast_finalized_blocksmap.Usage Example:
let thread_sync_service = ThreadSyncService::start(());
Important Implementation Considerations
The service exclusively deals with finalized blocks, which guarantees that forks are resolved before blocks are recorded. This design choice simplifies concurrency and consistency concerns.
The current implementation does not optimize for memory growth caused by thread lifecycle changes such as collapsing or splitting, but this is acknowledged as a non-critical issue.
A future improvement path mentioned in comments involves implementing optimistic thread state synchronization and comparing performance with the finalized-blocks referencing method on real networks.
Interactions with Other System Components
BlockIdentifier and ThreadIdentifier: These types, imported from the crate's
typesmodule, uniquely identify blocks and threads respectively and are integral to the service's tracking functionality.The service provides foundational support for cross-thread messaging by identifying which blocks from other threads may contain messages or accounts relevant to a given thread.
Other components responsible for message routing, block verification, or thread management would rely on this service to obtain accurate and up-to-date cross-thread synchronization information.
Since it tracks only finalized blocks, it complements components handling block finalization and fork resolution.
Visual Diagram: Class Structure of ThreadSyncService
classDiagram
class ThreadSyncService {
-last_finalized_blocks: HashMap<ThreadIdentifier, BlockIdentifier>
+on_block_finalized()
+list_blocks_sending_messages_to_thread()
+start()
}
This diagram illustrates the ThreadSyncService with its main private field and public methods, highlighting the simplicity and focused responsibility of the service.