hypersync_client/
types.rs

1use crate::simple_types::{Block, Event, InternalEventJoinStrategy, Log, Trace, Transaction};
2use anyhow::Context;
3use arrow::array::RecordBatch;
4use hypersync_net_types::RollbackGuard;
5
6/// Query response in Arrow format
7#[derive(Default, Debug, Clone)]
8pub struct ArrowResponseData {
9    /// Query blocks response
10    pub blocks: Vec<RecordBatch>,
11    /// Query transactions response
12    pub transactions: Vec<RecordBatch>,
13    /// Query logs response
14    pub logs: Vec<RecordBatch>,
15    /// Query traces response
16    pub traces: Vec<RecordBatch>,
17    /// Query decoded_logs response.
18    ///
19    /// Populated only if event_signature is present.
20    pub decoded_logs: Vec<RecordBatch>,
21}
22
23/// Query response data in Rust native format
24#[derive(Default, Debug, Clone)]
25pub struct ResponseData {
26    /// Query blocks response
27    pub blocks: Vec<Vec<Block>>,
28    /// Query transactions response
29    pub transactions: Vec<Vec<Transaction>>,
30    /// Query logs response
31    pub logs: Vec<Vec<Log>>,
32    /// Query traces response
33    pub traces: Vec<Vec<Trace>>,
34}
35
36impl EventResponse {
37    /// Create EventResponse from ArrowResponse with the specified event join strategy
38    pub(crate) fn try_from_arrow_response(
39        arrow_response: &ArrowResponse,
40        event_join_strategy: &InternalEventJoinStrategy,
41    ) -> anyhow::Result<Self> {
42        let r: QueryResponse = arrow_response
43            .try_into()
44            .context("convert arrow response")?;
45        Ok(Self {
46            archive_height: r.archive_height,
47            next_block: r.next_block,
48            total_execution_time: r.total_execution_time,
49            data: event_join_strategy.join_from_response_data(r.data),
50            rollback_guard: r.rollback_guard,
51        })
52    }
53}
54
55impl TryFrom<&'_ ArrowResponse> for QueryResponse {
56    type Error = anyhow::Error;
57    fn try_from(arrow_response: &ArrowResponse) -> Result<Self, Self::Error> {
58        let blocks = arrow_response
59            .data
60            .blocks
61            .iter()
62            .map(Block::from_arrow)
63            .collect::<anyhow::Result<Vec<_>>>()
64            .context("convert blocks")?;
65
66        let transactions = arrow_response
67            .data
68            .transactions
69            .iter()
70            .map(Transaction::from_arrow)
71            .collect::<anyhow::Result<Vec<_>>>()
72            .context("convert transactions")?;
73
74        let logs = arrow_response
75            .data
76            .logs
77            .iter()
78            .map(Log::from_arrow)
79            .collect::<anyhow::Result<Vec<_>>>()
80            .context("convert logs")?;
81        let traces = arrow_response
82            .data
83            .traces
84            .iter()
85            .map(Trace::from_arrow)
86            .collect::<anyhow::Result<Vec<_>>>()
87            .context("convert traces")?;
88
89        Ok(QueryResponse {
90            archive_height: arrow_response.archive_height,
91            next_block: arrow_response.next_block,
92            total_execution_time: arrow_response.total_execution_time,
93            data: ResponseData {
94                blocks,
95                transactions,
96                logs,
97                traces,
98            },
99            rollback_guard: arrow_response.rollback_guard.clone(),
100        })
101    }
102}
103
104/// Query response from hypersync instance.
105/// Contain next_block field in case query didn't process all the block range
106#[derive(Debug, Clone)]
107pub struct QueryResponse<T = ResponseData> {
108    /// Current height of the source hypersync instance
109    pub archive_height: Option<u64>,
110    /// Next block to query for, the responses are paginated so
111    /// the caller should continue the query from this block if they
112    /// didn't get responses up to the to_block they specified in the Query.
113    pub next_block: u64,
114    /// Total time it took the hypersync instance to execute the query.
115    pub total_execution_time: u64,
116    /// Response data
117    pub data: T,
118    /// Rollback guard
119    pub rollback_guard: Option<RollbackGuard>,
120}
121
122/// Alias for Arrow Query response
123pub type ArrowResponse = QueryResponse<ArrowResponseData>;
124/// Alias for Event oriented, vectorized QueryResponse
125pub type EventResponse = QueryResponse<Vec<Event>>;