Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 637eede

Browse files
authored
Change the type of weight to u64 for compatability (#89)
1 parent d10b827 commit 637eede

File tree

7 files changed

+22
-22
lines changed

7 files changed

+22
-22
lines changed

examples/bitcoin_crate/src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ fn create_outputgroup(
155155
}
156156
output_group_vec.push(OutputGroup {
157157
value: tx.output.iter().map(|op| op.value.to_sat()).sum(),
158-
weight: tx.total_size() as u32,
158+
weight: tx.total_size() as u64,
159159
input_count: tx.input.len(),
160160
creation_sequence: Some(creation_sequence),
161161
})
@@ -181,11 +181,11 @@ fn create_select_options() -> Result<Vec<CoinSelectionOpt>, Box<dyn std::error::
181181
target_feerate: rng.gen_range(1.0..5.0) as f32,
182182
long_term_feerate: Some(rng.gen_range(1..10) as f32),
183183
min_absolute_fee: rng.gen_range(1..20) as u64,
184-
base_weight: rng.gen_range(1..30) as u32,
185-
change_weight: rng.gen_range(5..30) as u32,
184+
base_weight: rng.gen_range(1..30) as u64,
185+
change_weight: rng.gen_range(5..30) as u64,
186186
change_cost: rng.gen_range(1..20) as u64,
187-
avg_input_weight: rng.gen_range(1..10) as u32,
188-
avg_output_weight: rng.gen_range(1..10) as u32,
187+
avg_input_weight: rng.gen_range(1..10) as u64,
188+
avg_output_weight: rng.gen_range(1..10) as u64,
189189
min_change_value: rng.gen_range(100..1000) as u64,
190190
excess_strategy,
191191
})

src/algorithms/bnb.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn select_coin_bnb(
5252
let accumulated_value: u64 = selected_coin
5353
.iter()
5454
.fold(0, |acc, &i| acc + inputs[i].value);
55-
let accumulated_weight: u32 = selected_coin
55+
let accumulated_weight: u64 = selected_coin
5656
.iter()
5757
.fold(0, |acc, &i| acc + inputs[i].weight);
5858
let estimated_fee = 0;

src/algorithms/fifo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn select_coin_fifo(
1111
options: &CoinSelectionOpt,
1212
) -> Result<SelectionOutput, SelectionError> {
1313
let mut accumulated_value: u64 = 0;
14-
let mut accumulated_weight: u32 = 0;
14+
let mut accumulated_weight: u64 = 0;
1515
let mut selected_inputs: Vec<usize> = Vec::new();
1616
let mut estimated_fees: u64 = 0;
1717

src/algorithms/knapsack.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ mod test {
129129

130130
fn knapsack_setup_output_groups(
131131
value: Vec<u64>,
132-
weights: Vec<u32>,
132+
weights: Vec<u64>,
133133
target_feerate: f32,
134134
) -> Vec<OutputGroup> {
135135
let mut inputs: Vec<OutputGroup> = Vec::new();
@@ -150,7 +150,7 @@ mod test {
150150
fn knapsack_add_to_output_group(
151151
inputs: &mut Vec<OutputGroup>,
152152
value: Vec<u64>,
153-
weights: Vec<u32>,
153+
weights: Vec<u64>,
154154
target_feerate: f32,
155155
) {
156156
for (i, j) in value.into_iter().zip(weights.into_iter()) {
@@ -475,7 +475,7 @@ mod test {
475475
inputs.clear();
476476
// Declare value and weights vectors
477477
let mut input_value: Vec<u64> = Vec::new();
478-
let mut input_weight: Vec<u32> = Vec::new();
478+
let mut input_weight: Vec<u64> = Vec::new();
479479
for _ in 0..676 {
480480
// Populate the vectors with the same value 'amt' and weight = 23 for 676 times
481481
// Using 676 as (old MAX_STANDARD_TX_SIZE = 100000)/(148 bytes per input) = 676
@@ -510,7 +510,7 @@ mod test {
510510
// Testing for Randomness
511511
// Declare input value and weights vectors
512512
let mut input_value: Vec<u64> = Vec::new();
513-
let mut input_weight: Vec<u32> = Vec::new();
513+
let mut input_weight: Vec<u64> = Vec::new();
514514
for _ in 0..=100 {
515515
// Populate the vectors with the same value, COIN = 100000000 sats, and weight = 23 for 100 times (to create 100 identical inputs)
516516
input_value.push(COIN as u64);

src/algorithms/lowestlarger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn select_coin_lowestlarger(
1111
options: &CoinSelectionOpt,
1212
) -> Result<SelectionOutput, SelectionError> {
1313
let mut accumulated_value: u64 = 0;
14-
let mut accumulated_weight: u32 = 0;
14+
let mut accumulated_weight: u64 = 0;
1515
let mut selected_inputs: Vec<usize> = Vec::new();
1616
let mut estimated_fees: u64 = 0;
1717
let target = options.target_value + options.min_change_value;

src/types.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct OutputGroup {
1212
///
1313
/// The `txin` fields: `prevout`, `nSequence`, `scriptSigLen`, `scriptSig`, `scriptWitnessLen`,
1414
/// and `scriptWitness` should all be included.
15-
pub weight: u32,
15+
pub weight: u64,
1616
/// The total number of inputs
1717
pub input_count: usize,
1818
/// Specifies the relative creation sequence for this group, used only for FIFO selection.
@@ -42,21 +42,21 @@ pub struct CoinSelectionOpt {
4242
///
4343
/// This includes weight of the header, total weight out outputs, weight of fields used
4444
/// to represent number number of inputs and number outputs, witness etc.,
45-
pub base_weight: u32,
45+
pub base_weight: u64,
4646

4747
/// Additional weight if we include the change output.
4848
///
4949
/// Used in weight metric computation.
50-
pub change_weight: u32,
50+
pub change_weight: u64,
5151

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

5555
/// Estimate of average weight of an input.
56-
pub avg_input_weight: u32,
56+
pub avg_input_weight: u64,
5757

5858
/// Estimate of average weight of an output.
59-
pub avg_output_weight: u32,
59+
pub avg_output_weight: u64,
6060

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

104104
/// Weight type alias
105-
pub type Weight = u32;
105+
pub type Weight = u64;

src/utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::collections::HashSet;
55
pub fn calculate_waste(
66
options: &CoinSelectionOpt,
77
accumulated_value: u64,
8-
accumulated_weight: u32,
8+
accumulated_weight: u64,
99
estimated_fee: u64,
1010
) -> u64 {
1111
// waste = weight*(target feerate - long term fee rate) + cost of change + excess
@@ -35,8 +35,8 @@ pub fn calculate_waste(
3535
pub fn calculate_accumulated_weight(
3636
smaller_coins: &[(usize, EffectiveValue, Weight)],
3737
selected_inputs: &HashSet<usize>,
38-
) -> u32 {
39-
let mut accumulated_weight: u32 = 0;
38+
) -> u64 {
39+
let mut accumulated_weight: u64 = 0;
4040
for &(index, _value, weight) in smaller_coins {
4141
if selected_inputs.contains(&index) {
4242
accumulated_weight += weight;
@@ -46,7 +46,7 @@ pub fn calculate_accumulated_weight(
4646
}
4747

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

0 commit comments

Comments
 (0)