use crate::rate_limit::RateLimitInfo;
use crate::simple_types::{Block, Event, InternalEventJoinStrategy, Log, Trace, Transaction};
use anyhow::Context;
use arrow::array::RecordBatch;
use hypersync_net_types::RollbackGuard;
#[derive(Default, Debug, Clone)]
pub struct ArrowResponseData {
pub blocks: Vec<RecordBatch>,
pub transactions: Vec<RecordBatch>,
pub logs: Vec<RecordBatch>,
pub traces: Vec<RecordBatch>,
pub decoded_logs: Vec<RecordBatch>,
}
#[derive(Default, Debug, Clone)]
pub struct ResponseData {
pub blocks: Vec<Vec<Block>>,
pub transactions: Vec<Vec<Transaction>>,
pub logs: Vec<Vec<Log>>,
pub traces: Vec<Vec<Trace>>,
}
impl EventResponse {
pub(crate) fn try_from_arrow_response(
arrow_response: &ArrowResponse,
event_join_strategy: &InternalEventJoinStrategy,
) -> anyhow::Result<Self> {
let r: QueryResponse = arrow_response
.try_into()
.context("convert arrow response")?;
Ok(Self {
archive_height: r.archive_height,
next_block: r.next_block,
total_execution_time: r.total_execution_time,
data: event_join_strategy.join_from_response_data(r.data),
rollback_guard: r.rollback_guard,
})
}
}
impl TryFrom<&'_ ArrowResponse> for QueryResponse {
type Error = anyhow::Error;
fn try_from(arrow_response: &ArrowResponse) -> Result<Self, Self::Error> {
let blocks = arrow_response
.data
.blocks
.iter()
.map(Block::from_arrow)
.collect::<anyhow::Result<Vec<_>>>()
.context("convert blocks")?;
let transactions = arrow_response
.data
.transactions
.iter()
.map(Transaction::from_arrow)
.collect::<anyhow::Result<Vec<_>>>()
.context("convert transactions")?;
let logs = arrow_response
.data
.logs
.iter()
.map(Log::from_arrow)
.collect::<anyhow::Result<Vec<_>>>()
.context("convert logs")?;
let traces = arrow_response
.data
.traces
.iter()
.map(Trace::from_arrow)
.collect::<anyhow::Result<Vec<_>>>()
.context("convert traces")?;
Ok(QueryResponse {
archive_height: arrow_response.archive_height,
next_block: arrow_response.next_block,
total_execution_time: arrow_response.total_execution_time,
data: ResponseData {
blocks,
transactions,
logs,
traces,
},
rollback_guard: arrow_response.rollback_guard.clone(),
})
}
}
#[derive(Debug, Clone)]
pub struct QueryResponse<T = ResponseData> {
pub archive_height: Option<u64>,
pub next_block: u64,
pub total_execution_time: u64,
pub data: T,
pub rollback_guard: Option<RollbackGuard>,
}
pub type ArrowResponse = QueryResponse<ArrowResponseData>;
pub type EventResponse = QueryResponse<Vec<Event>>;
#[derive(Debug, Clone)]
pub struct QueryResponseWithRateLimit<T = ResponseData> {
pub response: QueryResponse<T>,
pub rate_limit: RateLimitInfo,
}