From 7306b9d2a9ee04732b58a9b1f87f1c70db31f5fe Mon Sep 17 00:00:00 2001 From: Francisco Gindre Date: Wed, 11 Jan 2023 20:07:21 -0300 Subject: [PATCH] [#751] add support for rewind_to_height to FsBlockDb Closes #751 Ran `cargo fmt` return the result as an error rather than unwrap Co-authored-by: Kris Nuttycombe --- zcash_client_sqlite/CHANGELOG.md | 8 +++++++- zcash_client_sqlite/src/chain.rs | 10 ++++++++++ zcash_client_sqlite/src/lib.rs | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/zcash_client_sqlite/CHANGELOG.md b/zcash_client_sqlite/CHANGELOG.md index 19c171bb3..f3e9c6987 100644 --- a/zcash_client_sqlite/CHANGELOG.md +++ b/zcash_client_sqlite/CHANGELOG.md @@ -6,7 +6,13 @@ and this library adheres to Rust's notion of [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] - +### Added +- `zcash_client_sqlite::FsBlockDb::rewind_to_height` rewinds the BlockMeta Db + to the specified height following the same logic as homonymous functions on + `WalletDb`. This function does not delete the files referenced by the rows + that might be present and are deleted by this function call. +- `zcash_client_sqlite::chain::blockmetadb_rewind_to_height` implementation + of the function above. ## [0.4.2] - 2022-12-13 ### Fixed - `zcash_client_sqlite::WalletDb::get_transparent_balances` no longer returns an diff --git a/zcash_client_sqlite/src/chain.rs b/zcash_client_sqlite/src/chain.rs index 4c3f422c2..6a13f9405 100644 --- a/zcash_client_sqlite/src/chain.rs +++ b/zcash_client_sqlite/src/chain.rs @@ -139,6 +139,16 @@ pub(crate) fn blockmetadb_insert( } } +#[cfg(feature = "unstable")] +pub(crate) fn blockmetadb_rewind_to_height( + conn: &Connection, + block_height: BlockHeight, +) -> Result<(), rusqlite::Error> { + conn.prepare("DELETE FROM compactblocks_meta WHERE height > ?")? + .execute(params![u32::from(block_height)])?; + Ok(()) +} + #[cfg(feature = "unstable")] pub(crate) fn blockmetadb_get_max_cached_height( conn: &Connection, diff --git a/zcash_client_sqlite/src/lib.rs b/zcash_client_sqlite/src/lib.rs index bc25f4534..c7f9a1d74 100644 --- a/zcash_client_sqlite/src/lib.rs +++ b/zcash_client_sqlite/src/lib.rs @@ -929,6 +929,20 @@ impl FsBlockDb { Ok(chain::blockmetadb_insert(&self.conn, block_meta)?) } + /// Rewinds the BlockMeta Db to the `block_height` provided. + /// + /// This doesn't delete any files referenced by the records + /// stored in BlockMeta. + /// + /// If the requested height is greater than or equal to the height + /// of the last scanned block, or if the DB is empty, this function + /// does nothing. + pub fn rewind_to_height(&self, block_height: BlockHeight) -> Result<(), FsBlockDbError> { + Ok(chain::blockmetadb_rewind_to_height( + &self.conn, + block_height, + )?) + } } #[cfg(feature = "unstable")]