Skip to content

Commit c2a3fc7

Browse files
committed
Fix crash in chanmon_fail_consistency due to fuzz hash collisions
1 parent 50e16c9 commit c2a3fc7

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

fuzz/fuzz_targets/chanmon_fail_consistency.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ use utils::test_logger;
4949
use secp256k1::key::{PublicKey,SecretKey};
5050
use secp256k1::Secp256k1;
5151

52+
use std::cmp::Ordering;
53+
use std::collections::HashSet;
5254
use std::sync::{Arc,Mutex};
5355
use std::io::Cursor;
5456

@@ -435,13 +437,36 @@ pub fn do_test(data: &[u8]) {
435437

436438
macro_rules! process_events {
437439
($node: expr, $fail: expr) => { {
438-
for event in nodes[$node].get_and_clear_pending_events() {
440+
// In case we get 256 payments we may have a hash collision, resulting in the
441+
// second claim/fail call not finding the duplicate-hash HTLC, so we have to
442+
// deduplicate the calls here.
443+
let mut claim_set = HashSet::new();
444+
let mut events = nodes[$node].get_and_clear_pending_events();
445+
// Sort events so that PendingHTLCsForwardable get processed last. This avoids a
446+
// case where we first process a PendingHTLCsForwardable, then claim/fail on a
447+
// PaymentReceived, claiming/failing two HTLCs, but leaving a just-generated
448+
// PaymentReceived event for the second HTLC in our pending_events (and breaking
449+
// our claim_set deduplication).
450+
events.sort_by(|a, b| {
451+
if let events::Event::PaymentReceived { .. } = a {
452+
if let events::Event::PendingHTLCsForwardable { .. } = b {
453+
Ordering::Less
454+
} else { Ordering::Equal }
455+
} else if let events::Event::PendingHTLCsForwardable { .. } = a {
456+
if let events::Event::PaymentReceived { .. } = b {
457+
Ordering::Greater
458+
} else { Ordering::Equal }
459+
} else { Ordering::Equal }
460+
});
461+
for event in events.drain(..) {
439462
match event {
440463
events::Event::PaymentReceived { payment_hash, .. } => {
441-
if $fail {
442-
assert!(nodes[$node].fail_htlc_backwards(&payment_hash));
443-
} else {
444-
assert!(nodes[$node].claim_funds(PaymentPreimage(payment_hash.0)));
464+
if claim_set.insert(payment_hash.0) {
465+
if $fail {
466+
assert!(nodes[$node].fail_htlc_backwards(&payment_hash));
467+
} else {
468+
assert!(nodes[$node].claim_funds(PaymentPreimage(payment_hash.0)));
469+
}
445470
}
446471
},
447472
events::Event::PaymentSent { .. } => {},

0 commit comments

Comments
 (0)