]> Untitled Git - bdk/commitdiff
[wallet] Store the block height and timestamp after syncing
authorAlekos Filini <alekos.filini@gmail.com>
Sat, 23 Oct 2021 13:25:49 +0000 (15:25 +0200)
committerAlekos Filini <alekos.filini@gmail.com>
Wed, 10 Nov 2021 11:30:02 +0000 (12:30 +0100)
Closes #455

src/testutils/blockchain_tests.rs
src/wallet/mod.rs

index b44a9718236a3e20715691a855b7fd0f575fe886..7174bf58c133fb158e15cfd07affec1c0d5c127d 100644 (file)
@@ -394,6 +394,9 @@ macro_rules! bdk_blockchain_tests {
 
             #[test]
             fn test_sync_simple() {
+                use std::ops::Deref;
+                use crate::database::Database;
+
                 let (wallet, descriptors, mut test_client) = init_single_sig();
 
                 let tx = testutils! {
@@ -402,7 +405,13 @@ macro_rules! bdk_blockchain_tests {
                 println!("{:?}", tx);
                 let txid = test_client.receive(tx);
 
+                // the RPC blockchain needs to call `sync()` during initialization to import the
+                // addresses (see `init_single_sig()`), so we skip this assertion
+                #[cfg(not(feature = "test-rpc"))]
+                assert!(wallet.database().deref().get_sync_time().unwrap().is_none(), "initial sync_time not none");
+
                 wallet.sync(noop_progress(), None).unwrap();
+                assert!(wallet.database().deref().get_sync_time().unwrap().is_some(), "sync_time hasn't been updated");
 
                 assert_eq!(wallet.get_balance().unwrap(), 50_000, "incorrect balance");
                 assert_eq!(wallet.list_unspent().unwrap()[0].keychain, KeychainKind::External, "incorrect keychain kind");
index b811662b976557db3fd0740effca41d085f888f1..2244f927260ab0cf7eb054dd2839549ea8546a26 100644 (file)
@@ -1554,6 +1554,15 @@ where
             }
         }
 
+        let last_sync_time = ConfirmationTime {
+            height: maybe_await!(self.client.get_height())?,
+            timestamp: time::get_timestamp(),
+        };
+        debug!("Saving `last_sync_time` = {:?}", last_sync_time);
+        self.database
+            .borrow_mut()
+            .set_last_sync_time(last_sync_time)?;
+
         Ok(())
     }