serialize_block.rs

Overview

This file provides functionality for reflecting and archiving blocks and their related data (accounts, transactions, messages) into a database system. It primarily processes a tvm_block extracted from a cryptographic envelope, serializes its contents, prepares archival structures, and stores them in a document-oriented database. The file handles intricate interactions between blockchain block data, accounts, transactions, and messages, ensuring consistent representation and traceability of blockchain state changes.

Key responsibilities include:


Detailed Descriptions

Constants and Static References


Functions

reflect_block_in_db

pub fn reflect_block_in_db(
    archive: Arc<Mutex<dyn DocumentsDb>>,
    envelope: Envelope<GoshBLS, AckiNackiBlock>,
    raw_block: Option<Vec<u8>>,
    shard_state: Arc<ShardStateUnsplit>,
    transaction_traces: &mut HashMap<UInt256, Vec<EngineTraceInfoData>, RandomState>,
) -> anyhow::Result<()>

Purpose:
Processes a block encapsulated in an envelope, reflects its data into the database by preparing and storing archived versions of the block, transactions, accounts, and messages.

Parameters:

Returns:

Usage Example:

reflect_block_in_db(
    archive.clone(),
    envelope,
    Some(raw_block_bytes),
    shard_state.clone(),
    &mut transaction_traces,
)?;

Implementation Details:


prepare_messages_from_transaction

pub(crate) fn prepare_messages_from_transaction(
    transaction: &Transaction,
    block_id: UInt256,
    transaction_id: UInt256,
    transaction_index: &str,
    block_root_for_proof: Option<&Cell>,
    messages: &mut HashMap<UInt256, ArchMessage>,
) -> anyhow::Result<()>

Purpose:
Extracts and prepares message archive structures from a given transaction, including both inbound and outbound messages.

Parameters:

Returns:

Implementation Details:


prepare_message_archive_struct

pub(crate) fn prepare_message_archive_struct(
    message_cell: Cell,
    message: Message,
    block_root_for_proof: Option<&Cell>,
    block_id: UInt256,
    transaction_id: Option<UInt256>,
    transaction_now: Option<u32>,
) -> tvm_types::Result<ArchMessage>

Purpose:
Creates an archival database record for a message, optionally including cryptographic proof data.

Parameters:

Returns:

Usage:
Called internally during transaction processing to generate message records.


prepare_transaction_archive_struct

pub(crate) fn prepare_transaction_archive_struct(
    tr_cell: Cell,
    transaction: Transaction,
    block_root: &Cell,
    block_id: UInt256,
    workchain_id: i32,
    add_proof: bool,
    _trace: Option<Vec<EngineTraceInfoData>>,
) -> anyhow::Result<ArchTransaction>

Purpose:
Prepares an archival structure for a transaction including optional cryptographic proof.

Parameters:

Returns:

Implementation Details:


prepare_account_archive_struct

pub(crate) fn prepare_account_archive_struct(
    account: Account,
    prev_account_state: Option<Account>,
    last_trans_chain_order: Option<String>,
    last_trans_hash: UInt256,
    dapp_id: Option<UInt256>,
) -> anyhow::Result<ArchAccount>

Purpose:
Constructs an archive record representing an account's current state.

Parameters:

Returns:

Usage:
Used to archive changed accounts reflecting the latest blockchain state.


prepare_deleted_account_archive_struct

pub(crate) fn prepare_deleted_account_archive_struct(
    account_id: AccountId,
    workchain_id: i32,
    prev_account_state: Option<Account>,
    last_trans_chain_order: Option<String>,
) -> anyhow::Result<ArchAccount>

Purpose:
Prepares an archival record for an account that has been deleted (i.e., removed from the state).

Parameters:

Returns:


prepare_block_archive_struct

pub(crate) fn prepare_block_archive_struct(
    envelope: Envelope<GoshBLS, AckiNackiBlock>,
    block_root: &Cell,
    boc: &[u8],
    file_hash: &UInt256,
    block_order: String,
) -> anyhow::Result<ArchBlock>

Purpose:
Generates an archival record for a block including metadata, cryptographic signatures, message lists, and serialized block data.

Parameters:

Returns:

Implementation Details:


block_index

pub(crate) fn block_index(block: &Block) -> anyhow::Result<String>

Purpose:
Computes a unique string index for a block based on its generation time, shard prefix, and sequence number.

Parameters:

Returns:

Usage:
Used to order transactions and blocks consistently within the archival system.


u64_to_string

pub fn u64_to_string(value: u64) -> String

Purpose:
Encodes a 64-bit unsigned integer into a hexadecimal string with a prefixed length indicator.

Parameters:

Returns:


Important Implementation Details and Algorithms


Interactions with Other System Components


Visual Diagram: File Structure and Function Relationships

flowchart TD
A[reflect_block_in_db] --> B[prepare_messages_from_transaction]
A --> C[prepare_transaction_archive_struct]
A --> D[prepare_account_archive_struct]
A --> E[prepare_deleted_account_archive_struct]
A --> F[prepare_block_archive_struct]
B --> G[prepare_message_archive_struct]
A -->|calls| H[block_index]
H --> I[u64_to_string]
style A fill:#f9f,stroke:#333,stroke-width:2px
style B fill:#bbf,stroke:#333,stroke-width:1.5px
style C fill:#bbf,stroke:#333,stroke-width:1.5px
style D fill:#bbf,stroke:#333,stroke-width:1.5px
style E fill:#bbf,stroke:#333,stroke-width:1.5px
style F fill:#bbf,stroke:#333,stroke-width:1.5px
style G fill:#afa,stroke:#333,stroke-width:1.5px
style H fill:#fc9,stroke:#333,stroke-width:1.5px
style I fill:#fc9,stroke:#333,stroke-width:1.5px

Notes on Usage and Integration


This file is critical for maintaining a reliable, queryable, and traceable record of blockchain state transitions in the archival database. It bridges raw blockchain data structures and the persistent storage layer while preserving cryptographic integrity and ordering semantics.