@@ -146,14 +146,10 @@ func assertUnknownEventFail(t *testing.T, startingState ProtocolState) {
146
146
})
147
147
defer closeHarness .stopAndAssert ()
148
148
149
- closeHarness .expectFailure (ErrInvalidStateTransition )
150
-
151
- closeHarness .chanCloser .SendEvent (
149
+ closeHarness .sendEventAndExpectFailure (
152
150
context .Background (), & unknownEvent {},
151
+ ErrInvalidStateTransition ,
153
152
)
154
-
155
- // There should be no further state transitions.
156
- closeHarness .assertNoStateTransitions ()
157
153
})
158
154
}
159
155
@@ -239,6 +235,31 @@ func (r *rbfCloserTestHarness) assertExpectations() {
239
235
r .signer .AssertExpectations (r .T )
240
236
}
241
237
238
+ // sendEventAndExpectFailure is a helper to expect a failure, send an event,
239
+ // and wait for the error report.
240
+ func (r * rbfCloserTestHarness ) sendEventAndExpectFailure (
241
+ ctx context.Context , event ProtocolEvent , expectedErr error ) {
242
+
243
+ r .T .Helper ()
244
+
245
+ r .expectFailure (expectedErr )
246
+
247
+ r .chanCloser .SendEvent (ctx , event )
248
+
249
+ select {
250
+ case reportedErr := <- r .errReporter .errorReported :
251
+ r .T .Logf ("Test received error report: %v" , reportedErr )
252
+
253
+ if ! errors .Is (reportedErr , expectedErr ) {
254
+ r .T .Fatalf ("reported error (%v) is not the " +
255
+ "expected error (%v)" , reportedErr , expectedErr )
256
+ }
257
+
258
+ case <- time .After (1 * time .Second ):
259
+ r .T .Fatalf ("timed out waiting for error to be " +
260
+ "reported by mockErrorReporter for event %T" , event )
261
+ }
262
+ }
242
263
func (r * rbfCloserTestHarness ) stopAndAssert () {
243
264
r .T .Helper ()
244
265
@@ -728,7 +749,7 @@ func newRbfCloserTestHarness(t *testing.T,
728
749
729
750
feeEstimator := & mockFeeEstimator {}
730
751
mockObserver := & mockChanObserver {}
731
- errReporter := & mockErrorReporter {}
752
+ errReporter := newMockErrorReporter ( t )
732
753
mockSigner := & mockCloseSigner {}
733
754
734
755
harness := & rbfCloserTestHarness {
@@ -838,14 +859,13 @@ func TestRbfChannelActiveTransitions(t *testing.T) {
838
859
defer closeHarness .stopAndAssert ()
839
860
840
861
closeHarness .failNewAddrFunc ()
841
- closeHarness .expectFailure (errfailAddr )
842
862
843
863
// We don't specify an upfront shutdown addr, and don't specify
844
864
// on here in the vent, so we should call new addr, but then
845
865
// fail.
846
- closeHarness .chanCloser . SendEvent ( ctx , & SendShutdown {})
847
-
848
- // We shouldn't have transitioned to a new state.
866
+ closeHarness .sendEventAndExpectFailure (
867
+ ctx , & SendShutdown {}, errfailAddr ,
868
+ )
849
869
closeHarness .assertNoStateTransitions ()
850
870
})
851
871
@@ -902,16 +922,13 @@ func TestRbfChannelActiveTransitions(t *testing.T) {
902
922
903
923
// Next, we'll emit the recv event, with the addr of the remote
904
924
// party.
905
- closeHarness .chanCloser .SendEvent (
906
- ctx , & ShutdownReceived {
907
- ShutdownScript : remoteAddr ,
908
- BlockHeight : 1 ,
909
- },
925
+ event := & ShutdownReceived {
926
+ ShutdownScript : remoteAddr ,
927
+ BlockHeight : 1 ,
928
+ }
929
+ closeHarness .sendEventAndExpectFailure (
930
+ ctx , event , ErrThawHeightNotReached ,
910
931
)
911
-
912
- // We expect a failure as the block height is less than the
913
- // start height.
914
- closeHarness .expectFailure (ErrThawHeightNotReached )
915
932
})
916
933
917
934
// When we receive a shutdown, we should transition to the shutdown
@@ -998,18 +1015,16 @@ func TestRbfShutdownPendingTransitions(t *testing.T) {
998
1015
})
999
1016
defer closeHarness .stopAndAssert ()
1000
1017
1001
- // We should fail as the shutdown script isn't what we
1002
- // expected.
1003
- closeHarness .expectFailure (ErrUpfrontShutdownScriptMismatch )
1004
-
1005
1018
// We'll now send in a ShutdownReceived event, but with a
1006
1019
// different address provided in the shutdown message. This
1007
1020
// should result in an error.
1008
- closeHarness . chanCloser . SendEvent ( ctx , & ShutdownReceived {
1021
+ event := & ShutdownReceived {
1009
1022
ShutdownScript : localAddr ,
1010
- })
1023
+ }
1011
1024
1012
- // We shouldn't have transitioned to a new state.
1025
+ closeHarness .sendEventAndExpectFailure (
1026
+ ctx , event , ErrUpfrontShutdownScriptMismatch ,
1027
+ )
1013
1028
closeHarness .assertNoStateTransitions ()
1014
1029
})
1015
1030
@@ -1459,13 +1474,9 @@ func TestRbfCloseClosingNegotiationLocal(t *testing.T) {
1459
1474
},
1460
1475
}
1461
1476
1462
- // We expect that the state machine fails as we received more
1463
- // than one error.
1464
- closeHarness .expectFailure (ErrTooManySigs )
1465
-
1466
- // We should fail as the remote party sent us more than one
1467
- // signature.
1468
- closeHarness .chanCloser .SendEvent (ctx , localSigEvent )
1477
+ closeHarness .sendEventAndExpectFailure (
1478
+ ctx , localSigEvent , ErrTooManySigs ,
1479
+ )
1469
1480
})
1470
1481
1471
1482
// Next, we'll verify that if the balance of the remote party is dust,
@@ -1545,8 +1556,6 @@ func TestRbfCloseClosingNegotiationLocal(t *testing.T) {
1545
1556
1546
1557
// The remote party will send a ClosingSig message, but with the
1547
1558
// wrong local script. We should expect an error.
1548
- closeHarness .expectFailure (ErrWrongLocalScript )
1549
-
1550
1559
// We'll send this message in directly, as we shouldn't get any
1551
1560
// further in the process.
1552
1561
// assuming we start in this negotiation state.
@@ -1561,7 +1570,9 @@ func TestRbfCloseClosingNegotiationLocal(t *testing.T) {
1561
1570
},
1562
1571
},
1563
1572
}
1564
- closeHarness .chanCloser .SendEvent (ctx , localSigEvent )
1573
+ closeHarness .sendEventAndExpectFailure (
1574
+ ctx , localSigEvent , ErrWrongLocalScript ,
1575
+ )
1565
1576
})
1566
1577
1567
1578
// In this test, we'll assert that we're able to restart the RBF loop
@@ -1705,21 +1716,19 @@ func TestRbfCloseClosingNegotiationRemote(t *testing.T) {
1705
1716
})
1706
1717
defer closeHarness .stopAndAssert ()
1707
1718
1708
- // We should fail as they sent a sig, but can't pay for fees.
1709
- closeHarness .expectFailure (ErrRemoteCannotPay )
1710
-
1711
1719
// We'll send in a new fee proposal, but the proposed fee will
1712
1720
// be higher than the remote party's balance.
1713
- feeOffer := & OfferReceivedEvent {
1721
+ event := & OfferReceivedEvent {
1714
1722
SigMsg : lnwire.ClosingComplete {
1715
1723
CloserScript : remoteAddr ,
1716
1724
CloseeScript : localAddr ,
1717
1725
FeeSatoshis : absoluteFee * 10 ,
1718
1726
},
1719
1727
}
1720
- closeHarness .chanCloser .SendEvent (ctx , feeOffer )
1721
1728
1722
- // We shouldn't have transitioned to a new state.
1729
+ closeHarness .sendEventAndExpectFailure (
1730
+ ctx , event , ErrRemoteCannotPay ,
1731
+ )
1723
1732
closeHarness .assertNoStateTransitions ()
1724
1733
})
1725
1734
@@ -1747,12 +1756,9 @@ func TestRbfCloseClosingNegotiationRemote(t *testing.T) {
1747
1756
})
1748
1757
defer closeHarness .stopAndAssert ()
1749
1758
1750
- // We should fail as they included the wrong sig.
1751
- closeHarness .expectFailure (ErrCloserNoClosee )
1752
-
1753
1759
// Our balance is dust, so we should reject this signature that
1754
1760
// includes our output.
1755
- feeOffer := & OfferReceivedEvent {
1761
+ event := & OfferReceivedEvent {
1756
1762
SigMsg : lnwire.ClosingComplete {
1757
1763
FeeSatoshis : absoluteFee ,
1758
1764
CloserScript : remoteAddr ,
@@ -1764,9 +1770,9 @@ func TestRbfCloseClosingNegotiationRemote(t *testing.T) {
1764
1770
},
1765
1771
},
1766
1772
}
1767
- closeHarness .chanCloser . SendEvent ( ctx , feeOffer )
1768
-
1769
- // We shouldn't have transitioned to a new state.
1773
+ closeHarness .sendEventAndExpectFailure (
1774
+ ctx , event , ErrCloserNoClosee ,
1775
+ )
1770
1776
closeHarness .assertNoStateTransitions ()
1771
1777
})
1772
1778
@@ -1778,12 +1784,9 @@ func TestRbfCloseClosingNegotiationRemote(t *testing.T) {
1778
1784
})
1779
1785
defer closeHarness .stopAndAssert ()
1780
1786
1781
- // We should fail as they included the wrong sig.
1782
- closeHarness .expectFailure (ErrCloserAndClosee )
1783
-
1784
1787
// Both balances are above dust, we should reject this
1785
1788
// signature as it excludes an output.
1786
- feeOffer := & OfferReceivedEvent {
1789
+ event := & OfferReceivedEvent {
1787
1790
SigMsg : lnwire.ClosingComplete {
1788
1791
FeeSatoshis : absoluteFee ,
1789
1792
CloserScript : remoteAddr ,
@@ -1795,9 +1798,9 @@ func TestRbfCloseClosingNegotiationRemote(t *testing.T) {
1795
1798
},
1796
1799
},
1797
1800
}
1798
- closeHarness .chanCloser . SendEvent ( ctx , feeOffer )
1799
-
1800
- // We shouldn't have transitioned to a new state.
1801
+ closeHarness .sendEventAndExpectFailure (
1802
+ ctx , event , ErrCloserAndClosee ,
1803
+ )
1801
1804
closeHarness .assertNoStateTransitions ()
1802
1805
})
1803
1806
@@ -1875,11 +1878,9 @@ func TestRbfCloseClosingNegotiationRemote(t *testing.T) {
1875
1878
1876
1879
// The remote party will send a ClosingComplete message, but
1877
1880
// with the wrong local script. We should expect an error.
1878
- closeHarness .expectFailure (ErrWrongLocalScript )
1879
-
1880
1881
// We'll send our remote addr as the Closee script, which should
1881
1882
// trigger an error.
1882
- feeOffer := & OfferReceivedEvent {
1883
+ event := & OfferReceivedEvent {
1883
1884
SigMsg : lnwire.ClosingComplete {
1884
1885
FeeSatoshis : absoluteFee ,
1885
1886
CloserScript : remoteAddr ,
@@ -1891,9 +1892,9 @@ func TestRbfCloseClosingNegotiationRemote(t *testing.T) {
1891
1892
},
1892
1893
},
1893
1894
}
1894
- closeHarness .chanCloser . SendEvent ( ctx , feeOffer )
1895
-
1896
- // We shouldn't have transitioned to a new state.
1895
+ closeHarness .sendEventAndExpectFailure (
1896
+ ctx , event , ErrWrongLocalScript ,
1897
+ )
1897
1898
closeHarness .assertNoStateTransitions ()
1898
1899
})
1899
1900
0 commit comments