Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

Commit

Permalink
Merge #84
Browse files Browse the repository at this point in the history
84: Use size, not weight r=D4nte a=D4nte

The weight is in Weight Unit and is not used to calculate transaction
fee.

The size, in vB since segwit, is what is needed, which is a 4th of a WU,
is what is used when setting a fee.

Co-authored-by: Franck Royer <[email protected]>
  • Loading branch information
bors[bot] and Franck Royer authored Oct 6, 2020
2 parents 48d00a1 + 28a42d3 commit 4fb831d
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/src/bitcoin/witness/primed_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ impl PrimedTransaction {
pub fn sign_with_rate<C: secp256k1::Signing>(
self,
secp: &Secp256k1<C>,
fee_per_byte: Amount,
fee_per_vbyte: Amount,
) -> Result<Transaction, Error> {
let mut transaction = self._transaction_without_signatures_or_output_values();

let weight = transaction.get_weight();
let fee = fee_per_byte
let weight = transaction.get_size();
let fee = fee_per_vbyte
.checked_mul(weight as u64)
.ok_or(Error::OverflowingFee)?;

Expand Down Expand Up @@ -183,9 +183,9 @@ impl PrimedTransaction {
}
}

pub fn estimate_weight(&self) -> usize {
pub fn estimate_size(&self) -> usize {
self._transaction_without_signatures_or_output_values()
.get_weight()
.get_size()
}
}

Expand All @@ -197,7 +197,7 @@ mod test {
use std::str::FromStr;

#[test]
fn estimate_weight_and_sign_with_fee_are_correct_p2wpkh() -> Result<(), failure::Error> {
fn estimate_size_and_sign_with_fee_are_correct_p2wpkh() -> Result<(), failure::Error> {
let secp = Secp256k1::signing_only();
let private_key =
PrivateKey::from_str("L4nZrdzNnawCtaEcYGWuPqagQA3dJxVPgN8ARTXaMLCxiYCy89wm")?;
Expand All @@ -219,14 +219,18 @@ mod test {

let rate = Amount::from_sat(42);

let estimated_weight = primed_txn.estimate_weight();
let estimated_size = primed_txn.estimate_size();
let transaction = primed_txn.sign_with_rate(&secp, rate).unwrap();

let actual_weight = transaction.get_weight();
let actual_size = transaction.get_size();
let fee = total_input_value.as_sat() - transaction.output[0].value;

assert_eq!(estimated_weight, actual_weight, "weight is correct");
assert_eq!(fee, 18354, "actual fee paid is correct");
assert_eq!(estimated_size, actual_size, "weight is correct");
assert_eq!(
fee,
rate.as_sat() * actual_size as u64,
"actual fee paid is correct"
);
Ok(())
}
}

0 comments on commit 4fb831d

Please sign in to comment.