Skip to content

Commit 443ad17

Browse files
committed
remove the legacy sync algo
1 parent 2152bd9 commit 443ad17

File tree

6 files changed

+17
-188
lines changed

6 files changed

+17
-188
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ native = ["tonic/channel", "tonic/gzip", "tonic/tls-webpki-roots", "tokio/macros
4141
sqlite-db = ["dep:zcash_client_sqlite"]
4242
console_error_panic_hook = ["dep:console_error_panic_hook"]
4343
no-bundler = ["wasm-bindgen-rayon?/no-bundler", "wasm_thread/no-bundler"]
44-
sync2 = []
4544

4645
[dependencies]
4746
## Web dependencies

justfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@ default:
22
just --list
33

44
build *features:
5-
wasm-pack build --no-opt -t web --scope webzjs --release --out-dir ./packages/webz-core --no-default-features --features="wasm wasm-parallel sync2 {{features}}" -Z build-std="panic_abort,std"
5+
wasm-pack build --no-opt -t web --scope webzjs --release --out-dir ./packages/webz-core --no-default-features --features="wasm wasm-parallel {{features}}" -Z build-std="panic_abort,std"
66

77
# All Wasm Tests
88
test-web *features:
99
WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features "wasm no-bundler {{features}}" -Z build-std="panic_abort,std"
1010

11-
# sync message board in the web: addigional args: sync2
11+
# sync message board in the web: addigional args:
1212
test-message-board-web *features:
1313
WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --chrome --no-default-features --features "wasm no-bundler {{features}}" -Z build-std="panic_abort,std" --test message-board-sync
1414

15-
# simple example in the web: additional args: sync2
15+
# simple example in the web: additional args:
1616
test-simple-web *features:
1717
WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --chrome --no-default-features --features "wasm no-bundler {{features}}" -Z build-std="panic_abort,std" --test simple-sync-and-send
1818

1919

20-
# simple example: additional args: sync2, sqlite-db
20+
# simple example: additional args:, sqlite-db
2121
example-simple *features:
2222
RUST_LOG="info,zcash_client_backend::sync=debug" cargo run -r --example simple-sync --features "native {{features}}"
2323

24-
# sync the message board: additional args: sync2, sqlite-db
24+
# sync the message board: additional args:, sqlite-db
2525
example-message-board *features:
2626
RUST_LOG=info,zcash_client_backend::sync=debug cargo run -r --example message-board-sync --features "native {{features}}"
2727

src/bindgen/wallet.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,31 +117,14 @@ impl WebWallet {
117117
self.inner.suggest_scan_ranges().await
118118
}
119119

120-
/// Synchronize the wallet with the blockchain up to the tip
121-
/// The passed callback will be called for every batch of blocks processed with the current progress
122-
pub async fn sync(&self, callback: &js_sys::Function) -> Result<(), Error> {
123-
let callback = move |scanned_to: BlockHeight, tip: BlockHeight| {
124-
let this = JsValue::null();
125-
let _ = callback.call2(
126-
&this,
127-
&JsValue::from(Into::<u32>::into(scanned_to)),
128-
&JsValue::from(Into::<u32>::into(tip)),
129-
);
130-
};
131-
132-
self.inner.sync(callback).await?;
133-
134-
Ok(())
135-
}
136-
137120
/// Synchronize the wallet with the blockchain up to the tip using zcash_client_backend's algo
138-
pub async fn sync2(&self) -> Result<(), Error> {
121+
pub async fn sync(&self) -> Result<(), Error> {
139122
assert!(!thread::is_web_worker_thread());
140123

141124
let db = self.inner.clone();
142125

143126
let sync_handler = thread::Builder::new()
144-
.name("sync2".to_string())
127+
.name("sync".to_string())
145128
.spawn_async(|| async {
146129
assert!(thread::is_web_worker_thread());
147130
tracing::debug!(
@@ -150,7 +133,7 @@ impl WebWallet {
150133
);
151134

152135
let db = db;
153-
db.sync2().await.unwrap_throw();
136+
db.sync().await.unwrap_throw();
154137
})
155138
.unwrap_throw()
156139
.join_async();

src/wallet.rs

Lines changed: 4 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::num::NonZeroU32;
22

33
use bip0039::{English, Mnemonic};
4-
use futures_util::{StreamExt, TryStreamExt};
54
use nonempty::NonEmpty;
65
use secrecy::{ExposeSecret, SecretVec, Zeroize};
76
use tonic::{
@@ -22,23 +21,21 @@ use zcash_address::ZcashAddress;
2221
use zcash_client_backend::data_api::wallet::{
2322
create_proposed_transactions, input_selection::GreedyInputSelector, propose_transfer,
2423
};
25-
use zcash_client_backend::data_api::{scanning::ScanRange, WalletCommitmentTrees};
24+
use zcash_client_backend::data_api::WalletCommitmentTrees;
2625
use zcash_client_backend::data_api::{
27-
AccountBirthday, AccountPurpose, InputSource, NullifierQuery, WalletRead, WalletSummary,
28-
WalletWrite,
26+
AccountBirthday, AccountPurpose, InputSource, WalletRead, WalletSummary, WalletWrite,
2927
};
3028
use zcash_client_backend::fees::zip317::SingleOutputChangeStrategy;
3129
use zcash_client_backend::proposal::Proposal;
3230
use zcash_client_backend::proto::service::{
3331
self, compact_tx_streamer_client::CompactTxStreamerClient,
3432
};
35-
use zcash_client_backend::scanning::{scan_block, Nullifiers, ScanningKeys};
3633
use zcash_client_backend::wallet::OvkPolicy;
3734
use zcash_client_backend::zip321::{Payment, TransactionRequest};
3835
use zcash_client_backend::ShieldedProtocol;
3936
use zcash_client_memory::{MemBlockCache, MemoryWalletDb};
4037
use zcash_keys::keys::{UnifiedFullViewingKey, UnifiedSpendingKey};
41-
use zcash_primitives::consensus::{self, BlockHeight, Network};
38+
use zcash_primitives::consensus::{self, Network};
4239
use zcash_primitives::transaction::components::amount::NonNegativeAmount;
4340
use zcash_primitives::transaction::fees::zip317::FeeRule;
4441
use zcash_primitives::transaction::TxId;
@@ -228,7 +225,7 @@ where
228225
})?)
229226
}
230227

231-
pub async fn sync2(&self) -> Result<(), Error> {
228+
pub async fn sync(&self) -> Result<(), Error> {
232229
let mut client = self.client.clone();
233230
// TODO: This should be held in the Wallet struct so we can download in parallel
234231
let db_cache = MemBlockCache::new();
@@ -245,113 +242,6 @@ where
245242
.map_err(Into::into)
246243
}
247244

248-
/// Synchronize the wallet with the blockchain up to the tip
249-
/// The passed callback will be called for every batch of blocks processed with the current progress
250-
pub async fn sync(&self, callback: impl Fn(BlockHeight, BlockHeight)) -> Result<(), Error> {
251-
let tip = self.update_chain_tip().await?;
252-
253-
tracing::info!("Retrieving suggested scan ranges from wallet");
254-
let scan_ranges = self.db.read().await.suggest_scan_ranges()?;
255-
tracing::info!("Suggested scan ranges: {:?}", scan_ranges);
256-
257-
// TODO: Ensure wallet's view of the chain tip as of the previous wallet session is valid.
258-
// See https://github.com/Electric-Coin-Company/zec-sqlite-cli/blob/8c2e49f6d3067ec6cc85248488915278c3cb1c5a/src/commands/sync.rs#L157
259-
260-
// Download and process all blocks in the requested ranges
261-
// Split each range into BATCH_SIZE chunks to avoid requesting too many blocks at once
262-
for scan_range in scan_ranges.into_iter().flat_map(|r| {
263-
// Limit the number of blocks we download and scan at any one time.
264-
(0..).scan(r, |acc, _| {
265-
if acc.is_empty() {
266-
None
267-
} else if let Some((cur, next)) = acc.split_at(acc.block_range().start + BATCH_SIZE)
268-
{
269-
*acc = next;
270-
Some(cur)
271-
} else {
272-
let cur = acc.clone();
273-
let end = acc.block_range().end;
274-
*acc = ScanRange::from_parts(end..end, acc.priority());
275-
Some(cur)
276-
}
277-
})
278-
}) {
279-
self.fetch_and_scan_range(
280-
scan_range.block_range().start.into(),
281-
scan_range.block_range().end.into(),
282-
)
283-
.await?;
284-
callback(scan_range.block_range().end, tip);
285-
}
286-
287-
Ok(())
288-
}
289-
290-
/// Download and process all blocks in the given range
291-
async fn fetch_and_scan_range(&self, start: u32, end: u32) -> Result<(), Error> {
292-
let mut client = self.client.clone();
293-
// get the chainstate prior to the range
294-
let tree_state = client
295-
.get_tree_state(service::BlockId {
296-
height: (start - 1).into(),
297-
..Default::default()
298-
})
299-
.await?;
300-
let chainstate = tree_state.into_inner().to_chain_state()?;
301-
302-
// Get the scanning keys from the DB
303-
let account_ufvks = self.db.read().await.get_unified_full_viewing_keys()?;
304-
let scanning_keys = ScanningKeys::from_account_ufvks(account_ufvks);
305-
306-
// Get the nullifiers for the unspent notes we are tracking
307-
let nullifiers = Nullifiers::new(
308-
self.db
309-
.read()
310-
.await
311-
.get_sapling_nullifiers(NullifierQuery::Unspent)?,
312-
self.db
313-
.read()
314-
.await
315-
.get_orchard_nullifiers(NullifierQuery::Unspent)?,
316-
);
317-
318-
let range = service::BlockRange {
319-
start: Some(service::BlockId {
320-
height: start.into(),
321-
..Default::default()
322-
}),
323-
end: Some(service::BlockId {
324-
height: (end - 1).into(),
325-
..Default::default()
326-
}),
327-
};
328-
329-
tracing::info!("Scanning block range: {:?} to {:?}", start, end);
330-
331-
let scanned_blocks = client
332-
.get_block_range(range)
333-
.await?
334-
.into_inner()
335-
.map(|compact_block| {
336-
scan_block(
337-
&self.network,
338-
compact_block.unwrap(),
339-
&scanning_keys,
340-
&nullifiers,
341-
None,
342-
)
343-
})
344-
.try_collect()
345-
.await?;
346-
347-
self.db
348-
.write()
349-
.await
350-
.put_blocks(&chainstate, scanned_blocks)?;
351-
352-
Ok(())
353-
}
354-
355245
pub async fn get_wallet_summary(&self) -> Result<Option<WalletSummary<AccountId>>, Error> {
356246
Ok(self
357247
.db
@@ -360,25 +250,6 @@ where
360250
.get_wallet_summary(self.min_confirmations.into())?)
361251
}
362252

363-
pub(crate) async fn update_chain_tip(&self) -> Result<BlockHeight, Error> {
364-
tracing::info!("Retrieving chain tip from lightwalletd");
365-
366-
let tip_height = self
367-
.client
368-
.clone()
369-
.get_latest_block(service::ChainSpec::default())
370-
.await?
371-
.get_ref()
372-
.height
373-
.try_into()
374-
.unwrap();
375-
376-
tracing::info!("Latest block height is {}", tip_height);
377-
self.db.write().await.update_chain_tip(tip_height)?;
378-
379-
Ok(tip_height)
380-
}
381-
382253
///
383254
/// Create a transaction proposal to send funds from the wallet to a given address
384255
///

tests/message-board-sync.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,9 @@ async fn test_message_board() {
3939
let id = w.import_ufvk(&ufvk_str, Some(2477329)).await.unwrap();
4040
tracing::info!("Created account with id: {}", id);
4141

42-
#[cfg(not(feature = "sync2"))]
43-
{
44-
tracing::info!("Syncing wallet with our sync impl");
45-
w.sync(&js_sys::Function::new_with_args(
46-
"scanned_to, tip",
47-
"console.log('Scanned: ', scanned_to, '/', tip)",
48-
))
49-
.await
50-
.unwrap();
51-
}
52-
#[cfg(feature = "sync2")]
53-
{
54-
tracing::info!("Syncing wallet with sync2");
55-
w.sync2().await.unwrap();
56-
}
42+
tracing::info!("Syncing wallet with our sync impl");
43+
w.sync().await.unwrap();
44+
5745
tracing::info!("Syncing complete :)");
5846

5947
let summary = w.get_wallet_summary().await.unwrap();

tests/simple-sync-and-send.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,8 @@ async fn test_get_and_scan_range() {
3333
let w_clone = w.clone();
3434
let w = w_clone;
3535

36-
#[cfg(not(feature = "sync2"))]
37-
{
38-
w.sync(&js_sys::Function::new_with_args(
39-
"scanned_to, tip",
40-
"console.log('Scanned: ', scanned_to, '/', tip)",
41-
))
42-
.await
43-
.unwrap();
44-
}
45-
#[cfg(feature = "sync2")]
46-
{
47-
tracing::info!("Syncing wallet with sync2");
48-
w.sync2().await.unwrap();
49-
}
36+
w.sync().await.unwrap();
37+
5038
tracing::info!("Syncing complete :)");
5139

5240
let summary = w.get_wallet_summary().await.unwrap();

0 commit comments

Comments
 (0)