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

zcash_client_backend: Implement async wallet synchronization function #1184

Merged
merged 2 commits into from
Apr 3, 2024
Merged
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
1 change: 1 addition & 0 deletions .github/actions/prepare/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ runs:
bundled-prover
download-params
lightwalletd-tonic
sync
temporary-zcashd
transparent-inputs
unstable
Expand Down
14 changes: 14 additions & 0 deletions Cargo.lock

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

9 changes: 7 additions & 2 deletions zcash_client_backend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this library adheres to Rust's notion of

## [Unreleased]

### Added
- `zcash_client_backend::data_api`:
- `chain::BlockCache` trait, behind the `sync` feature flag.
- `zcash_client_backend::scanning`:
- `testing` module
- `zcash_client_backend::sync` module, behind the `sync` feature flag.

## [0.12.1] - 2024-03-27

### Fixed
Expand Down Expand Up @@ -39,7 +46,6 @@ and this library adheres to Rust's notion of
- `WalletSummary::next_orchard_subtree_index`
- `chain::ChainState`
- `chain::ScanSummary::{spent_orchard_note_count, received_orchard_note_count}`
- `chain::BlockCache` trait
- `impl Debug for chain::CommitmentTreeRoot`
- `zcash_client_backend::fees`:
- `orchard`
Expand All @@ -55,7 +61,6 @@ and this library adheres to Rust's notion of
- `Nullifiers::{orchard, extend_orchard, retain_orchard}`
- `TaggedOrchardBatch`
- `TaggedOrchardBatchRunner`
- `testing` module
- `zcash_client_backend::wallet`:
- `Note::Orchard`
- `WalletOrchardSpend`
Expand Down
18 changes: 13 additions & 5 deletions zcash_client_backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ tracing.workspace = true
# - Protobuf interfaces and gRPC bindings
hex.workspace = true
prost.workspace = true
tonic = { workspace = true, optional = true, features = ["prost", "codegen"]}
tonic = { workspace = true, optional = true, features = ["prost", "codegen"] }

# - Secret management
secrecy.workspace = true
Expand All @@ -78,6 +78,10 @@ group.workspace = true
orchard = { workspace = true, optional = true }
sapling.workspace = true

# - Sync engine
async-trait = { version = "0.1", optional = true }
futures-util = { version = "0.3", optional = true }

# - Note commitment trees
incrementalmerkletree.workspace = true
shardtree.workspace = true
Expand All @@ -89,9 +93,6 @@ jubjub = { workspace = true, optional = true }
# - ZIP 321
nom = "7"

# - Asychronous
async-trait = "0.1.78"

# Dependencies used internally:
# (Breaking upgrades to these are usually backwards-compatible, but check MSRVs.)
# - Documentation
Expand All @@ -106,7 +107,7 @@ crossbeam-channel.workspace = true
rayon.workspace = true

[build-dependencies]
tonic-build = { workspace = true, features = ["prost"]}
tonic-build = { workspace = true, features = ["prost"] }
which = "4"

[dev-dependencies]
Expand Down Expand Up @@ -141,6 +142,13 @@ transparent-inputs = [
## Enables receiving and spending Orchard funds.
orchard = ["dep:orchard", "zcash_keys/orchard"]

## Exposes a wallet synchronization function that implements the necessary state machine.
sync = [
"lightwalletd-tonic",
"dep:async-trait",
"dep:futures-util",
]

## Exposes APIs that are useful for testing, such as `proptest` strategies.
test-dependencies = [
"dep:proptest",
Expand Down
25 changes: 15 additions & 10 deletions zcash_client_backend/src/data_api/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,8 @@
//! # }
//! ```

use std::ops::{Add, Range};
use std::ops::Range;

use async_trait::async_trait;
use incrementalmerkletree::frontier::Frontier;
use subtle::ConditionallySelectable;
use zcash_primitives::{
Expand All @@ -162,15 +161,20 @@
};

use crate::{
data_api::{scanning::ScanRange, NullifierQuery, WalletWrite},
data_api::{NullifierQuery, WalletWrite},
proto::compact_formats::CompactBlock,
scanning::{scan_block_with_runners, BatchRunners, Nullifiers, ScanningKeys},
};

#[cfg(feature = "sync")]
use {
super::scanning::ScanPriority, crate::data_api::scanning::ScanRange, async_trait::async_trait,
};

pub mod error;
use error::Error;

use super::{scanning::ScanPriority, WalletRead};
use super::WalletRead;

/// A struct containing metadata about a subtree root of the note commitment tree.
///
Expand Down Expand Up @@ -300,7 +304,7 @@
/// Ok(())
/// }
///
/// async fn delete(&self, range: &ScanRange) -> Result<(), Self::Error> {
/// async fn delete(&self, range: ScanRange) -> Result<(), Self::Error> {
/// self.cached_blocks
/// .lock()
/// .unwrap()
Expand Down Expand Up @@ -368,11 +372,12 @@
///
/// // Delete blocks from the block cache
/// rt.block_on(async {
/// block_cache.delete(&range).await.unwrap();
/// block_cache.delete(range).await.unwrap();
/// });
/// assert_eq!(block_cache.cached_blocks.lock().unwrap().len(), 0);
/// assert_eq!(block_cache.get_tip_height(None).unwrap(), None);
/// ```
#[cfg(feature = "sync")]
#[async_trait]
pub trait BlockCache: BlockSource + Send + Sync
where
Expand Down Expand Up @@ -404,10 +409,10 @@
/// Removes all cached blocks above a specified block height.
async fn truncate(&self, block_height: BlockHeight) -> Result<(), Self::Error> {
if let Some(latest) = self.get_tip_height(None)? {
self.delete(&ScanRange::from_parts(
self.delete(ScanRange::from_parts(

Check warning on line 412 in zcash_client_backend/src/data_api/chain.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_backend/src/data_api/chain.rs#L412

Added line #L412 was not covered by tests
Range {
start: block_height.add(1),
end: latest.add(1),
start: block_height + 1,
end: latest + 1,

Check warning on line 415 in zcash_client_backend/src/data_api/chain.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_backend/src/data_api/chain.rs#L414-L415

Added lines #L414 - L415 were not covered by tests
},
ScanPriority::Ignored,
))
Expand All @@ -421,7 +426,7 @@
/// # Errors
///
/// In the case of an error, some blocks requested for deletion may remain in the block cache.
async fn delete(&self, range: &ScanRange) -> Result<(), Self::Error>;
async fn delete(&self, range: ScanRange) -> Result<(), Self::Error>;
}

/// Metadata about modifications to the wallet state made in the course of scanning a set of
Expand Down
3 changes: 3 additions & 0 deletions zcash_client_backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ pub mod scanning;
pub mod wallet;
pub mod zip321;

#[cfg(feature = "sync")]
pub mod sync;

#[cfg(feature = "unstable-serialization")]
pub mod serialization;

Expand Down
Loading
Loading