[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

@@ -33,6 +33,7 @@ use crate::types::*;
// transactions t<txid> -> tx details
// deriv indexes c{i,e} -> u32
// descriptor checksum d{i,e} -> vec<u8>
// last sync time l -> { height, timestamp }
pub(crate) enum MapKey<'a> {
Path((Option<KeychainKind>, Option<u32>)),
@@ -41,6 +42,7 @@ pub(crate) enum MapKey<'a> {
RawTx(Option<&'a Txid>),
Transaction(Option<&'a Txid>),
LastIndex(KeychainKind),
LastSyncTime,
DescriptorChecksum(KeychainKind),
}
@@ -59,6 +61,7 @@ impl MapKey<'_> {
MapKey::RawTx(_) => b"r".to_vec(),
MapKey::Transaction(_) => b"t".to_vec(),
MapKey::LastIndex(st) => [b"c", st.as_ref()].concat(),
MapKey::LastSyncTime => b"l".to_vec(),
MapKey::DescriptorChecksum(st) => [b"d", st.as_ref()].concat(),
}
}
@@ -180,6 +183,12 @@ impl BatchOperations for MemoryDatabase {
Ok(())
}
fn set_last_sync_time(&mut self, ct: ConfirmationTime) -> Result<(), Error> {
let key = MapKey::LastSyncTime.as_map_key();
self.map.insert(key, Box::new(ct));
Ok(())
}
fn del_script_pubkey_from_path(
&mut self,
@@ -270,6 +279,13 @@ impl BatchOperations for MemoryDatabase {
Some(b) => Ok(Some(*b.downcast_ref().unwrap())),
}
}
fn del_last_sync_time(&mut self) -> Result<Option<ConfirmationTime>, Error> {
let key = MapKey::LastSyncTime.as_map_key();
let res = self.map.remove(&key);
self.deleted_keys.push(key);
Ok(res.map(|b| b.downcast_ref().cloned().unwrap()))
}
}
impl Database for MemoryDatabase {
@@ -407,6 +423,14 @@ impl Database for MemoryDatabase {
Ok(self.map.get(&key).map(|b| *b.downcast_ref().unwrap()))
}
fn get_last_sync_time(&self) -> Result<Option<ConfirmationTime>, Error> {
let key = MapKey::LastSyncTime.as_map_key();
Ok(self
.map
.get(&key)
.map(|b| b.downcast_ref().cloned().unwrap()))
}
// inserts 0 if not present
fn increment_last_index(&mut self, keychain: KeychainKind) -> Result<u32, Error> {
let key = MapKey::LastIndex(keychain).as_map_key();
@@ -590,4 +614,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());
}
}