hypersync_net_types/
lib.rs

1use std::collections::BTreeSet;
2
3use arrayvec::ArrayVec;
4use hypersync_format::{Address, FilterWrapper, FixedSizeData, Hash, LogArgument};
5use serde::{Deserialize, Serialize};
6
7pub type Sighash = FixedSizeData<4>;
8
9#[allow(clippy::all)]
10pub mod hypersync_net_types_capnp {
11    include!(concat!(env!("OUT_DIR"), "/hypersync_net_types_capnp.rs"));
12}
13
14#[derive(Default, Serialize, Deserialize, Clone, Debug, PartialEq)]
15pub struct BlockSelection {
16    /// Hash of a block, any blocks that have one of these hashes will be returned.
17    /// Empty means match all.
18    #[serde(default)]
19    pub hash: Vec<Hash>,
20    /// Miner address of a block, any blocks that have one of these miners will be returned.
21    /// Empty means match all.
22    #[serde(default)]
23    pub miner: Vec<Address>,
24}
25
26#[derive(Default, Serialize, Deserialize, Clone, Debug, PartialEq)]
27pub struct LogSelection {
28    /// Address of the contract, any logs that has any of these addresses will be returned.
29    /// Empty means match all.
30    #[serde(default)]
31    pub address: Vec<Address>,
32    #[serde(default)]
33    pub address_filter: Option<FilterWrapper>,
34    /// Topics to match, each member of the top level array is another array, if the nth topic matches any
35    ///  topic specified in nth element of topics, the log will be returned. Empty means match all.
36    #[serde(default)]
37    pub topics: ArrayVec<Vec<LogArgument>, 4>,
38}
39
40#[derive(Default, Serialize, Deserialize, Clone, Debug)]
41pub struct TransactionSelection {
42    /// Address the transaction should originate from. If transaction.from matches any of these, the transaction
43    /// will be returned. Keep in mind that this has an and relationship with to filter, so each transaction should
44    /// match both of them. Empty means match all.
45    #[serde(default)]
46    pub from: Vec<Address>,
47    #[serde(default)]
48    pub from_filter: Option<FilterWrapper>,
49    /// Address the transaction should go to. If transaction.to matches any of these, the transaction will
50    /// be returned. Keep in mind that this has an and relationship with from filter, so each transaction should
51    /// match both of them. Empty means match all.
52    #[serde(default)]
53    pub to: Vec<Address>,
54    #[serde(default)]
55    pub to_filter: Option<FilterWrapper>,
56    /// If first 4 bytes of transaction input matches any of these, transaction will be returned. Empty means match all.
57    #[serde(default)]
58    pub sighash: Vec<Sighash>,
59    /// If transaction.status matches this value, the transaction will be returned.
60    pub status: Option<u8>,
61    /// If transaction.type matches any of these values, the transaction will be returned
62    #[serde(rename = "type")]
63    #[serde(default)]
64    pub kind: Vec<u8>,
65    /// If transaction.contract_address matches any of these values, the transaction will be returned.
66    #[serde(default)]
67    pub contract_address: Vec<Address>,
68    /// Bloom filter to filter by transaction.contract_address field. If the bloom filter contains the hash
69    /// of transaction.contract_address then the transaction will be returned. This field doesn't utilize the server side filtering
70    /// so it should be used alongside some non-probabilistic filters if possible.
71    #[serde(default)]
72    pub contract_address_filter: Option<FilterWrapper>,
73    /// If transaction.hash matches any of these values the transaction will be returned.
74    /// empty means match all.
75    #[serde(default)]
76    pub hash: Vec<Hash>,
77
78    /// List of authorizations from eip-7702 transactions, the query will return transactions that match any of these selections
79    #[serde(default)]
80    pub authorization_list: Vec<AuthorizationSelection>,
81}
82
83#[derive(Default, Serialize, Deserialize, Clone, Debug, PartialEq)]
84pub struct AuthorizationSelection {
85    /// List of chain ids to match in the transaction authorizationList
86    #[serde(default)]
87    pub chain_id: Vec<u64>,
88    /// List of addresses to match in the transaction authorizationList
89    #[serde(default)]
90    pub address: Vec<Address>,
91}
92
93#[derive(Default, Serialize, Deserialize, Clone, Debug)]
94pub struct TraceSelection {
95    #[serde(default)]
96    pub from: Vec<Address>,
97    #[serde(default)]
98    pub from_filter: Option<FilterWrapper>,
99    #[serde(default)]
100    pub to: Vec<Address>,
101    #[serde(default)]
102    pub to_filter: Option<FilterWrapper>,
103    #[serde(default)]
104    pub address: Vec<Address>,
105    #[serde(default)]
106    pub address_filter: Option<FilterWrapper>,
107    #[serde(default)]
108    pub call_type: Vec<String>,
109    #[serde(default)]
110    pub reward_type: Vec<String>,
111    #[serde(default)]
112    #[serde(rename = "type")]
113    pub kind: Vec<String>,
114    #[serde(default)]
115    pub sighash: Vec<Sighash>,
116}
117
118#[derive(Default, Serialize, Deserialize, Clone, Debug)]
119pub struct Query {
120    /// The block to start the query from
121    pub from_block: u64,
122    /// The block to end the query at. If not specified, the query will go until the
123    ///  end of data. Exclusive, the returned range will be [from_block..to_block).
124    ///
125    /// The query will return before it reaches this target block if it hits the time limit
126    ///  configured on the server. The user should continue their query by putting the
127    ///  next_block field in the response into from_block field of their next query. This implements
128    ///  pagination.
129    pub to_block: Option<u64>,
130    /// List of log selections, these have an OR relationship between them, so the query will return logs
131    /// that match any of these selections.
132    #[serde(default)]
133    pub logs: Vec<LogSelection>,
134    /// List of transaction selections, the query will return transactions that match any of these selections
135    #[serde(default)]
136    pub transactions: Vec<TransactionSelection>,
137    /// List of trace selections, the query will return traces that match any of these selections
138    #[serde(default)]
139    pub traces: Vec<TraceSelection>,
140    /// List of block selections, the query will return blocks that match any of these selections
141    #[serde(default)]
142    pub blocks: Vec<BlockSelection>,
143    /// Weather to include all blocks regardless of if they are related to a returned transaction or log. Normally
144    ///  the server will return only the blocks that are related to the transaction or logs in the response. But if this
145    ///  is set to true, the server will return data for all blocks in the requested range [from_block, to_block).
146    #[serde(default)]
147    pub include_all_blocks: bool,
148    /// Field selection. The user can select which fields they are interested in, requesting less fields will improve
149    ///  query execution time and reduce the payload size so the user should always use a minimal number of fields.
150    #[serde(default)]
151    pub field_selection: FieldSelection,
152    /// Maximum number of blocks that should be returned, the server might return more blocks than this number but
153    ///  it won't overshoot by too much.
154    #[serde(default)]
155    pub max_num_blocks: Option<usize>,
156    /// Maximum number of transactions that should be returned, the server might return more transactions than this number but
157    ///  it won't overshoot by too much.
158    #[serde(default)]
159    pub max_num_transactions: Option<usize>,
160    /// Maximum number of logs that should be returned, the server might return more logs than this number but
161    ///  it won't overshoot by too much.
162    #[serde(default)]
163    pub max_num_logs: Option<usize>,
164    /// Maximum number of traces that should be returned, the server might return more traces than this number but
165    ///  it won't overshoot by too much.
166    #[serde(default)]
167    pub max_num_traces: Option<usize>,
168    /// Selects join mode for the query,
169    /// Default: join in this order logs -> transactions -> traces -> blocks
170    /// JoinAll: join everything to everything. For example if logSelection matches log0, we get the
171    /// associated transaction of log0 and then we get associated logs of that transaction as well. Applites similarly
172    /// to blocks, traces.
173    /// JoinNothing: join nothing.
174    #[serde(default)]
175    pub join_mode: JoinMode,
176}
177
178#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Copy)]
179pub enum JoinMode {
180    Default,
181    JoinAll,
182    JoinNothing,
183}
184
185impl Default for JoinMode {
186    fn default() -> Self {
187        Self::Default
188    }
189}
190
191#[derive(Default, Serialize, Deserialize, Clone, Debug)]
192pub struct FieldSelection {
193    #[serde(default)]
194    pub block: BTreeSet<String>,
195    #[serde(default)]
196    pub transaction: BTreeSet<String>,
197    #[serde(default)]
198    pub log: BTreeSet<String>,
199    #[serde(default)]
200    pub trace: BTreeSet<String>,
201}
202
203#[derive(Clone, Copy, Deserialize, Serialize, Debug)]
204pub struct ArchiveHeight {
205    pub height: Option<u64>,
206}
207
208#[derive(Clone, Copy, Deserialize, Serialize, Debug)]
209pub struct ChainId {
210    pub chain_id: u64,
211}
212
213/// Guard for detecting rollbacks
214#[derive(Debug, Clone, Serialize)]
215pub struct RollbackGuard {
216    /// Block number of last block scanned in memory
217    pub block_number: u64,
218    /// Block timestamp of last block scanned in memory
219    pub timestamp: i64,
220    /// Block hash of last block scanned in memory
221    pub hash: Hash,
222    /// Block number of first block scanned in memory
223    pub first_block_number: u64,
224    /// Parent hash of first block scanned in memory
225    pub first_parent_hash: Hash,
226}