Skip to content

Commit 7642ef2

Browse files
committed
move native test to example
1 parent fbb8e2d commit 7642ef2

File tree

4 files changed

+93
-77
lines changed

4 files changed

+93
-77
lines changed

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ edition = "2021"
1010
[lib]
1111
crate-type = ["cdylib", "rlib"]
1212

13+
[[example]]
14+
name = "simple-sync"
15+
path = "examples/simple-sync.rs"
16+
1317
[profile.release]
1418
# Tell `rustc` to optimize for small code size.
1519
opt-level = 3
@@ -54,7 +58,7 @@ tonic = { version = "0.12", default-features = false, features = [
5458
] }
5559

5660
# Used in Native tests
57-
tokio = { version = "1.0", features = ["rt", "macros"], optional = true }
61+
tokio = { version = "1.0", features = ["rt", "macros", "rt-multi-thread"], optional = true }
5862
zcash_client_sqlite = { git = "https://github.com/ChainSafe/librustzcash", rev = "a77e8a0204dab421fdbf5822e585716339567b96", default-features = false, features = ["unstable", "orchard"], optional = true }
5963

6064
getrandom = { version = "0.2", features = ["js"] }

examples/simple-sync.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use std::sync::Once;
2+
3+
use std::num::NonZeroU32;
4+
use webz_core::Wallet;
5+
use zcash_address::ZcashAddress;
6+
use zcash_primitives::consensus::Network;
7+
8+
const SEED: &str = "visit armed kite pen cradle toward reward clay marble oil write dove blind oyster silk oyster original message skate bench tone enable stadium element";
9+
const HD_INDEX: u32 = 0;
10+
const BIRTHDAY: Option<u32> = Some(2577329);
11+
12+
static INIT: Once = Once::new();
13+
pub fn initialize() {
14+
INIT.call_once(|| {
15+
webz_core::init::start();
16+
});
17+
}
18+
19+
#[cfg(feature = "native")]
20+
#[tokio::main]
21+
async fn main() {
22+
let db_cache = tempfile::tempdir().unwrap();
23+
let _db_data = tempfile::NamedTempFile::new_in(db_cache.path()).unwrap();
24+
25+
initialize();
26+
let url = "https://testnet.zec.rocks:443";
27+
let c = tonic::transport::Channel::from_shared(url).unwrap();
28+
29+
let tls = tonic::transport::ClientTlsConfig::new()
30+
.domain_name("testnet.zec.rocks")
31+
.with_webpki_roots();
32+
let channel = c.tls_config(tls).unwrap();
33+
34+
#[cfg(feature = "sqlite-db")]
35+
let wallet_db = {
36+
use zcash_client_sqlite::{
37+
chain::init::init_blockmeta_db, wallet::init::init_wallet_db, FsBlockDb, WalletDb,
38+
};
39+
40+
let mut db_cache = FsBlockDb::for_path(&db_cache).unwrap();
41+
let mut wallet_db = WalletDb::for_path(&_db_data, Network::TestNetwork).unwrap();
42+
init_blockmeta_db(&mut db_cache).unwrap();
43+
init_wallet_db(&mut wallet_db, None).unwrap();
44+
wallet_db
45+
};
46+
47+
#[cfg(not(feature = "sqlite-db"))]
48+
let wallet_db =
49+
zcash_client_memory::MemoryWalletDb::new(Network::TestNetwork, webz_core::PRUNING_DEPTH);
50+
51+
let mut w = Wallet::new(
52+
wallet_db,
53+
channel.connect().await.unwrap(),
54+
Network::TestNetwork,
55+
NonZeroU32::try_from(1).unwrap(),
56+
)
57+
.unwrap();
58+
59+
let id = w.create_account(SEED, HD_INDEX, BIRTHDAY).await.unwrap();
60+
tracing::info!("Created account with id: {}", id);
61+
62+
tracing::info!("Syncing wallet");
63+
w.sync(|scanned_to, tip| {
64+
println!("Scanned: {}/{}", scanned_to, tip);
65+
})
66+
.await
67+
.unwrap();
68+
69+
tracing::info!("Syncing complete :)");
70+
71+
let summary = w.get_wallet_summary().unwrap();
72+
tracing::info!("Wallet summary: {:?}", summary);
73+
74+
tracing::info!("Proposing a transaction");
75+
let addr = ZcashAddress::try_from_encoded("utest1z00xn09t4eyeqw9zmjss75sf460423dymgyfjn8rtlj26cffy0yad3eea82xekk24s00wnm38cvyrm2c6x7fxlc0ns4a5j7utgl6lchvglfvl9g9p56fqwzvzvj9d3z6r6ft88j654d7dj0ep6myq5duz9s8x78fdzmtx04d2qn8ydkxr4lfdhlkx9ktrw98gd97dateegrr68vl8xu");
76+
77+
w.transfer(SEED, 0, addr.unwrap(), 1000).await.unwrap();
78+
tracing::info!("Transaction proposed");
79+
80+
let summary = w.get_wallet_summary().unwrap();
81+
tracing::info!("Wallet summary: {:?}", summary);
82+
}

justfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ build:
77
test-web:
88
WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features="wasm" -Z build-std="panic_abort,std"
99

10-
test-native:
11-
cargo test -r -- --nocapture
10+
run-example-native:
11+
cargo run -r --example simple-sync
1212

13-
test-sqlite:
14-
cargo test -r --features="sqlite-db" -- --nocapture
13+
run-example-sqlite:
14+
cargo run -r --example simple-sync --features="sqlite-db"
1515

1616
check:
1717
cargo check

tests/tests.rs

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
use wasm_bindgen_test::*;
22
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
33

4-
use webz_core::{bindgen::wallet::WebWallet, Wallet};
5-
use zcash_address::ZcashAddress;
6-
use zcash_primitives::consensus::Network;
4+
use std::sync::Once;
5+
use webz_core::bindgen::wallet::WebWallet;
76

87
const SEED: &str = "visit armed kite pen cradle toward reward clay marble oil write dove blind oyster silk oyster original message skate bench tone enable stadium element";
98
const HD_INDEX: u32 = 0;
109
const BIRTHDAY: Option<u32> = Some(2577329);
1110

1211
// Required to initialize the logger and panic hooks only once
13-
use std::{num::NonZeroU32, sync::Once};
1412
static INIT: Once = Once::new();
1513
pub fn initialize() {
1614
INIT.call_once(|| {
@@ -46,71 +44,3 @@ async fn test_get_and_scan_range() {
4644
let summary = w.get_wallet_summary().unwrap();
4745
tracing::info!("Wallet summary: {:?}", summary);
4846
}
49-
50-
#[cfg(feature = "native")]
51-
#[tokio::test]
52-
async fn test_get_and_scan_range_native() {
53-
use zcash_primitives::consensus;
54-
let db_cache = tempfile::tempdir().unwrap();
55-
let _db_data = tempfile::NamedTempFile::new_in(db_cache.path()).unwrap();
56-
57-
initialize();
58-
let url = "https://testnet.zec.rocks:443";
59-
let c = tonic::transport::Channel::from_shared(url).unwrap();
60-
61-
let tls = tonic::transport::ClientTlsConfig::new()
62-
.domain_name("testnet.zec.rocks")
63-
.with_webpki_roots();
64-
let channel = c.tls_config(tls).unwrap();
65-
66-
#[cfg(feature = "sqlite-db")]
67-
let wallet_db = {
68-
use zcash_client_sqlite::{
69-
chain::init::init_blockmeta_db, wallet::init::init_wallet_db, FsBlockDb, WalletDb,
70-
};
71-
72-
let mut db_cache = FsBlockDb::for_path(&db_cache).unwrap();
73-
let mut wallet_db = WalletDb::for_path(&_db_data, consensus::Network::TestNetwork).unwrap();
74-
init_blockmeta_db(&mut db_cache).unwrap();
75-
init_wallet_db(&mut wallet_db, None).unwrap();
76-
wallet_db
77-
};
78-
79-
#[cfg(not(feature = "sqlite-db"))]
80-
let wallet_db = zcash_client_memory::MemoryWalletDb::new(
81-
consensus::Network::TestNetwork,
82-
webz_core::PRUNING_DEPTH,
83-
);
84-
85-
let mut w = Wallet::new(
86-
wallet_db,
87-
channel.connect().await.unwrap(),
88-
Network::TestNetwork,
89-
NonZeroU32::try_from(1).unwrap(),
90-
)
91-
.unwrap();
92-
93-
let id = w.create_account(SEED, HD_INDEX, BIRTHDAY).await.unwrap();
94-
tracing::info!("Created account with id: {}", id);
95-
96-
tracing::info!("Syncing wallet");
97-
w.sync(|scanned_to, tip| {
98-
println!("Scanned: {}/{}", scanned_to, tip);
99-
})
100-
.await
101-
.unwrap();
102-
103-
tracing::info!("Syncing complete :)");
104-
105-
let summary = w.get_wallet_summary().unwrap();
106-
tracing::info!("Wallet summary: {:?}", summary);
107-
108-
tracing::info!("Proposing a transaction");
109-
let addr = ZcashAddress::try_from_encoded("utest1z00xn09t4eyeqw9zmjss75sf460423dymgyfjn8rtlj26cffy0yad3eea82xekk24s00wnm38cvyrm2c6x7fxlc0ns4a5j7utgl6lchvglfvl9g9p56fqwzvzvj9d3z6r6ft88j654d7dj0ep6myq5duz9s8x78fdzmtx04d2qn8ydkxr4lfdhlkx9ktrw98gd97dateegrr68vl8xu");
110-
111-
w.transfer(SEED, 0, addr.unwrap(), 1000).await.unwrap();
112-
tracing::info!("Transaction proposed");
113-
114-
let summary = w.get_wallet_summary().unwrap();
115-
tracing::info!("Wallet summary: {:?}", summary);
116-
}

0 commit comments

Comments
 (0)