Skip to content

Commit

Permalink
break out fetch_blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ec2 committed Sep 13, 2024
1 parent ff7e2af commit b89fd3d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
11 changes: 11 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
default:
just --list




build:
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"

Expand All @@ -10,5 +13,13 @@ test-web:
test-native:
cargo test -r -- --nocapture

alias c := check

check:
cargo check

alias cw := check-wasm

check-wasm:
cargo check --no-default-features --features="wasm-parallel,no-bundler" --target=wasm32-unknown-unknown

43 changes: 26 additions & 17 deletions src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::num::NonZeroU32;

use bip0039::{English, Mnemonic};
use futures_util::{StreamExt, TryStreamExt};
use futures_util::{Stream, StreamExt, TryStreamExt};
use nonempty::NonEmpty;
use secrecy::{ExposeSecret, SecretVec, Zeroize};
use tonic::{
client::GrpcService,
codegen::{Body, Bytes, StdError},
Status,
};

use zcash_address::ZcashAddress;
use zcash_client_backend::data_api::scanning::ScanRange;
use zcash_client_backend::data_api::wallet::{
create_proposed_transactions, input_selection::GreedyInputSelector, propose_transfer,
};
Expand All @@ -26,6 +26,7 @@ use zcash_client_backend::scanning::{scan_block, Nullifiers, ScanningKeys};
use zcash_client_backend::wallet::OvkPolicy;
use zcash_client_backend::zip321::{Payment, TransactionRequest};
use zcash_client_backend::ShieldedProtocol;
use zcash_client_backend::{data_api::scanning::ScanRange, proto::compact_formats::CompactBlock};
use zcash_client_memory::MemoryWalletDb;
use zcash_keys::keys::UnifiedSpendingKey;
use zcash_primitives::consensus::{self, BlockHeight, Network};
Expand Down Expand Up @@ -200,6 +201,27 @@ where
Ok(())
}

async fn fetch_blocks(
&mut self,
start: u32,
end: u32,
) -> Result<impl StreamExt<Item = Result<CompactBlock, Status>>, Error> {
let range = service::BlockRange {
start: Some(service::BlockId {
height: start.into(),
..Default::default()
}),
end: Some(service::BlockId {
height: end.into(),
..Default::default()
}),
};

let blocks = self.client.get_block_range(range).await?.into_inner();

Ok(blocks)
}

/// Download and process all blocks in the given range
async fn fetch_and_scan_range(&mut self, start: u32, end: u32) -> Result<(), Error> {
// get the chainstate prior to the range
Expand All @@ -222,24 +244,11 @@ where
self.db.get_orchard_nullifiers(NullifierQuery::Unspent)?,
);

let range = service::BlockRange {
start: Some(service::BlockId {
height: start.into(),
..Default::default()
}),
end: Some(service::BlockId {
height: (end - 1).into(),
..Default::default()
}),
};

tracing::info!("Scanning block range: {:?} to {:?}", start, end);

let scanned_blocks = self
.client
.get_block_range(range)
.fetch_blocks(start, end)
.await?
.into_inner()
.map(|compact_block| {
scan_block(
&self.network,
Expand Down Expand Up @@ -401,7 +410,7 @@ where
})
.unwrap();

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

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

Expand Down
9 changes: 6 additions & 3 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ static INIT: Once = Once::new();
// Required to initialize the logger and panic hooks only once
use std::{num::NonZeroU32, sync::Once};
pub fn initialize() {
console_log!("Calling initialize");
INIT.call_once(|| {
webz_core::init::start();
});
Expand All @@ -29,7 +28,8 @@ async fn init_threadpool(threads: usize) -> wasm_bindgen_futures::JsFuture {
console_log!("Initializing thread pool with {} threads", threads);
wasm_bindgen_futures::JsFuture::from(wasm_bindgen_rayon::init_thread_pool(threads))
}

fn is_sync<T: Sync>() {}
fn is_send<T: Send>() {}
#[wasm_bindgen_test]
async fn test_get_and_scan_range() {
initialize();
Expand All @@ -38,11 +38,14 @@ async fn test_get_and_scan_range() {
init_threadpool(THREADS).await;

let mut w = WebWallet::new("test", "https://zcash-testnet.chainsafe.dev", 1).unwrap();

is_sync::<WebWallet>();
is_send::<WebWallet>();
let id = w.create_account(SEED, HD_INDEX, BIRTHDAY).await.unwrap();
tracing::info!("Created account with id: {}", id);

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

0 comments on commit b89fd3d

Please sign in to comment.