block.rs

Overview

This file defines the Block struct representing blockchain block records stored in a SQLite database and provides asynchronous methods to query these blocks. The main functionality revolves around retrieving blocks from the database with various filtering, ordering, and pagination options. It leverages SQLx for database interactions and supports integration with GraphQL query arguments defined elsewhere.

Block Struct

The Block struct models the blocks table in the SQLite database, with fields corresponding to columns in the table. It derives Clone, Debug, and FromRow traits to support cloning, debugging, and direct deserialization from SQL query results.

Fields

The fields closely follow the blockchain domain model, storing details such as cryptographic signatures, references to previous blocks, transaction counts, merge/split states, and timestamps.

Methods

list

pub async fn list(
    pool: &SqlitePool,
    where_clause: String,
    order_by: String,
    limit: Option<i32>,
) -> anyhow::Result<Vec<Block>>

Retrieves a list of blocks from the database applying a custom SQL WHERE clause and ordering.

let blocks = Block::list(
    &pool,
    "WHERE seq_no > 100".to_string(),
    "ORDER BY seq_no DESC".to_string(),
    Some(10)
).await?;

latest_block

pub async fn latest_block(pool: &SqlitePool) -> anyhow::Result<Option<Block>>

Retrieves the most recent block in the database based on descending chain_order.

if let Some(block) = Block::latest_block(&pool).await? {
    println!("Latest block ID: {}", block.id);
}

blockchain_blocks

pub async fn blockchain_blocks(
    pool: &SqlitePool,
    args: &BlockchainBlocksQueryArgs,
) -> anyhow::Result<Vec<Block>>

Retrieves blockchain blocks filtered and paginated based on GraphQL query arguments.

let blocks = Block::blockchain_blocks(&pool, &args).await?;

Interaction with Other Modules

This file is primarily a backend data access layer component supporting the blockchain API and GraphQL queries by providing structured access to block data stored in SQLite.


Important Implementation Details


Mermaid Class Diagram

classDiagram
class Block {
+rowid: i64
+id: String
+after_merge: Option<i64>
+after_split: Option<i64>
+aggregated_signature: Option<Vec<u8>>
+before_split: Option<i64>
+boc: Option<Vec<u8>>
+chain_order: Option<String>
+end_lt: Option<String>
+file_hash: Option<String>
+flags: Option<i64>
+gen_catchain_seqno: Option<i64>
+gen_software_version: Option<i64>
+gen_software_capabilities: Option<String>
+gen_utime: Option<i64>
+gen_utime_ms_part: Option<i64>
+gen_validator_list_hash_short: Option<i64>
+global_id: Option<i64>
+in_msgs: Option<String>
+key_block: Option<i64>
+min_ref_mc_seqno: Option<i64>
+out_msgs: Option<String>
+parent: String
+prev_alt_ref_seq_no: Option<i64>
+prev_alt_ref_end_lt: Option<String>
+prev_alt_ref_file_hash: Option<String>
+prev_alt_ref_root_hash: Option<String>
+prev_key_block_seqno: Option<i64>
+prev_ref_seq_no: Option<i64>
+prev_ref_end_lt: Option<String>
+prev_ref_file_hash: Option<String>
+prev_ref_root_hash: Option<String>
+producer_id: Option<String>
+root_hash: Option<String>
+seq_no: i64
+signature_occurrences: Option<Vec<u8>>
+shard: Option<String>
+share_state_resource_address: Option<String>
+start_lt: Option<String>
+status: Option<i64>
+tr_count: Option<i64>
+version: Option<i64>
+want_merge: Option<i64>
+want_split: Option<i64>
+workchain_id: Option<i64>
+data: Option<Vec<u8>>
+thread_id: Option<String>
+list()
+latest_block()
+blockchain_blocks()
}

References

For more on GraphQL query arguments and pagination, see BlockchainBlocksQueryArgs and PaginateDirection.

For details on utility conversions used in SQL queries, see u64_to_string.

For default batch sizes and query configurations, see defaults.