Skip to content

Commit

Permalink
Move txin base weight calculation
Browse files Browse the repository at this point in the history
The base weight calculation was previously provided by rust-bitcoin but
has since been removed.  Adding the base weight calculation to this
local crate instead.
  • Loading branch information
yancyribbens committed Nov 21, 2023
1 parent 17fbd82 commit f7c76f5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ keywords = ["crypto", "bitcoin"]
readme = "README.md"

[dependencies]
bitcoin = { git="https://github.com/rust-bitcoin/rust-bitcoin", branch="master" }
bitcoin = { git="https://github.com/yancyribbens/rust-bitcoin.git", branch="add-from-vb-const" }
rand = {version = "0.8.5", default-features = false, optional = true}

[dev-dependencies]
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ pub trait Utxo: Clone {
// https://github.com/bitcoin/bitcoin/blob/f722a9bd132222d9d5cd503b5af25c905b205cdb/src/wallet/coinselection.h#L20
const CHANGE_LOWER: Amount = Amount::from_sat(50_000);

/// The weight of a `TxIn` excluding the `script_sig` and `witness`.
pub const BASE_WEIGHT: Weight = Weight::from_vb_const(32 + 4 + 4);

/// This struct contains the weight of all params needed to satisfy the UTXO.
///
/// The idea of using a WeightUtxo type was inspired by the BDK implementation:
Expand Down
8 changes: 4 additions & 4 deletions src/single_random_draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
use crate::errors::Error;
use crate::WeightedUtxo;
use crate::CHANGE_LOWER;
use crate::BASE_WEIGHT;
use bitcoin::Amount;
use bitcoin::FeeRate;
use bitcoin::TxIn;
use rand::seq::SliceRandom;

/// Calculates the effective_value of an input.
Expand All @@ -24,8 +24,8 @@ fn get_effective_value(
let satisfaction_weight = weighted_utxo.satisfaction_weight;

let weight = satisfaction_weight
.checked_add(TxIn::BASE_WEIGHT)
.ok_or(Error::AdditionOverflow(satisfaction_weight, TxIn::BASE_WEIGHT))?;
.checked_add(BASE_WEIGHT)
.ok_or(Error::AdditionOverflow(satisfaction_weight, BASE_WEIGHT))?;

let input_fee = fee_rate
.checked_mul_by_weight(weight)
Expand Down Expand Up @@ -239,6 +239,6 @@ mod tests {
let result: Error = select_coins_srd(target, FEE_RATE, &mut weighted_utxos, &mut get_rng())
.expect_err("expected error");

assert_eq!(result.to_string(), "18446744073709551615 + 40 exceeds u64 Max");
assert_eq!(result.to_string(), "18446744073709551615 + 160 exceeds u64 Max");
}
}

0 comments on commit f7c76f5

Please sign in to comment.