@@ -59,6 +59,10 @@ public class HumanObjectPeerTestInstance {
59
59
case failedToBindSocket
60
60
}
61
61
62
+ fileprivate enum ChannelManagerEventError : Error {
63
+ case notUsingChannelManagerConstructor
64
+ }
65
+
62
66
fileprivate actor PendingEventTracker {
63
67
64
68
private( set) var pendingManagerEvents : [ Event ] = [ ]
@@ -357,17 +361,19 @@ public class HumanObjectPeerTestInstance {
357
361
358
362
}
359
363
360
- fileprivate func getManagerEvents( expectedCount: UInt ) async -> [ Event ] {
361
- var response = [ Event] ( ) ;
362
- if self . master. use_chan_manager_constructor {
363
- while true {
364
- if await self . pendingEventTracker. getCount ( ) >= expectedCount {
365
- return await self . pendingEventTracker. getAndClearEvents ( )
366
- }
367
- await self . pendingEventTracker. awaitAddition ( )
364
+ fileprivate func getManagerEvents( expectedCount: UInt ) async throws -> [ Event ] {
365
+ guard self . master. use_chan_manager_constructor else {
366
+ throw ChannelManagerEventError . notUsingChannelManagerConstructor
367
+ }
368
+ while true {
369
+ if await self . pendingEventTracker. getCount ( ) >= expectedCount {
370
+ print ( " Found enough events for expected count of \( expectedCount) " )
371
+ let events = await self . pendingEventTracker. getAndClearEvents ( )
372
+ print ( " Found event count: \( events. count) " )
373
+ return events
368
374
}
375
+ await self . pendingEventTracker. awaitAddition ( )
369
376
}
370
- return response
371
377
}
372
378
373
379
fileprivate func getPendingBroadcasts( expectedCount: UInt ) async -> Set < [ UInt8 ] > {
@@ -511,11 +517,13 @@ public class HumanObjectPeerTestInstance {
511
517
XCTAssertNil ( fundingTxo)
512
518
}
513
519
514
- let managerEvents = await peer1. getManagerEvents ( expectedCount: 1 )
520
+ let managerEvents = try ! await peer1. getManagerEvents ( expectedCount: 1 )
515
521
XCTAssertEqual ( managerEvents. count, 1 )
516
522
517
523
let managerEvent = managerEvents [ 0 ]
518
- XCTAssertEqual ( managerEvent. getValueType ( ) , . FundingGenerationReady)
524
+ guard case . FundingGenerationReady = managerEvent. getValueType ( ) else {
525
+ return XCTAssert ( false , " Expected .FundingGenerationReady, got \( managerEvent. getValueType ( ) ) " )
526
+ }
519
527
520
528
let fundingReadyEvent = managerEvent. getValueAsFundingGenerationReady ( ) !
521
529
XCTAssertEqual ( fundingReadyEvent. getChannel_value_satoshis ( ) , FUNDING_SATOSHI_AMOUNT)
@@ -612,13 +620,17 @@ public class HumanObjectPeerTestInstance {
612
620
let recreatedInvoice = Invoice . from_str ( s: invoice. to_str ( ) )
613
621
XCTAssertTrue ( recreatedInvoice. isOk ( ) )
614
622
615
- let invoicePaymentResult = peer1. constructor!. payer!. pay_invoice ( invoice: invoice)
623
+ let channelManagerConstructor = peer1. constructor!
624
+ let invoicePayer = channelManagerConstructor. payer!
625
+ let invoicePaymentResult = invoicePayer. pay_invoice ( invoice: invoice)
616
626
XCTAssertTrue ( invoicePaymentResult. isOk ( ) )
617
627
618
628
do {
619
629
// process HTLCs
620
- let peer2Event = await peer2. getManagerEvents ( expectedCount: 1 ) [ 0 ]
621
- XCTAssertEqual ( peer2Event. getValueType ( ) , . PendingHTLCsForwardable)
630
+ let peer2Event = try ! await peer2. getManagerEvents ( expectedCount: 1 ) [ 0 ]
631
+ guard case . PendingHTLCsForwardable = peer2Event. getValueType ( ) else {
632
+ return XCTAssert ( false , " Expected .PendingHTLCsForwardable, got \( peer2Event. getValueType ( ) ) " )
633
+ }
622
634
let pendingHTLCsForwardable = peer2Event. getValueAsPendingHTLCsForwardable ( ) !
623
635
print ( " forwardable time: \( pendingHTLCsForwardable. getTime_forwardable ( ) ) " )
624
636
peer2. channelManager. process_pending_htlc_forwards ( )
@@ -627,13 +639,17 @@ public class HumanObjectPeerTestInstance {
627
639
628
640
do {
629
641
// process payment
630
- let peer2Event = await peer2. getManagerEvents ( expectedCount: 1 ) [ 0 ]
631
- XCTAssertEqual ( peer2Event. getValueType ( ) , . PaymentReceived)
642
+ let peer2Event = try ! await peer2. getManagerEvents ( expectedCount: 1 ) [ 0 ]
643
+ guard case . PaymentReceived = peer2Event. getValueType ( ) else {
644
+ return XCTAssert ( false , " Expected .PaymentReceived, got \( peer2Event. getValueType ( ) ) " )
645
+ }
632
646
let paymentReceived = peer2Event. getValueAsPaymentReceived ( ) !
633
647
let paymentHash = paymentReceived. getPayment_hash ( )
634
648
print ( " received payment of \( paymentReceived. getAmount_msat ( ) ) with hash \( paymentHash) " )
635
649
let paymentPurpose = paymentReceived. getPurpose ( )
636
- XCTAssertEqual ( paymentPurpose. getValueType ( ) , . InvoicePayment)
650
+ guard case . InvoicePayment = paymentPurpose. getValueType ( ) else {
651
+ return XCTAssert ( false , " Expected .InvoicePayment, got \( paymentPurpose. getValueType ( ) ) " )
652
+ }
637
653
let invoicePayment = paymentPurpose. getValueAsInvoicePayment ( ) !
638
654
let preimage = invoicePayment. getPayment_preimage ( )
639
655
let secret = invoicePayment. getPayment_secret ( )
@@ -643,11 +659,15 @@ public class HumanObjectPeerTestInstance {
643
659
644
660
do {
645
661
// process payment
646
- let peer1Events = await peer1. getManagerEvents ( expectedCount: 2 )
662
+ let peer1Events = try ! await peer1. getManagerEvents ( expectedCount: 2 )
647
663
let paymentSentEvent = peer1Events [ 0 ]
648
664
let paymentPathSuccessfulEvent = peer1Events [ 1 ]
649
- XCTAssertEqual ( paymentSentEvent. getValueType ( ) , . PaymentSent)
650
- XCTAssertEqual ( paymentPathSuccessfulEvent. getValueType ( ) , . PaymentPathSuccessful)
665
+ guard case . PaymentSent = paymentSentEvent. getValueType ( ) else {
666
+ return XCTAssert ( false , " Expected .PaymentSent, got \( paymentSentEvent. getValueType ( ) ) " )
667
+ }
668
+ guard case . PaymentPathSuccessful = paymentPathSuccessfulEvent. getValueType ( ) else {
669
+ return XCTAssert ( false , " Expected .PaymentPathSuccessful, got \( paymentPathSuccessfulEvent. getValueType ( ) ) " )
670
+ }
651
671
let paymentSent = paymentSentEvent. getValueAsPaymentSent ( ) !
652
672
let paymentPathSuccessful = paymentPathSuccessfulEvent. getValueAsPaymentPathSuccessful ( ) !
653
673
print ( " sent payment \( paymentSent. getPayment_id ( ) ) with fee \( paymentSent. getFee_paid_msat ( ) . getValue ( ) ) via \( paymentPathSuccessful. getPath ( ) . map { h in h. get_short_channel_id ( ) } ) " )
@@ -699,8 +719,10 @@ public class HumanObjectPeerTestInstance {
699
719
700
720
do {
701
721
// process HTLCs
702
- let peer1Event = await peer1. getManagerEvents ( expectedCount: 1 ) [ 0 ]
703
- XCTAssertEqual ( peer1Event. getValueType ( ) , . PendingHTLCsForwardable)
722
+ let peer1Event = try ! await peer1. getManagerEvents ( expectedCount: 1 ) [ 0 ]
723
+ guard case . PendingHTLCsForwardable = peer1Event. getValueType ( ) else {
724
+ return XCTAssert ( false , " Expected .PendingHTLCsForwardable, got \( peer1Event. getValueType ( ) ) " )
725
+ }
704
726
let pendingHTLCsForwardable = peer1Event. getValueAsPendingHTLCsForwardable ( ) !
705
727
print ( " forwardable time: \( pendingHTLCsForwardable. getTime_forwardable ( ) ) " )
706
728
peer1. channelManager. process_pending_htlc_forwards ( )
@@ -709,13 +731,17 @@ public class HumanObjectPeerTestInstance {
709
731
710
732
do {
711
733
// process payment
712
- let peer1Event = await peer1. getManagerEvents ( expectedCount: 1 ) [ 0 ]
713
- XCTAssertEqual ( peer1Event. getValueType ( ) , . PaymentReceived)
734
+ let peer1Event = try ! await peer1. getManagerEvents ( expectedCount: 1 ) [ 0 ]
735
+ guard case . PaymentReceived = peer1Event. getValueType ( ) else {
736
+ return XCTAssert ( false , " Expected .PaymentReceived, got \( peer1Event. getValueType ( ) ) " )
737
+ }
714
738
let paymentReceived = peer1Event. getValueAsPaymentReceived ( ) !
715
739
let paymentHash = paymentReceived. getPayment_hash ( )
716
740
print ( " received payment of \( paymentReceived. getAmount_msat ( ) ) with hash \( paymentHash) " )
717
741
let paymentPurpose = paymentReceived. getPurpose ( )
718
- XCTAssertEqual ( paymentPurpose. getValueType ( ) , . InvoicePayment)
742
+ guard case . InvoicePayment = paymentPurpose. getValueType ( ) else {
743
+ return XCTAssert ( false , " Expected .InvoicePayment, got \( paymentPurpose. getValueType ( ) ) " )
744
+ }
719
745
let invoicePayment = paymentPurpose. getValueAsInvoicePayment ( ) !
720
746
let preimage = invoicePayment. getPayment_preimage ( )
721
747
let secret = invoicePayment. getPayment_secret ( )
@@ -725,14 +751,24 @@ public class HumanObjectPeerTestInstance {
725
751
726
752
do {
727
753
// process payment
728
- let peer2Events = await peer2. getManagerEvents ( expectedCount: 2 )
729
- let paymentSentEvent = peer2Events [ 0 ]
730
- let paymentPathSuccessfulEvent = peer2Events [ 1 ]
731
- XCTAssertEqual ( paymentSentEvent. getValueType ( ) , . PaymentSent)
732
- XCTAssertEqual ( paymentPathSuccessfulEvent. getValueType ( ) , . PaymentPathSuccessful)
754
+ let peer2Events = try ! await peer2. getManagerEvents ( expectedCount: 3 )
755
+ print ( " received event count: \( peer2Events. count) " )
756
+ let paymentClaimedEvent = peer2Events [ 0 ]
757
+ let paymentSentEvent = peer2Events [ 1 ]
758
+ let paymentPathSuccessfulEvent = peer2Events [ 2 ]
759
+ guard case . PaymentClaimed = paymentClaimedEvent. getValueType ( ) else {
760
+ return XCTAssert ( false , " Expected .PaymentClaimed, got \( paymentClaimedEvent. getValueType ( ) ) " )
761
+ }
762
+ guard case . PaymentSent = paymentSentEvent. getValueType ( ) else {
763
+ return XCTAssert ( false , " Expected .PaymentSent, got \( paymentSentEvent. getValueType ( ) ) " )
764
+ }
765
+ guard case . PaymentPathSuccessful = paymentPathSuccessfulEvent. getValueType ( ) else {
766
+ return XCTAssert ( false , " Expected .PaymentPathSuccessful, got \( paymentPathSuccessfulEvent. getValueType ( ) ) " )
767
+ }
768
+ let paymentClaimed = paymentClaimedEvent. getValueAsPaymentClaimed ( ) !
733
769
let paymentSent = paymentSentEvent. getValueAsPaymentSent ( ) !
734
770
let paymentPathSuccessful = paymentPathSuccessfulEvent. getValueAsPaymentPathSuccessful ( ) !
735
- print ( " sent payment \( paymentSent. getPayment_id ( ) ) with fee \( paymentSent. getFee_paid_msat ( ) . getValue ( ) ) via \( paymentPathSuccessful. getPath ( ) . map { h in h. get_short_channel_id ( ) } ) " )
771
+ print ( " sent payment \( paymentSent. getPayment_id ( ) ) worth \( paymentClaimed . getAmount_msat ( ) ) with fee \( paymentSent. getFee_paid_msat ( ) . getValue ( ) ) via \( paymentPathSuccessful. getPath ( ) . map { h in h. get_short_channel_id ( ) } ) " )
736
772
}
737
773
738
774
var currentChannelABalance = prePaymentBalanceAToB
0 commit comments