Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wallet read part 1 #966

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions zingolib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ zingo-netutils = { path = "../zingo-netutils" }

http-body = { workspace = true }
hyper = { workspace = true }
hyper-rustls = { workspace = true }
prost = { workspace = true }
tonic = { workspace = true }
tower = { workspace = true }
prost = { workspace = true }

orchard = { workspace = true }
shardtree = { workspace = true, features = ["legacy-api"] }
incrementalmerkletree = { workspace = true, features = ["test-dependencies", "legacy-api"] }
zcash_address = { workspace = true }
zcash_client_backend = { workspace = true, features = ["unstable", "transparent-inputs", "unstable-serialization", "unstable-spanning-tree"] }
zcash_encoding = { workspace = true }
zcash_keys = { workspace = true }
zcash_note_encryption = { workspace = true }
zcash_primitives = { workspace = true }
zcash_proofs = { workspace = true }
hyper-rustls = { workspace = true }
zip32.workspace = true

append-only-vec = { git = "https://github.com/zancas/append-only-vec.git", branch = "add_debug_impl" }
Expand Down Expand Up @@ -80,6 +82,7 @@ derive_more = "0.99.17"
either = "1.8.1"
serde = { version = "1.0.188", features = ["derive"] }
sapling-crypto.workspace = true
secrecy = "0.8.0"

[dev-dependencies]
portpicker = "0.1.0"
Expand Down
266 changes: 265 additions & 1 deletion zingolib/src/wallet/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
//! Functionality for managing transactions

use crate::wallet::{data::WitnessTrees, transaction_records_by_id::TransactionRecordsById};
use secrecy::SecretVec;
use shardtree::store::ShardStore;
use zcash_client_backend::{
data_api::{Account, WalletRead},
keys::UnifiedFullViewingKey,
};
use zcash_primitives::consensus::BlockHeight;
use zip32::AccountId;

use crate::{
error::ZingoLibError,
wallet::{data::WitnessTrees, transaction_records_by_id::TransactionRecordsById},
};

/// HashMap of all transactions in a wallet, keyed by txid.
/// Note that the parent is expected to hold a RwLock, so we will assume that all accesses to
Expand Down Expand Up @@ -38,3 +50,255 @@ impl TxMapAndMaybeTrees {
self.witness_trees.as_mut().map(WitnessTrees::clear);
}
}

pub struct ZingoAccount(AccountId, UnifiedFullViewingKey);

impl Account<AccountId> for ZingoAccount {
fn id(&self) -> AccountId {
self.0
}

fn source(&self) -> zcash_client_backend::data_api::AccountSource {
unimplemented!()
}

fn ufvk(&self) -> Option<&UnifiedFullViewingKey> {
Some(&self.1)
}

fn uivk(&self) -> zcash_keys::keys::UnifiedIncomingViewingKey {
unimplemented!()
}
}

impl WalletRead for TxMapAndMaybeTrees {
type Error = ZingoLibError;
type AccountId = AccountId;
type Account = ZingoAccount;

fn get_account_for_ufvk(
&self,
ufvk: &UnifiedFullViewingKey,
) -> Result<Option<Self::Account>, Self::Error> {
Ok(Some(ZingoAccount(AccountId::ZERO, ufvk.clone())))
}

fn get_target_and_anchor_heights(
&self,
min_confirmations: std::num::NonZeroU32,
) -> Result<
Option<(
zcash_primitives::consensus::BlockHeight,
zcash_primitives::consensus::BlockHeight,
)>,
Self::Error,
> {
match self.witness_trees.as_ref() {
Some(trees) => {
let highest_block_height =
match trees.witness_tree_orchard.store().max_checkpoint_id() {
Ok(height) => height,
// Infallible
Err(e) => match e {},
};

Ok(highest_block_height.map(|height| {
(
height + 1,
BlockHeight::from_u32(std::cmp::max(
1,
u32::from(height).saturating_sub(u32::from(min_confirmations)),
)),
)
}))
}
None => Err(ZingoLibError::UnknownError),
}
}

fn get_min_unspent_height(
&self,
) -> Result<Option<zcash_primitives::consensus::BlockHeight>, Self::Error> {
todo!()
// Ok(self
// .record_map
// .map
// .values()
// .fold(None, |height, transaction| {
// let transaction_height = transaction.status.get_confirmed_height();
// match (height, transaction_height) {
// (None, None) => None,
// (Some(h), None) | (None, Some(h)) => Some(h),
// (Some(h1), Some(h2)) => Some(std::cmp::min(h1, h2)),
// }
// }))
}

fn get_tx_height(
&self,
_txid: zcash_primitives::transaction::TxId,
) -> Result<Option<zcash_primitives::consensus::BlockHeight>, Self::Error> {
todo!()
// Ok(self
// .record_map
// .map
// .get(&txid)
// .and_then(|transaction| transaction.status.get_confirmed_height()))
}

fn get_account_ids(&self) -> Result<Vec<Self::AccountId>, Self::Error> {
unimplemented!()
}
fn get_account(
&self,
_account_id: Self::AccountId,
) -> Result<Option<Self::Account>, Self::Error> {
unimplemented!()
}
fn get_derived_account(
&self,
_seed: &zip32::fingerprint::SeedFingerprint,
_account_id: zcash_primitives::zip32::AccountId,
) -> Result<Option<Self::Account>, Self::Error> {
unimplemented!()
}
fn validate_seed(
&self,
_account_id: Self::AccountId,
_seed: &SecretVec<u8>,
) -> Result<bool, Self::Error> {
unimplemented!()
}
fn get_current_address(
&self,
_account: Self::AccountId,
) -> Result<Option<zcash_keys::address::UnifiedAddress>, Self::Error> {
unimplemented!()
}
fn get_account_birthday(
&self,
_account: Self::AccountId,
) -> Result<zcash_primitives::consensus::BlockHeight, Self::Error> {
unimplemented!()
}
fn get_wallet_birthday(
&self,
) -> Result<Option<zcash_primitives::consensus::BlockHeight>, Self::Error> {
unimplemented!()
}
fn get_wallet_summary(
&self,
_min_confirmations: u32,
) -> Result<Option<zcash_client_backend::data_api::WalletSummary<Self::AccountId>>, Self::Error>
{
unimplemented!()
}
fn chain_height(
&self,
) -> Result<Option<zcash_primitives::consensus::BlockHeight>, Self::Error> {
unimplemented!()
}
fn get_block_hash(
&self,
_block_height: zcash_primitives::consensus::BlockHeight,
) -> Result<Option<zcash_primitives::block::BlockHash>, Self::Error> {
unimplemented!()
}
fn block_metadata(
&self,
_height: zcash_primitives::consensus::BlockHeight,
) -> Result<Option<zcash_client_backend::data_api::BlockMetadata>, Self::Error> {
unimplemented!()
}
fn block_fully_scanned(
&self,
) -> Result<Option<zcash_client_backend::data_api::BlockMetadata>, Self::Error> {
unimplemented!()
}
fn get_max_height_hash(
&self,
) -> Result<
Option<(
zcash_primitives::consensus::BlockHeight,
zcash_primitives::block::BlockHash,
)>,
Self::Error,
> {
unimplemented!()
}
fn block_max_scanned(
&self,
) -> Result<Option<zcash_client_backend::data_api::BlockMetadata>, Self::Error> {
unimplemented!()
}
fn suggest_scan_ranges(
&self,
) -> Result<Vec<zcash_client_backend::data_api::scanning::ScanRange>, Self::Error> {
unimplemented!()
}
fn get_unified_full_viewing_keys(
&self,
) -> Result<std::collections::HashMap<Self::AccountId, UnifiedFullViewingKey>, Self::Error>
{
unimplemented!()
}
fn get_memo(
&self,
_note_id: zcash_client_backend::wallet::NoteId,
) -> Result<Option<zcash_primitives::memo::Memo>, Self::Error> {
unimplemented!()
}
fn get_transaction(
&self,
_txid: zcash_primitives::transaction::TxId,
) -> Result<std::option::Option<zcash_primitives::transaction::Transaction>, ZingoLibError>
{
unimplemented!()
}
fn get_sapling_nullifiers(
&self,
_query: zcash_client_backend::data_api::NullifierQuery,
) -> Result<Vec<(Self::AccountId, sapling_crypto::Nullifier)>, Self::Error> {
unimplemented!()
}
fn get_orchard_nullifiers(
&self,
_query: zcash_client_backend::data_api::NullifierQuery,
) -> Result<Vec<(Self::AccountId, orchard::note::Nullifier)>, Self::Error> {
unimplemented!()
}
fn seed_relevance_to_derived_accounts(
&self,
_seed: &SecretVec<u8>,
) -> Result<zcash_client_backend::data_api::SeedRelevance<Self::AccountId>, Self::Error> {
unimplemented!()
}
}

#[cfg(test)]
mod tests {
use std::num::NonZeroU32;

use zcash_client_backend::data_api::WalletRead;
use zcash_primitives::consensus::BlockHeight;

#[test]
fn test_get_target_and_anchor_heights() {
use super::TxMapAndMaybeTrees;

let mut transaction_records_and_maybe_trees = TxMapAndMaybeTrees::new_with_witness_trees();
transaction_records_and_maybe_trees
.witness_trees
.as_mut()
.unwrap()
.add_checkpoint(8421.into());

assert_eq!(
transaction_records_and_maybe_trees
.get_target_and_anchor_heights(NonZeroU32::new(10).unwrap())
.unwrap()
.unwrap(),
(BlockHeight::from_u32(8422), BlockHeight::from_u32(8411))
);
}
}
Loading