From bf3efc2b5276fe8017895ac868865591cb688f57 Mon Sep 17 00:00:00 2001 From: Steve Myers Date: Fri, 4 Oct 2024 09:24:06 -0500 Subject: [PATCH] refactor: remove unneeded Arc Mutex for pool --- src/lib.rs | 4 +--- src/postgres.rs | 12 ++++-------- src/sqlite.rs | 12 ++++-------- src/test.rs | 11 ++--------- 4 files changed, 11 insertions(+), 28 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 05e82b9..4f848a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,6 @@ mod test; use std::future::Future; use std::pin::Pin; -use std::sync::Arc; use bdk_chain::miniscript; use bdk_wallet::bitcoin; @@ -18,7 +17,6 @@ use bdk_wallet::chain as bdk_chain; use sqlx::Database; use sqlx::Pool; -use tokio::sync::Mutex; /// Crate error #[derive(Debug, thiserror::Error)] @@ -40,7 +38,7 @@ pub enum BdkSqlxError { /// Manages a pool of database connections. #[derive(Debug)] pub struct Store { - pub(crate) pool: Arc>>, + pub(crate) pool: Pool, wallet_name: String, migration: bool, } diff --git a/src/postgres.rs b/src/postgres.rs index 1bc5034..b222822 100644 --- a/src/postgres.rs +++ b/src/postgres.rs @@ -26,7 +26,6 @@ use sqlx::{ postgres::{PgPool, Postgres}, FromRow, Pool, Row, Transaction, }; -use tokio::sync::Mutex; use tracing::info; impl AsyncWalletPersister for Store { @@ -58,7 +57,7 @@ impl Store { /// Construct a new [`Store`] with an existing pg connection. #[tracing::instrument] pub async fn new( - pool: Arc>>, + pool: Pool, wallet_name: Option, migration: bool, ) -> Result { @@ -82,7 +81,6 @@ impl Store { info!("new store with url"); let pool = PgPool::connect(url.as_str()).await?; - let pool = Arc::new(Mutex::new(pool)); let wallet_name = wallet_name.unwrap_or_else(|| "bdk_pg_wallet".to_string()); Ok(Self { @@ -97,15 +95,14 @@ impl Store { #[tracing::instrument] pub(crate) async fn migrate_and_read(&self) -> Result { info!("migrate and read"); - let pool = self.pool.lock().await; if self.migration { let migrator = Migrator::new(std::path::Path::new("./migrations/postgres/")) .await .unwrap(); - migrator.run(&*pool).await.unwrap(); + migrator.run(&self.pool).await.unwrap(); } - let mut tx = pool.begin().await?; + let mut tx = self.pool.begin().await?; let mut changeset = ChangeSet::default(); @@ -180,8 +177,7 @@ impl Store { } let wallet_name = &self.wallet_name; - let pool = self.pool.lock().await; - let mut tx = pool.begin().await?; + let mut tx = self.pool.begin().await?; if let Some(ref descriptor) = changeset.descriptor { insert_descriptor(&mut tx, wallet_name, descriptor, External).await?; diff --git a/src/sqlite.rs b/src/sqlite.rs index 8734358..1583fab 100644 --- a/src/sqlite.rs +++ b/src/sqlite.rs @@ -26,7 +26,6 @@ use sqlx::{ sqlite::{SqlitePool, SqlitePoolOptions}, }; use sqlx::{sqlite::Sqlite, FromRow, Pool, Row, Transaction}; -use tokio::sync::Mutex; use tracing::info; impl AsyncWalletPersister for Store { @@ -58,7 +57,7 @@ impl Store { /// Construct a new [`Store`] with an existing sqlite connection. #[tracing::instrument] pub async fn new( - pool: Arc>>, + pool: Pool, wallet_name: Option, migration: bool, ) -> Result { @@ -98,7 +97,6 @@ impl Store { .connect(":memory:") .await? }; - let pool = Arc::new(Mutex::new(pool)); let wallet_name = wallet_name.unwrap_or_else(|| "bdk_sqlite_wallet".to_string()); Ok(Self { @@ -113,15 +111,14 @@ impl Store { #[tracing::instrument] pub(crate) async fn migrate_and_read(&self) -> Result { info!("migrate and read"); - let pool = self.pool.lock().await; if self.migration { let migrator = Migrator::new(std::path::Path::new("./migrations/sqlite/")) .await .unwrap(); - migrator.run(&*pool).await.unwrap(); + migrator.run(&self.pool).await.unwrap(); } - let mut tx = pool.begin().await?; + let mut tx = self.pool.begin().await?; let mut changeset = ChangeSet::default(); @@ -196,8 +193,7 @@ impl Store { } let wallet_name = &self.wallet_name; - let pool = self.pool.lock().await; - let mut tx = pool.begin().await?; + let mut tx = self.pool.begin().await?; if let Some(ref descriptor) = changeset.descriptor { insert_descriptor(&mut tx, wallet_name, descriptor, External).await?; diff --git a/src/test.rs b/src/test.rs index eea4316..76668fb 100644 --- a/src/test.rs +++ b/src/test.rs @@ -26,7 +26,6 @@ use std::io::Write; use std::time::Duration; use std::sync::Arc; use std::sync::Once; -use tokio::sync::Mutex; use tracing_subscriber::layer::SubscriberExt; use tracing_subscriber::util::SubscriberInitExt; use tracing_subscriber::EnvFilter; @@ -482,12 +481,7 @@ async fn wallet_is_persisted_sqlite() -> anyhow::Result<()> { )?; // Create a new wallet, use sqlite in memory DB - let mut store = Store::::new( - Arc::new(Mutex::new(pool.clone())), - Some(wallet_name.clone()), - true, - ) - .await?; + let mut store = Store::::new(pool.clone(), Some(wallet_name.clone()), true).await?; let mut wallet = Wallet::create(external_desc, internal_desc) .network(NETWORK) .create_wallet_async(&mut store) @@ -503,8 +497,7 @@ async fn wallet_is_persisted_sqlite() -> anyhow::Result<()> { { // Recover the wallet - let mut store = - Store::::new(Arc::new(Mutex::new(pool)), Some(wallet_name), false).await?; + let mut store = Store::::new(pool, Some(wallet_name), false).await?; let wallet = Wallet::load() .descriptor(KeychainKind::External, Some(external_desc)) .descriptor(KeychainKind::Internal, Some(internal_desc))