hopr_db_api/
logs.rs

1use async_trait::async_trait;
2use hopr_crypto_types::prelude::Hash;
3use hopr_primitive_types::prelude::{Address, SerializableLog};
4
5use crate::errors::Result;
6
7#[async_trait]
8pub trait HoprDbLogOperations {
9    /// Ensures that logs in this database have been created by scanning the given contract address
10    /// and their corresponding topics. If the log DB is empty, the given addresses and topics
11    /// are used to prime the table.
12    ///
13    /// # Arguments
14    /// * `contract_address_topics` - list of topics for a contract address. There may be multiple topics
15    /// with the same contract address.
16    ///
17    /// # Returns
18    /// A `Result` which is `Ok(())` if the database contains correct log data,
19    /// or it has been primed successfully. An `Err` is returned otherwise.
20    async fn ensure_logs_origin(&self, contract_address_topics: Vec<(Address, Hash)>) -> Result<()>;
21
22    /// Stores a single log entry in the database.
23    ///
24    /// # Arguments
25    ///
26    /// * `log` - The log entry to store, of type `SerializableLog`.
27    ///
28    /// # Returns
29    ///
30    /// A `Result` which is `Ok(())` if the operation succeeds or an error if it fails.
31    async fn store_log<'a>(&'a self, log: SerializableLog) -> Result<()>;
32
33    /// Stores multiple log entries in the database.
34    ///
35    /// # Arguments
36    ///
37    /// * `logs` - A vector of log entries to store, each of type `SerializableLog`.
38    ///
39    /// # Returns
40    ///
41    /// A `Result` containing a vector of `Result<()>`, each representing the result of storing an individual log entry.
42    async fn store_logs(&self, logs: Vec<SerializableLog>) -> Result<Vec<Result<()>>>;
43
44    /// Retrieves a specific log entry from the database.
45    ///
46    /// # Arguments
47    ///
48    /// * `block_number` - The block number of the log entry.
49    /// * `tx_index` - The transaction index of the log entry.
50    /// * `log_index` - The log index of the log entry.
51    ///
52    /// # Returns
53    ///
54    /// A `Result` containing the `SerializableLog` if the operation succeeds or an error if it fails.
55    async fn get_log(&self, block_number: u64, tx_index: u64, log_index: u64) -> Result<SerializableLog>;
56
57    /// Retrieves multiple log entries from the database.
58    ///
59    /// # Arguments
60    ///
61    /// * `block_number` - An optional block number filter.
62    /// * `block_offset` - An optional block offset filter.
63    ///
64    /// # Returns
65    ///
66    /// A `Result` containing a `Vec` of `SerializableLog` entries if the operation succeeds or an error if it fails.
67    async fn get_logs<'a>(
68        &'a self,
69        block_number: Option<u64>,
70        block_offset: Option<u64>,
71    ) -> Result<Vec<SerializableLog>>;
72
73    /// Retrieves the count of log entries from the database.
74    ///
75    /// # Arguments
76    ///
77    /// * `block_number` - An optional block number filter.
78    /// * `block_offset` - An optional block offset filter.
79    ///
80    /// # Returns
81    ///
82    /// A `Result` containing the count of log entries if the operation succeeds or an error if it fails.
83    async fn get_logs_count(&self, block_number: Option<u64>, block_offset: Option<u64>) -> Result<u64>;
84
85    /// Retrieves block numbers of log entries from the database.
86    ///
87    /// # Arguments
88    ///
89    /// * `block_number` - An optional block number filter.
90    /// * `block_offset` - An optional block offset filter.
91    /// * `processed` - An optional processed filter.
92    ///
93    /// # Returns
94    ///
95    /// A `Result` containing a `Vec` of block numbers if the operation succeeds or an error if it fails.
96    async fn get_logs_block_numbers<'a>(
97        &'a self,
98        block_number: Option<u64>,
99        block_offset: Option<u64>,
100        processed: Option<bool>,
101    ) -> Result<Vec<u64>>;
102
103    /// Marks a specific log entry as processed.
104    ///
105    /// # Arguments
106    ///
107    /// * `log` - The log entry to mark as processed, of type `SerializableLog`.
108    ///
109    /// # Returns
110    ///
111    /// A `Result` which is `Ok(())` if the operation succeeds or an error if it fails.
112    async fn set_log_processed(&self, log: SerializableLog) -> Result<()>;
113
114    /// Marks multiple log entries as processed.
115    ///
116    /// # Arguments
117    ///
118    /// * `block_number` - An optional block number filter.
119    /// * `block_offset` - An optional block offset filter.
120    ///
121    /// # Returns
122    ///
123    /// A `Result` which is `Ok(())` if the operation succeeds or an error if it fails.
124    async fn set_logs_processed(&self, block_number: Option<u64>, block_offset: Option<u64>) -> Result<()>;
125
126    /// Marks multiple log entries as unprocessed.
127    ///
128    /// # Arguments
129    ///
130    /// * `block_number` - An optional block number filter.
131    /// * `block_offset` - An optional block offset filter.
132    ///
133    /// # Returns
134    ///
135    /// A `Result` which is `Ok(())` if the operation succeeds or an error if it fails.
136    async fn set_logs_unprocessed(&self, block_number: Option<u64>, block_offset: Option<u64>) -> Result<()>;
137
138    /// Retrieves the last checksummed log entry from the database.
139    ///
140    /// # Returns
141    ///
142    /// A `Result` containing an `Option<SerializableLog>` if the operation succeeds or an error if it fails.
143    async fn get_last_checksummed_log(&self) -> Result<Option<SerializableLog>>;
144
145    /// Updates checksums for log entries in the database.
146    ///
147    /// # Returns
148    ///
149    /// A `Result` which is `Ok(Hash)` if the operation succeeds or an error if it fails.
150    async fn update_logs_checksums(&self) -> Result<Hash>;
151}