zcash_client_backend: Move essential wallet types into the `wallet` module.

This commit is contained in:
Kris Nuttycombe 2023-10-31 10:03:37 -06:00
parent 4a7dd2bed2
commit 6b10a6dc86
14 changed files with 129 additions and 118 deletions

View File

@ -49,6 +49,12 @@ and this library adheres to Rust's notion of
wallet::input_selection::{Proposal, SaplingInputs},
}`
### Moved
- `zcash_client_backend::data_api::{PoolType, ShieldedProtocol}` have
been moved into the `zcash_client_backend` root module.
- `zcash_client_backend::data_api::{NoteId, Recipient}` have
been moved into the `zcash_client_backend::wallet` module.
### Changed
- `zcash_client_backend::data_api`:
- Arguments to `BlockMetadata::from_parts` have changed to include Orchard.

View File

@ -2,7 +2,7 @@
use std::{
collections::{BTreeMap, HashMap},
fmt::{self, Debug},
fmt::Debug,
io,
num::{NonZeroU32, TryFromIntError},
};
@ -31,7 +31,7 @@ use crate::{
decrypt::DecryptedOutput,
keys::{UnifiedFullViewingKey, UnifiedSpendingKey},
proto::service::TreeState,
wallet::{ReceivedSaplingNote, WalletTransparentOutput, WalletTx},
wallet::{NoteId, ReceivedSaplingNote, Recipient, WalletTransparentOutput, WalletTx},
};
use self::chain::CommitmentTreeRoot;
@ -756,81 +756,6 @@ pub struct SentTransaction<'a> {
pub utxos_spent: Vec<OutPoint>,
}
/// A shielded transfer protocol supported by the wallet.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum ShieldedProtocol {
/// The Sapling protocol
Sapling,
/// The Orchard protocol
Orchard,
}
/// A unique identifier for a shielded transaction output
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct NoteId {
txid: TxId,
protocol: ShieldedProtocol,
output_index: u16,
}
impl NoteId {
/// Constructs a new `NoteId` from its parts.
pub fn new(txid: TxId, protocol: ShieldedProtocol, output_index: u16) -> Self {
Self {
txid,
protocol,
output_index,
}
}
/// Returns the ID of the transaction containing this note.
pub fn txid(&self) -> &TxId {
&self.txid
}
/// Returns the shielded protocol used by this note.
pub fn protocol(&self) -> ShieldedProtocol {
self.protocol
}
/// Returns the index of this note within its transaction's corresponding list of
/// shielded outputs.
pub fn output_index(&self) -> u16 {
self.output_index
}
}
/// A value pool to which the wallet supports sending transaction outputs.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum PoolType {
/// The transparent value pool
Transparent,
/// A shielded value pool.
Shielded(ShieldedProtocol),
}
impl fmt::Display for PoolType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PoolType::Transparent => f.write_str("Transparent"),
PoolType::Shielded(ShieldedProtocol::Sapling) => f.write_str("Sapling"),
PoolType::Shielded(ShieldedProtocol::Orchard) => f.write_str("Orchard"),
}
}
}
/// A type that represents the recipient of a transaction output; a recipient address (and, for
/// unified addresses, the pool to which the payment is sent) in the case of outgoing output, or an
/// internal account ID and the pool to which funds were sent in the case of a wallet-internal
/// output.
#[derive(Debug, Clone)]
pub enum Recipient {
Transparent(TransparentAddress),
Sapling(sapling::PaymentAddress),
Unified(UnifiedAddress, PoolType),
InternalAccount(AccountId, PoolType),
}
/// A type that represents an output (either Sapling or transparent) that was sent by the wallet.
pub struct SentTransactionOutput {
output_index: usize,
@ -1149,14 +1074,13 @@ pub mod testing {
use crate::{
address::{AddressMetadata, UnifiedAddress},
keys::{UnifiedFullViewingKey, UnifiedSpendingKey},
wallet::{ReceivedSaplingNote, WalletTransparentOutput},
wallet::{NoteId, ReceivedSaplingNote, WalletTransparentOutput},
};
use super::{
chain::CommitmentTreeRoot, scanning::ScanRange, AccountBirthday, BlockMetadata,
DecryptedTransaction, NoteId, NullifierQuery, SaplingInputSource, ScannedBlock,
SentTransaction, WalletCommitmentTrees, WalletRead, WalletSummary, WalletWrite,
SAPLING_SHARD_HEIGHT,
DecryptedTransaction, NullifierQuery, SaplingInputSource, ScannedBlock, SentTransaction,
WalletCommitmentTrees, WalletRead, WalletSummary, WalletWrite, SAPLING_SHARD_HEIGHT,
};
pub struct MockWalletDb {

View File

@ -15,12 +15,12 @@ use zcash_primitives::{
};
use crate::data_api::wallet::input_selection::InputSelectorError;
use crate::data_api::PoolType;
use crate::PoolType;
#[cfg(feature = "transparent-inputs")]
use zcash_primitives::{legacy::TransparentAddress, zip32::DiversifierIndex};
use super::NoteId;
use crate::wallet::NoteId;
/// Errors that can occur as a consequence of wallet operations.
#[derive(Debug)]

View File

@ -24,15 +24,16 @@ use zcash_primitives::{
use crate::{
address::RecipientAddress,
data_api::{
error::Error, wallet::input_selection::Proposal, DecryptedTransaction, PoolType, Recipient,
SentTransaction, SentTransactionOutput, WalletCommitmentTrees, WalletRead, WalletWrite,
error::Error, wallet::input_selection::Proposal, DecryptedTransaction, SentTransaction,
SentTransactionOutput, WalletCommitmentTrees, WalletRead, WalletWrite,
SAPLING_SHARD_HEIGHT,
},
decrypt_transaction,
fees::{self, ChangeValue, DustOutputPolicy},
keys::UnifiedSpendingKey,
wallet::{OvkPolicy, ReceivedSaplingNote},
wallet::{NoteId, OvkPolicy, ReceivedSaplingNote, Recipient},
zip321::{self, Payment},
PoolType, ShieldedProtocol,
};
pub mod input_selection;
@ -40,7 +41,7 @@ use input_selection::{
GreedyInputSelector, GreedyInputSelectorError, InputSelector, InputSelectorError,
};
use super::{NoteId, SaplingInputSource, ShieldedProtocol};
use super::SaplingInputSource;
#[cfg(feature = "transparent-inputs")]
use {
@ -597,11 +598,11 @@ where
sapling_inputs.anchor_height(),
)?
.ok_or_else(|| {
Error::NoteMismatch(NoteId {
txid: *selected.txid(),
protocol: ShieldedProtocol::Sapling,
output_index: selected.output_index(),
})
Error::NoteMismatch(NoteId::new(
*selected.txid(),
ShieldedProtocol::Sapling,
selected.output_index(),
))
})?;
let key = match scope {

View File

@ -23,8 +23,38 @@ pub mod zip321;
#[cfg(feature = "unstable-serialization")]
pub mod serialization;
use std::fmt;
pub use decrypt::{decrypt_transaction, DecryptedOutput, TransferType};
#[cfg(test)]
#[macro_use]
extern crate assert_matches;
/// A shielded transfer protocol supported by the wallet.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum ShieldedProtocol {
/// The Sapling protocol
Sapling,
/// The Orchard protocol
Orchard,
}
/// A value pool to which the wallet supports sending transaction outputs.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum PoolType {
/// The transparent value pool
Transparent,
/// A shielded value pool.
Shielded(ShieldedProtocol),
}
impl fmt::Display for PoolType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PoolType::Transparent => f.write_str("Transparent"),
PoolType::Shielded(ShieldedProtocol::Sapling) => f.write_str("Sapling"),
PoolType::Shielded(ShieldedProtocol::Orchard) => f.write_str("Orchard"),
}
}
}

View File

@ -27,10 +27,11 @@ use zcash_note_encryption::{EphemeralKeyBytes, COMPACT_NOTE_SIZE};
use crate::{
data_api::{
wallet::input_selection::{Proposal, ProposalError, SaplingInputs},
PoolType, SaplingInputSource, ShieldedProtocol, TransparentInputSource,
SaplingInputSource, TransparentInputSource,
},
fees::{ChangeValue, TransactionBalance},
zip321::{TransactionRequest, Zip321Error},
PoolType, ShieldedProtocol,
};
#[rustfmt::skip]

View File

@ -19,11 +19,12 @@ use zcash_primitives::{
zip32::{AccountId, Scope},
};
use crate::data_api::{BlockMetadata, ScannedBlock, ShieldedProtocol};
use crate::data_api::{BlockMetadata, ScannedBlock};
use crate::{
proto::compact_formats::CompactBlock,
scan::{Batch, BatchRunner, Tasks},
wallet::{WalletSaplingOutput, WalletSaplingSpend, WalletTx},
ShieldedProtocol,
};
/// A key that can be used to perform trial decryption and nullifier

View File

@ -18,6 +18,55 @@ use zcash_primitives::{
zip32::AccountId,
};
use crate::{address::UnifiedAddress, PoolType, ShieldedProtocol};
/// A unique identifier for a shielded transaction output
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct NoteId {
txid: TxId,
protocol: ShieldedProtocol,
output_index: u16,
}
impl NoteId {
/// Constructs a new `NoteId` from its parts.
pub fn new(txid: TxId, protocol: ShieldedProtocol, output_index: u16) -> Self {
Self {
txid,
protocol,
output_index,
}
}
/// Returns the ID of the transaction containing this note.
pub fn txid(&self) -> &TxId {
&self.txid
}
/// Returns the shielded protocol used by this note.
pub fn protocol(&self) -> ShieldedProtocol {
self.protocol
}
/// Returns the index of this note within its transaction's corresponding list of
/// shielded outputs.
pub fn output_index(&self) -> u16 {
self.output_index
}
}
/// A type that represents the recipient of a transaction output; a recipient address (and, for
/// unified addresses, the pool to which the payment is sent) in the case of outgoing output, or an
/// internal account ID and the pool to which funds were sent in the case of a wallet-internal
/// output.
#[derive(Debug, Clone)]
pub enum Recipient {
Transparent(TransparentAddress),
Sapling(sapling::PaymentAddress),
Unified(UnifiedAddress, PoolType),
InternalAccount(AccountId, PoolType),
}
/// A subset of a [`Transaction`] relevant to wallets and light clients.
///
/// [`Transaction`]: zcash_primitives::transaction::Transaction

View File

@ -4,10 +4,13 @@ use std::error;
use std::fmt;
use shardtree::error::ShardTreeError;
use zcash_client_backend::data_api::PoolType;
use zcash_client_backend::encoding::{Bech32DecodeError, TransparentCodecError};
use zcash_primitives::transaction::components::amount::BalanceError;
use zcash_primitives::{consensus::BlockHeight, zip32::AccountId};
use zcash_client_backend::{
encoding::{Bech32DecodeError, TransparentCodecError},
PoolType,
};
use zcash_primitives::{
consensus::BlockHeight, transaction::components::amount::BalanceError, zip32::AccountId,
};
use crate::wallet::commitment_tree;
use crate::PRUNING_DEPTH;

View File

@ -64,14 +64,14 @@ use zcash_client_backend::{
self,
chain::{BlockSource, CommitmentTreeRoot},
scanning::{ScanPriority, ScanRange},
AccountBirthday, BlockMetadata, DecryptedTransaction, NoteId, NullifierQuery, PoolType,
Recipient, SaplingInputSource, ScannedBlock, SentTransaction, ShieldedProtocol,
WalletCommitmentTrees, WalletRead, WalletSummary, WalletWrite, SAPLING_SHARD_HEIGHT,
AccountBirthday, BlockMetadata, DecryptedTransaction, NullifierQuery, SaplingInputSource,
ScannedBlock, SentTransaction, WalletCommitmentTrees, WalletRead, WalletSummary,
WalletWrite, SAPLING_SHARD_HEIGHT,
},
keys::{UnifiedFullViewingKey, UnifiedSpendingKey},
proto::compact_formats::CompactBlock,
wallet::{ReceivedSaplingNote, WalletTransparentOutput},
DecryptedOutput, TransferType,
wallet::{NoteId, ReceivedSaplingNote, Recipient, WalletTransparentOutput},
DecryptedOutput, PoolType, ShieldedProtocol, TransferType,
};
use crate::{error::SqliteClientError, wallet::commitment_tree::SqliteShardStore};

View File

@ -77,27 +77,25 @@ use zcash_client_backend::data_api::{AccountBalance, Ratio, WalletSummary};
use zcash_primitives::transaction::components::amount::NonNegativeAmount;
use zcash_primitives::zip32::Scope;
use zcash_client_backend::data_api::{
scanning::{ScanPriority, ScanRange},
AccountBirthday, NoteId, ShieldedProtocol, SAPLING_SHARD_HEIGHT,
};
use zcash_primitives::transaction::TransactionData;
use zcash_primitives::{
block::BlockHash,
consensus::{self, BlockHeight, BranchId, NetworkUpgrade, Parameters},
memo::{Memo, MemoBytes},
merkle_tree::read_commitment_tree,
transaction::{components::Amount, Transaction, TxId},
transaction::{components::Amount, Transaction, TransactionData, TxId},
zip32::{AccountId, DiversifierIndex},
};
use zcash_client_backend::{
address::{RecipientAddress, UnifiedAddress},
data_api::{BlockMetadata, PoolType, Recipient, SentTransactionOutput},
data_api::{
scanning::{ScanPriority, ScanRange},
AccountBirthday, BlockMetadata, SentTransactionOutput, SAPLING_SHARD_HEIGHT,
},
encoding::AddressCodec,
keys::UnifiedFullViewingKey,
wallet::WalletTx,
wallet::{NoteId, Recipient, WalletTx},
PoolType, ShieldedProtocol,
};
use crate::wallet::commitment_tree::{get_max_checkpointed_height, SqliteShardStore};

View File

@ -8,9 +8,7 @@ use secrecy::{ExposeSecret, SecretVec};
use uuid::Uuid;
use zcash_client_backend::{
address::RecipientAddress,
data_api::{PoolType, ShieldedProtocol},
keys::UnifiedSpendingKey,
address::RecipientAddress, keys::UnifiedSpendingKey, PoolType, ShieldedProtocol,
};
use zcash_primitives::{consensus, zip32::AccountId};

View File

@ -6,7 +6,7 @@ use rusqlite::{self, named_params};
use schemer;
use schemer_rusqlite::RusqliteMigration;
use uuid::Uuid;
use zcash_client_backend::data_api::{PoolType, ShieldedProtocol};
use zcash_client_backend::{PoolType, ShieldedProtocol};
use super::add_transaction_views;
use crate::wallet::{init::WalletMigrationError, pool_code};

View File

@ -483,14 +483,14 @@ pub(crate) mod tests {
chain::CommitmentTreeRoot,
error::Error,
wallet::input_selection::{GreedyInputSelector, GreedyInputSelectorError},
AccountBirthday, Ratio, ShieldedProtocol, WalletCommitmentTrees, WalletRead,
WalletWrite,
AccountBirthday, Ratio, WalletCommitmentTrees, WalletRead, WalletWrite,
},
decrypt_transaction,
fees::{fixed, standard, DustOutputPolicy},
keys::UnifiedSpendingKey,
wallet::OvkPolicy,
zip321::{self, Payment, TransactionRequest},
ShieldedProtocol,
};
use crate::{