Skip to content

Commit

Permalink
refactor: remove unneeded Arc Mutex for pool
Browse files Browse the repository at this point in the history
  • Loading branch information
notmandatory committed Oct 4, 2024
1 parent 0c12dc8 commit bf3efc2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 28 deletions.
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ mod test;

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use bdk_chain::miniscript;
use bdk_wallet::bitcoin;
use bdk_wallet::chain as bdk_chain;

use sqlx::Database;
use sqlx::Pool;
use tokio::sync::Mutex;

/// Crate error
#[derive(Debug, thiserror::Error)]
Expand All @@ -40,7 +38,7 @@ pub enum BdkSqlxError {
/// Manages a pool of database connections.
#[derive(Debug)]
pub struct Store<DB: Database> {
pub(crate) pool: Arc<Mutex<Pool<DB>>>,
pub(crate) pool: Pool<DB>,
wallet_name: String,
migration: bool,
}
Expand Down
12 changes: 4 additions & 8 deletions src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use sqlx::{
postgres::{PgPool, Postgres},
FromRow, Pool, Row, Transaction,
};
use tokio::sync::Mutex;
use tracing::info;

impl AsyncWalletPersister for Store<Postgres> {
Expand Down Expand Up @@ -58,7 +57,7 @@ impl Store<Postgres> {
/// Construct a new [`Store`] with an existing pg connection.
#[tracing::instrument]
pub async fn new(
pool: Arc<Mutex<Pool<Postgres>>>,
pool: Pool<Postgres>,
wallet_name: Option<String>,
migration: bool,
) -> Result<Self, BdkSqlxError> {
Expand All @@ -82,7 +81,6 @@ impl Store<Postgres> {
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 {
Expand All @@ -97,15 +95,14 @@ impl Store<Postgres> {
#[tracing::instrument]
pub(crate) async fn migrate_and_read(&self) -> Result<ChangeSet, BdkSqlxError> {
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();

Expand Down Expand Up @@ -180,8 +177,7 @@ impl Store<Postgres> {
}

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?;
Expand Down
12 changes: 4 additions & 8 deletions src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sqlite> {
Expand Down Expand Up @@ -58,7 +57,7 @@ impl Store<Sqlite> {
/// Construct a new [`Store`] with an existing sqlite connection.
#[tracing::instrument]
pub async fn new(
pool: Arc<Mutex<Pool<Sqlite>>>,
pool: Pool<Sqlite>,
wallet_name: Option<String>,
migration: bool,
) -> Result<Self, BdkSqlxError> {
Expand Down Expand Up @@ -98,7 +97,6 @@ impl Store<Sqlite> {
.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 {
Expand All @@ -113,15 +111,14 @@ impl Store<Sqlite> {
#[tracing::instrument]
pub(crate) async fn migrate_and_read(&self) -> Result<ChangeSet, BdkSqlxError> {
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();

Expand Down Expand Up @@ -196,8 +193,7 @@ impl Store<Sqlite> {
}

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?;
Expand Down
11 changes: 2 additions & 9 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<Sqlite>::new(
Arc::new(Mutex::new(pool.clone())),
Some(wallet_name.clone()),
true,
)
.await?;
let mut store = Store::<Sqlite>::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)
Expand All @@ -503,8 +497,7 @@ async fn wallet_is_persisted_sqlite() -> anyhow::Result<()> {

{
// Recover the wallet
let mut store =
Store::<Sqlite>::new(Arc::new(Mutex::new(pool)), Some(wallet_name), false).await?;
let mut store = Store::<Sqlite>::new(pool, Some(wallet_name), false).await?;
let wallet = Wallet::load()
.descriptor(KeychainKind::External, Some(external_desc))
.descriptor(KeychainKind::Internal, Some(internal_desc))
Expand Down

0 comments on commit bf3efc2

Please sign in to comment.