hypersync_client/
types.rs1use crate::simple_types::{Block, Event, InternalEventJoinStrategy, Log, Trace, Transaction};
2use anyhow::Context;
3use arrow::array::RecordBatch;
4use hypersync_net_types::RollbackGuard;
5
6#[derive(Default, Debug, Clone)]
8pub struct ArrowResponseData {
9 pub blocks: Vec<RecordBatch>,
11 pub transactions: Vec<RecordBatch>,
13 pub logs: Vec<RecordBatch>,
15 pub traces: Vec<RecordBatch>,
17 pub decoded_logs: Vec<RecordBatch>,
21}
22
23#[derive(Default, Debug, Clone)]
25pub struct ResponseData {
26 pub blocks: Vec<Vec<Block>>,
28 pub transactions: Vec<Vec<Transaction>>,
30 pub logs: Vec<Vec<Log>>,
32 pub traces: Vec<Vec<Trace>>,
34}
35
36impl EventResponse {
37 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#[derive(Debug, Clone)]
107pub struct QueryResponse<T = ResponseData> {
108 pub archive_height: Option<u64>,
110 pub next_block: u64,
114 pub total_execution_time: u64,
116 pub data: T,
118 pub rollback_guard: Option<RollbackGuard>,
120}
121
122pub type ArrowResponse = QueryResponse<ArrowResponseData>;
124pub type EventResponse = QueryResponse<Vec<Event>>;