[db] Add the last_sync_time database entry

This will be used to store the height and timestamp after every sync.
This commit is contained in:
Alekos Filini
2021-10-23 14:51:42 +02:00
parent aa075f0b2f
commit 2892edf94b
5 changed files with 159 additions and 0 deletions

View File

@@ -82,6 +82,13 @@ macro_rules! impl_batch_operations {
Ok(())
}
fn set_last_sync_time(&mut self, ct: ConfirmationTime) -> Result<(), Error> {
let key = MapKey::LastSyncTime.as_map_key();
self.insert(key, serde_json::to_vec(&ct)?)$($after_insert)*;
Ok(())
}
fn del_script_pubkey_from_path(&mut self, keychain: KeychainKind, path: u32) -> Result<Option<Script>, Error> {
let key = MapKey::Path((Some(keychain), Some(path))).as_map_key();
let res = self.remove(key);
@@ -168,6 +175,14 @@ macro_rules! impl_batch_operations {
}
}
}
fn del_last_sync_time(&mut self) -> Result<Option<ConfirmationTime>, Error> {
let key = MapKey::LastSyncTime.as_map_key();
let res = self.remove(key);
let res = $process_delete!(res);
Ok(res.map(|b| serde_json::from_slice(&b)).transpose()?)
}
}
}
@@ -342,6 +357,14 @@ impl Database for Tree {
.transpose()
}
fn get_last_sync_time(&self) -> Result<Option<ConfirmationTime>, Error> {
let key = MapKey::LastSyncTime.as_map_key();
Ok(self
.get(key)?
.map(|b| serde_json::from_slice(&b))
.transpose()?)
}
// inserts 0 if not present
fn increment_last_index(&mut self, keychain: KeychainKind) -> Result<u32, Error> {
let key = MapKey::LastIndex(keychain).as_map_key();
@@ -470,4 +493,9 @@ mod test {
fn test_last_index() {
crate::database::test::test_last_index(get_tree());
}
#[test]
fn test_last_sync_time() {
crate::database::test::test_last_sync_time(get_tree());
}
}