Skip to content

Commit

Permalink
wip: use effective_value
Browse files Browse the repository at this point in the history
  • Loading branch information
yancyribbens committed Dec 26, 2023
1 parent 4e5a2bb commit 3566654
Showing 1 changed file with 43 additions and 28 deletions.
71 changes: 43 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn select_coins<T: Utxo>(
fee_rate: FeeRate,
weighted_utxos: &mut [WeightedUtxo],
) -> Option<Vec<WeightedUtxo>> {
match select_coins_bnb(target, cost_of_change, weighted_utxos.to_vec()) {
match select_coins_bnb(target, cost_of_change, fee_rate, weighted_utxos.to_vec()) {
Some(_res) => Some(Vec::new()),
None => select_coins_srd(target, fee_rate, weighted_utxos, &mut thread_rng()),
}
Expand Down Expand Up @@ -150,6 +150,7 @@ pub fn select_coins<T: Utxo>(
pub fn select_coins_bnb(
target: Amount,
cost_of_change: Amount,
_fee_rate: FeeRate,
mut weighted_utxos: Vec<WeightedUtxo>,
) -> Option<Vec<usize>> {
// Total_Tries in Core:
Expand Down Expand Up @@ -320,35 +321,33 @@ mod tests {
use bitcoin::Weight;
use core::str::FromStr;

const SATISFACTION_SIZE: Weight = Weight::from_wu(204);

fn create_weighted_utxos() -> Vec<WeightedUtxo> {
let utxo_one = WeightedUtxo {
satisfaction_weight: SATISFACTION_SIZE,
satisfaction_weight: Weight::ZERO,
utxo: TxOut {
value: Amount::from_str("1 cBTC").unwrap(),
script_pubkey: ScriptBuf::new(),
},
};

let utxo_two = WeightedUtxo {
satisfaction_weight: SATISFACTION_SIZE,
satisfaction_weight: Weight::ZERO,
utxo: TxOut {
value: Amount::from_str("2 cBTC").unwrap(),
script_pubkey: ScriptBuf::new(),
},
};

let utxo_three = WeightedUtxo {
satisfaction_weight: SATISFACTION_SIZE,
satisfaction_weight: Weight::ZERO,
utxo: TxOut {
value: Amount::from_str("3 cBTC").unwrap(),
script_pubkey: ScriptBuf::new(),
},
};

let utxo_four = WeightedUtxo {
satisfaction_weight: SATISFACTION_SIZE,
satisfaction_weight: Weight::ZERO,
utxo: TxOut {
value: Amount::from_str("4 cBTC").unwrap(),
script_pubkey: ScriptBuf::new(),
Expand All @@ -363,14 +362,15 @@ mod tests {
let target = Amount::from_str("1 cBTC").unwrap();

let weighted_utxos = vec![WeightedUtxo {
satisfaction_weight: SATISFACTION_SIZE,
satisfaction_weight: Weight::ZERO,
utxo: TxOut {
value: Amount::from_str("1 cBTC").unwrap(),
script_pubkey: ScriptBuf::new(),
},
}];

let index_list = select_coins_bnb(target, Amount::ZERO, weighted_utxos).unwrap();
let index_list =
select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, weighted_utxos).unwrap();
let expected_index_list = vec![0];
assert_eq!(index_list, expected_index_list);
}
Expand All @@ -381,22 +381,23 @@ mod tests {

let weighted_utxos = vec![
WeightedUtxo {
satisfaction_weight: SATISFACTION_SIZE,
satisfaction_weight: Weight::ZERO,
utxo: TxOut {
value: Amount::from_str("4 cBTC").unwrap(),
script_pubkey: ScriptBuf::new(),
},
},
WeightedUtxo {
satisfaction_weight: SATISFACTION_SIZE,
satisfaction_weight: Weight::ZERO,
utxo: TxOut {
value: Amount::from_str("3 cBTC").unwrap(),
script_pubkey: ScriptBuf::new(),
},
},
];

let index_list = select_coins_bnb(target, Amount::ZERO, weighted_utxos).unwrap();
let index_list =
select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, weighted_utxos).unwrap();
let expected_index_list = vec![0];
assert_eq!(index_list, expected_index_list);
}
Expand All @@ -407,22 +408,23 @@ mod tests {

let weighted_utxos = vec![
WeightedUtxo {
satisfaction_weight: SATISFACTION_SIZE,
satisfaction_weight: Weight::ZERO,
utxo: TxOut {
value: Amount::from_str("4 cBTC").unwrap(),
script_pubkey: ScriptBuf::new(),
},
},
WeightedUtxo {
satisfaction_weight: SATISFACTION_SIZE,
satisfaction_weight: Weight::ZERO,
utxo: TxOut {
value: Amount::from_str("3 cBTC").unwrap(),
script_pubkey: ScriptBuf::new(),
},
},
];

let index_list = select_coins_bnb(target, Amount::ZERO, weighted_utxos).unwrap();
let index_list =
select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, weighted_utxos).unwrap();
let expected_index_list = vec![0, 1];
assert_eq!(index_list, expected_index_list);
}
Expand All @@ -431,7 +433,8 @@ mod tests {
fn one() {
let target = Amount::from_str("1 cBTC").unwrap();
let weighted_utxos = create_weighted_utxos();
let index_list = select_coins_bnb(target, Amount::ZERO, weighted_utxos).unwrap();
let index_list =
select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, weighted_utxos).unwrap();
let expected_index_list = vec![3];
assert_eq!(index_list, expected_index_list);
}
Expand All @@ -440,7 +443,8 @@ mod tests {
fn two() {
let target = Amount::from_str("2 cBTC").unwrap();
let weighted_utxos = create_weighted_utxos();
let index_list = select_coins_bnb(target, Amount::ZERO, weighted_utxos).unwrap();
let index_list =
select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, weighted_utxos).unwrap();
let expected_index_list = vec![2];
assert_eq!(index_list, expected_index_list);
}
Expand All @@ -449,7 +453,8 @@ mod tests {
fn three() {
let target = Amount::from_str("3 cBTC").unwrap();
let weighted_utxos = create_weighted_utxos();
let index_list = select_coins_bnb(target, Amount::ZERO, weighted_utxos).unwrap();
let index_list =
select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, weighted_utxos).unwrap();
let expected_index_list = vec![2, 3];
assert_eq!(index_list, expected_index_list);
}
Expand All @@ -458,7 +463,8 @@ mod tests {
fn four() {
let target = Amount::from_str("4 cBTC").unwrap();
let weighted_utxos = create_weighted_utxos();
let index_list = select_coins_bnb(target, Amount::ZERO, weighted_utxos).unwrap();
let index_list =
select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, weighted_utxos).unwrap();
let expected_index_list = vec![1, 3];
assert_eq!(index_list, expected_index_list);
}
Expand All @@ -467,7 +473,8 @@ mod tests {
fn five() {
let target = Amount::from_str("5 cBTC").unwrap();
let weighted_utxos = create_weighted_utxos();
let index_list = select_coins_bnb(target, Amount::ZERO, weighted_utxos).unwrap();
let index_list =
select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, weighted_utxos).unwrap();
let expected_index_list = vec![1, 2];
assert_eq!(index_list, expected_index_list);
}
Expand All @@ -476,7 +483,8 @@ mod tests {
fn six() {
let target = Amount::from_str("6 cBTC").unwrap();
let weighted_utxos = create_weighted_utxos();
let index_list = select_coins_bnb(target, Amount::ZERO, weighted_utxos).unwrap();
let index_list =
select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, weighted_utxos).unwrap();
let expected_index_list = vec![1, 2, 3];
assert_eq!(index_list, expected_index_list);
}
Expand All @@ -485,7 +493,8 @@ mod tests {
fn seven() {
let target = Amount::from_str("7 cBTC").unwrap();
let weighted_utxos = create_weighted_utxos();
let index_list = select_coins_bnb(target, Amount::ZERO, weighted_utxos).unwrap();
let index_list =
select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, weighted_utxos).unwrap();
let expected_index_list = vec![0, 2, 3];
assert_eq!(index_list, expected_index_list);
}
Expand All @@ -494,7 +503,8 @@ mod tests {
fn eight() {
let target = Amount::from_str("8 cBTC").unwrap();
let weighted_utxos = create_weighted_utxos();
let index_list = select_coins_bnb(target, Amount::ZERO, weighted_utxos).unwrap();
let index_list =
select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, weighted_utxos).unwrap();
let expected_index_list = vec![0, 1, 3];
assert_eq!(index_list, expected_index_list);
}
Expand All @@ -503,7 +513,8 @@ mod tests {
fn nine() {
let target = Amount::from_str("9 cBTC").unwrap();
let weighted_utxos = create_weighted_utxos();
let index_list = select_coins_bnb(target, Amount::ZERO, weighted_utxos).unwrap();
let index_list =
select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, weighted_utxos).unwrap();
let expected_index_list = vec![0, 1, 2];
assert_eq!(index_list, expected_index_list);
}
Expand All @@ -512,7 +523,8 @@ mod tests {
fn ten() {
let target = Amount::from_str("10 cBTC").unwrap();
let weighted_utxos = create_weighted_utxos();
let index_list = select_coins_bnb(target, Amount::ZERO, weighted_utxos).unwrap();
let index_list =
select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, weighted_utxos).unwrap();
let expected_index_list = vec![0, 1, 2, 3];
assert_eq!(index_list, expected_index_list);
}
Expand All @@ -526,18 +538,21 @@ mod tests {
let cost_of_change = target;

let weighted_utxos = vec![WeightedUtxo {
satisfaction_weight: SATISFACTION_SIZE,
satisfaction_weight: Weight::ZERO,
utxo: TxOut {
value: Amount::from_str("1.5 cBTC").unwrap(),
script_pubkey: ScriptBuf::new(),
},
}];

let index_list = select_coins_bnb(target, cost_of_change, weighted_utxos.clone()).unwrap();
let index_list =
select_coins_bnb(target, cost_of_change, FeeRate::ZERO, weighted_utxos.clone())
.unwrap();
let expected_index_list = vec![0];
assert_eq!(index_list, expected_index_list);

let index_list = select_coins_bnb(target, Amount::ZERO, weighted_utxos).unwrap();
let index_list =
select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, weighted_utxos).unwrap();
assert_eq!(index_list, vec![]);
}
}
Expand Down

0 comments on commit 3566654

Please sign in to comment.