Skip to content

Commit b89fd3d

Browse files
committed
break out fetch_blocks
1 parent ff7e2af commit b89fd3d

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

justfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
default:
22
just --list
33

4+
5+
6+
47
build:
58
wasm-pack build -t web --release --out-dir ./packages/webz-core -Z --no-default-features --features="wasm-parallel,no-bundler" build-std="panic_abort,std"
69

@@ -10,5 +13,13 @@ test-web:
1013
test-native:
1114
cargo test -r -- --nocapture
1215

16+
alias c := check
17+
1318
check:
1419
cargo check
20+
21+
alias cw := check-wasm
22+
23+
check-wasm:
24+
cargo check --no-default-features --features="wasm-parallel,no-bundler" --target=wasm32-unknown-unknown
25+

src/wallet.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use std::num::NonZeroU32;
22

33
use bip0039::{English, Mnemonic};
4-
use futures_util::{StreamExt, TryStreamExt};
4+
use futures_util::{Stream, StreamExt, TryStreamExt};
55
use nonempty::NonEmpty;
66
use secrecy::{ExposeSecret, SecretVec, Zeroize};
77
use tonic::{
88
client::GrpcService,
99
codegen::{Body, Bytes, StdError},
10+
Status,
1011
};
1112

1213
use zcash_address::ZcashAddress;
13-
use zcash_client_backend::data_api::scanning::ScanRange;
1414
use zcash_client_backend::data_api::wallet::{
1515
create_proposed_transactions, input_selection::GreedyInputSelector, propose_transfer,
1616
};
@@ -26,6 +26,7 @@ use zcash_client_backend::scanning::{scan_block, Nullifiers, ScanningKeys};
2626
use zcash_client_backend::wallet::OvkPolicy;
2727
use zcash_client_backend::zip321::{Payment, TransactionRequest};
2828
use zcash_client_backend::ShieldedProtocol;
29+
use zcash_client_backend::{data_api::scanning::ScanRange, proto::compact_formats::CompactBlock};
2930
use zcash_client_memory::MemoryWalletDb;
3031
use zcash_keys::keys::UnifiedSpendingKey;
3132
use zcash_primitives::consensus::{self, BlockHeight, Network};
@@ -200,6 +201,27 @@ where
200201
Ok(())
201202
}
202203

204+
async fn fetch_blocks(
205+
&mut self,
206+
start: u32,
207+
end: u32,
208+
) -> Result<impl StreamExt<Item = Result<CompactBlock, Status>>, Error> {
209+
let range = service::BlockRange {
210+
start: Some(service::BlockId {
211+
height: start.into(),
212+
..Default::default()
213+
}),
214+
end: Some(service::BlockId {
215+
height: end.into(),
216+
..Default::default()
217+
}),
218+
};
219+
220+
let blocks = self.client.get_block_range(range).await?.into_inner();
221+
222+
Ok(blocks)
223+
}
224+
203225
/// Download and process all blocks in the given range
204226
async fn fetch_and_scan_range(&mut self, start: u32, end: u32) -> Result<(), Error> {
205227
// get the chainstate prior to the range
@@ -222,24 +244,11 @@ where
222244
self.db.get_orchard_nullifiers(NullifierQuery::Unspent)?,
223245
);
224246

225-
let range = service::BlockRange {
226-
start: Some(service::BlockId {
227-
height: start.into(),
228-
..Default::default()
229-
}),
230-
end: Some(service::BlockId {
231-
height: (end - 1).into(),
232-
..Default::default()
233-
}),
234-
};
235-
236247
tracing::info!("Scanning block range: {:?} to {:?}", start, end);
237248

238249
let scanned_blocks = self
239-
.client
240-
.get_block_range(range)
250+
.fetch_blocks(start, end)
241251
.await?
242-
.into_inner()
243252
.map(|compact_block| {
244253
scan_block(
245254
&self.network,
@@ -401,7 +410,7 @@ where
401410
})
402411
.unwrap();
403412

404-
tracing::info!("Transaction hex: 0x{}", hex::encode(&raw_tx.data));
413+
// tracing::info!("Transaction hex: 0x{}", hex::encode(&raw_tx.data));
405414

406415
let response = self.client.send_transaction(raw_tx).await?.into_inner();
407416

tests/tests.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ static INIT: Once = Once::new();
1919
// Required to initialize the logger and panic hooks only once
2020
use std::{num::NonZeroU32, sync::Once};
2121
pub fn initialize() {
22-
console_log!("Calling initialize");
2322
INIT.call_once(|| {
2423
webz_core::init::start();
2524
});
@@ -29,7 +28,8 @@ async fn init_threadpool(threads: usize) -> wasm_bindgen_futures::JsFuture {
2928
console_log!("Initializing thread pool with {} threads", threads);
3029
wasm_bindgen_futures::JsFuture::from(wasm_bindgen_rayon::init_thread_pool(threads))
3130
}
32-
31+
fn is_sync<T: Sync>() {}
32+
fn is_send<T: Send>() {}
3333
#[wasm_bindgen_test]
3434
async fn test_get_and_scan_range() {
3535
initialize();
@@ -38,11 +38,14 @@ async fn test_get_and_scan_range() {
3838
init_threadpool(THREADS).await;
3939

4040
let mut w = WebWallet::new("test", "https://zcash-testnet.chainsafe.dev", 1).unwrap();
41-
41+
is_sync::<WebWallet>();
42+
is_send::<WebWallet>();
4243
let id = w.create_account(SEED, HD_INDEX, BIRTHDAY).await.unwrap();
4344
tracing::info!("Created account with id: {}", id);
4445

4546
tracing::info!("Syncing wallet");
47+
// Note to self: js_sys::Function is NOT send or sync
48+
// TODO: Find a better way to do this... Perhaps passing a Stream or something instead?
4649
w.sync(&js_sys::Function::new_with_args(
4750
"scanned_to, tip",
4851
"console.log('Scanned: ', scanned_to, '/', tip)",

0 commit comments

Comments
 (0)