Skip to content

Commit 7a3d854

Browse files
committed
wip
1 parent 9c1e853 commit 7a3d854

File tree

2 files changed

+46
-35
lines changed

2 files changed

+46
-35
lines changed

src/lib.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,16 @@ mod tests {
191191

192192
// TODO check about adding this to rust-bitcoins from_str for Weight
193193
fn parse_weight(weight: &str) -> Weight {
194-
let size_parts: Vec<_> = weight.split(" ").collect();
195-
let size_int = size_parts[0].parse::<u64>().unwrap();
196-
match size_parts[1] {
197-
"wu" => Weight::from_wu(size_int),
198-
"vB" => Weight::from_vb(size_int).unwrap(),
199-
_ => panic!("only support wu or vB sizes"),
194+
if weight == "MAX" {
195+
Weight::MAX
196+
} else {
197+
let size_parts: Vec<_> = weight.split(" ").collect();
198+
let size_int = size_parts[0].parse::<u64>().unwrap();
199+
match size_parts[1] {
200+
"wu" => Weight::from_wu(size_int),
201+
"vB" => Weight::from_vb(size_int).unwrap(),
202+
_ => panic!("only support wu or vB sizes"),
203+
}
200204
}
201205
}
202206

@@ -230,6 +234,7 @@ mod tests {
230234
match self.weight.to_wu() {
231235
230 => InputWeightPrediction::P2TR_KEY_DEFAULT_SIGHASH,
232236
272 => InputWeightPrediction::P2WPKH_MAX,
237+
18446744073709551615 => InputWeightPrediction::from_slice((usize::MAX / 4) - 49, &[1]),
233238
_ => panic!("unknown input weight: {:?}", self.weight),
234239
}
235240
}

src/single_random_draw.rs

+35-29
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub fn select_coins_srd<'a, R: rand::Rng + ?Sized, Utxo: WeightedUtxo>(
4848
let mut value = Amount::ZERO;
4949

5050
let mut iteration = 0;
51+
5152
for w_utxo in origin {
5253
iteration += 1;
5354
let effective_value = w_utxo.effective_value(fee_rate);
@@ -248,17 +249,18 @@ mod tests {
248249
.assert();
249250
}
250251

251-
//#[test]
252-
//fn select_coins_srd_addition_overflow() {
253-
//TestSRD {
254-
//target: "2 cBTC",
255-
//fee_rate: "10 sat/kwu",
256-
//weighted_utxos: &["1 cBTC/18446744073709551615 wu"], // weight= u64::MAX
257-
//expected_utxos: None,
258-
//expected_iterations: 0,
259-
//}
260-
//.assert();
261-
//}
252+
#[test]
253+
fn select_coins_srd_addition_overflow() {
254+
// Overflow when effective_value is computed with MAX weight.
255+
TestSRD {
256+
target: "2 cBTC",
257+
fee_rate: "10 sat/kwu",
258+
weighted_utxos: &["1 cBTC/MAX"],
259+
expected_utxos: None,
260+
expected_iterations: 0,
261+
}
262+
.assert();
263+
}
262264

263265
#[test]
264266
fn select_coins_srd_threshold_overflow() {
@@ -272,24 +274,28 @@ mod tests {
272274
.assert();
273275
}
274276

275-
//#[test]
276-
//fn select_coins_srd_pool_overflow() {
277-
//TestSRD {
278-
//target: "18315535.91666658 BTC",
279-
//fee_rate: "0",
280-
//weighted_utxos: &[
281-
//"e(1550078614956004 sats)/68 vB",
282-
//"e(1831540706090689 sats)/18446744073692774400 wu",
283-
//"e(12885625970 sats)/68 vB",
284-
//],
285-
//expected_utxos: Some(&[
286-
//"e(1831540706090689 sats)/18446744073692774400 wu",
287-
//"e(12885625970 sats)/68 vB",
288-
//]),
289-
//expected_iterations: 2,
290-
//}
291-
//.assert();
292-
//}
277+
#[test]
278+
fn select_coins_srd_pool_overflow() {
279+
// Adding the first utxo to the second overflows.
280+
// The test shows that the second UTXO is then skipped
281+
// due to overflow and the next one is still selected.
282+
283+
TestSRD {
284+
target: "18315535.91666658 BTC",
285+
fee_rate: "0",
286+
weighted_utxos: &[
287+
"e(1550078614956004 sats)/68 vB",
288+
"e(1831540706090689 sats)/68 vB",
289+
"e(12885625970 sats)/68 vB",
290+
],
291+
expected_utxos: Some(&[
292+
"e(1831540706090689 sats)/68 vB",
293+
"e(12885625970 sats)/68 vB",
294+
]),
295+
expected_iterations: 2,
296+
}
297+
.assert();
298+
}
293299

294300
//#[test]
295301
//fn select_srd_match_proptest() {

0 commit comments

Comments
 (0)