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