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


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

Return Value

Behavior and Implementation Details

  1. Block Extraction
    The block data is extracted from the envelope via a clone operation to avoid mutating the original envelope.

  2. Shard State Determination

    • If shard_state is provided, it is used as-is.

    • If not, the function asserts that shard_state_cell is present, then deserializes it into a ShardStateUnsplit object using construct_from_cell.

    • The deserialization failure triggers a panic with a clear error message.

  3. Logging
    Tracing logs with tracing::trace! record the block's sequence number and identifier before and after the database write operation.

  4. Database Write
    The actual persistence is delegated to reflect_block_in_db, passing the cloned database handle, the envelope, the shard state, and a mutable map to track transaction traces.

  5. Error Handling
    Any failure in reflect_block_in_db is wrapped as an anyhow::Error with 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


Interactions with Other System Components


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.