Improve wallet "database" trait names.

This commit is contained in:
Kris Nuttycombe 2020-10-19 15:20:34 -06:00
parent b2cc240454
commit 8a215d67fe
7 changed files with 89 additions and 37 deletions

View File

@ -24,11 +24,11 @@ pub mod chain;
pub mod error;
pub mod wallet;
pub trait DBOps {
pub trait WalletRead {
type Error;
type NoteRef: Copy + Debug; // Backend-specific note identifier
type TxRef: Copy + Debug;
type UpdateOps: DBUpdate<Error = Self::Error, NoteRef = Self::NoteRef, TxRef = Self::TxRef>;
type UpdateOps: WalletWrite<Error = Self::Error, NoteRef = Self::NoteRef, TxRef = Self::TxRef>;
fn block_height_extrema(&self) -> Result<Option<(BlockHeight, BlockHeight)>, Self::Error>;
@ -54,13 +54,16 @@ pub trait DBOps {
fn get_block_hash(&self, block_height: BlockHeight) -> Result<Option<BlockHash>, Self::Error>;
fn get_max_height_hash(&self) -> Result<Option<(BlockHeight, BlockHash)>, Self::Error> {
self.block_height_extrema().and_then(|extrema_opt| {
extrema_opt.map(|(_, max_height)| {
self.get_block_hash(max_height).map(|hash_opt|
hash_opt.map(move |hash| (max_height, hash))
)
}).transpose()
}).map(|oo| oo.flatten())
self.block_height_extrema()
.and_then(|extrema_opt| {
extrema_opt
.map(|(_, max_height)| {
self.get_block_hash(max_height)
.map(|hash_opt| hash_opt.map(move |hash| (max_height, hash)))
})
.transpose()
})
.map(|oo| oo.flatten())
}
fn get_tx_height(&self, txid: TxId) -> Result<Option<BlockHeight>, Self::Error>;
@ -120,7 +123,7 @@ pub trait DBOps {
fn get_update_ops(&self) -> Result<Self::UpdateOps, Self::Error>;
}
pub trait DBUpdate {
pub trait WalletWrite {
type Error;
type NoteRef: Copy;
type TxRef: Copy;
@ -194,7 +197,7 @@ pub trait DBUpdate {
) -> Result<(), Self::Error>;
}
pub trait CacheOps {
pub trait BlockSource {
type Error;
fn init_cache(&self) -> Result<(), Self::Error>;

View File

@ -9,7 +9,7 @@ use zcash_primitives::{
use crate::{
data_api::{
error::{ChainInvalid, Error},
CacheOps, DBOps, DBUpdate,
BlockSource, WalletRead, WalletWrite,
},
proto::compact_formats::CompactBlock,
wallet::{AccountId, WalletTx},
@ -41,12 +41,12 @@ use crate::{
pub fn validate_combined_chain<'db, E0, N, E, P, C>(
parameters: &P,
cache: &C,
validate_from: Option<(BlockHeight, BlockHash)>
validate_from: Option<(BlockHeight, BlockHash)>,
) -> Result<(), E>
where
E: From<Error<E0, N>>,
P: consensus::Parameters,
C: CacheOps<Error = E>,
C: BlockSource<Error = E>,
{
let sapling_activation_height = parameters
.activation_height(NetworkUpgrade::Sapling)
@ -56,7 +56,9 @@ where
// height up to the chain tip, returning the hash of the block found in the cache at the
// `validate_from` height, which can then be used to verify chain integrity by comparing
// against the `validate_from` hash.
let from_height = validate_from.map(|(height, _)| height).unwrap_or(sapling_activation_height - 1);
let from_height = validate_from
.map(|(height, _)| height)
.unwrap_or(sapling_activation_height - 1);
let scan_start_hash = cache.validate_chain(from_height, |top_block, next_block| {
if next_block.height() != top_block.height() - 1 {
Err(
@ -137,8 +139,8 @@ pub fn scan_cached_blocks<'db, E, E0, N, P, C, D>(
) -> Result<(), E>
where
P: consensus::Parameters,
C: CacheOps<Error = E>,
&'db D: DBOps<Error = E, NoteRef = N>,
C: BlockSource<Error = E>,
&'db D: WalletRead<Error = E, NoteRef = N>,
N: Copy + Debug,
E: From<Error<E0, N>>,
{

View File

@ -15,7 +15,7 @@ use zcash_primitives::{
use crate::{
address::RecipientAddress,
data_api::{error::Error, DBOps, DBUpdate},
data_api::{error::Error, WalletRead, WalletWrite},
decrypt_transaction,
wallet::{AccountId, OvkPolicy},
};
@ -32,7 +32,7 @@ pub fn decrypt_and_store_transaction<'db, E0, N, E, P, D>(
where
E: From<Error<E0, N>>,
P: consensus::Parameters,
&'db D: DBOps<Error = E>,
&'db D: WalletRead<Error = E>,
{
// Fetch the ExtendedFullViewingKeys we are tracking
let extfvks = data.get_extended_full_viewing_keys(params)?;
@ -159,7 +159,7 @@ where
E0: Into<Error<E, N>>,
P: consensus::Parameters + Clone,
R: Copy + Debug,
&'db D: DBOps<Error = E0, TxRef = R>,
&'db D: WalletRead<Error = E0, TxRef = R>,
{
// Check that the ExtendedSpendingKey we have been given corresponds to the
// ExtendedFullViewingKey for the account we are spending from.

View File

@ -10,7 +10,7 @@
//!
//! use zcash_client_backend::{
//! data_api::{
//! DBOps,
//! WalletRead,
//! chain::{
//! validate_combined_chain,
//! scan_cached_blocks,
@ -193,7 +193,7 @@ mod tests {
zip32::{ExtendedFullViewingKey, ExtendedSpendingKey},
};
use zcash_client_backend::data_api::DBOps;
use zcash_client_backend::data_api::WalletRead;
use zcash_client_backend::data_api::{
chain::{scan_cached_blocks, validate_combined_chain},
error::{ChainInvalid, Error},
@ -229,7 +229,12 @@ mod tests {
init_accounts_table(&db_data, &tests::network(), &[extfvk.clone()]).unwrap();
// Empty chain should be valid
validate_combined_chain(&tests::network(), &db_cache, (&db_data).get_max_height_hash().unwrap()).unwrap();
validate_combined_chain(
&tests::network(),
&db_cache,
(&db_data).get_max_height_hash().unwrap(),
)
.unwrap();
// Create a fake CompactBlock sending value to the address
let (cb, _) = fake_compact_block(
@ -241,13 +246,23 @@ mod tests {
insert_into_cache(&db_cache, &cb);
// Cache-only chain should be valid
validate_combined_chain(&tests::network(), &db_cache, (&db_data).get_max_height_hash().unwrap()).unwrap();
validate_combined_chain(
&tests::network(),
&db_cache,
(&db_data).get_max_height_hash().unwrap(),
)
.unwrap();
// Scan the cache
scan_cached_blocks(&tests::network(), &db_cache, &db_data, None).unwrap();
// Data-only chain should be valid
validate_combined_chain(&tests::network(), &db_cache, (&db_data).get_max_height_hash().unwrap()).unwrap();
validate_combined_chain(
&tests::network(),
&db_cache,
(&db_data).get_max_height_hash().unwrap(),
)
.unwrap();
// Create a second fake CompactBlock sending more value to the address
let (cb2, _) = fake_compact_block(
@ -259,13 +274,23 @@ mod tests {
insert_into_cache(&db_cache, &cb2);
// Data+cache chain should be valid
validate_combined_chain(&tests::network(), &db_cache, (&db_data).get_max_height_hash().unwrap()).unwrap();
validate_combined_chain(
&tests::network(),
&db_cache,
(&db_data).get_max_height_hash().unwrap(),
)
.unwrap();
// Scan the cache again
scan_cached_blocks(&tests::network(), &db_cache, &db_data, None).unwrap();
// Data-only chain should be valid
validate_combined_chain(&tests::network(), &db_cache, (&db_data).get_max_height_hash().unwrap()).unwrap();
validate_combined_chain(
&tests::network(),
&db_cache,
(&db_data).get_max_height_hash().unwrap(),
)
.unwrap();
}
#[test]
@ -303,7 +328,12 @@ mod tests {
scan_cached_blocks(&tests::network(), &db_cache, &db_data, None).unwrap();
// Data-only chain should be valid
validate_combined_chain(&tests::network(), &db_cache, (&db_data).get_max_height_hash().unwrap()).unwrap();
validate_combined_chain(
&tests::network(),
&db_cache,
(&db_data).get_max_height_hash().unwrap(),
)
.unwrap();
// Create more fake CompactBlocks that don't connect to the scanned ones
let (cb3, _) = fake_compact_block(
@ -322,7 +352,13 @@ mod tests {
insert_into_cache(&db_cache, &cb4);
// Data+cache chain should be invalid at the data/cache boundary
match validate_combined_chain(&tests::network(), &db_cache, (&db_data).get_max_height_hash().unwrap()).map_err(|e| e.0) {
match validate_combined_chain(
&tests::network(),
&db_cache,
(&db_data).get_max_height_hash().unwrap(),
)
.map_err(|e| e.0)
{
Err(Error::InvalidChain(upper_bound, _)) => {
assert_eq!(upper_bound, sapling_activation_height() + 1)
}
@ -365,7 +401,12 @@ mod tests {
scan_cached_blocks(&tests::network(), &db_cache, &db_data, None).unwrap();
// Data-only chain should be valid
validate_combined_chain(&tests::network(), &db_cache, (&db_data).get_max_height_hash().unwrap()).unwrap();
validate_combined_chain(
&tests::network(),
&db_cache,
(&db_data).get_max_height_hash().unwrap(),
)
.unwrap();
// Create more fake CompactBlocks that contain a reorg
let (cb3, _) = fake_compact_block(
@ -384,7 +425,13 @@ mod tests {
insert_into_cache(&db_cache, &cb4);
// Data+cache chain should be invalid inside the cache
match validate_combined_chain(&tests::network(), &db_cache, (&db_data).get_max_height_hash().unwrap()).map_err(|e| e.0) {
match validate_combined_chain(
&tests::network(),
&db_cache,
(&db_data).get_max_height_hash().unwrap(),
)
.map_err(|e| e.0)
{
Err(Error::InvalidChain(upper_bound, _)) => {
assert_eq!(upper_bound, sapling_activation_height() + 2)
}

View File

@ -44,7 +44,7 @@ use zcash_primitives::{
use zcash_client_backend::{
address::RecipientAddress,
data_api::{error::Error, CacheOps, DBOps, DBUpdate, ShieldedOutput},
data_api::{error::Error, BlockSource, ShieldedOutput, WalletRead, WalletWrite},
encoding::encode_payment_address,
proto::compact_formats::CompactBlock,
wallet::{AccountId, SpendableNote, WalletTx},
@ -74,7 +74,7 @@ impl DataConnection {
}
}
impl<'a> DBOps for &'a DataConnection {
impl<'a> WalletRead for &'a DataConnection {
type Error = SqliteClientError;
type NoteRef = NoteId;
type TxRef = i64;
@ -266,7 +266,7 @@ pub struct DataConnStmtCache<'a> {
stmt_update_expired: Statement<'a>,
}
impl<'a> DBUpdate for DataConnStmtCache<'a> {
impl<'a> WalletWrite for DataConnStmtCache<'a> {
type Error = SqliteClientError;
type TxRef = i64;
type NoteRef = NoteId;
@ -544,7 +544,7 @@ impl CacheConnection {
}
}
impl CacheOps for CacheConnection {
impl BlockSource for CacheConnection {
type Error = SqliteClientError;
fn init_cache(&self) -> Result<(), Self::Error> {

View File

@ -446,7 +446,7 @@ mod tests {
zip32::{ExtendedFullViewingKey, ExtendedSpendingKey},
};
use zcash_client_backend::data_api::DBOps;
use zcash_client_backend::data_api::WalletRead;
use crate::{
tests,

View File

@ -137,7 +137,7 @@ mod tests {
use zcash_proofs::prover::LocalTxProver;
use zcash_client_backend::{
data_api::{chain::scan_cached_blocks, wallet::create_spend_to_address, DBOps},
data_api::{chain::scan_cached_blocks, wallet::create_spend_to_address, WalletRead},
wallet::OvkPolicy,
};