file_saving_service.rs

Overview

The file_saving_service.rs file implements the FileSavingService struct and its associated functionality. This service is responsible for asynchronously saving the state of blockchain-related data to the file system. Its primary role is to serialize an optimistic state snapshot along with other relevant block and repository data into a binary format and persist it to a file on disk. This allows for durable storage of intermediate blockchain state data, facilitating recovery, synchronization, or diagnostic workflows.

The file leverages multi-threading to offload the potentially blocking I/O operations from the main execution thread, using Rust's standard threading primitives alongside concurrency utilities like parking_lot::Mutex for thread-safe access to shared resources.


Struct: FileSavingService

#[derive(Clone, TypedBuilder)]
pub struct FileSavingService {
    root_path: PathBuf,
    threads: Arc<Mutex<Vec<JoinHandle<anyhow::Result<()>>>>>,
    repository: RepositoryImpl,
    block_state_repository: BlockStateRepository,
    shared_services: SharedServices,
    message_db: MessageDurableStorage,
}

Description

FileSavingService encapsulates all dependencies and state required to perform state saving operations. It maintains:

The struct is cloneable and uses the TypedBuilder pattern for ergonomic construction.


Method: save_object

pub fn save_object(
    &self,
    state: Arc<OptimisticStateImpl>,
    path: PathBuf,
) -> anyhow::Result<()>

Purpose

This method initiates the process of saving a snapshot of the optimistic blockchain state, along with related metadata and repository data, to disk asynchronously in a separate thread.

Parameters

Returns

Usage Example

let file_saving_service = FileSavingService::builder()
    .root_path(root_dir)
    .repository(repo_impl)
    .block_state_repository(block_repo)
    .shared_services(shared)
    .message_db(message_storage)
    .build();

let optimistic_state = Arc::new(current_state);

file_saving_service.save_object(optimistic_state, PathBuf::from("state_snapshot.bin"))?;

Implementation Details

Algorithms and Patterns


Interactions with Other Modules


Detailed Workflow Diagram

flowchart TD
A[save_object called] --> B[Construct full save path]
B --> C[Clone dependencies for thread]
C --> D[Spawn save thread]
D --> E[Fetch block messages from message_db]
E --> F[Serialize optimistic state]
F --> G[Get cross-thread ref data via shared_services]
G --> H[Retrieve finalized block]
H --> I[Retrieve block state and extract fields]
I --> J[Retrieve parent block state and checkpoints]
J --> K[Build ThreadSnapshot struct]
K --> L[Serialize ThreadSnapshot]
L --> M[Write to temp file]
M --> N[Rename temp file to target path]
N --> O[Thread completes]
D --> P[Add thread handle to threads Vec]
P --> Q[Return Ok]

Supporting Types and Traits


Notes on Error Handling


This file is integral for the persistence layer of blockchain state snapshots, enabling asynchronous, thread-safe saving of complex state data to disk. It connects multiple repository and service modules to gather comprehensive state information for durable storage. For details on state representation, serialization, and cross-thread synchronization, refer to topics like Repository Pattern, State Serialization, and Threading and Concurrency.