Skip to content

Commit 8dfe72f

Browse files
committed
[database] Wrap BlockTime in another struct to allow adding more
fields in the future
1 parent c86d7f2 commit 8dfe72f

File tree

6 files changed

+103
-88
lines changed

6 files changed

+103
-88
lines changed

src/database/any.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ impl BatchOperations for AnyDatabase {
144144
fn set_last_index(&mut self, keychain: KeychainKind, value: u32) -> Result<(), Error> {
145145
impl_inner_method!(AnyDatabase, self, set_last_index, keychain, value)
146146
}
147-
fn set_last_sync_time(&mut self, last_sync_time: BlockTime) -> Result<(), Error> {
148-
impl_inner_method!(AnyDatabase, self, set_last_sync_time, last_sync_time)
147+
fn set_sync_time(&mut self, sync_time: SyncTime) -> Result<(), Error> {
148+
impl_inner_method!(AnyDatabase, self, set_sync_time, sync_time)
149149
}
150150

151151
fn del_script_pubkey_from_path(
@@ -183,8 +183,8 @@ impl BatchOperations for AnyDatabase {
183183
fn del_last_index(&mut self, keychain: KeychainKind) -> Result<Option<u32>, Error> {
184184
impl_inner_method!(AnyDatabase, self, del_last_index, keychain)
185185
}
186-
fn del_last_sync_time(&mut self) -> Result<Option<BlockTime>, Error> {
187-
impl_inner_method!(AnyDatabase, self, del_last_sync_time)
186+
fn del_sync_time(&mut self) -> Result<Option<SyncTime>, Error> {
187+
impl_inner_method!(AnyDatabase, self, del_sync_time)
188188
}
189189
}
190190

@@ -247,8 +247,8 @@ impl Database for AnyDatabase {
247247
fn get_last_index(&self, keychain: KeychainKind) -> Result<Option<u32>, Error> {
248248
impl_inner_method!(AnyDatabase, self, get_last_index, keychain)
249249
}
250-
fn get_last_sync_time(&self) -> Result<Option<BlockTime>, Error> {
251-
impl_inner_method!(AnyDatabase, self, get_last_sync_time)
250+
fn get_sync_time(&self) -> Result<Option<SyncTime>, Error> {
251+
impl_inner_method!(AnyDatabase, self, get_sync_time)
252252
}
253253

254254
fn increment_last_index(&mut self, keychain: KeychainKind) -> Result<u32, Error> {
@@ -281,8 +281,8 @@ impl BatchOperations for AnyBatch {
281281
fn set_last_index(&mut self, keychain: KeychainKind, value: u32) -> Result<(), Error> {
282282
impl_inner_method!(AnyBatch, self, set_last_index, keychain, value)
283283
}
284-
fn set_last_sync_time(&mut self, last_sync_time: BlockTime) -> Result<(), Error> {
285-
impl_inner_method!(AnyBatch, self, set_last_sync_time, last_sync_time)
284+
fn set_sync_time(&mut self, sync_time: SyncTime) -> Result<(), Error> {
285+
impl_inner_method!(AnyBatch, self, set_sync_time, sync_time)
286286
}
287287

288288
fn del_script_pubkey_from_path(
@@ -314,8 +314,8 @@ impl BatchOperations for AnyBatch {
314314
fn del_last_index(&mut self, keychain: KeychainKind) -> Result<Option<u32>, Error> {
315315
impl_inner_method!(AnyBatch, self, del_last_index, keychain)
316316
}
317-
fn del_last_sync_time(&mut self) -> Result<Option<BlockTime>, Error> {
318-
impl_inner_method!(AnyBatch, self, del_last_sync_time)
317+
fn del_sync_time(&mut self) -> Result<Option<SyncTime>, Error> {
318+
impl_inner_method!(AnyBatch, self, del_sync_time)
319319
}
320320
}
321321

src/database/keyvalue.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use bitcoin::hash_types::Txid;
1818
use bitcoin::{OutPoint, Script, Transaction};
1919

2020
use crate::database::memory::MapKey;
21-
use crate::database::{BatchDatabase, BatchOperations, Database};
21+
use crate::database::{BatchDatabase, BatchOperations, Database, SyncTime};
2222
use crate::error::Error;
2323
use crate::types::*;
2424

@@ -82,9 +82,9 @@ macro_rules! impl_batch_operations {
8282
Ok(())
8383
}
8484

85-
fn set_last_sync_time(&mut self, ct: BlockTime) -> Result<(), Error> {
86-
let key = MapKey::LastSyncTime.as_map_key();
87-
self.insert(key, serde_json::to_vec(&ct)?)$($after_insert)*;
85+
fn set_sync_time(&mut self, data: SyncTime) -> Result<(), Error> {
86+
let key = MapKey::SyncTime.as_map_key();
87+
self.insert(key, serde_json::to_vec(&data)?)$($after_insert)*;
8888

8989
Ok(())
9090
}
@@ -176,8 +176,8 @@ macro_rules! impl_batch_operations {
176176
}
177177
}
178178

179-
fn del_last_sync_time(&mut self) -> Result<Option<BlockTime>, Error> {
180-
let key = MapKey::LastSyncTime.as_map_key();
179+
fn del_sync_time(&mut self) -> Result<Option<SyncTime>, Error> {
180+
let key = MapKey::SyncTime.as_map_key();
181181
let res = self.remove(key);
182182
let res = $process_delete!(res);
183183

@@ -357,8 +357,8 @@ impl Database for Tree {
357357
.transpose()
358358
}
359359

360-
fn get_last_sync_time(&self) -> Result<Option<BlockTime>, Error> {
361-
let key = MapKey::LastSyncTime.as_map_key();
360+
fn get_sync_time(&self) -> Result<Option<SyncTime>, Error> {
361+
let key = MapKey::SyncTime.as_map_key();
362362
Ok(self
363363
.get(key)?
364364
.map(|b| serde_json::from_slice(&b))
@@ -495,7 +495,7 @@ mod test {
495495
}
496496

497497
#[test]
498-
fn test_last_sync_time() {
499-
crate::database::test::test_last_sync_time(get_tree());
498+
fn test_sync_time() {
499+
crate::database::test::test_sync_time(get_tree());
500500
}
501501
}

src/database/memory.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use bitcoin::consensus::encode::{deserialize, serialize};
2222
use bitcoin::hash_types::Txid;
2323
use bitcoin::{OutPoint, Script, Transaction};
2424

25-
use crate::database::{BatchDatabase, BatchOperations, ConfigurableDatabase, Database};
25+
use crate::database::{BatchDatabase, BatchOperations, ConfigurableDatabase, Database, SyncTime};
2626
use crate::error::Error;
2727
use crate::types::*;
2828

@@ -42,7 +42,7 @@ pub(crate) enum MapKey<'a> {
4242
RawTx(Option<&'a Txid>),
4343
Transaction(Option<&'a Txid>),
4444
LastIndex(KeychainKind),
45-
LastSyncTime,
45+
SyncTime,
4646
DescriptorChecksum(KeychainKind),
4747
}
4848

@@ -61,7 +61,7 @@ impl MapKey<'_> {
6161
MapKey::RawTx(_) => b"r".to_vec(),
6262
MapKey::Transaction(_) => b"t".to_vec(),
6363
MapKey::LastIndex(st) => [b"c", st.as_ref()].concat(),
64-
MapKey::LastSyncTime => b"l".to_vec(),
64+
MapKey::SyncTime => b"l".to_vec(),
6565
MapKey::DescriptorChecksum(st) => [b"d", st.as_ref()].concat(),
6666
}
6767
}
@@ -183,9 +183,9 @@ impl BatchOperations for MemoryDatabase {
183183

184184
Ok(())
185185
}
186-
fn set_last_sync_time(&mut self, ct: BlockTime) -> Result<(), Error> {
187-
let key = MapKey::LastSyncTime.as_map_key();
188-
self.map.insert(key, Box::new(ct));
186+
fn set_sync_time(&mut self, data: SyncTime) -> Result<(), Error> {
187+
let key = MapKey::SyncTime.as_map_key();
188+
self.map.insert(key, Box::new(data));
189189

190190
Ok(())
191191
}
@@ -279,8 +279,8 @@ impl BatchOperations for MemoryDatabase {
279279
Some(b) => Ok(Some(*b.downcast_ref().unwrap())),
280280
}
281281
}
282-
fn del_last_sync_time(&mut self) -> Result<Option<BlockTime>, Error> {
283-
let key = MapKey::LastSyncTime.as_map_key();
282+
fn del_sync_time(&mut self) -> Result<Option<SyncTime>, Error> {
283+
let key = MapKey::SyncTime.as_map_key();
284284
let res = self.map.remove(&key);
285285
self.deleted_keys.push(key);
286286

@@ -423,8 +423,8 @@ impl Database for MemoryDatabase {
423423
Ok(self.map.get(&key).map(|b| *b.downcast_ref().unwrap()))
424424
}
425425

426-
fn get_last_sync_time(&self) -> Result<Option<BlockTime>, Error> {
427-
let key = MapKey::LastSyncTime.as_map_key();
426+
fn get_sync_time(&self) -> Result<Option<SyncTime>, Error> {
427+
let key = MapKey::SyncTime.as_map_key();
428428
Ok(self
429429
.map
430430
.get(&key)
@@ -503,12 +503,10 @@ macro_rules! populate_test_db {
503503
};
504504

505505
let txid = tx.txid();
506-
let confirmation_time = tx_meta
507-
.min_confirmations
508-
.map(|conf| $crate::BlockTime {
509-
height: current_height.unwrap().checked_sub(conf as u32).unwrap(),
510-
timestamp: 0,
511-
});
506+
let confirmation_time = tx_meta.min_confirmations.map(|conf| $crate::BlockTime {
507+
height: current_height.unwrap().checked_sub(conf as u32).unwrap(),
508+
timestamp: 0,
509+
});
512510

513511
let tx_details = $crate::TransactionDetails {
514512
transaction: Some(tx.clone()),
@@ -616,7 +614,7 @@ mod test {
616614
}
617615

618616
#[test]
619-
fn test_last_sync_time() {
620-
crate::database::test::test_last_sync_time(get_tree());
617+
fn test_sync_time() {
618+
crate::database::test::test_sync_time(get_tree());
621619
}
622620
}

src/database/mod.rs

+29-16
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
//!
2525
//! [`Wallet`]: crate::wallet::Wallet
2626
27+
use serde::{Deserialize, Serialize};
28+
2729
use bitcoin::hash_types::Txid;
2830
use bitcoin::{OutPoint, Script, Transaction, TxOut};
2931

@@ -44,6 +46,15 @@ pub use sqlite::SqliteDatabase;
4446
pub mod memory;
4547
pub use memory::MemoryDatabase;
4648

49+
/// Blockchain state at the time of syncing
50+
///
51+
/// Contains only the block time and height at the moment
52+
#[derive(Clone, Debug, Serialize, Deserialize)]
53+
pub struct SyncTime {
54+
/// Block timestamp and height at the time of sync
55+
pub block_time: BlockTime,
56+
}
57+
4758
/// Trait for operations that can be batched
4859
///
4960
/// This trait defines the list of operations that must be implemented on the [`Database`] type and
@@ -64,8 +75,8 @@ pub trait BatchOperations {
6475
fn set_tx(&mut self, transaction: &TransactionDetails) -> Result<(), Error>;
6576
/// Store the last derivation index for a given keychain.
6677
fn set_last_index(&mut self, keychain: KeychainKind, value: u32) -> Result<(), Error>;
67-
/// Store the sync time in terms of block height and timestamp
68-
fn set_last_sync_time(&mut self, last_sync_time: BlockTime) -> Result<(), Error>;
78+
/// Store the sync time
79+
fn set_sync_time(&mut self, sync_time: SyncTime) -> Result<(), Error>;
6980

7081
/// Delete a script_pubkey given the keychain and its child number.
7182
fn del_script_pubkey_from_path(
@@ -91,10 +102,10 @@ pub trait BatchOperations {
91102
) -> Result<Option<TransactionDetails>, Error>;
92103
/// Delete the last derivation index for a keychain.
93104
fn del_last_index(&mut self, keychain: KeychainKind) -> Result<Option<u32>, Error>;
94-
/// Reset the last sync time to `None`
105+
/// Reset the sync time to `None`
95106
///
96107
/// Returns the removed value
97-
fn del_last_sync_time(&mut self) -> Result<Option<BlockTime>, Error>;
108+
fn del_sync_time(&mut self) -> Result<Option<SyncTime>, Error>;
98109
}
99110

100111
/// Trait for reading data from a database
@@ -140,8 +151,8 @@ pub trait Database: BatchOperations {
140151
fn get_tx(&self, txid: &Txid, include_raw: bool) -> Result<Option<TransactionDetails>, Error>;
141152
/// Return the last defivation index for a keychain.
142153
fn get_last_index(&self, keychain: KeychainKind) -> Result<Option<u32>, Error>;
143-
/// Return the last sync time, if present
144-
fn get_last_sync_time(&self) -> Result<Option<BlockTime>, Error>;
154+
/// Return the sync time, if present
155+
fn get_sync_time(&self) -> Result<Option<SyncTime>, Error>;
145156

146157
/// Increment the last derivation index for a keychain and return it
147158
///
@@ -385,22 +396,24 @@ pub mod test {
385396
);
386397
}
387398

388-
pub fn test_last_sync_time<D: Database>(mut tree: D) {
389-
assert!(tree.get_last_sync_time().unwrap().is_none());
399+
pub fn test_sync_time<D: Database>(mut tree: D) {
400+
assert!(tree.get_sync_time().unwrap().is_none());
390401

391-
tree.set_last_sync_time(BlockTime {
392-
height: 100,
393-
timestamp: 1000,
402+
tree.set_sync_time(SyncTime {
403+
block_time: BlockTime {
404+
height: 100,
405+
timestamp: 1000,
406+
},
394407
})
395408
.unwrap();
396409

397-
let extracted = tree.get_last_sync_time().unwrap();
410+
let extracted = tree.get_sync_time().unwrap();
398411
assert!(extracted.is_some());
399-
assert_eq!(extracted.as_ref().unwrap().height, 100);
400-
assert_eq!(extracted.as_ref().unwrap().timestamp, 1000);
412+
assert_eq!(extracted.as_ref().unwrap().block_time.height, 100);
413+
assert_eq!(extracted.as_ref().unwrap().block_time.timestamp, 1000);
401414

402-
tree.del_last_sync_time().unwrap();
403-
assert!(tree.get_last_sync_time().unwrap().is_none());
415+
tree.del_sync_time().unwrap();
416+
assert!(tree.get_sync_time().unwrap().is_none());
404417
}
405418

406419
// TODO: more tests...

0 commit comments

Comments
 (0)