1
1
use std:: num:: NonZeroU32 ;
2
2
3
3
use bip0039:: { English , Mnemonic } ;
4
- use futures_util:: { StreamExt , TryStreamExt } ;
5
4
use nonempty:: NonEmpty ;
6
5
use secrecy:: { ExposeSecret , SecretVec , Zeroize } ;
7
6
use tonic:: {
@@ -22,23 +21,21 @@ use zcash_address::ZcashAddress;
22
21
use zcash_client_backend:: data_api:: wallet:: {
23
22
create_proposed_transactions, input_selection:: GreedyInputSelector , propose_transfer,
24
23
} ;
25
- use zcash_client_backend:: data_api:: { scanning :: ScanRange , WalletCommitmentTrees } ;
24
+ use zcash_client_backend:: data_api:: WalletCommitmentTrees ;
26
25
use zcash_client_backend:: data_api:: {
27
- AccountBirthday , AccountPurpose , InputSource , NullifierQuery , WalletRead , WalletSummary ,
28
- WalletWrite ,
26
+ AccountBirthday , AccountPurpose , InputSource , WalletRead , WalletSummary , WalletWrite ,
29
27
} ;
30
28
use zcash_client_backend:: fees:: zip317:: SingleOutputChangeStrategy ;
31
29
use zcash_client_backend:: proposal:: Proposal ;
32
30
use zcash_client_backend:: proto:: service:: {
33
31
self , compact_tx_streamer_client:: CompactTxStreamerClient ,
34
32
} ;
35
- use zcash_client_backend:: scanning:: { scan_block, Nullifiers , ScanningKeys } ;
36
33
use zcash_client_backend:: wallet:: OvkPolicy ;
37
34
use zcash_client_backend:: zip321:: { Payment , TransactionRequest } ;
38
35
use zcash_client_backend:: ShieldedProtocol ;
39
36
use zcash_client_memory:: { MemBlockCache , MemoryWalletDb } ;
40
37
use zcash_keys:: keys:: { UnifiedFullViewingKey , UnifiedSpendingKey } ;
41
- use zcash_primitives:: consensus:: { self , BlockHeight , Network } ;
38
+ use zcash_primitives:: consensus:: { self , Network } ;
42
39
use zcash_primitives:: transaction:: components:: amount:: NonNegativeAmount ;
43
40
use zcash_primitives:: transaction:: fees:: zip317:: FeeRule ;
44
41
use zcash_primitives:: transaction:: TxId ;
@@ -228,7 +225,7 @@ where
228
225
} ) ?)
229
226
}
230
227
231
- pub async fn sync2 ( & self ) -> Result < ( ) , Error > {
228
+ pub async fn sync ( & self ) -> Result < ( ) , Error > {
232
229
let mut client = self . client . clone ( ) ;
233
230
// TODO: This should be held in the Wallet struct so we can download in parallel
234
231
let db_cache = MemBlockCache :: new ( ) ;
@@ -245,113 +242,6 @@ where
245
242
. map_err ( Into :: into)
246
243
}
247
244
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
-
355
245
pub async fn get_wallet_summary ( & self ) -> Result < Option < WalletSummary < AccountId > > , Error > {
356
246
Ok ( self
357
247
. db
@@ -360,25 +250,6 @@ where
360
250
. get_wallet_summary ( self . min_confirmations . into ( ) ) ?)
361
251
}
362
252
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
-
382
253
///
383
254
/// Create a transaction proposal to send funds from the wallet to a given address
384
255
///
0 commit comments