zcash_client_sqlite: Add `FsBlockDb::find_block`

This commit is contained in:
Jack Grigg 2023-01-24 23:30:04 +00:00
parent 5d62b68d70
commit e37f458a70
3 changed files with 34 additions and 4 deletions

View File

@ -6,19 +6,16 @@ and this library adheres to Rust's notion of
[Semantic Versioning](https://semver.org/spec/v2.0.0.html). [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased] ## [Unreleased]
### Added ### Added
- `zcash_client_sqlite::FsBlockDb::rewind_to_height` rewinds the BlockMeta Db - `zcash_client_sqlite::FsBlockDb::rewind_to_height` rewinds the BlockMeta Db
to the specified height following the same logic as homonymous functions on to the specified height following the same logic as homonymous functions on
`WalletDb`. This function does not delete the files referenced by the rows `WalletDb`. This function does not delete the files referenced by the rows
that might be present and are deleted by this function call. that might be present and are deleted by this function call.
- `zcash_client_sqlite::chain::blockmetadb_rewind_to_height` implementation - `zcash_client_sqlite::FsBlockDb::find_block`
of the function above.
### Changed ### Changed
- MSRV is now 1.60.0. - MSRV is now 1.60.0.
## [0.4.2] - 2022-12-13 ## [0.4.2] - 2022-12-13
### Fixed ### Fixed
- `zcash_client_sqlite::WalletDb::get_transparent_balances` no longer returns an - `zcash_client_sqlite::WalletDb::get_transparent_balances` no longer returns an

View File

@ -161,6 +161,32 @@ pub(crate) fn blockmetadb_get_max_cached_height(
}) })
} }
/// Returns the metadata for the block with the given height, if it exists in the database.
#[cfg(feature = "unstable")]
pub(crate) fn blockmetadb_find_block(
conn: &Connection,
height: BlockHeight,
) -> Result<Option<BlockMeta>, rusqlite::Error> {
use rusqlite::OptionalExtension;
conn.query_row(
"SELECT blockhash, time, sapling_outputs_count, orchard_actions_count
FROM compactblocks_meta
WHERE height = ?",
[u32::from(height)],
|row| {
Ok(BlockMeta {
height,
block_hash: BlockHash::from_slice(&row.get::<_, Vec<_>>(0)?),
block_time: row.get(1)?,
sapling_outputs_count: row.get(2)?,
orchard_actions_count: row.get(3)?,
})
},
)
.optional()
}
/// Implements a traversal of `limit` blocks of the filesystem-backed /// Implements a traversal of `limit` blocks of the filesystem-backed
/// block cache. /// block cache.
/// ///

View File

@ -929,6 +929,13 @@ impl FsBlockDb {
Ok(chain::blockmetadb_insert(&self.conn, block_meta)?) Ok(chain::blockmetadb_insert(&self.conn, block_meta)?)
} }
/// Returns the metadata for the block with the given height, if it exists in the
/// database.
pub fn find_block(&self, height: BlockHeight) -> Result<Option<BlockMeta>, FsBlockDbError> {
Ok(chain::blockmetadb_find_block(&self.conn, height)?)
}
/// Rewinds the BlockMeta Db to the `block_height` provided. /// Rewinds the BlockMeta Db to the `block_height` provided.
/// ///
/// This doesn't delete any files referenced by the records /// This doesn't delete any files referenced by the records