/
indexer.rs
50 lines (43 loc) · 1.82 KB
/
indexer.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! An Indexer provides a common interface for bubbling up chain-specific
//! event-data to another entity (e.g. a `ContractSync`). For example, the only
//! way to retrieve data such as the chain's latest block number or a list of
//! checkpoints/messages emitted within a certain block range by calling out to
//! a chain-specific library and provider (e.g. ethers::provider).
use std::fmt::Debug;
use std::ops::RangeInclusive;
use async_trait::async_trait;
use auto_impl::auto_impl;
use serde::Deserialize;
use crate::{ChainResult, Indexed, LogMeta};
/// Indexing mode.
#[derive(Copy, Debug, Default, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub enum IndexMode {
/// Block based indexing.
#[default]
Block,
/// Sequence based indexing.
Sequence,
}
/// Interface for an indexer.
#[async_trait]
#[auto_impl(&, Box, Arc,)]
pub trait Indexer<T: Sized>: Send + Sync + Debug {
/// Fetch list of logs between blocks `from` and `to`, inclusive.
async fn fetch_logs(
&self,
range: RangeInclusive<u32>,
) -> ChainResult<Vec<(Indexed<T>, LogMeta)>>;
/// Get the chain's latest block number that has reached finality
async fn get_finalized_block_number(&self) -> ChainResult<u32>;
}
/// Interface for indexing data in sequence.
/// SequenceAwareIndexer is an umbrella trait for all indexers types (sequence-aware and rate-limited).
/// The rate-limited indexer doesn't need `SequenceAwareIndexer`, so impls of `SequenceAwareIndexer` just return nullish values.
/// TODO: Refactor such that indexers aren't required to implement `SequenceAwareIndexer`
#[async_trait]
#[auto_impl(&, Box, Arc)]
pub trait SequenceAwareIndexer<T>: Indexer<T> {
/// Return the latest finalized sequence (if any) and block number
async fn latest_sequence_count_and_tip(&self) -> ChainResult<(Option<u32>, u32)>;
}