From 98db781931a228e2dabd8c20c0cec3d0f23d49ec Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 8 Apr 2019 12:24:49 +0100 Subject: [PATCH] Add mainnet support to zcash_client_sqlite via a feature flag --- zcash_client_sqlite/Cargo.toml | 3 +++ zcash_client_sqlite/README.md | 3 +-- zcash_client_sqlite/src/init.rs | 12 ++++-------- zcash_client_sqlite/src/lib.rs | 24 +++++++++++++++++++++--- zcash_client_sqlite/src/scan.rs | 3 +-- zcash_client_sqlite/src/transact.rs | 8 +++----- 6 files changed, 33 insertions(+), 20 deletions(-) diff --git a/zcash_client_sqlite/Cargo.toml b/zcash_client_sqlite/Cargo.toml index 167068f..697cb12 100644 --- a/zcash_client_sqlite/Cargo.toml +++ b/zcash_client_sqlite/Cargo.toml @@ -21,3 +21,6 @@ rand_core = "0.5" rand_os = "0.2" tempfile = "3" zcash_proofs = { path = "../zcash_proofs" } + +[features] +mainnet = [] diff --git a/zcash_client_sqlite/README.md b/zcash_client_sqlite/README.md index b5fc4f1..d73e3fe 100644 --- a/zcash_client_sqlite/README.md +++ b/zcash_client_sqlite/README.md @@ -16,8 +16,7 @@ of the following: * ❌ The code **has not been subjected to thorough review** by engineers at the Electric Coin Company. * :warning: This library **is** compatible with the latest version of zcashd, but there **is no** automated testing of this. * :heavy_check_mark: The library **is not** majorly broken in some way. -* ❌ The library **only runs** on testnet. -* ❌ The library **does not run** on mainnet or regtest. +* :heavy_check_mark: The library **does run** on mainnet and testnet. * ❌ We **are actively rebasing** this branch and adding features where/when needed. * ❌ We **do not** undertake appropriate security coverage (threat models, review, response, etc.). * :heavy_check_mark: There is a product manager for this library. diff --git a/zcash_client_sqlite/src/init.rs b/zcash_client_sqlite/src/init.rs index e457ad9..e6eeaff 100644 --- a/zcash_client_sqlite/src/init.rs +++ b/zcash_client_sqlite/src/init.rs @@ -2,15 +2,13 @@ use rusqlite::{types::ToSql, Connection, NO_PARAMS}; use std::path::Path; -use zcash_client_backend::{ - constants::testnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, - encoding::encode_extended_full_viewing_key, -}; +use zcash_client_backend::encoding::encode_extended_full_viewing_key; use zcash_primitives::{block::BlockHash, zip32::ExtendedFullViewingKey}; use crate::{ address_from_extfvk, error::{Error, ErrorKind}, + HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, }; /// Sets up the internal structure of the cache database. @@ -243,16 +241,14 @@ pub fn init_blocks_table>( #[cfg(test)] mod tests { use tempfile::NamedTempFile; - use zcash_client_backend::{ - constants::testnet::HRP_SAPLING_PAYMENT_ADDRESS, encoding::decode_payment_address, - }; + use zcash_client_backend::encoding::decode_payment_address; use zcash_primitives::{ block::BlockHash, zip32::{ExtendedFullViewingKey, ExtendedSpendingKey}, }; use super::{init_accounts_table, init_blocks_table, init_data_database}; - use crate::query::get_address; + use crate::{query::get_address, HRP_SAPLING_PAYMENT_ADDRESS}; #[test] fn init_accounts_table_only_works_once() { diff --git a/zcash_client_sqlite/src/lib.rs b/zcash_client_sqlite/src/lib.rs index 1da09d5..b26df3e 100644 --- a/zcash_client_sqlite/src/lib.rs +++ b/zcash_client_sqlite/src/lib.rs @@ -16,16 +16,29 @@ //! **MUST NOT** write to the database without using these APIs. Callers **MAY** read //! the database directly in order to extract information for display to users. //! +//! # Features +//! +//! The `mainnet` feature configures the light client for use with the Zcash mainnet. By +//! default, the light client is configured for use with the Zcash testnet. +//! //! [`CompactBlock`]: zcash_client_backend::proto::compact_formats::CompactBlock //! [`init_cache_database`]: crate::init::init_cache_database use rusqlite::{Connection, NO_PARAMS}; use std::cmp; -use zcash_client_backend::{ - constants::testnet::HRP_SAPLING_PAYMENT_ADDRESS, encoding::encode_payment_address, -}; +use zcash_client_backend::encoding::encode_payment_address; use zcash_primitives::zip32::ExtendedFullViewingKey; +#[cfg(feature = "mainnet")] +use zcash_client_backend::constants::mainnet::{ + HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, HRP_SAPLING_PAYMENT_ADDRESS, +}; + +#[cfg(not(feature = "mainnet"))] +use zcash_client_backend::constants::testnet::{ + HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, HRP_SAPLING_PAYMENT_ADDRESS, +}; + pub mod error; pub mod init; pub mod query; @@ -33,6 +46,11 @@ pub mod scan; pub mod transact; const ANCHOR_OFFSET: u32 = 10; + +#[cfg(feature = "mainnet")] +const SAPLING_ACTIVATION_HEIGHT: i32 = 419_200; + +#[cfg(not(feature = "mainnet"))] const SAPLING_ACTIVATION_HEIGHT: i32 = 280_000; fn address_from_extfvk(extfvk: &ExtendedFullViewingKey) -> String { diff --git a/zcash_client_sqlite/src/scan.rs b/zcash_client_sqlite/src/scan.rs index 44ec1cb..75fd9ca 100644 --- a/zcash_client_sqlite/src/scan.rs +++ b/zcash_client_sqlite/src/scan.rs @@ -5,7 +5,6 @@ use protobuf::parse_from_bytes; use rusqlite::{types::ToSql, Connection, NO_PARAMS}; use std::path::Path; use zcash_client_backend::{ - constants::testnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, encoding::decode_extended_full_viewing_key, proto::compact_formats::CompactBlock, welding_rig::scan_block, }; @@ -17,7 +16,7 @@ use zcash_primitives::{ use crate::{ error::{Error, ErrorKind}, - SAPLING_ACTIVATION_HEIGHT, + HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, SAPLING_ACTIVATION_HEIGHT, }; struct CompactBlockRow { diff --git a/zcash_client_sqlite/src/transact.rs b/zcash_client_sqlite/src/transact.rs index eb9cd68..936cc9f 100644 --- a/zcash_client_sqlite/src/transact.rs +++ b/zcash_client_sqlite/src/transact.rs @@ -4,10 +4,7 @@ use ff::{PrimeField, PrimeFieldRepr}; use pairing::bls12_381::Bls12; use rusqlite::{types::ToSql, Connection, NO_PARAMS}; use std::path::Path; -use zcash_client_backend::{ - constants::testnet::{HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, HRP_SAPLING_PAYMENT_ADDRESS}, - encoding::{encode_extended_full_viewing_key, encode_payment_address}, -}; +use zcash_client_backend::encoding::{encode_extended_full_viewing_key, encode_payment_address}; use zcash_primitives::{ jubjub::fs::{Fs, FsRepr}, merkle_tree::IncrementalWitness, @@ -25,7 +22,8 @@ use zcash_primitives::{ use crate::{ error::{Error, ErrorKind}, - get_target_and_anchor_heights, + get_target_and_anchor_heights, HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, + HRP_SAPLING_PAYMENT_ADDRESS, }; struct SelectedNoteRow {