mod.rs
Overview
This file provides core functionality for archiving blockchain data into a persistent storage system. It primarily focuses on writing block data, wrapped inside cryptographic envelopes, into a document-oriented database. The file integrates several components related to block serialization, shard state deserialization, and cryptographic envelope handling.
The key functionality is encapsulated in the write_to_db function, which accepts a database reference, a cryptographically signed block envelope, and optionally a shard state or its serialized cell form. The function ensures that the block and its associated shard state are properly deserialized, traced, and persisted.
The file also declares a submodule serialize_block which likely contains utilities for block serialization and deserialization, supporting the core write operation.
Detailed Elements
Module: serialize_block
Declared as a public submodule.
Expected to provide serialization/deserialization utilities for blocks.
Interacts with the main archiving function by enabling the conversion of block data to/from persistent storage formats.
Function: write_to_db
pub fn write_to_db(
archive: Arc<Mutex<dyn DocumentsDb>>,
envelope: Envelope<GoshBLS, AckiNackiBlock>,
shard_state: Option<Arc<ShardStateUnsplit>>,
shard_state_cell: Option<Cell>,
) -> anyhow::Result<()>
Purpose
Writes a blockchain block, wrapped in a BLS-signed envelope, along with its shard state, into the provided document-oriented database archive.
Parameters
archive: Arc<Mutex<dyn DocumentsDb>>
A thread-safe, shared reference to a database interface supporting document operations.Mutexensures safe concurrent access.envelope: Envelope<GoshBLS, AckiNackiBlock>
A cryptographic envelope containing the block data (AckiNackiBlock) signed using the BLS scheme (GoshBLS). This ensures data authenticity and integrity.shard_state: Option<Arc<ShardStateUnsplit>>
An optional deserialized shard state object representing the blockchain shard state at the block's height. If provided, it is used directly.shard_state_cell: Option<Cell>
An optional serialized cell representation of the shard state. This is used to reconstruct the shard state if theshard_stateparameter is not provided.
Return Value
anyhow::Result<()>
ReturnsOk(())on successful write to the database, or an error wrapped inanyhow::Errordescribing any failure during the operation.
Behavior and Implementation Details
Block Extraction
The block data is extracted from the envelope via a clone operation to avoid mutating the original envelope.Shard State Determination
If
shard_stateis provided, it is used as-is.If not, the function asserts that
shard_state_cellis present, then deserializes it into aShardStateUnsplitobject usingconstruct_from_cell.The deserialization failure triggers a panic with a clear error message.
Logging
Tracing logs withtracing::trace!record the block's sequence number and identifier before and after the database write operation.Database Write
The actual persistence is delegated toreflect_block_in_db, passing the cloned database handle, the envelope, the shard state, and a mutable map to track transaction traces.Error Handling
Any failure inreflect_block_in_dbis wrapped as ananyhow::Errorwith a descriptive message. The function panics if this occurs, indicating a critical failure.
Usage Example
use std::sync::Arc;
use parking_lot::Mutex;
use tvm_types::Cell;
use crate::database::DocumentsDb;
use crate::bls::envelope::Envelope;
use crate::bls::GoshBLS;
use crate::types::AckiNackiBlock;
use crate::write_to_db;
let archive: Arc<Mutex<dyn DocumentsDb>> = /* obtain database reference */;
let envelope: Envelope<GoshBLS, AckiNackiBlock> = /* obtain signed block envelope */;
let shard_state_cell: Option<Cell> = /* obtain serialized shard state if available */;
let shard_state: Option<Arc<ShardStateUnsplit>> = None;
write_to_db(archive, envelope, shard_state, shard_state_cell)?;
Important Implementation Details
The function relies on the
ShardStateUnsplit::construct_from_cellmethod for deserialization of shard state from a low-level cell structure. This deserialization is critical for ensuring the block is archived along with its shard context.The
reflect_block_in_dbfunction, imported from theserialize_blocksubmodule, handles the translation from in-memory block representation to persistent storage format, including transaction trace extraction.Usage of
Arc<Mutex<...>>for the database handle ensures thread safety and concurrency control, allowing multiple operations to safely serialize blocks into the document database.The function uses Rust's
anyhowcrate for flexible error handling, converting internal errors into a unified error type with contextual messages.The file uses
tracingfor lightweight, structured debug logging, useful for tracing block archival operations during runtime.
Interactions with Other System Components
Database Layer (
DocumentsDb)
The file interacts with the database abstraction layer, which supports document-style operations. It depends on this layer for persistent storage of block data and related transaction traces.Blockchain Types (
AckiNackiBlock,ShardStateUnsplit)
It handles blockchain-specific data structures representing blocks and shard state.Cryptographic Envelopes (
Envelope<GoshBLS, AckiNackiBlock>)
The blocks are wrapped in BLS-signed envelopes ensuring authenticity, which this module processes for archival.Serialization Module (
serialize_block)
Provides the core serialization routines used for reflecting blocks into the database.Tracing and Logging
Integrates with the system-wide tracing infrastructure for observability.
Visual Diagram
flowchart TD
A[write_to_db] --> B[Extract Block from Envelope]
A --> C{Shard State Provided?}
C -- Yes --> D[Use Provided Shard State]
C -- No --> E[Deserialize Shard State from Cell]
E --> F[ShardStateUnsplit::construct_from_cell]
D --> G[Prepare Transaction Trace Map]
F --> G
G --> H[Call reflect_block_in_db]
H --> I[Write Block & Traces to DocumentsDb]
I --> J[Return Result]
This flowchart outlines the main workflow of the write_to_db function, showing the decision process for handling shard state and the subsequent database write operation.