Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call init_wallet_db in Wallet::open #19

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions Cargo.lock

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

11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ deadpool = "0.12"
deadpool-sqlite = "0.9"
deadpool-sync = "0.1"
rusqlite = "0.32"
schemerz = "0.2"
secrecy = "0.8"
shardtree = "0.5"
zcash_client_backend = "0.16"
Expand All @@ -76,8 +77,8 @@ tonic = "0.12"
orchard = { git = "https://github.com/zcash/orchard.git", rev = "c684e9185a0449efb00428f807d3bf286b5dae03" }
redjubjub = { git = "https://github.com/ZcashFoundation/redjubjub", rev = "eae848c5c14d9c795d000dd9f4c4762d1aee7ee1" }
sapling = { package = "sapling-crypto", git = "https://github.com/zcash/sapling-crypto.git", rev = "e607c52d13bb7ade66293f9ab8d07e311f4ad868" }
transparent = { package = "zcash_transparent", git = "https://github.com/zcash/librustzcash.git", rev = "b106a89841c493c37bc269b6b5c490727f10ed91" }
zcash_client_backend = { git = "https://github.com/zcash/librustzcash.git", rev = "b106a89841c493c37bc269b6b5c490727f10ed91" }
zcash_client_sqlite = { git = "https://github.com/zcash/librustzcash.git", rev = "b106a89841c493c37bc269b6b5c490727f10ed91" }
zcash_primitives = { git = "https://github.com/zcash/librustzcash.git", rev = "b106a89841c493c37bc269b6b5c490727f10ed91" }
zcash_protocol = { git = "https://github.com/zcash/librustzcash.git", rev = "b106a89841c493c37bc269b6b5c490727f10ed91" }
transparent = { package = "zcash_transparent", git = "https://github.com/zcash/librustzcash.git", rev = "895b298a2b042ae352a8a9f0fd4176c3db78253e" }
zcash_client_backend = { git = "https://github.com/zcash/librustzcash.git", rev = "895b298a2b042ae352a8a9f0fd4176c3db78253e" }
zcash_client_sqlite = { git = "https://github.com/zcash/librustzcash.git", rev = "895b298a2b042ae352a8a9f0fd4176c3db78253e" }
zcash_primitives = { git = "https://github.com/zcash/librustzcash.git", rev = "895b298a2b042ae352a8a9f0fd4176c3db78253e" }
zcash_protocol = { git = "https://github.com/zcash/librustzcash.git", rev = "895b298a2b042ae352a8a9f0fd4176c3db78253e" }
1 change: 1 addition & 0 deletions zallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ rand.workspace = true
rusqlite.workspace = true
rust-embed.workspace = true
sapling.workspace = true
schemerz.workspace = true
secrecy.workspace = true
serde.workspace = true
serde_json.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion zallet/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ impl StartCmd {
.into());
}

Wallet::open(path, config.network(), self.lwd_server.clone())?
info!("Opening wallet");
Wallet::open(path, config.network(), self.lwd_server.clone()).await?
};

// Launch RPC server.
Expand Down
43 changes: 36 additions & 7 deletions zallet/src/components/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::fmt;
use std::path::Path;
use std::time::Duration;

use abscissa_core::{Component, FrameworkError};
use abscissa_core::{tracing::info, Component, FrameworkError};
use abscissa_tokio::TokioComponent;
use tokio::{task::JoinHandle, time};
use tokio::{fs, task::JoinHandle, time};
use zcash_client_backend::sync;
use zcash_client_sqlite::wallet::init::{init_wallet_db, WalletMigrationError};

use crate::{
error::{Error, ErrorKind},
Expand Down Expand Up @@ -38,17 +39,45 @@ impl fmt::Debug for Wallet {
}

impl Wallet {
pub fn open(
pub async fn open(
path: impl AsRef<Path>,
params: Network,
lightwalletd_server: Servers,
) -> Result<Self, Error> {
let db_data_pool = connection::pool(path, params)?;
Ok(Self {
let wallet_exists = fs::try_exists(&path)
.await
.map_err(|e| ErrorKind::Init.context(e))?;

let wallet = Self {
params,
db_data_pool,
db_data_pool: connection::pool(path, params)?,
lightwalletd_server,
})
};

if wallet_exists {
info!("Applying latest database migrations");
} else {
info!("Creating empty database");
}
let handle = wallet.handle().await?;
handle.with_mut(|mut db_data| {
match init_wallet_db(&mut db_data, None) {
Ok(()) => Ok(()),
// TODO: Support single-seed migrations once we have key storage.
// https://github.com/zcash/wallet/issues/18
// TODO: Support multi-seed or seed-absent migrations.
// https://github.com/zcash/librustzcash/issues/1284
Err(schemerz::MigratorError::Migration {
error: WalletMigrationError::SeedRequired,
..
}) => Err(ErrorKind::Init.context("TODO: Support seed-required migrations")),
Err(e) => Err(ErrorKind::Init.context(e)),
}?;

Ok::<(), Error>(())
})?;

Ok(wallet)
}

/// Called automatically after `TokioComponent` is initialized
Expand Down
2 changes: 1 addition & 1 deletion zallet/src/components/wallet/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl WalletConnection {
})
}

fn with_mut<T>(&self, f: impl FnOnce(WalletDb<&mut rusqlite::Connection, Network>) -> T) -> T {
pub(crate) fn with_mut<T>(&self, f: impl FnOnce(WalletDb<&mut rusqlite::Connection, Network>) -> T) -> T {
tokio::task::block_in_place(|| {
f(WalletDb::from_connection(
self.inner.lock().unwrap().as_mut(),
Expand Down