diff --git a/zcash_client_backend/Cargo.toml b/zcash_client_backend/Cargo.toml index 355ee3767..de89c4900 100644 --- a/zcash_client_backend/Cargo.toml +++ b/zcash_client_backend/Cargo.toml @@ -43,7 +43,6 @@ protobuf-codegen-pure = "2.20" gumdrop = "0.8" rand_xorshift = "0.3" tempfile = "3.1.0" -zcash_client_sqlite = { version = "0.3", path = "../zcash_client_sqlite", features = ["transparent-inputs"] } zcash_proofs = { version = "0.5", path = "../zcash_proofs" } [features] diff --git a/zcash_client_backend/src/data_api/chain.rs b/zcash_client_backend/src/data_api/chain.rs index 38a47007f..b70b08705 100644 --- a/zcash_client_backend/src/data_api/chain.rs +++ b/zcash_client_backend/src/data_api/chain.rs @@ -4,7 +4,8 @@ //! # Examples //! //! ``` -//! use tempfile::NamedTempFile; +//! # #[cfg(feature = "test-dependencies")] +//! # { //! use zcash_primitives::{ //! consensus::{BlockHeight, Network, Parameters} //! }; @@ -17,32 +18,18 @@ //! scan_cached_blocks, //! }, //! error::Error, +//! testing, //! }, //! }; //! -//! use zcash_client_sqlite::{ -//! BlockDb, -//! WalletDb, -//! error::SqliteClientError, -//! wallet::{rewind_to_height}, -//! wallet::init::{init_wallet_db}, -//! }; -//! -//! # // doctests have a problem with sqlite IO, so we ignore errors -//! # // generated in this example code as it's not really testing anything //! # fn main() { //! # test(); //! # } //! # -//! # fn test() -> Result<(), SqliteClientError> { +//! # fn test() -> Result<(), Error> { //! let network = Network::TestNetwork; -//! let cache_file = NamedTempFile::new()?; -//! let db_cache = BlockDb::for_path(cache_file)?; -//! let db_file = NamedTempFile::new()?; -//! let db_read = WalletDb::for_path(db_file, network)?; -//! init_wallet_db(&db_read)?; -//! -//! let mut db_data = db_read.get_update_ops()?; +//! let db_cache = testing::MockBlockSource {}; +//! let mut db_data = testing::MockWalletDb {}; //! //! // 1) Download new CompactBlocks into db_cache. //! @@ -52,7 +39,7 @@ //! // errors are in the blocks we have previously cached or scanned. //! if let Err(e) = validate_chain(&network, &db_cache, db_data.get_max_height_hash()?) { //! match e { -//! SqliteClientError::BackendError(Error::InvalidChain(lower_bound, _)) => { +//! Error::InvalidChain(lower_bound, _) => { //! // a) Pick a height to rewind to. //! // //! // This might be informed by some external chain reorg information, or @@ -72,10 +59,10 @@ //! // d) If there is some separate thread or service downloading //! // CompactBlocks, tell it to go back and download from rewind_height //! // onwards. -//! } +//! }, //! e => { -//! // Handle or return other errors. -//! return Err(e); +//! // handle or return other errors +//! //! } //! } //! } @@ -87,6 +74,7 @@ //! // next time this codepath is executed after new blocks are received). //! scan_cached_blocks(&network, &db_cache, &mut db_data, None) //! # } +//! # } //! ``` use std::fmt::Debug; @@ -198,44 +186,6 @@ where /// /// Scanned blocks are required to be height-sequential. If a block is missing from the /// cache, an error will be returned with kind [`ChainInvalid::BlockHeightDiscontinuity`]. -/// -/// # Examples -/// -/// ``` -/// use tempfile::NamedTempFile; -/// use zcash_primitives::consensus::{ -/// Network, -/// Parameters, -/// }; -/// use zcash_client_backend::{ -/// data_api::chain::scan_cached_blocks, -/// }; -/// use zcash_client_sqlite::{ -/// BlockDb, -/// WalletDb, -/// error::SqliteClientError, -/// wallet::init::init_wallet_db, -/// }; -/// -/// # // doctests have a problem with sqlite IO, so we ignore errors -/// # // generated in this example code as it's not really testing anything -/// # fn main() { -/// # test(); -/// # } -/// # -/// # fn test() -> Result<(), SqliteClientError> { -/// let cache_file = NamedTempFile::new().unwrap(); -/// let cache = BlockDb::for_path(cache_file).unwrap(); -/// -/// let data_file = NamedTempFile::new().unwrap(); -/// let db_read = WalletDb::for_path(data_file, Network::TestNetwork)?; -/// init_wallet_db(&db_read)?; -/// -/// let mut data = db_read.get_update_ops()?; -/// scan_cached_blocks(&Network::TestNetwork, &cache, &mut data, None)?; -/// # Ok(()) -/// # } -/// ``` pub fn scan_cached_blocks( params: &P, cache: &C, diff --git a/zcash_client_backend/src/data_api/wallet.rs b/zcash_client_backend/src/data_api/wallet.rs index 5940c2baa..d2b0a42ea 100644 --- a/zcash_client_backend/src/data_api/wallet.rs +++ b/zcash_client_backend/src/data_api/wallet.rs @@ -95,31 +95,27 @@ where /// # Examples /// /// ``` +/// # #[cfg(feature = "test-dependencies")] +/// # { /// use tempfile::NamedTempFile; /// use zcash_primitives::{ /// consensus::{self, Network}, /// constants::testnet::COIN_TYPE, -/// transaction::components::Amount +/// transaction::{TxId, components::Amount}, /// }; /// use zcash_proofs::prover::LocalTxProver; /// use zcash_client_backend::{ /// keys::sapling, -/// data_api::wallet::create_spend_to_address, +/// data_api::{wallet::create_spend_to_address, error::Error, testing}, /// wallet::{AccountId, OvkPolicy}, /// }; -/// use zcash_client_sqlite::{ -/// WalletDb, -/// error::SqliteClientError, -/// wallet::init::init_wallet_db, -/// }; /// -/// # // doctests have a problem with sqlite IO, so we ignore errors -/// # // generated in this example code as it's not really testing anything /// # fn main() { /// # test(); /// # } /// # -/// # fn test() -> Result<(), SqliteClientError> { +/// # fn test() -> Result> { +/// /// let tx_prover = match LocalTxProver::with_default_location() { /// Some(tx_prover) => tx_prover, /// None => { @@ -131,13 +127,10 @@ where /// let extsk = sapling::spending_key(&[0; 32][..], COIN_TYPE, account); /// let to = extsk.default_address().1.into(); /// -/// let data_file = NamedTempFile::new().unwrap(); -/// let db_read = WalletDb::for_path(data_file, Network::TestNetwork).unwrap(); -/// init_wallet_db(&db_read)?; -/// let mut db = db_read.get_update_ops()?; +/// let mut db_read = testing::MockWalletDb {}; /// /// create_spend_to_address( -/// &mut db, +/// &mut db_read, /// &Network::TestNetwork, /// tx_prover, /// account, @@ -147,9 +140,9 @@ where /// None, /// OvkPolicy::Sender, /// 10 -/// )?; +/// ) /// -/// # Ok(()) +/// # } /// # } /// ``` #[allow(clippy::too_many_arguments)] diff --git a/zcash_client_backend/src/keys.rs b/zcash_client_backend/src/keys.rs index 2101237fa..a9696d7d4 100644 --- a/zcash_client_backend/src/keys.rs +++ b/zcash_client_backend/src/keys.rs @@ -212,6 +212,7 @@ pub mod transparent { #[derive(Clone, Debug)] pub struct UnifiedFullViewingKey { account: AccountId, + #[cfg(feature = "transparent-inputs")] transparent: Option, sapling: Option, } @@ -220,7 +221,7 @@ impl UnifiedFullViewingKey { /// Construct a new unified full viewing key, if the required components are present. pub fn new( account: AccountId, - transparent: Option, + #[cfg(feature = "transparent-inputs")] transparent: Option, sapling: Option, ) -> Option { if sapling.is_none() { @@ -228,6 +229,7 @@ impl UnifiedFullViewingKey { } else { Some(UnifiedFullViewingKey { account, + #[cfg(feature = "transparent-inputs")] transparent, sapling, }) @@ -240,6 +242,7 @@ impl UnifiedFullViewingKey { self.account } + #[cfg(feature = "transparent-inputs")] /// Returns the transparent component of the unified key at the /// BIP44 path `m/44'/'/'`. pub fn transparent(&self) -> Option<&transparent::AccountPubKey> { diff --git a/zcash_client_sqlite/src/wallet/init.rs b/zcash_client_sqlite/src/wallet/init.rs index aababd4cd..25e7e4d2e 100644 --- a/zcash_client_sqlite/src/wallet/init.rs +++ b/zcash_client_sqlite/src/wallet/init.rs @@ -8,12 +8,14 @@ use zcash_primitives::{ }; use zcash_client_backend::{ - encoding::{encode_extended_full_viewing_key, AddressCodec}, - keys::UnifiedFullViewingKey, + encoding::encode_extended_full_viewing_key, keys::UnifiedFullViewingKey, }; use crate::{address_from_extfvk, error::SqliteClientError, WalletDb}; +#[cfg(feature = "transparent-inputs")] +use zcash_client_backend::encoding::AddressCodec; + /// Sets up the internal structure of the data database. /// /// The database structure is the same irrespective of whether or @@ -201,11 +203,14 @@ pub fn init_accounts_table( let address_str: Option = key .sapling() .map(|extfvk| address_from_extfvk(&wdb.params, extfvk)); + #[cfg(feature = "transparent-inputs")] let taddress_str: Option = key.transparent().and_then(|k| { k.to_external_pubkey(0) .ok() .map(|k| k.to_address().encode(&wdb.params)) }); + #[cfg(not(feature = "transparent-inputs"))] + let taddress_str: Option = None; wdb.conn.execute( "INSERT INTO accounts (account, extfvk, address, transparent_address)