mod.rs

Overview

This file serves as a module entry point primarily focused on blockchain state management operations. It exposes a submodule named downloader and implements a key internal function for applying blockchain state updates onto a shared state. The core functionality revolves around deserializing blockchain data structures and applying state transitions, leveraging types and utilities provided by the tvm_block and tvm_types crates.


Module Structure


Function: apply_block_on_state()

Purpose

This function demonstrates the process of applying a blockchain block's state update to a shared blockchain state. It orchestrates the deserialization of a block and shared state, reads the state update from the block, and applies this update to produce a new state.

Signature

fn apply_block_on_state() -> anyhow::Result<tvm_types::Cell>

Parameters

Return Value

Usage Example

match apply_block_on_state() {
    Ok(new_state) => {
        // Use the updated blockchain state (new_state)
    }
    Err(e) => {
        eprintln!("Error applying block on state: {}", e);
    }
}

Implementation Details

  1. Block Construction:
    Invokes tvm_block::Block::construct_from_bytes(&[]) with an empty byte slice to construct a blockchain block object. This operation may fail if the bytes are invalid or insufficient, thus the error is captured and mapped with a contextual message.

  2. Shared State Deserialization:
    Uses tvm_types::read_single_root_boc([]) to deserialize a shared blockchain state from an empty byte slice. The function expects the serialized BOC (Bag of Cells) format. Failure to read the shared state is handled with an error message.

  3. State Update Extraction:
    Calls block.read_state_update() to extract the state update embedded within the block. This step is critical as it retrieves the delta changes to apply to the current state.

  4. Applying State Update:
    The extracted state update is applied to the shared state by invoking state_update.apply_for(&shared_state). This results in a new state reflecting the changes in the block.

  5. Error Handling:
    Each step uses map_err to convert errors into anyhow::Error with meaningful context strings, facilitating debugging.

Important Notes


Dependencies and Interactions


Diagram: File Function Structure

flowchart TD
A["apply_block_on_state()"] --> B[Construct Block from bytes]
B -->|Success| C[Read shared state from BOC]
C -->|Success| D[Extract state update from block]
D -->|Success| E[Apply state update to shared state]
E -->|Success| F["Return new state (tvm_types::Cell)"]
B -->|Error| G[Return error with message]
C -->|Error| G
D -->|Error| G
E -->|Error| G

References to Related Topics