Skip to content

Commit b5963a4

Browse files
committed
added sqlite
1 parent 507edfa commit b5963a4

File tree

5 files changed

+232
-1
lines changed

5 files changed

+232
-1
lines changed

Cargo.lock

Lines changed: 163 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ default = ["native"]
2424

2525
wasm = ["console_error_panic_hook", "dep:tracing-web"]
2626
native = ["dep:tokio", "tonic/channel", "tonic/gzip", "tonic/tls-webpki-roots"]
27-
27+
sqlite-db = ["dep:zcash_client_sqlite"]
2828
console_error_panic_hook = ["dep:console_error_panic_hook"]
2929

3030
[dependencies]
@@ -55,6 +55,7 @@ tonic = { version = "0.12", default-features = false, features = [
5555

5656
# Used in Native tests
5757
tokio = { version = "1.0", features = ["rt", "macros"], optional = true }
58+
zcash_client_sqlite = { git = "https://github.com/ChainSafe/librustzcash", rev = "a77e8a0204dab421fdbf5822e585716339567b96", default-features = false, features = ["unstable", "orchard"], optional = true }
5859

5960
getrandom = { version = "0.2", features = ["js"] }
6061
thiserror = "1.0.63"
@@ -72,6 +73,7 @@ subtle = "2.6.1"
7273

7374
[dev-dependencies]
7475
wasm-bindgen-test = "0.3.42"
76+
tempfile = "3.12"
7577

7678
[patch.crates-io]
7779
zip32 = { git = "https://github.com/zcash/zip32.git", branch = "diversifier_index_ord"}

justfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ test-web:
1010
test-native:
1111
cargo test -r -- --nocapture
1212

13+
test-sqlite:
14+
cargo test -r --features="sqlite-db" test_get_and_scan_range_native_sqlite -- --nocapture
15+
1316
check:
1417
cargo check

src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ pub enum Error {
4444
InvalidAmount(#[from] zcash_primitives::transaction::components::amount::BalanceError),
4545
#[error("Failed to send transaction")]
4646
SendFailed { code: i32, reason: String },
47+
48+
#[cfg(feature = "sqlite-db")]
49+
#[error("Sqlite error: {0}")]
50+
SqliteError(#[from] zcash_client_sqlite::error::SqliteClientError),
4751
}
4852

4953
impl From<Error> for JsValue {

tests/tests.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,62 @@ async fn test_get_and_scan_range_native() {
9595
let summary = w.get_wallet_summary().unwrap();
9696
tracing::info!("Wallet summary: {:?}", summary);
9797
}
98+
99+
#[cfg(all(feature = "native", feature = "sqlite-db"))]
100+
#[tokio::test]
101+
async fn test_get_and_scan_range_native_sqlite() {
102+
use tempfile::{tempdir, NamedTempFile};
103+
use zcash_client_sqlite::{
104+
chain::init::init_blockmeta_db, wallet::init::init_wallet_db, FsBlockDb, WalletDb,
105+
};
106+
use zcash_primitives::consensus;
107+
108+
initialize();
109+
let url = "https://testnet.zec.rocks:443";
110+
let c = tonic::transport::Channel::from_shared(url).unwrap();
111+
112+
let tls = tonic::transport::ClientTlsConfig::new()
113+
.domain_name("testnet.zec.rocks")
114+
.with_webpki_roots();
115+
let channel = c.tls_config(tls).unwrap();
116+
117+
let db_cache = tempdir().unwrap();
118+
let db_data = NamedTempFile::new_in(db_cache.path()).unwrap();
119+
120+
let mut db_cache = FsBlockDb::for_path(&db_cache).unwrap();
121+
let mut wallet_db = WalletDb::for_path(&db_data, consensus::Network::TestNetwork).unwrap();
122+
init_blockmeta_db(&mut db_cache).unwrap();
123+
init_wallet_db(&mut wallet_db, None).unwrap();
124+
125+
let mut w = Wallet::new(
126+
wallet_db,
127+
channel.connect().await.unwrap(),
128+
Network::TestNetwork,
129+
NonZeroU32::try_from(1).unwrap(),
130+
)
131+
.unwrap();
132+
133+
let id = w.create_account(SEED, HD_INDEX, BIRTHDAY).await.unwrap();
134+
tracing::info!("Created account with id: {}", id);
135+
136+
tracing::info!("Syncing wallet");
137+
w.sync(|scanned_to, tip| {
138+
println!("Scanned: {}/{}", scanned_to, tip);
139+
})
140+
.await
141+
.unwrap();
142+
143+
tracing::info!("Syncing complete :)");
144+
145+
let summary = w.get_wallet_summary().unwrap();
146+
tracing::info!("Wallet summary: {:?}", summary);
147+
148+
tracing::info!("Proposing a transaction");
149+
let addr = ZcashAddress::try_from_encoded("utest1z00xn09t4eyeqw9zmjss75sf460423dymgyfjn8rtlj26cffy0yad3eea82xekk24s00wnm38cvyrm2c6x7fxlc0ns4a5j7utgl6lchvglfvl9g9p56fqwzvzvj9d3z6r6ft88j654d7dj0ep6myq5duz9s8x78fdzmtx04d2qn8ydkxr4lfdhlkx9ktrw98gd97dateegrr68vl8xu");
150+
151+
w.transfer(SEED, 0, addr.unwrap(), 1000).await.unwrap();
152+
tracing::info!("Transaction proposed");
153+
154+
let summary = w.get_wallet_summary().unwrap();
155+
tracing::info!("Wallet summary: {:?}", summary);
156+
}

0 commit comments

Comments
 (0)