feat(tx_graph)!: change TxGraph::calculate_fee to return Result<u64,CalculateFeeError>

added
- tx_graph::CalculateFeeError enum

BREAKING CHANGES:

changed
- TxGraph::calculate_fee function to return Result<u64,CalculateFeeError> instead of Option<i64>
This commit is contained in:
Steve Myers
2023-08-01 12:42:37 -05:00
parent b4c31cd5ba
commit d443fe7f66
6 changed files with 72 additions and 59 deletions

View File

@@ -9,10 +9,6 @@
// You may not use this file except in accordance with one or both of these
// licenses.
//! Errors
//!
//! This module defines the errors that can be thrown by [`crate`] functions.
use crate::bitcoin::Network;
use crate::{descriptor, wallet};
use alloc::{string::String, vec::Vec};
@@ -93,17 +89,7 @@ pub enum Error {
Psbt(bitcoin::psbt::Error),
}
/// Errors returned by `Wallet::calculate_fee`.
#[derive(Debug)]
pub enum CalculateFeeError {
/// Missing `TxOut` for one of the inputs of the tx
MissingTxOut,
/// When the transaction is invalid according to the graph it has a negative fee
NegativeFee(i64),
}
/// Errors returned by miniscript when updating inconsistent PSBTs
#[allow(missing_docs)] // TODO add docs
#[derive(Debug, Clone)]
pub enum MiniscriptPsbtError {
Conversion(miniscript::descriptor::ConversionError),

View File

@@ -29,7 +29,7 @@ extern crate bip39;
#[allow(unused_imports)]
#[macro_use]
pub mod error;
pub(crate) mod error;
pub mod descriptor;
pub mod keys;
pub mod psbt;

View File

@@ -40,6 +40,7 @@ use core::fmt;
use core::ops::Deref;
use miniscript::psbt::{PsbtExt, PsbtInputExt, PsbtInputSatisfier};
use bdk_chain::tx_graph::CalculateFeeError;
#[allow(unused_imports)]
use log::{debug, error, info, trace};
@@ -66,7 +67,7 @@ use crate::descriptor::{
calc_checksum, into_wallet_descriptor_checked, DerivedDescriptor, DescriptorMeta,
ExtendedDescriptor, ExtractPolicy, IntoWalletDescriptor, Policy, XKeyUtils,
};
use crate::error::{CalculateFeeError, Error, MiniscriptPsbtError};
use crate::error::{Error, MiniscriptPsbtError};
use crate::psbt::PsbtUtils;
use crate::signer::SignerError;
use crate::types::*;
@@ -434,11 +435,7 @@ impl<D> Wallet<D> {
///
/// Note `tx` does not have to be in the graph for this to work.
pub fn calculate_fee(&self, tx: &Transaction) -> Result<u64, CalculateFeeError> {
match self.indexed_graph.graph().calculate_fee(tx) {
None => Err(CalculateFeeError::MissingTxOut),
Some(fee) if fee < 0 => Err(CalculateFeeError::NegativeFee(fee)),
Some(fee) => Ok(u64::try_from(fee).unwrap()),
}
self.indexed_graph.graph().calculate_fee(tx)
}
/// Calculate the `FeeRate` for a given transaction.
@@ -1072,13 +1069,12 @@ impl<D> Wallet<D> {
return Err(Error::IrreplaceableTransaction);
}
let fee = graph.calculate_fee(&tx).ok_or(Error::FeeRateUnavailable)?;
if fee < 0 {
// It's available but it's wrong so let's say it's unavailable
return Err(Error::FeeRateUnavailable)?;
}
let fee = fee as u64;
let feerate = FeeRate::from_wu(fee, tx.weight());
let fee = self
.calculate_fee(&tx)
.map_err(|_| Error::FeeRateUnavailable)?;
let fee_rate = self
.calculate_fee_rate(&tx)
.map_err(|_| Error::FeeRateUnavailable)?;
// remove the inputs from the tx and process them
let original_txin = tx.input.drain(..).collect::<Vec<_>>();
@@ -1162,7 +1158,7 @@ impl<D> Wallet<D> {
utxos: original_utxos,
bumping_fee: Some(tx_builder::PreviousFee {
absolute: fee,
rate: feerate.as_sat_per_vb(),
rate: fee_rate.as_sat_per_vb(),
}),
..Default::default()
};

View File

@@ -6,6 +6,7 @@ use bdk::wallet::coin_selection::LargestFirstCoinSelection;
use bdk::wallet::AddressIndex::*;
use bdk::wallet::{AddressIndex, AddressInfo, Balance, Wallet};
use bdk::{Error, FeeRate, KeychainKind};
use bdk_chain::tx_graph::CalculateFeeError;
use bdk_chain::COINBASE_MATURITY;
use bdk_chain::{BlockId, ConfirmationTime};
use bitcoin::hashes::Hash;
@@ -104,7 +105,7 @@ fn test_get_funded_wallet_sent_and_received() {
fn test_get_funded_wallet_tx_fees() {
let (wallet, _) = get_funded_wallet(get_test_wpkh());
assert_eq!(wallet.get_balance().confirmed, 50000);
let mut tx_fee_amounts: Vec<(Txid, Result<u64, bdk::error::CalculateFeeError>)> = wallet
let mut tx_fee_amounts: Vec<(Txid, Result<u64, CalculateFeeError>)> = wallet
.transactions()
.map(|ct| {
let fee = wallet.calculate_fee(ct.node.tx);
@@ -116,7 +117,7 @@ fn test_get_funded_wallet_tx_fees() {
assert_eq!(tx_fee_amounts.len(), 2);
assert_matches!(
tx_fee_amounts.get(1),
Some((_, Err(bdk::error::CalculateFeeError::MissingTxOut)))
Some((_, Err(CalculateFeeError::MissingTxOut(_))))
);
assert_matches!(tx_fee_amounts.get(0), Some((_, Ok(1000))))
}
@@ -125,7 +126,7 @@ fn test_get_funded_wallet_tx_fees() {
fn test_get_funded_wallet_tx_fee_rate() {
let (wallet, _) = get_funded_wallet(get_test_wpkh());
assert_eq!(wallet.get_balance().confirmed, 50000);
let mut tx_fee_rates: Vec<(Txid, Result<FeeRate, bdk::error::CalculateFeeError>)> = wallet
let mut tx_fee_rates: Vec<(Txid, Result<FeeRate, CalculateFeeError>)> = wallet
.transactions()
.map(|ct| {
let fee_rate = wallet.calculate_fee_rate(ct.node.tx);
@@ -137,7 +138,7 @@ fn test_get_funded_wallet_tx_fee_rate() {
assert_eq!(tx_fee_rates.len(), 2);
assert_matches!(
tx_fee_rates.get(1),
Some((_, Err(bdk::error::CalculateFeeError::MissingTxOut)))
Some((_, Err(CalculateFeeError::MissingTxOut(_))))
);
assert_matches!(tx_fee_rates.get(0), Some((_, Ok(_))))
}