@@ -49,6 +49,8 @@ use utils::test_logger;
49
49
use secp256k1:: key:: { PublicKey , SecretKey } ;
50
50
use secp256k1:: Secp256k1 ;
51
51
52
+ use std:: cmp:: Ordering ;
53
+ use std:: collections:: HashSet ;
52
54
use std:: sync:: { Arc , Mutex } ;
53
55
use std:: io:: Cursor ;
54
56
@@ -435,13 +437,36 @@ pub fn do_test(data: &[u8]) {
435
437
436
438
macro_rules! process_events {
437
439
( $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( ..) {
439
462
match event {
440
463
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
+ }
445
470
}
446
471
} ,
447
472
events:: Event :: PaymentSent { .. } => { } ,
0 commit comments