Skip to content

Commit

Permalink
WIP: eliminate input selection trait inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttycom committed Nov 2, 2023
1 parent 6c46405 commit 5d3905c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 88 deletions.
36 changes: 4 additions & 32 deletions zcash_client_backend/src/data_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,19 +245,12 @@ pub trait TransparentInputSource {
) -> Result<Vec<WalletTransparentOutput>, Self::Error>;
}

pub trait InputSource:
SaplingInputSource<Error = <Self as InputSource>::Error>
+ TransparentInputSource<Error = <Self as InputSource>::Error>
{
type Error;
}

/// Read-only operations required for light wallet functions.
///
/// This trait defines the read-only portion of the storage interface atop which
/// higher-level wallet operations are implemented. It serves to allow wallet functions to
/// be abstracted away from any particular data storage substrate.
pub trait WalletRead: InputSource<Error = <Self as WalletRead>::Error> {
pub trait WalletRead {
type Error;

/// Returns the height of the chain as known to the wallet as of the most recent call to
Expand Down Expand Up @@ -398,14 +391,6 @@ pub trait WalletRead: InputSource<Error = <Self as WalletRead>::Error> {
query: NullifierQuery,
) -> Result<Vec<(AccountId, sapling::Nullifier)>, <Self as WalletRead>::Error>;

/// Return all unspent Sapling notes, excluding the specified note IDs.
fn get_spendable_sapling_notes(
&self,
account: AccountId,
anchor_height: BlockHeight,
exclude: &[Self::NoteRef],
) -> Result<Vec<ReceivedSaplingNote<Self::NoteRef>>, <Self as WalletRead>::Error>;

/// Returns the set of all transparent receivers associated with the given account.
///
/// The set contains all transparent receivers that are known to have been derived
Expand Down Expand Up @@ -994,9 +979,9 @@ pub mod testing {

use super::{
chain::CommitmentTreeRoot, scanning::ScanRange, AccountBirthday, BlockMetadata,
DecryptedTransaction, InputSource, NoteId, NullifierQuery, SaplingInputSource,
ScannedBlock, SentTransaction, TransparentInputSource, WalletCommitmentTrees, WalletRead,
WalletSummary, WalletWrite, SAPLING_SHARD_HEIGHT,
DecryptedTransaction, NoteId, NullifierQuery, SaplingInputSource, ScannedBlock,
SentTransaction, TransparentInputSource, WalletCommitmentTrees, WalletRead, WalletSummary,
WalletWrite, SAPLING_SHARD_HEIGHT,
};

pub struct MockWalletDb {
Expand Down Expand Up @@ -1045,10 +1030,6 @@ pub mod testing {
}
}

impl InputSource for MockWalletDb {
type Error = ();
}

impl WalletRead for MockWalletDb {
type Error = ();

Expand Down Expand Up @@ -1164,15 +1145,6 @@ pub mod testing {
Ok(Vec::new())
}

fn get_spendable_sapling_notes(
&self,
_account: AccountId,
_anchor_height: BlockHeight,
_exclude: &[Self::NoteRef],
) -> Result<Vec<ReceivedSaplingNote<Self::NoteRef>>, <Self as WalletRead>::Error> {
Ok(Vec::new())
}

fn get_transparent_receivers(
&self,
_account: AccountId,
Expand Down
35 changes: 20 additions & 15 deletions zcash_client_backend/src/data_api/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ use crate::{
pub mod input_selection;
use input_selection::{GreedyInputSelector, GreedyInputSelectorError, InputSelector};

use super::{NoteId, ShieldedProtocol};
use super::{NoteId, SaplingInputSource, ShieldedProtocol};

#[cfg(feature = "transparent-inputs")]
use {
super::TransparentInputSource,
crate::wallet::WalletTransparentOutput,
std::convert::Infallible,
zcash_primitives::{legacy::TransparentAddress, sapling::keys::OutgoingViewingKey},
};

Expand Down Expand Up @@ -210,7 +212,9 @@ pub fn create_spend_to_address<DbT, ParamsT>(
>
where
ParamsT: consensus::Parameters + Clone,
DbT: WalletWrite + WalletCommitmentTrees,
DbT: WalletWrite
+ WalletCommitmentTrees
+ SaplingInputSource<Error = <DbT as WalletRead>::Error>,
DbT::NoteRef: Copy + Eq + Ord,
{
let account = wallet_db
Expand Down Expand Up @@ -305,10 +309,12 @@ pub fn spend<DbT, ParamsT, InputsT>(
>,
>
where
DbT: WalletWrite + WalletCommitmentTrees,
DbT: WalletWrite
+ WalletCommitmentTrees
+ SaplingInputSource<Error = <DbT as WalletRead>::Error>,
DbT::NoteRef: Copy + Eq + Ord,
ParamsT: consensus::Parameters + Clone,
InputsT: InputSelector<DataSource = DbT>,
InputsT: InputSelector<InputSource = DbT>,
{
let account = wallet_db
.get_account_for_ufvk(&usk.to_unified_full_viewing_key())
Expand Down Expand Up @@ -349,10 +355,10 @@ pub fn propose_transfer<DbT, ParamsT, InputsT, CommitmentTreeErrT>(
>,
>
where
DbT: WalletWrite,
DbT: WalletRead + SaplingInputSource<Error = <DbT as WalletRead>::Error>,
DbT::NoteRef: Copy + Eq + Ord,
ParamsT: consensus::Parameters + Clone,
InputsT: InputSelector<DataSource = DbT>,
InputsT: InputSelector<InputSource = DbT>,
{
use self::input_selection::InputSelectorError;
let (target_height, anchor_height) = wallet_db
Expand Down Expand Up @@ -414,7 +420,7 @@ pub fn propose_standard_transfer_to_address<DbT, ParamsT, CommitmentTreeErrT>(
>
where
ParamsT: consensus::Parameters + Clone,
DbT: WalletWrite,
DbT: WalletRead + SaplingInputSource<Error = <DbT as WalletRead>::Error>,
DbT::NoteRef: Copy + Eq + Ord,
{
let request = zip321::TransactionRequest::new(vec![Payment {
Expand Down Expand Up @@ -454,7 +460,7 @@ pub fn propose_shielding<DbT, ParamsT, InputsT, CommitmentTreeErrT>(
from_addrs: &[TransparentAddress],
min_confirmations: NonZeroU32,
) -> Result<
Proposal<InputsT::FeeRule, DbT::NoteRef>,
Proposal<InputsT::FeeRule, Infallible>,
Error<
<DbT as WalletRead>::Error,
CommitmentTreeErrT,
Expand All @@ -464,9 +470,8 @@ pub fn propose_shielding<DbT, ParamsT, InputsT, CommitmentTreeErrT>(
>
where
ParamsT: consensus::Parameters,
DbT: WalletWrite,
DbT::NoteRef: Copy + Eq + Ord,
InputsT: InputSelector<DataSource = DbT>,
DbT: WalletRead + TransparentInputSource<Error = <DbT as WalletRead>::Error>,
InputsT: InputSelector<InputSource = DbT>,
{
use self::input_selection::InputSelectorError;
let (target_height, anchor_height) = wallet_db
Expand Down Expand Up @@ -496,13 +501,13 @@ where
/// to fall back to the transparent receiver until full Orchard support is implemented.
#[allow(clippy::too_many_arguments)]
#[allow(clippy::type_complexity)]
pub fn create_proposed_transaction<DbT, ParamsT, InputsErrT, FeeRuleT>(
pub fn create_proposed_transaction<DbT, ParamsT, InputsErrT, FeeRuleT, N>(
wallet_db: &mut DbT,
params: &ParamsT,
prover: impl SaplingProver,
usk: &UnifiedSpendingKey,
ovk_policy: OvkPolicy,
proposal: &Proposal<FeeRuleT, DbT::NoteRef>,
proposal: Proposal<FeeRuleT, N>,
) -> Result<
TxId,
Error<
Expand All @@ -514,7 +519,6 @@ pub fn create_proposed_transaction<DbT, ParamsT, InputsErrT, FeeRuleT>(
>
where
DbT: WalletWrite + WalletCommitmentTrees,
DbT::NoteRef: Copy + Eq + Ord,
ParamsT: consensus::Parameters + Clone,
FeeRuleT: FeeRule,
{
Expand Down Expand Up @@ -553,7 +557,8 @@ where
// are no possible transparent inputs, so we ignore those
let mut builder = Builder::new(params.clone(), proposal.min_target_height(), None);

let confirmations = wallet_db.chain_height()?.ok_or(Error::ScanRequired)? - proposal.min_anchor_height();
let confirmations =
wallet_db.chain_height()?.ok_or(Error::ScanRequired)? - proposal.min_anchor_height();
let checkpoint_depth = wallet_db.get_checkpoint_depth(confirmations.into())?;
wallet_db.with_sapling_tree_mut::<_, _, Error<_, _, _, _>>(|sapling_tree| {
for selected in proposal.sapling_inputs() {
Expand Down
Loading

0 comments on commit 5d3905c

Please sign in to comment.