Skip to content

Commit

Permalink
chore: more tracing for bdk <-> pg interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
bodymindarts committed Feb 14, 2024
1 parent 730a141 commit 30cfd21
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/bdk/pg/descriptor_checksum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use sqlx::PgPool;
use tracing::instrument;
use uuid::Uuid;

use super::convert::BdkKeychainKind;
Expand All @@ -14,6 +15,10 @@ impl DescriptorChecksums {
Self { keychain_id, pool }
}

#[instrument(
name = "bdk.descriptor_checksums.check_or_persist_descriptor_checksum",
skip_all
)]
pub async fn check_or_persist_descriptor_checksum(
&self,
keychain: impl Into<BdkKeychainKind>,
Expand Down
4 changes: 4 additions & 0 deletions src/bdk/pg/index.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use sqlx::PgPool;
use tracing::instrument;
use uuid::Uuid;

use super::convert::BdkKeychainKind;
Expand All @@ -14,6 +15,7 @@ impl Indexes {
Self { keychain_id, pool }
}

#[instrument(name = "bdk.indexes.increment", skip_all)]
pub async fn increment(&self, keychain: impl Into<BdkKeychainKind>) -> Result<u32, bdk::Error> {
let kind = keychain.into();
let result = sqlx::query!(
Expand All @@ -36,6 +38,7 @@ impl Indexes {
Ok(new_idx as u32)
}

#[instrument(name = "bdk.indexes.persist_last_index", skip_all)]
pub async fn persist_last_index(
&self,
keychain: impl Into<BdkKeychainKind>,
Expand All @@ -58,6 +61,7 @@ impl Indexes {
Ok(())
}

#[instrument(name = "bdk.indexes.get_latest", skip_all)]
pub async fn get_latest(
&self,
keychain: impl Into<BdkKeychainKind>,
Expand Down
5 changes: 5 additions & 0 deletions src/bdk/pg/script_pubkeys.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use sqlx::{PgPool, Postgres, QueryBuilder};
use tracing::instrument;
use uuid::Uuid;

use super::convert::BdkKeychainKind;
Expand All @@ -14,6 +15,7 @@ impl ScriptPubkeys {
Self { keychain_id, pool }
}

#[instrument(name = "bdk.script_pubkeys.persist_all", skip_all)]
pub async fn persist_all(
&self,
keys: Vec<(BdkKeychainKind, u32, ScriptBuf)>,
Expand Down Expand Up @@ -45,6 +47,7 @@ impl ScriptPubkeys {
Ok(())
}

#[instrument(name = "bdk.script_pubkeys.find_script", skip_all)]
pub async fn find_script(
&self,
keychain: impl Into<BdkKeychainKind>,
Expand All @@ -67,6 +70,7 @@ impl ScriptPubkeys {
.map(|row| ScriptBuf::from(row.script)))
}

#[instrument(name = "bdk.script_pubkeys.find_path", skip_all)]
pub async fn find_path(
&self,
script: &ScriptBuf,
Expand All @@ -87,6 +91,7 @@ impl ScriptPubkeys {
}
}

#[instrument(name = "bdk.script_pubkeys.list_scripts", skip_all)]
pub async fn list_scripts(
&self,
keychain: Option<impl Into<BdkKeychainKind>>,
Expand Down
4 changes: 4 additions & 0 deletions src/bdk/pg/sync_times.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bdk::database::SyncTime;
use sqlx::PgPool;
use tracing::instrument;
use uuid::Uuid;

use crate::{bdk::error::BdkError, primitives::*};
Expand All @@ -14,6 +15,7 @@ impl SyncTimes {
Self { keychain_id, pool }
}

#[instrument(name = "bdk.sync_times.persist", skip_all)]
pub async fn persist(&self, time: SyncTime) -> Result<(), bdk::Error> {
sqlx::query!(
r#"INSERT INTO bdk_sync_times (keychain_id, height, timestamp)
Expand All @@ -29,6 +31,7 @@ impl SyncTimes {
Ok(())
}

#[instrument(name = "bdk.sync_times.get", skip_all)]
pub async fn get(&self) -> Result<Option<SyncTime>, bdk::Error> {
let sync_time = sqlx::query!(
r#"SELECT height, timestamp FROM bdk_sync_times WHERE keychain_id = $1"#,
Expand All @@ -45,6 +48,7 @@ impl SyncTimes {
}))
}

#[instrument(name = "bdk.sync_times.last_sync_time", skip_all)]
pub async fn last_sync_time(pool: &PgPool) -> Result<u32, BdkError> {
let sync_time =
sqlx::query!(r#"SELECT COALESCE(MAX(height), 0) as "height!" FROM bdk_sync_times"#,)
Expand Down
15 changes: 9 additions & 6 deletions src/bdk/pg/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl Transactions {
Self { keychain_id, pool }
}

#[instrument(name = "bdk.transactions.persist", skip_all)]
pub async fn persist_all(&self, txs: Vec<TransactionDetails>) -> Result<(), bdk::Error> {
const BATCH_SIZE: usize = 5000;
let batches = txs.chunks(BATCH_SIZE);
Expand Down Expand Up @@ -63,6 +64,7 @@ impl Transactions {
Ok(())
}

#[instrument(name = "bdk.transactions.delete", skip_all)]
pub async fn delete(&self, tx_id: &Txid) -> Result<Option<TransactionDetails>, bdk::Error> {
let tx = sqlx::query!(
r#"UPDATE bdk_transactions
Expand All @@ -81,6 +83,7 @@ impl Transactions {
}))
}

#[instrument(name = "bdk.transactions.find_by_id", skip_all)]
pub async fn find_by_id(&self, tx_id: &Txid) -> Result<Option<TransactionDetails>, bdk::Error> {
let tx = sqlx::query!(
r#"
Expand All @@ -94,7 +97,7 @@ impl Transactions {
Ok(tx.map(|tx| serde_json::from_value(tx.details_json).unwrap()))
}

#[instrument(name = "bdk_transactions.list", skip(self), fields(n_rows))]
#[instrument(name = "bdk.transactions.list", skip(self), fields(n_rows))]
pub async fn list(&self) -> Result<Vec<TransactionDetails>, bdk::Error> {
let txs = sqlx::query!(
r#"
Expand All @@ -111,7 +114,7 @@ impl Transactions {
.collect())
}

#[instrument(name = "bdk_transactions.find_unsynced_tx", skip(self), fields(n_rows))]
#[instrument(name = "bdk.transactions.find_unsynced_tx", skip(self), fields(n_rows))]
pub async fn find_unsynced_tx(
&self,
excluded_tx_ids: &[String],
Expand Down Expand Up @@ -182,7 +185,7 @@ impl Transactions {
}))
}

#[instrument(name = "bdk_transactions.find_confirmed_spend_tx", skip(self, tx))]
#[instrument(name = "bdk.transactions.find_confirmed_spend_tx", skip(self, tx))]
pub async fn find_confirmed_spend_tx(
&self,
tx: &mut Transaction<'_, Postgres>,
Expand Down Expand Up @@ -251,7 +254,7 @@ impl Transactions {
}))
}

#[instrument(name = "bdk_transactions.mark_as_synced", skip(self))]
#[instrument(name = "bdk.transactions.mark_as_synced", skip(self))]
pub async fn mark_as_synced(&self, tx_id: bitcoin::Txid) -> Result<(), BdkError> {
sqlx::query!(
r#"UPDATE bdk_transactions SET synced_to_bria = true, modified_at = NOW()
Expand All @@ -264,7 +267,7 @@ impl Transactions {
Ok(())
}

#[instrument(name = "bdk_transactions.mark_confirmed", skip(self))]
#[instrument(name = "bdk.transactions.mark_confirmed", skip(self))]
pub async fn mark_confirmed(
&self,
tx: &mut Transaction<'_, Postgres>,
Expand All @@ -282,7 +285,7 @@ impl Transactions {
}

#[instrument(
name = "bdk_transactions.delete_transaction_if_no_more_utxos_exist",
name = "bdk.transactions.delete_transaction_if_no_more_utxos_exist",
skip(self, tx)
)]
pub async fn delete_transaction_if_no_more_utxos_exist(
Expand Down
12 changes: 9 additions & 3 deletions src/bdk/pg/utxos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl Utxos {
Self { keychain_id, pool }
}

#[instrument(name = "bdk.utxos.persist_all", skip_all)]
pub async fn persist_all(&self, utxos: Vec<LocalUtxo>) -> Result<(), bdk::Error> {
const BATCH_SIZE: usize = 5000;
let batches = utxos.chunks(BATCH_SIZE);
Expand Down Expand Up @@ -51,6 +52,7 @@ impl Utxos {
Ok(())
}

#[instrument(name = "bdk.utxos.delete", skip_all)]
pub async fn delete(
&self,
outpoint: &bitcoin::OutPoint,
Expand All @@ -72,6 +74,7 @@ impl Utxos {
}))
}

#[instrument(name = "bdk.utxos.undelete", skip_all)]
pub async fn undelete(&self, outpoint: bitcoin::OutPoint) -> Result<(), BdkError> {
sqlx::query!(
r#"UPDATE bdk_utxos SET deleted_at = NULL
Expand All @@ -86,6 +89,7 @@ impl Utxos {
Ok(())
}

#[instrument(name = "bdk.utxos.find", skip_all)]
pub async fn find(&self, outpoint: &OutPoint) -> Result<Option<LocalUtxo>, bdk::Error> {
let utxo = sqlx::query!(
r#"
Expand All @@ -108,6 +112,7 @@ impl Utxos {
}))
}

#[instrument(name = "bdk.utxos.list_local_utxos", skip_all)]
pub async fn list_local_utxos(&self) -> Result<Vec<LocalUtxo>, bdk::Error> {
let utxos = sqlx::query!(
r#"SELECT utxo_json FROM bdk_utxos WHERE keychain_id = $1 AND deleted_at IS NULL"#,
Expand All @@ -122,7 +127,7 @@ impl Utxos {
.collect())
}

#[instrument(name = "bdk_utxos.mark_as_synced", skip(self, tx))]
#[instrument(name = "bdk.utxos.mark_as_synced", skip(self, tx))]
pub async fn mark_as_synced(
&self,
tx: &mut Transaction<'_, Postgres>,
Expand All @@ -140,7 +145,7 @@ impl Utxos {
Ok(())
}

#[instrument(name = "bdk_utxos.mark_confirmed", skip(self, tx))]
#[instrument(name = "bdk.utxos.mark_confirmed", skip(self, tx))]
pub async fn mark_confirmed(
&self,
tx: &mut Transaction<'_, Postgres>,
Expand All @@ -158,7 +163,7 @@ impl Utxos {
Ok(())
}

#[instrument(name = "bdk_utxos.find_confirmed_income_utxo", skip(self, tx))]
#[instrument(name = "bdk.utxos.find_confirmed_income_utxo", skip(self, tx))]
pub async fn find_confirmed_income_utxo(
&self,
tx: &mut Transaction<'_, Postgres>,
Expand Down Expand Up @@ -207,6 +212,7 @@ impl Utxos {
}))
}

#[instrument(name = "bdk.utxos.find_and_remove_soft_deleted_utxo", skip_all)]
pub async fn find_and_remove_soft_deleted_utxo(
&self,
tx: &mut Transaction<'_, Postgres>,
Expand Down

0 comments on commit 30cfd21

Please sign in to comment.