Skip to content

Commit

Permalink
Getting rid of Responder::{trackers,tx_tracker_map}
Browse files Browse the repository at this point in the history
This commit introduced the minimal changes to get rid of trackers
and tx_tracker_map hashmaps.

`trackersq usage was replaced with DB calls when needed.
while `tx_tracker_map` wasn't actually needed after all.

The memory usage didn't change much (any) on our testing 2M appointments DB since there weren't any trackers/breaches there.
  • Loading branch information
mariocynicys committed May 22, 2023
1 parent a7a3a69 commit 8b357e0
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 297 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 39 additions & 5 deletions teos/src/dbm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ impl DBM {
stmt.query_row([], |row| row.get(0)).unwrap()
}

/// Get the number of stored trackers.
pub(crate) fn get_trackers_count(&self) -> usize {
let mut stmt = self
.connection
.prepare("SELECT COUNT(*) FROM trackers")
.unwrap();
stmt.query_row([], |row| row.get(0)).unwrap()
}

/// Stores an [Appointment] into the database.
pub(crate) fn store_appointment(
&self,
Expand Down Expand Up @@ -276,8 +285,8 @@ impl DBM {
Ok(_) => {
log::debug!("Appointment successfully updated: {uuid}");
}
Err(_) => {
log::error!("Appointment not found, data cannot be updated: {uuid}");
Err(e) => {
log::error!("Appointment not found, data cannot be updated: {uuid}. Error: {e:?}");
}
}
}
Expand Down Expand Up @@ -471,7 +480,7 @@ impl DBM {
.collect()
}

pub(crate) fn batch_check_locator_exists(&self, locators: Vec<Locator>) -> Vec<Locator> {
pub(crate) fn batch_check_locators_exist(&self, locators: Vec<Locator>) -> Vec<Locator> {
let mut registered_locators = Vec::new();
let locators: Vec<Vec<u8>> = locators.iter().map(|l| l.to_vec()).collect();
let limit = self.connection.limit(Limit::SQLITE_LIMIT_VARIABLE_NUMBER) as usize;
Expand Down Expand Up @@ -529,6 +538,31 @@ impl DBM {
}
}

/// Updates the tracker information in the database.
///
/// The only updatable fields are `height` and `confirmed`. Such an update will be issued when:
/// 1- A tracker was confirmed in a block.
/// 2- A tracker was reorged out of the main chain and is back in the mempool.
pub(crate) fn update_tracker_status(
&self,
uuid: UUID,
status: &ConfirmationStatus,
) -> Result<(), Error> {
let (height, confirmed) = status.to_db_data().ok_or(Error::MissingField)?;

let query = "UPDATE trackers SET height=(?1), confirmed=(?2) WHERE UUID=(?3)";
match self.update_data(query, params![height, confirmed, uuid.to_vec(),]) {
Ok(x) => {
log::debug!("Tracker successfully updated: {uuid}");
Ok(x)
}
Err(e) => {
log::error!("Couldn't update tracker: {uuid}. Error: {e:?}");
Err(e)
}
}
}

/// Loads a [TransactionTracker] from the database.
pub(crate) fn load_tracker(&self, uuid: UUID) -> Result<TransactionTracker, Error> {
let key = uuid.to_vec();
Expand Down Expand Up @@ -931,7 +965,7 @@ mod tests {
}

#[test]
fn test_batch_check_locator_exists() {
fn test_batch_check_locators_exist() {
let dbm = DBM::in_memory().unwrap();
// Generate `count` appointments which we will store in the DB.
let count = 100;
Expand Down Expand Up @@ -972,7 +1006,7 @@ mod tests {
.cloned()
.collect();

assert_eq!(known_locators, dbm.batch_check_locator_exists(all_locators));
assert_eq!(known_locators, dbm.batch_check_locators_exist(all_locators));
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions teos/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ async fn main() {
let shutdown_signal_tor = shutdown_signal_rpc_api.clone();

// The ordering here actually matters. Listeners are called by order, and we want the gatekeeper to be called
// last, so both the Watcher and the Responder can query the necessary data from it during data deletion.
let listener = &(watcher.clone(), &(responder, gatekeeper));
// first so it updates the users' states and both the Watcher and the Responder operate only on registered users.
let listener = &(gatekeeper, &(watcher.clone(), responder));
let cache = &mut UnboundedCache::new();
let spv_client = SpvClient::new(tip, poller, cache, listener);
let mut chain_monitor = ChainMonitor::new(
Expand Down
Loading

0 comments on commit 8b357e0

Please sign in to comment.