Skip to content

Commit 584dba4

Browse files
committed
wip
1 parent eefd368 commit 584dba4

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

benches/coin_selection.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ impl WeightedUtxo for Utxo {
1616

1717
pub fn criterion_benchmark(c: &mut Criterion) {
1818
// https://github.com/bitcoin/bitcoin/blob/f3bc1a72825fe2b51f4bc20e004cef464f05b965/src/wallet/coinselection.h#L18
19-
let cost_of_change = Amount::from_sat(50_000);
19+
let cost_of_change = Amount::from_sat(50_000).unwrap();
2020

2121
let one = Utxo {
22-
output: TxOut { value: Amount::from_sat(1_000), script_pubkey: ScriptBuf::new() },
22+
output: TxOut { value: Amount::from_sat(1_000).unwrap(), script_pubkey: ScriptBuf::new() },
2323
satisfaction_weight: Weight::ZERO,
2424
};
2525

2626
let two = Utxo {
27-
output: TxOut { value: Amount::from_sat(3), script_pubkey: ScriptBuf::new() },
27+
output: TxOut { value: Amount::from_sat(3).unwrap(), script_pubkey: ScriptBuf::new() },
2828
satisfaction_weight: Weight::ZERO,
2929
};
3030

31-
let target = Amount::from_sat(1_003);
31+
let target = Amount::from_sat(1_003).unwrap();
3232
let mut utxo_pool = vec![one; 1000];
3333
utxo_pool.push(two);
3434

@@ -45,8 +45,8 @@ pub fn criterion_benchmark(c: &mut Criterion) {
4545
assert_eq!(iteration_count, 100000);
4646

4747
assert_eq!(2, inputs.len());
48-
assert_eq!(Amount::from_sat(1_000), inputs[0].value());
49-
assert_eq!(Amount::from_sat(3), inputs[1].value());
48+
assert_eq!(Amount::from_sat(1_000).unwrap(), inputs[0].value());
49+
assert_eq!(Amount::from_sat(3).unwrap(), inputs[1].value());
5050
})
5151
});
5252
}

fuzz/Cargo.toml

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ cargo-fuzz = true
1010
[dependencies]
1111
libfuzzer-sys = "0.4"
1212
rand = "0.8.5"
13-
bitcoin = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "7df5e7c1bcb4aaf3247f0b76591db9744f03425e", features = ["arbitrary"] }
13+
bitcoin = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "52f9c13358c97c358543f3302b325f37ac49392f", features = ["arbitrary"] }
1414
arbitrary = { version = "1", features = ["derive"] }
1515

1616
[dependencies.bitcoin-coin-selection]
@@ -39,10 +39,10 @@ doc = false
3939
bench = false
4040

4141
[patch.crates-io]
42-
bitcoin_hashes = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "7df5e7c1bcb4aaf3247f0b76591db9744f03425e" }
43-
base58ck = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "7df5e7c1bcb4aaf3247f0b76591db9744f03425e" }
44-
bitcoin-internals = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "7df5e7c1bcb4aaf3247f0b76591db9744f03425e" }
45-
bitcoin-io = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "7df5e7c1bcb4aaf3247f0b76591db9744f03425e" }
46-
bitcoin-primitives = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "7df5e7c1bcb4aaf3247f0b76591db9744f03425e" }
47-
bitcoin-addresses = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "7df5e7c1bcb4aaf3247f0b76591db9744f03425e" }
48-
bitcoin-units = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "7df5e7c1bcb4aaf3247f0b76591db9744f03425e" }
42+
bitcoin_hashes = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "52f9c13358c97c358543f3302b325f37ac49392f" }
43+
base58ck = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "52f9c13358c97c358543f3302b325f37ac49392f" }
44+
bitcoin-internals = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "52f9c13358c97c358543f3302b325f37ac49392f" }
45+
bitcoin-io = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "52f9c13358c97c358543f3302b325f37ac49392f" }
46+
bitcoin-primitives = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "52f9c13358c97c358543f3302b325f37ac49392f" }
47+
bitcoin-addresses = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "52f9c13358c97c358543f3302b325f37ac49392f" }
48+
bitcoin-units = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "52f9c13358c97c358543f3302b325f37ac49392f" }

src/single_random_draw.rs

+27-14
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ pub fn select_coins_srd<'a, R: rand::Rng + ?Sized, Utxo: WeightedUtxo>(
3939
weighted_utxos: &'a [Utxo],
4040
rng: &mut R,
4141
) -> Return<'a, Utxo> {
42-
println!("start");
4342
let mut result: Vec<_> = weighted_utxos.iter().collect();
4443
let mut origin = result.to_owned();
4544
origin.shuffle(rng);
@@ -50,32 +49,29 @@ pub fn select_coins_srd<'a, R: rand::Rng + ?Sized, Utxo: WeightedUtxo>(
5049
let mut value = Amount::ZERO;
5150

5251
let mut iteration = 0;
53-
println!("start loop");
5452
for w_utxo in origin {
5553
iteration += 1;
5654
let utxo_value = w_utxo.value();
5755
let utxo_weight = w_utxo.satisfaction_weight();
5856
let effective_value = effective_value(fee_rate, utxo_weight, utxo_value);
5957

58+
// if effective_value has no overflow
6059
if let Some(e) = effective_value {
60+
// if effective_value is positive
6161
if let Ok(v) = e.to_unsigned() {
62-
println!("hi");
63-
println!("{} + {} or {} {} ", value.to_sat(), v.to_sat(), value, v);
64-
println!("{:?} {}", Amount::MAX, Amount::MAX);
65-
value = value.checked_add(v).unwrap();
66-
67-
result.push(w_utxo);
68-
69-
println!("value {} threshold {} is >=: {}", value, threshold, value >= threshold);
70-
if value >= threshold {
71-
println!("return the result");
72-
return Some((iteration, result));
62+
// if addition does not overflow
63+
if let Some(new_val) = value.checked_add(v) {
64+
value = new_val;
65+
result.push(w_utxo);
66+
67+
if value >= threshold {
68+
return Some((iteration, result));
69+
}
7370
}
7471
}
7572
}
7673
}
7774

78-
println!("none found");
7975
None
8076
}
8177

@@ -109,6 +105,7 @@ mod tests {
109105
let pool: UtxoPool = UtxoPool::new(self.weighted_utxos, fee_rate);
110106

111107
let result = select_coins_srd(target, fee_rate, &pool.utxos, &mut get_rng());
108+
println!("result {:?}", result);
112109

113110
if let Some((iterations, inputs)) = result {
114111
assert_eq!(iterations, self.expected_iterations);
@@ -272,6 +269,22 @@ mod tests {
272269
.assert();
273270
}
274271

272+
#[test]
273+
fn select_coins_srd_pool_overflow() {
274+
TestSRD {
275+
target: "18315535.91666658 BTC",
276+
fee_rate: "0",
277+
weighted_utxos: &[
278+
"e(1550078614956004 sats)/68 vB",
279+
"e(1831540706090689 sats)/18446744073692774400 wu",
280+
"e(12885625970 sats)/68 vB",
281+
],
282+
expected_utxos: Some(&["e(1831540706090689 sats)/18446744073692774400 wu", "e(12885625970 sats)/68 vB"]),
283+
expected_iterations: 2,
284+
}
285+
.assert();
286+
}
287+
275288
#[test]
276289
fn select_srd_match_proptest() {
277290
arbtest(|u| {

0 commit comments

Comments
 (0)