Skip to main content

reth_rpc_eth_api/
filter.rs

1//! `eth_` RPC API for filtering.
2
3use alloy_json_rpc::RpcObject;
4use alloy_rpc_types_eth::{Filter, FilterChanges, FilterId, PendingTransactionFilterKind};
5use jsonrpsee::{core::RpcResult, proc_macros::rpc};
6use std::future::Future;
7
8/// Rpc Interface for poll-based ethereum filter API.
9#[rpc(server, namespace = "eth")]
10pub trait EthFilterApi<T: RpcObject, L: RpcObject> {
11    /// Creates a new filter and returns its id.
12    #[method(name = "newFilter")]
13    async fn new_filter(&self, filter: Filter) -> RpcResult<FilterId>;
14
15    /// Creates a new block filter and returns its id.
16    #[method(name = "newBlockFilter")]
17    async fn new_block_filter(&self) -> RpcResult<FilterId>;
18
19    /// Creates a pending transaction filter and returns its id.
20    #[method(name = "newPendingTransactionFilter")]
21    async fn new_pending_transaction_filter(
22        &self,
23        kind: Option<PendingTransactionFilterKind>,
24    ) -> RpcResult<FilterId>;
25
26    /// Returns all filter changes since last poll.
27    #[method(name = "getFilterChanges")]
28    async fn filter_changes(&self, id: FilterId) -> RpcResult<FilterChanges<T, L>>;
29
30    /// Returns all logs matching given filter (in a range 'from' - 'to').
31    #[method(name = "getFilterLogs")]
32    async fn filter_logs(&self, id: FilterId) -> RpcResult<Vec<L>>;
33
34    /// Uninstalls filter.
35    #[method(name = "uninstallFilter")]
36    async fn uninstall_filter(&self, id: FilterId) -> RpcResult<bool>;
37
38    /// Returns logs matching given filter object.
39    #[method(name = "getLogs")]
40    async fn logs(&self, filter: Filter) -> RpcResult<Vec<L>>;
41}
42
43/// Limits for logs queries
44#[derive(Default, Debug, Clone, Copy)]
45pub struct QueryLimits {
46    /// Maximum number of blocks that could be scanned per filter
47    pub max_blocks_per_filter: Option<u64>,
48    /// Maximum number of logs that can be returned in a response
49    pub max_logs_per_response: Option<usize>,
50    /// Maximum JSON size in bytes of the logs a multi-block query collects, estimated from a lower
51    /// bound on each log's encoding.
52    ///
53    /// Like `max_logs_per_response`, this does not apply to single-block queries.
54    pub max_response_size: Option<usize>,
55}
56
57impl QueryLimits {
58    /// Construct an object with no limits (more explicit than using default constructor)
59    pub fn no_limits() -> Self {
60        Default::default()
61    }
62}
63
64/// Rpc Interface for poll-based ethereum filter API, implementing only the `eth_getLogs` method.
65/// Used for the engine API, with possibility to specify [`QueryLimits`].
66pub trait EngineEthFilter<L: RpcObject>: Send + Sync + 'static {
67    /// Returns logs matching given filter object.
68    fn logs(
69        &self,
70        filter: Filter,
71        limits: QueryLimits,
72    ) -> impl Future<Output = RpcResult<Vec<L>>> + Send;
73}