feat: use Amount on calculate_fee, fee_absolute, fee_amount and others

- update to use `bitcoin::Amount` on `CreateTxError::FeeTooLow` variant.
- update to use `bitcoin::Amount` on `Wallet::calculate_fee()`.
- update to use `bitcoin::Amount` on `FeePolicy::fee_absolute()`.
- update to use `bitcoin::SignedAmount` on
  `CalculateFeeError::NegativeFee` variant.
- update to use `bitcoin::Amount` on `TxGraph::calculate_fee()`.
- update to use `bitcoin::Amount` on `PsbUtils::fee_amount()`
This commit is contained in:
Leonardo Lima
2024-05-05 17:41:31 -03:00
parent 4a8452f9b8
commit a03949adb0
10 changed files with 113 additions and 118 deletions

View File

@@ -95,7 +95,7 @@ use crate::{
use alloc::collections::vec_deque::VecDeque;
use alloc::sync::Arc;
use alloc::vec::Vec;
use bitcoin::{Amount, OutPoint, Script, Transaction, TxOut, Txid};
use bitcoin::{Amount, OutPoint, Script, SignedAmount, Transaction, TxOut, Txid};
use core::fmt::{self, Formatter};
use core::{
convert::Infallible,
@@ -182,7 +182,7 @@ pub enum CalculateFeeError {
/// Missing `TxOut` for one or more of the inputs of the tx
MissingTxOut(Vec<OutPoint>),
/// When the transaction is invalid according to the graph it has a negative fee
NegativeFee(i64),
NegativeFee(SignedAmount),
}
impl fmt::Display for CalculateFeeError {
@@ -196,7 +196,7 @@ impl fmt::Display for CalculateFeeError {
CalculateFeeError::NegativeFee(fee) => write!(
f,
"transaction is invalid according to the graph and has negative fee: {}",
fee
fee.display_dynamic()
),
}
}
@@ -307,7 +307,7 @@ impl<A> TxGraph<A> {
})
}
/// Calculates the fee of a given transaction. Returns 0 if `tx` is a coinbase transaction.
/// Calculates the fee of a given transaction. Returns [`Amount::ZERO`] if `tx` is a coinbase transaction.
/// Returns `OK(_)` if we have all the [`TxOut`]s being spent by `tx` in the graph (either as
/// the full transactions or individual txouts).
///
@@ -318,20 +318,20 @@ impl<A> TxGraph<A> {
/// Note `tx` does not have to be in the graph for this to work.
///
/// [`insert_txout`]: Self::insert_txout
pub fn calculate_fee(&self, tx: &Transaction) -> Result<u64, CalculateFeeError> {
pub fn calculate_fee(&self, tx: &Transaction) -> Result<Amount, CalculateFeeError> {
if tx.is_coinbase() {
return Ok(0);
return Ok(Amount::ZERO);
}
let (inputs_sum, missing_outputs) = tx.input.iter().fold(
(0_i64, Vec::new()),
(SignedAmount::ZERO, Vec::new()),
|(mut sum, mut missing_outpoints), txin| match self.get_txout(txin.previous_output) {
None => {
missing_outpoints.push(txin.previous_output);
(sum, missing_outpoints)
}
Some(txout) => {
sum += txout.value.to_sat() as i64;
sum += txout.value.to_signed().expect("valid `SignedAmount`");
(sum, missing_outpoints)
}
},
@@ -343,15 +343,12 @@ impl<A> TxGraph<A> {
let outputs_sum = tx
.output
.iter()
.map(|txout| txout.value.to_sat() as i64)
.sum::<i64>();
.map(|txout| txout.value.to_signed().expect("valid `SignedAmount`"))
.sum::<SignedAmount>();
let fee = inputs_sum - outputs_sum;
if fee < 0 {
Err(CalculateFeeError::NegativeFee(fee))
} else {
Ok(fee as u64)
}
fee.to_unsigned()
.map_err(|_| CalculateFeeError::NegativeFee(fee))
}
/// The transactions spending from this output.

View File

@@ -10,8 +10,8 @@ use bdk_chain::{
Anchor, Append, BlockId, ChainOracle, ChainPosition, ConfirmationHeightAnchor,
};
use bitcoin::{
absolute, hashes::Hash, transaction, Amount, BlockHash, OutPoint, ScriptBuf, Transaction, TxIn,
TxOut, Txid,
absolute, hashes::Hash, transaction, Amount, BlockHash, OutPoint, ScriptBuf, SignedAmount,
Transaction, TxIn, TxOut, Txid,
};
use common::*;
use core::iter;
@@ -466,14 +466,14 @@ fn test_calculate_fee() {
}],
};
assert_eq!(graph.calculate_fee(&tx), Ok(100));
assert_eq!(graph.calculate_fee(&tx), Ok(Amount::from_sat(100)));
tx.input.remove(2);
// fee would be negative, should return CalculateFeeError::NegativeFee
assert_eq!(
graph.calculate_fee(&tx),
Err(CalculateFeeError::NegativeFee(-200))
Err(CalculateFeeError::NegativeFee(SignedAmount::from_sat(-200)))
);
// If we have an unknown outpoint, fee should return CalculateFeeError::MissingTxOut.
@@ -505,7 +505,7 @@ fn test_calculate_fee_on_coinbase() {
let graph = TxGraph::<()>::default();
assert_eq!(graph.calculate_fee(&tx), Ok(0));
assert_eq!(graph.calculate_fee(&tx), Ok(Amount::ZERO));
}
// `test_walk_ancestors` uses the following transaction structure: