Skip to content

Commit

Permalink
Change the type of weight to u64 for compatability (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
delcin-raj authored Dec 13, 2024
1 parent d10b827 commit 637eede
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 22 deletions.
10 changes: 5 additions & 5 deletions examples/bitcoin_crate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ fn create_outputgroup(
}
output_group_vec.push(OutputGroup {
value: tx.output.iter().map(|op| op.value.to_sat()).sum(),
weight: tx.total_size() as u32,
weight: tx.total_size() as u64,
input_count: tx.input.len(),
creation_sequence: Some(creation_sequence),
})
Expand All @@ -181,11 +181,11 @@ fn create_select_options() -> Result<Vec<CoinSelectionOpt>, Box<dyn std::error::
target_feerate: rng.gen_range(1.0..5.0) as f32,
long_term_feerate: Some(rng.gen_range(1..10) as f32),
min_absolute_fee: rng.gen_range(1..20) as u64,
base_weight: rng.gen_range(1..30) as u32,
change_weight: rng.gen_range(5..30) as u32,
base_weight: rng.gen_range(1..30) as u64,
change_weight: rng.gen_range(5..30) as u64,
change_cost: rng.gen_range(1..20) as u64,
avg_input_weight: rng.gen_range(1..10) as u32,
avg_output_weight: rng.gen_range(1..10) as u32,
avg_input_weight: rng.gen_range(1..10) as u64,
avg_output_weight: rng.gen_range(1..10) as u64,
min_change_value: rng.gen_range(100..1000) as u64,
excess_strategy,
})
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/bnb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn select_coin_bnb(
let accumulated_value: u64 = selected_coin
.iter()
.fold(0, |acc, &i| acc + inputs[i].value);
let accumulated_weight: u32 = selected_coin
let accumulated_weight: u64 = selected_coin
.iter()
.fold(0, |acc, &i| acc + inputs[i].weight);
let estimated_fee = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/fifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn select_coin_fifo(
options: &CoinSelectionOpt,
) -> Result<SelectionOutput, SelectionError> {
let mut accumulated_value: u64 = 0;
let mut accumulated_weight: u32 = 0;
let mut accumulated_weight: u64 = 0;
let mut selected_inputs: Vec<usize> = Vec::new();
let mut estimated_fees: u64 = 0;

Expand Down
8 changes: 4 additions & 4 deletions src/algorithms/knapsack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ mod test {

fn knapsack_setup_output_groups(
value: Vec<u64>,
weights: Vec<u32>,
weights: Vec<u64>,
target_feerate: f32,
) -> Vec<OutputGroup> {
let mut inputs: Vec<OutputGroup> = Vec::new();
Expand All @@ -150,7 +150,7 @@ mod test {
fn knapsack_add_to_output_group(
inputs: &mut Vec<OutputGroup>,
value: Vec<u64>,
weights: Vec<u32>,
weights: Vec<u64>,
target_feerate: f32,
) {
for (i, j) in value.into_iter().zip(weights.into_iter()) {
Expand Down Expand Up @@ -475,7 +475,7 @@ mod test {
inputs.clear();
// Declare value and weights vectors
let mut input_value: Vec<u64> = Vec::new();
let mut input_weight: Vec<u32> = Vec::new();
let mut input_weight: Vec<u64> = Vec::new();
for _ in 0..676 {
// Populate the vectors with the same value 'amt' and weight = 23 for 676 times
// Using 676 as (old MAX_STANDARD_TX_SIZE = 100000)/(148 bytes per input) = 676
Expand Down Expand Up @@ -510,7 +510,7 @@ mod test {
// Testing for Randomness
// Declare input value and weights vectors
let mut input_value: Vec<u64> = Vec::new();
let mut input_weight: Vec<u32> = Vec::new();
let mut input_weight: Vec<u64> = Vec::new();
for _ in 0..=100 {
// Populate the vectors with the same value, COIN = 100000000 sats, and weight = 23 for 100 times (to create 100 identical inputs)
input_value.push(COIN as u64);
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/lowestlarger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn select_coin_lowestlarger(
options: &CoinSelectionOpt,
) -> Result<SelectionOutput, SelectionError> {
let mut accumulated_value: u64 = 0;
let mut accumulated_weight: u32 = 0;
let mut accumulated_weight: u64 = 0;
let mut selected_inputs: Vec<usize> = Vec::new();
let mut estimated_fees: u64 = 0;
let target = options.target_value + options.min_change_value;
Expand Down
12 changes: 6 additions & 6 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct OutputGroup {
///
/// The `txin` fields: `prevout`, `nSequence`, `scriptSigLen`, `scriptSig`, `scriptWitnessLen`,
/// and `scriptWitness` should all be included.
pub weight: u32,
pub weight: u64,
/// The total number of inputs
pub input_count: usize,
/// Specifies the relative creation sequence for this group, used only for FIFO selection.
Expand Down Expand Up @@ -42,21 +42,21 @@ pub struct CoinSelectionOpt {
///
/// This includes weight of the header, total weight out outputs, weight of fields used
/// to represent number number of inputs and number outputs, witness etc.,
pub base_weight: u32,
pub base_weight: u64,

/// Additional weight if we include the change output.
///
/// Used in weight metric computation.
pub change_weight: u32,
pub change_weight: u64,

/// Weight of spending the change output in the future.
pub change_cost: u64,

/// Estimate of average weight of an input.
pub avg_input_weight: u32,
pub avg_input_weight: u64,

/// Estimate of average weight of an output.
pub avg_output_weight: u32,
pub avg_output_weight: u64,

/// Minimum value allowed for a change output to avoid dusts.
pub min_change_value: u64,
Expand Down Expand Up @@ -102,4 +102,4 @@ pub struct SelectionOutput {
pub type EffectiveValue = u64;

/// Weight type alias
pub type Weight = u32;
pub type Weight = u64;
8 changes: 4 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::collections::HashSet;
pub fn calculate_waste(
options: &CoinSelectionOpt,
accumulated_value: u64,
accumulated_weight: u32,
accumulated_weight: u64,
estimated_fee: u64,
) -> u64 {
// waste = weight*(target feerate - long term fee rate) + cost of change + excess
Expand Down Expand Up @@ -35,8 +35,8 @@ pub fn calculate_waste(
pub fn calculate_accumulated_weight(
smaller_coins: &[(usize, EffectiveValue, Weight)],
selected_inputs: &HashSet<usize>,
) -> u32 {
let mut accumulated_weight: u32 = 0;
) -> u64 {
let mut accumulated_weight: u64 = 0;
for &(index, _value, weight) in smaller_coins {
if selected_inputs.contains(&index) {
accumulated_weight += weight;
Expand All @@ -46,7 +46,7 @@ pub fn calculate_accumulated_weight(
}

#[inline]
pub fn calculate_fee(weight: u32, rate: f32) -> u64 {
pub fn calculate_fee(weight: u64, rate: f32) -> u64 {
(weight as f32 * rate).ceil() as u64
}

Expand Down

0 comments on commit 637eede

Please sign in to comment.