cross_thread_ref_repository.rs

Overview

This file implements a repository for managing CrossThreadRefData, which represents cross-thread reference data associated with blockchain blocks identified by BlockIdentifier. It is designed to operate efficiently in multithreaded environments, supporting lookup, caching, and persistence of cross-thread reference data. The repository provides methods to retrieve individual cross-thread reference data items as well as a historical tail of such data for up to 100 blocks, facilitating state synchronization during node join operations.

The file relies on an LRU cache (LruCache) to optimize access to frequently requested data and supports two modes of data storage: file-based and database-backed (controlled by the messages_db feature flag). It uses synchronization primitives (Mutex, Arc) from the parking_lot crate to ensure thread-safe access to the cache.

Traits

CrossThreadRefDataRead

pub trait CrossThreadRefDataRead {
    fn get_cross_thread_ref_data(
        &self,
        identifier: &BlockIdentifier,
    ) -> anyhow::Result<CrossThreadRefData>;
}

CrossThreadRefDataHistory

pub trait CrossThreadRefDataHistory {
    fn get_history_tail(
        &self,
        identifier: &BlockIdentifier,
    ) -> anyhow::Result<Vec<CrossThreadRefData>>;
}

Structs

CrossThreadRefDataRepository

#[derive(Clone)]
pub struct CrossThreadRefDataRepository {
    data_dir: PathBuf,
    cross_thread_ref_data_cache: Arc<Mutex<LruCache<BlockIdentifier, Option<CrossThreadRefData>>>>,
    crossref_store: CrossRefStorage,
}

Implementation of CrossThreadRefDataRead

Implementation of CrossThreadRefDataHistory

Other Methods

Important Implementation Details

Interaction with Other System Components

Visual Diagram: Class Structure and Method Relationships

classDiagram
class CrossThreadRefDataRepository {
-data_dir: PathBuf
-cross_thread_ref_data_cache: Arc<Mutex<LruCache<BlockIdentifier, Option<CrossThreadRefData>>>>
-crossref_store: CrossRefStorage
+new()
+get_cross_thread_ref_data_path()
+set_cross_thread_ref_data()
+get_cross_thread_ref_data()
+get_history_tail()
}
CrossThreadRefDataRepository ..|> CrossThreadRefDataRead
CrossThreadRefDataRepository ..|> CrossThreadRefDataHistory

This diagram shows the main struct CrossThreadRefDataRepository with its fields and key methods, illustrating its implementation of the two traits for reading and retrieving history. The private helper method for path construction and the mutator method for setting data are included to show internal workflow.