Merge rust-bitcoin/rust-bitcoin#2456: Use InputWeightPrediction to calculate effective_value
f6105ea417
Use InputWeightPrediction to calculate effective_value (yancy) Pull request description: closes: https://github.com/rust-bitcoin/rust-bitcoin/issues/2455 Also, what about moving `effective_value` to `InputWeightPrediction`? Marking as a draft until we can add api changes again. ACKs for top commit: apoelstra: ACK f6105ea4171a85ce21443d7eb76b7aa9cadab53a; successfully ran local tests; yeah, this API does look nicer Kixunil: ACKf6105ea417
Tree-SHA512: 20592e49cb93343b1aefa340c3c870e2e21c747711da68a6aa57342f59ff2981c30e9c91de7eab32bcd11da33f040f9df62008db991d93b549079f91a6908055
This commit is contained in:
commit
aa804ee1b1
|
@ -772,13 +772,13 @@ impl Decodable for Transaction {
|
||||||
/// # Parameters
|
/// # Parameters
|
||||||
///
|
///
|
||||||
/// * `fee_rate` - the fee rate of the transaction being created.
|
/// * `fee_rate` - the fee rate of the transaction being created.
|
||||||
/// * `satisfaction_weight` - satisfied spending conditions weight.
|
/// * `input_weight_prediction` - the predicted input weight.
|
||||||
pub fn effective_value(
|
pub fn effective_value(
|
||||||
fee_rate: FeeRate,
|
fee_rate: FeeRate,
|
||||||
satisfaction_weight: Weight,
|
input_weight_prediction: InputWeightPrediction,
|
||||||
value: Amount,
|
value: Amount,
|
||||||
) -> Option<SignedAmount> {
|
) -> Option<SignedAmount> {
|
||||||
let weight = satisfaction_weight.checked_add(TX_IN_BASE_WEIGHT)?;
|
let weight = input_weight_prediction.total_weight();
|
||||||
let signed_input_fee = fee_rate.to_fee(weight)?.to_signed();
|
let signed_input_fee = fee_rate.to_fee(weight)?.to_signed();
|
||||||
value.to_signed().checked_sub(signed_input_fee)
|
value.to_signed().checked_sub(signed_input_fee)
|
||||||
}
|
}
|
||||||
|
@ -1651,24 +1651,19 @@ mod tests {
|
||||||
fn effective_value_happy_path() {
|
fn effective_value_happy_path() {
|
||||||
let value = "1 cBTC".parse::<Amount>().unwrap();
|
let value = "1 cBTC".parse::<Amount>().unwrap();
|
||||||
let fee_rate = FeeRate::from_sat_per_kwu(10);
|
let fee_rate = FeeRate::from_sat_per_kwu(10);
|
||||||
let satisfaction_weight = Weight::from_wu(204);
|
let effective_value =
|
||||||
let effective_value = effective_value(fee_rate, satisfaction_weight, value).unwrap();
|
effective_value(fee_rate, InputWeightPrediction::P2WPKH_MAX, value).unwrap();
|
||||||
|
|
||||||
// 10 sat/kwu * (204wu + BASE_WEIGHT) = 4 sats
|
// 10 sat/kwu * 272 wu = 4 sats (rounding up)
|
||||||
let expected_fee = "4 sats".parse::<SignedAmount>().unwrap();
|
let expected_fee = "3 sats".parse::<SignedAmount>().unwrap();
|
||||||
let expected_effective_value = (value.to_signed() - expected_fee).unwrap();
|
let expected_effective_value = (value.to_signed() - expected_fee).unwrap();
|
||||||
assert_eq!(effective_value, expected_effective_value);
|
assert_eq!(effective_value, expected_effective_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn effective_value_fee_rate_does_not_overflow() {
|
fn effective_value_fee_rate_does_not_overflow() {
|
||||||
let eff_value = effective_value(FeeRate::MAX, Weight::ZERO, Amount::ZERO);
|
let eff_value =
|
||||||
assert!(eff_value.is_none());
|
effective_value(FeeRate::MAX, InputWeightPrediction::P2WPKH_MAX, Amount::ZERO);
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn effective_value_weight_does_not_overflow() {
|
|
||||||
let eff_value = effective_value(FeeRate::ZERO, Weight::MAX, Amount::ZERO);
|
|
||||||
assert!(eff_value.is_none());
|
assert!(eff_value.is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue