Skip to content

Commit

Permalink
Merge branch 'main' of github.com:GaloyMoney/bria into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
git committed Nov 24, 2023
2 parents 8d6243a + 3fc0257 commit 6449339
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ impl App {
&self.wallets,
payout_queue,
fee_rate,
false,
true,
)
.await?
};
Expand Down
2 changes: 2 additions & 0 deletions src/bdk/pg/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,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))]
pub async fn list(&self) -> Result<Vec<TransactionDetails>, bdk::Error> {
let txs = sqlx::query!(
r#"
Expand All @@ -103,6 +104,7 @@ impl Transactions {
.fetch_all(&self.pool)
.await
.map_err(|e| bdk::Error::Generic(e.to_string()))?;
tracing::Span::current().record("n_rows", txs.len());
Ok(txs
.into_iter()
.map(|tx| serde_json::from_value(tx.details_json).unwrap())
Expand Down
7 changes: 4 additions & 3 deletions src/job/process_payout_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub(super) async fn execute<'a>(
&wallets,
payout_queue,
fee_rate,
true,
false,
)
.await?;

Expand Down Expand Up @@ -159,7 +159,7 @@ pub async fn construct_psbt(
wallets: &Wallets,
payout_queue: PayoutQueue,
fee_rate: bitcoin::FeeRate,
include_cpfp: bool,
for_estimation: bool,
) -> Result<FinishedPsbtBuild, JobError> {
let span = tracing::Span::current();
let PayoutQueue {
Expand All @@ -185,7 +185,7 @@ pub async fn construct_psbt(
);

let mut mandatory_cpfp_utxos = HashMap::new();
if include_cpfp {
if !for_estimation {
if let Some(min_age) = queue_cfg.cpfp_payouts_after() {
let keychain_ids = wallets.values().flat_map(|w| w.keychain_ids());
mandatory_cpfp_utxos = utxos
Expand All @@ -210,6 +210,7 @@ pub async fn construct_psbt(
mandatory_cpfp_utxos,
tx_payouts,
wallets,
for_estimation,
)
.await?)
}
Expand Down
27 changes: 25 additions & 2 deletions src/wallet/psbt_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
collections::{HashMap, HashSet},
marker::PhantomData,
};
use tracing::instrument;

use super::{keychain::*, Wallet as WalletEntity};
use crate::{
Expand Down Expand Up @@ -73,6 +74,7 @@ pub struct PsbtBuilder<T> {
result: FinishedPsbtBuild,
input_weights: HashMap<OutPoint, usize>,
all_included_utxos: HashSet<OutPoint>,
for_estimation: bool,
_phantom: PhantomData<T>,
}

Expand Down Expand Up @@ -153,6 +155,7 @@ impl Default for PsbtBuilder<InitialPsbtBuilderState> {
}

impl PsbtBuilder<InitialPsbtBuilderState> {
#[instrument(name = "psbt_builder.construct_psbt", skip_all)]
pub async fn construct_psbt(
pool: &sqlx::PgPool,
consolidate_deprecated_keychains: bool,
Expand All @@ -161,12 +164,14 @@ impl PsbtBuilder<InitialPsbtBuilderState> {
cpfp_utxos: HashMap<KeychainId, Vec<CpfpUtxo>>,
unbatched_payouts: HashMap<WalletId, Vec<TxPayout>>,
mut wallets: HashMap<WalletId, WalletEntity>,
for_estimation: bool,
) -> Result<FinishedPsbtBuild, BdkError> {
let mut outer_builder = PsbtBuilder::new()
.consolidate_deprecated_keychains(consolidate_deprecated_keychains)
.fee_rate(fee_rate)
.reserved_utxos(reserved_utxos)
.cpfp_utxos(cpfp_utxos)
.for_estimation(for_estimation)
.accept_wallets();

for (wallet_id, payouts) in unbatched_payouts {
Expand Down Expand Up @@ -207,6 +212,7 @@ impl PsbtBuilder<InitialPsbtBuilderState> {
tx_id: None,
psbt: None,
},
for_estimation: false,
_phantom: PhantomData,
}
}
Expand All @@ -229,6 +235,11 @@ impl PsbtBuilder<InitialPsbtBuilderState> {
self
}

pub fn for_estimation(mut self, for_estimation: bool) -> Self {
self.for_estimation = for_estimation;
self
}

pub fn fee_rate(mut self, fee_rate: FeeRate) -> Self {
self.fee_rate = Some(fee_rate);
self
Expand Down Expand Up @@ -275,6 +286,7 @@ impl PsbtBuilder<InitialPsbtBuilderState> {
all_included_utxos: self.all_included_utxos,
input_weights: self.input_weights,
result: self.result,
for_estimation: self.for_estimation,
_phantom: PhantomData,
}
}
Expand All @@ -301,6 +313,7 @@ impl PsbtBuilder<AcceptingWalletState> {
all_included_utxos: self.all_included_utxos,
input_weights: self.input_weights,
result: self.result,
for_estimation: self.for_estimation,
_phantom: PhantomData,
}
}
Expand All @@ -325,7 +338,11 @@ impl BdkWalletVisitor for PsbtBuilder<AcceptingDeprecatedKeychainState> {
.max_satisfaction_weight()
.expect("Unsupported descriptor");

let drain_address = wallet.get_internal_address(AddressIndex::LastUnused)?;
let drain_address = if self.for_estimation {
wallet.get_internal_address(AddressIndex::Peek(0))?
} else {
wallet.get_internal_address(AddressIndex::LastUnused)?
};

let mut builder = wallet.build_tx();
if let Some(reserved_utxos) = self
Expand Down Expand Up @@ -374,6 +391,7 @@ impl PsbtBuilder<AcceptingDeprecatedKeychainState> {
all_included_utxos: self.all_included_utxos,
input_weights: self.input_weights,
result: self.result,
for_estimation: self.for_estimation,
_phantom: PhantomData,
}
}
Expand All @@ -385,7 +403,11 @@ impl BdkWalletVisitor for PsbtBuilder<AcceptingCurrentKeychainState> {
current_keychain_id: KeychainId,
wallet: &Wallet<D>,
) -> Result<Self, BdkError> {
let change_address = wallet.get_internal_address(AddressIndex::LastUnused)?;
let change_address = if self.for_estimation {
wallet.get_internal_address(AddressIndex::Peek(0))?
} else {
wallet.get_internal_address(AddressIndex::LastUnused)?
};
let keychain_satisfaction_weight = wallet
.get_descriptor_for_keychain(KeychainKind::External)
.max_satisfaction_weight()
Expand Down Expand Up @@ -586,6 +608,7 @@ impl PsbtBuilder<AcceptingCurrentKeychainState> {
all_included_utxos: self.all_included_utxos,
input_weights: self.input_weights,
result: self.result,
for_estimation: self.for_estimation,
_phantom: PhantomData,
}
}
Expand Down

0 comments on commit 6449339

Please sign in to comment.