serialize.rs

Overview

This file implements serialization and deserialization logic for the AckiNackiBlock data structure. It provides methods to convert the block into raw byte arrays including and excluding the hash, and integrates with Rust's serde framework to support transparent serialization and deserialization. The serialization process carefully encodes constituent parts of the block such as the common section, the TVM block, transaction count, and the block hash. The deserialization process reverses this, reconstructing the full AckiNackiBlock instance from raw bytes.

The file relies on external crates and modules like serde for serialization traits, tvm_block for handling TVM blocks, and tvm_types for encoding and decoding BOC (Bag of Cells) cells. It also references internal modules such as common_section and the main AckiNackiBlock definition.


Detailed Explanation

AckiNackiBlock::get_raw_data_without_hash(&self) -> anyhow::Result<Vec<u8>>


impl Serialize for AckiNackiBlock


impl<'de> Deserialize<'de> for AckiNackiBlock


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Mermaid Diagram: Flowchart of Serialization & Deserialization Workflow

flowchart TD
A[AckiNackiBlock Instance] --> B["get_raw_data_without_hash()"]
B --> C[Serialize common_section with length prefix]
C --> D[Serialize TVM block cell with length prefix]
D --> E[Append tx_cnt as 8-byte BE]
E --> F["Return raw data bytes (excluding hash)"]
A --> G[serde Serialize trait]
G -->|If raw_data exists| H[Serialize raw_data directly]
G -->|Else| B
B --> I[Append hash bytes]
I --> J[Serialize combined bytes]
K[Deserialize raw bytes] --> L[Parse 8-byte length: common_section_len]
L --> M[Deserialize common_section]
M --> N[Parse 8-byte length: block_len]
N --> O[Read BOC and construct TVM block]
O --> P[Parse 8-byte tx_cnt]
P --> Q[Parse 32-byte hash]
Q --> R[Construct AckiNackiBlock with all fields]
classDef serialize fill:#cfe2f3,stroke:#2f5597;
classDef deserialize fill:#f4cccc,stroke:#a61c00;
B,C,D,E,F,G,H,I,J serialize
K,L,M,N,O,P,Q,R deserialize

Summary of Key Functions and Traits

Function/Trait

Purpose

Parameters

Returns

get_raw_data_without_hash(&self)

Serialize block fields except hash

&self

anyhow::Result<Vec<u8>>

Serialize::serialize(&self, serializer)

Serialize full block, including hash

&self, serializer

Result<S::Ok, S::Error>

Deserialize::deserialize(deserializer)

Parse raw bytes into AckiNackiBlock instance

deserializer

Result<AckiNackiBlock, D::Error>


For additional technical details on serialization and deserialization in Rust, refer to the Serde Serialization topic. For understanding the BOC (Bag of Cells) format and TVM block specifics, see BOC Format and TVM Block subtopics.