save_service.rs

Overview

This file implements a long-running service responsible for saving the state of blockchain blocks asynchronously. It listens for updates to the BlockStateInner via a message receiver and triggers persistent storage operations when the state changes. The service ensures that the latest state is saved only when necessary, optimizing disk I/O and maintaining consistency of the block state data.

Functions

start_state_save_service

pub fn start_state_save_service(
    state_receiver: InstrumentedReceiver<Arc<BlockStateInner>>,
) -> anyhow::Result<()>

Purpose

Starts the state save service, which continuously listens for new block state updates delivered through the provided InstrumentedReceiver. Upon receiving a new state, it checks whether the state has changed since the last save and if so, persists the updated state.

Parameters

Return Value

Usage Example

let receiver = ...; // acquire an InstrumentedReceiver<Arc<BlockStateInner>>
start_state_save_service(receiver)?;

This function is typically run in a dedicated thread or asynchronous task as part of the node's background services.

Behavior and Implementation Details

Important Implementation Notes

Key Structures and Concepts

BlockStateInner

InstrumentedReceiver

Guarded

Interaction with Other System Components

Mermaid Diagram

classDiagram
class save_service {
+start_state_save_service(state_receiver)
}
class BlockStateInner {
+block_seq_no()
+block_identifier
+last_saved_object_state_version
+object_state_version
+save()
+shared_access
}
class InstrumentedReceiver {
+recv()
}
class Guarded {
+guarded()
}
save_service --> InstrumentedReceiver : consumes
save_service --> BlockStateInner : processes and saves
BlockStateInner o-- Guarded : uses for access control

This diagram illustrates the relationship between the main function and the key types it interacts with in the file. The start_state_save_service function consumes data from InstrumentedReceiver, processes BlockStateInner instances, and uses Guarded for safe concurrent data access.