Skip to content

Commit

Permalink
implement BlockCacheTrait for MockBlocksource and fix failing doc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar-Pepper committed Sep 11, 2024
1 parent beb6833 commit e0d5339
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
48 changes: 40 additions & 8 deletions zcash_client_backend/src/data_api/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
//! # }
//! #
//! # fn test() -> Result<(), Error<(), Infallible>> {
//! # let rt = tokio::runtime::Runtime::new().unwrap();
//! # rt.block_on(async {
//! let network = Network::TestNetwork;
//! let block_source = chain_testing::MockBlockSource;
//! let mut wallet_db = testing::MockWalletDb::new(Network::TestNetwork);
Expand Down Expand Up @@ -70,10 +72,9 @@
//! &network,
//! &block_source,
//! &mut wallet_db,
//! scan_range.block_range().start,
//! chain_state,
//! scan_range.len()
//! );
//! scan_range
//! ).await;
//!
//! // Check for scanning errors that indicate that the wallet's chain tip is out of
//! // sync with blockchain history.
Expand Down Expand Up @@ -139,14 +140,14 @@
//! &network,
//! &block_source,
//! &mut wallet_db,
//! scan_range.block_range().start,
//! chain_state,
//! scan_range.len()
//! )?;
//! &scan_range
//! ).await?;
//!
//! // Handle scan errors, etc.
//! }
//! # Ok(())
//! # })
//! # }
//! # }
//! ```
Expand Down Expand Up @@ -705,9 +706,9 @@ pub mod testing {
use std::convert::Infallible;
use zcash_primitives::consensus::BlockHeight;

use crate::proto::compact_formats::CompactBlock;
use crate::{data_api::scanning::ScanRange, proto::compact_formats::CompactBlock};

use super::{error::Error, BlockSource};
use super::{error::Error, BlockCache, BlockSource};

pub struct MockBlockSource;

Expand All @@ -727,4 +728,35 @@ pub mod testing {
Ok(())
}
}

#[async_trait::async_trait]
impl BlockCache for MockBlockSource {
async fn get_tip_height<WalletErrT>(
&self,
_range: Option<&ScanRange>,
) -> Result<Option<BlockHeight>, Error<WalletErrT, Self::Error>> {
Ok(None)
}

async fn read<WalletErrT>(
&self,
_range: &ScanRange,
) -> Result<Vec<CompactBlock>, Error<WalletErrT, Self::Error>> {
Ok(Vec::new())
}

async fn insert<WalletErrT>(
&self,
_compact_blocks: Vec<CompactBlock>,
) -> Result<(), Error<WalletErrT, Self::Error>> {
Ok(())
}

async fn delete<WalletErrT>(
&self,
_range: ScanRange,
) -> Result<(), Error<WalletErrT, Self::Error>> {
Ok(())
}
}
}
5 changes: 4 additions & 1 deletion zcash_client_sqlite/src/chain/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use {
/// # Examples
///
/// ```
/// # let rt = tokio::runtime::Runtime::new().unwrap();
/// # rt.block_on(async {
/// use tempfile::NamedTempFile;
/// use zcash_client_sqlite::{
/// BlockDb,
Expand All @@ -22,7 +24,8 @@ use {
///
/// let cache_file = NamedTempFile::new().unwrap();
/// let db = BlockDb::for_path(cache_file.path()).unwrap();
/// init_cache_database(&db).unwrap();
/// init_cache_database(&db).await.unwrap();
/// # });
/// ```
pub async fn init_cache_database(db_cache: &BlockDb) -> Result<(), rusqlite::Error> {
db_cache.0.lock().await.execute(
Expand Down

0 comments on commit e0d5339

Please sign in to comment.