filter.rs

Overview

This file defines GraphQL input filter structures and their serialization for querying blocks and their external block references. It provides a flexible interface to specify field-based filtering criteria for blockchain blocks, supporting complex logical combinations with OR conditions.

The filters are designed to generate SQL-like WHERE clauses for querying block data, leveraging optional typed filters such as boolean, integer, float, and string filters. These filters enable clients to build detailed conditional queries for blockchain data retrieval.

Main Structures

ExtBlkRefFilter

Represents filtering options for external block references with the following fields:

Field

Type

Description

end_lt

OptStringFilter

Filter for the end_lt string value

file_hash

OptStringFilter

Filter for the file hash string

root_hash

OptStringFilter

Filter for the root hash string

seq_no

OptFloatFilter

Filter for the sequence number as a floating-point value

or

Option>

Optional nested filter combined with OR logic

This struct supports nested OR conditions allowing for complex filtering logic on external block references.

Usage Example

let ext_filter = ExtBlkRefFilter {
    root_hash: Some(StringFilter { eq: Some("some_hash".into()), ..Default::default() }),
    or: Some(Box::new(ExtBlkRefFilter {
        file_hash: Some(StringFilter { eq: Some("another_hash".into()), ..Default::default() }),
        ..Default::default()
    })),
    ..Default::default()
};

BlockFilter

Represents filtering options for blocks with a comprehensive set of optional filters:

Field

Type

Description

id

OptStringFilter

Filter for block ID

after_merge

OptBooleanFilter

Filter indicating if block is after a merge

after_split

OptBooleanFilter

Filter indicating if block is after a split

flags

OptIntFilter

Integer filter for block flags

gen_utime

OptFloatFilter

Floating-point filter for generation Unix time

key_block

OptBooleanFilter

Boolean filter indicating if block is a key block

prev_alt_ref

Option<ExtBlkRefFilter> (with prefix prev_alt_ref_)

Filter for previous alternate block reference

prev_ref

Option<ExtBlkRefFilter> (with prefix prev_ref_)

Filter for previous block reference

shard

OptStringFilter

String filter for shard identifier

status

OptIntFilter

Integer filter for block status

tr_count

OptIntFilter

Integer filter for transaction count

workchain_id

OptIntFilter

Integer filter for workchain identifier

or

Option<Box<BlockFilter>>

Optional nested filter combined with OR logic

This struct uses field prefixing for prev_ref and prev_alt_ref filters to flatten the nested fields with specific prefixes, enabling clear and distinct query parameterization.

Usage Example

let block_filter = BlockFilter {
    key_block: Some(BooleanFilter { eq: Some(true), ..Default::default() }),
    workchain_id: Some(IntFilter { eq: Some(-1), ..Default::default() }),
    ..Default::default()
};

The above example filters for blocks that are key blocks and belong to workchain ID -1.

Prefixing Macros

Two macros define prefixes for flattening nested filters:

This prefixing ensures that nested filter fields are serialized with distinct and meaningful field names when flattened, facilitating straightforward querying.

Trait Implementations

WhereOp for BlockFilter

The BlockFilter struct implements the WhereOp trait, which provides functionality to convert the filter into a SQL-like WHERE clause string. This allows easy integration with database querying mechanisms.

Example

The test cases demonstrate converting a BlockFilter instance into a WHERE clause string:

let bf = BlockFilter {
    key_block: Some(BooleanFilter { eq: Some(true), ..Default::default() }),
    workchain_id: Some(IntFilter { eq: Some(-1), ..Default::default() }),
    ..Default::default()
};
assert_eq!(bf.to_where().unwrap(), "WHERE key_block = true AND workchain_id = -1");

Testing

The tests module contains unit tests validating the behavior of BlockFilter filtering and SQL WHERE clause generation:

These tests demonstrate the correctness of filter construction and serialization to query strings.

Interaction with Other Modules

This file depends on and interacts with:

Together, these components enable defining complex, typed, and nested filtering logic for GraphQL queries related to blockchain blocks.

Visual Diagram

classDiagram
class ExtBlkRefFilter {
+end_lt: OptStringFilter
+file_hash: OptStringFilter
+root_hash: OptStringFilter
+seq_no: OptFloatFilter
+or: Option<ExtBlkRefFilter>
}
class BlockFilter {
+id: OptStringFilter
+after_merge: OptBooleanFilter
+after_split: OptBooleanFilter
+flags: OptIntFilter
+gen_utime: OptFloatFilter
+key_block: OptBooleanFilter
+prev_alt_ref: Option<ExtBlkRefFilter>
+prev_ref: Option<ExtBlkRefFilter>
+shard: OptStringFilter
+status: OptIntFilter
+tr_count: OptIntFilter
+workchain_id: OptIntFilter
+or: Option<BlockFilter>
+to_where()
}
BlockFilter --> ExtBlkRefFilter : uses
ExtBlkRefFilter --> ExtBlkRefFilter : nested OR
BlockFilter --> BlockFilter : nested OR