[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

@@ -64,6 +64,8 @@ pub trait BatchOperations {
fn set_tx(&mut self, transaction: &TransactionDetails) -> Result<(), Error>;
/// Store the last derivation index for a given keychain.
fn set_last_index(&mut self, keychain: KeychainKind, value: u32) -> Result<(), Error>;
/// Store the sync time in terms of block height and timestamp
fn set_last_sync_time(&mut self, last_sync_time: ConfirmationTime) -> Result<(), Error>;
/// Delete a script_pubkey given the keychain and its child number.
fn del_script_pubkey_from_path(
@@ -89,6 +91,10 @@ pub trait BatchOperations {
) -> Result<Option<TransactionDetails>, Error>;
/// Delete the last derivation index for a keychain.
fn del_last_index(&mut self, keychain: KeychainKind) -> Result<Option<u32>, Error>;
/// Reset the last sync time to `None`
///
/// Returns the removed value
fn del_last_sync_time(&mut self) -> Result<Option<ConfirmationTime>, Error>;
}
/// Trait for reading data from a database
@@ -134,6 +140,8 @@ pub trait Database: BatchOperations {
fn get_tx(&self, txid: &Txid, include_raw: bool) -> Result<Option<TransactionDetails>, Error>;
/// Return the last defivation index for a keychain.
fn get_last_index(&self, keychain: KeychainKind) -> Result<Option<u32>, Error>;
/// Return the last sync time, if present
fn get_last_sync_time(&self) -> Result<Option<ConfirmationTime>, Error>;
/// Increment the last derivation index for a keychain and return it
///
@@ -377,5 +385,23 @@ pub mod test {
);
}
pub fn test_last_sync_time<D: Database>(mut tree: D) {
assert!(tree.get_last_sync_time().unwrap().is_none());
tree.set_last_sync_time(ConfirmationTime {
height: 100,
timestamp: 1000,
})
.unwrap();
let extracted = tree.get_last_sync_time().unwrap();
assert!(extracted.is_some());
assert_eq!(extracted.as_ref().unwrap().height, 100);
assert_eq!(extracted.as_ref().unwrap().timestamp, 1000);
tree.del_last_sync_time().unwrap();
assert!(tree.get_last_sync_time().unwrap().is_none());
}
// TODO: more tests...
}