Skip to content

Commit

Permalink
added sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
ec2 committed Sep 13, 2024
1 parent 507edfa commit b5963a4
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 1 deletion.
163 changes: 163 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ default = ["native"]

wasm = ["console_error_panic_hook", "dep:tracing-web"]
native = ["dep:tokio", "tonic/channel", "tonic/gzip", "tonic/tls-webpki-roots"]

sqlite-db = ["dep:zcash_client_sqlite"]
console_error_panic_hook = ["dep:console_error_panic_hook"]

[dependencies]
Expand Down Expand Up @@ -55,6 +55,7 @@ tonic = { version = "0.12", default-features = false, features = [

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

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

[dev-dependencies]
wasm-bindgen-test = "0.3.42"
tempfile = "3.12"

[patch.crates-io]
zip32 = { git = "https://github.com/zcash/zip32.git", branch = "diversifier_index_ord"}
3 changes: 3 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ test-web:
test-native:
cargo test -r -- --nocapture

test-sqlite:
cargo test -r --features="sqlite-db" test_get_and_scan_range_native_sqlite -- --nocapture

check:
cargo check
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ pub enum Error {
InvalidAmount(#[from] zcash_primitives::transaction::components::amount::BalanceError),
#[error("Failed to send transaction")]
SendFailed { code: i32, reason: String },

#[cfg(feature = "sqlite-db")]
#[error("Sqlite error: {0}")]
SqliteError(#[from] zcash_client_sqlite::error::SqliteClientError),
}

impl From<Error> for JsValue {
Expand Down
59 changes: 59 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,62 @@ async fn test_get_and_scan_range_native() {
let summary = w.get_wallet_summary().unwrap();
tracing::info!("Wallet summary: {:?}", summary);
}

#[cfg(all(feature = "native", feature = "sqlite-db"))]
#[tokio::test]
async fn test_get_and_scan_range_native_sqlite() {
use tempfile::{tempdir, NamedTempFile};
use zcash_client_sqlite::{
chain::init::init_blockmeta_db, wallet::init::init_wallet_db, FsBlockDb, WalletDb,
};
use zcash_primitives::consensus;

initialize();
let url = "https://testnet.zec.rocks:443";
let c = tonic::transport::Channel::from_shared(url).unwrap();

let tls = tonic::transport::ClientTlsConfig::new()
.domain_name("testnet.zec.rocks")
.with_webpki_roots();
let channel = c.tls_config(tls).unwrap();

let db_cache = tempdir().unwrap();
let db_data = NamedTempFile::new_in(db_cache.path()).unwrap();

let mut db_cache = FsBlockDb::for_path(&db_cache).unwrap();
let mut wallet_db = WalletDb::for_path(&db_data, consensus::Network::TestNetwork).unwrap();
init_blockmeta_db(&mut db_cache).unwrap();
init_wallet_db(&mut wallet_db, None).unwrap();

let mut w = Wallet::new(
wallet_db,
channel.connect().await.unwrap(),
Network::TestNetwork,
NonZeroU32::try_from(1).unwrap(),
)
.unwrap();

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

tracing::info!("Syncing wallet");
w.sync(|scanned_to, tip| {
println!("Scanned: {}/{}", scanned_to, tip);
})
.await
.unwrap();

tracing::info!("Syncing complete :)");

let summary = w.get_wallet_summary().unwrap();
tracing::info!("Wallet summary: {:?}", summary);

tracing::info!("Proposing a transaction");
let addr = ZcashAddress::try_from_encoded("utest1z00xn09t4eyeqw9zmjss75sf460423dymgyfjn8rtlj26cffy0yad3eea82xekk24s00wnm38cvyrm2c6x7fxlc0ns4a5j7utgl6lchvglfvl9g9p56fqwzvzvj9d3z6r6ft88j654d7dj0ep6myq5duz9s8x78fdzmtx04d2qn8ydkxr4lfdhlkx9ktrw98gd97dateegrr68vl8xu");

w.transfer(SEED, 0, addr.unwrap(), 1000).await.unwrap();
tracing::info!("Transaction proposed");

let summary = w.get_wallet_summary().unwrap();
tracing::info!("Wallet summary: {:?}", summary);
}

0 comments on commit b5963a4

Please sign in to comment.