|
| 1 | +use std::fmt; |
| 2 | +use std::path::PathBuf; |
| 3 | + |
| 4 | +use abscissa_core::{ |
| 5 | + component::Injectable, tracing::info, Component, FrameworkError, FrameworkErrorKind, |
| 6 | +}; |
| 7 | +use abscissa_tokio::TokioComponent; |
| 8 | +use tokio::fs; |
| 9 | +use zcash_client_sqlite::wallet::init::{init_wallet_db, WalletMigrationError}; |
| 10 | + |
| 11 | +use crate::{ |
| 12 | + application::ZalletApp, |
| 13 | + config::ZalletConfig, |
| 14 | + error::{Error, ErrorKind}, |
| 15 | +}; |
| 16 | + |
| 17 | +mod connection; |
| 18 | +pub(crate) use connection::DbConnection; |
| 19 | + |
| 20 | +pub(crate) type DbHandle = deadpool::managed::Object<connection::WalletManager>; |
| 21 | + |
| 22 | +#[derive(Clone, Default, Injectable)] |
| 23 | +#[component(inject = "init_tokio(abscissa_tokio::TokioComponent)")] |
| 24 | +pub(crate) struct Database { |
| 25 | + path: Option<PathBuf>, |
| 26 | + db_data_pool: Option<connection::WalletPool>, |
| 27 | +} |
| 28 | + |
| 29 | +impl fmt::Debug for Database { |
| 30 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 31 | + f.debug_struct("Database") |
| 32 | + .field("path", &self.path) |
| 33 | + .finish_non_exhaustive() |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl Component<ZalletApp> for Database { |
| 38 | + fn after_config(&mut self, config: &ZalletConfig) -> Result<(), FrameworkError> { |
| 39 | + let path = config.wallet_db.clone().ok_or_else(|| { |
| 40 | + FrameworkErrorKind::ComponentError |
| 41 | + .context(ErrorKind::Init.context("wallet_db must be set (for now)")) |
| 42 | + })?; |
| 43 | + if path.is_relative() { |
| 44 | + return Err(FrameworkErrorKind::ComponentError |
| 45 | + .context(ErrorKind::Init.context("wallet_db must be an absolute path (for now)")) |
| 46 | + .into()); |
| 47 | + } |
| 48 | + |
| 49 | + self.db_data_pool = Some( |
| 50 | + connection::pool(&path, config.network()) |
| 51 | + .map_err(|e| FrameworkErrorKind::ComponentError.context(e))?, |
| 52 | + ); |
| 53 | + self.path = Some(path); |
| 54 | + |
| 55 | + Ok(()) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl Database { |
| 60 | + /// Called automatically after `TokioComponent` is initialized |
| 61 | + pub fn init_tokio(&mut self, tokio_cmp: &TokioComponent) -> Result<(), FrameworkError> { |
| 62 | + let runtime = tokio_cmp.runtime()?; |
| 63 | + |
| 64 | + // Initialize the database before we go any further. |
| 65 | + runtime |
| 66 | + .block_on(async { |
| 67 | + let path = self.path.as_ref().expect("configured"); |
| 68 | + |
| 69 | + let db_exists = fs::try_exists(path) |
| 70 | + .await |
| 71 | + .map_err(|e| ErrorKind::Init.context(e))?; |
| 72 | + |
| 73 | + if db_exists { |
| 74 | + info!("Applying latest database migrations"); |
| 75 | + } else { |
| 76 | + info!("Creating empty database"); |
| 77 | + } |
| 78 | + let handle = self.handle().await?; |
| 79 | + handle.with_mut(|mut db_data| { |
| 80 | + match init_wallet_db(&mut db_data, None) { |
| 81 | + Ok(()) => Ok(()), |
| 82 | + // TODO: Support single-seed migrations once we have key storage. |
| 83 | + // https://github.com/zcash/wallet/issues/18 |
| 84 | + // TODO: Support multi-seed or seed-absent migrations. |
| 85 | + // https://github.com/zcash/librustzcash/issues/1284 |
| 86 | + Err(schemerz::MigratorError::Migration { |
| 87 | + error: WalletMigrationError::SeedRequired, |
| 88 | + .. |
| 89 | + }) => { |
| 90 | + Err(ErrorKind::Init.context("TODO: Support seed-required migrations")) |
| 91 | + } |
| 92 | + Err(e) => Err(ErrorKind::Init.context(e)), |
| 93 | + }?; |
| 94 | + |
| 95 | + Ok::<(), Error>(()) |
| 96 | + })?; |
| 97 | + |
| 98 | + Ok::<_, Error>(()) |
| 99 | + }) |
| 100 | + .map_err(|e| FrameworkErrorKind::ComponentError.context(e))?; |
| 101 | + |
| 102 | + Ok(()) |
| 103 | + } |
| 104 | + |
| 105 | + pub(crate) async fn handle(&self) -> Result<DbHandle, Error> { |
| 106 | + self.db_data_pool |
| 107 | + .as_ref() |
| 108 | + .ok_or_else(|| { |
| 109 | + ErrorKind::Init |
| 110 | + .context("Database component must be configured before calling `handle`") |
| 111 | + })? |
| 112 | + .get() |
| 113 | + .await |
| 114 | + .map_err(|e| ErrorKind::Generic.context(e).into()) |
| 115 | + } |
| 116 | +} |
0 commit comments