@@ -644,8 +644,37 @@ func signUpdate(nodeKey *btcec.PrivateKey, a *lnwire.ChannelUpdate1) error {
644
644
return nil
645
645
}
646
646
647
+ // fundingTxPrepType determines how we will prep the mock Chain for calls during
648
+ // a test run.
649
+ type fundingTxPrepType int
650
+
651
+ const (
652
+ // fundingTxPrepTypeGood is the default type and will result in a valid
653
+ // block and funding transaction being returned by the mock Chain.
654
+ fundingTxPrepTypeGood fundingTxPrepType = iota
655
+
656
+ // fundingTxPrepTypeNone will result in the mock Chain not being prepped
657
+ // for any calls.
658
+ fundingTxPrepTypeNone
659
+
660
+ // fundingTxPrepTypeInvalidOutput will result in the mock Chain
661
+ // behaving such that the funding transaction it returns in a block is
662
+ // invalid.
663
+ fundingTxPrepTypeInvalidOutput
664
+
665
+ // fundingTxPrepTypeNoTx will result in the mock Chain behaving such
666
+ // the desired block cannot be found.
667
+ fundingTxPrepTypeNoTx
668
+
669
+ // fundingTxPrepTypeSpent will result in the mock Chain behaving such
670
+ // that the block is valid but the GetUtxo call will return a
671
+ // btcwallet.ErrOutputSpent error.
672
+ fundingTxPrepTypeSpent
673
+ )
674
+
647
675
type fundingTxOpts struct {
648
- extraBytes []byte
676
+ extraBytes []byte
677
+ fundingTxPrep fundingTxPrepType
649
678
}
650
679
651
680
type fundingTxOption func (* fundingTxOpts )
@@ -656,6 +685,12 @@ func withExtraBytes(extraBytes []byte) fundingTxOption {
656
685
}
657
686
}
658
687
688
+ func withFundingTxPrep (prep fundingTxPrepType ) fundingTxOption {
689
+ return func (opts * fundingTxOpts ) {
690
+ opts .fundingTxPrep = prep
691
+ }
692
+ }
693
+
659
694
func (ctx * testCtx ) createAnnouncementWithoutProof (blockHeight uint32 ,
660
695
key1 , key2 * btcec.PublicKey ,
661
696
options ... fundingTxOption ) * lnwire.ChannelAnnouncement1 {
@@ -665,6 +700,19 @@ func (ctx *testCtx) createAnnouncementWithoutProof(blockHeight uint32,
665
700
opt (& opts )
666
701
}
667
702
703
+ // TODO(elle): prepare the mock chain calls accordingly.
704
+ switch opts .fundingTxPrep {
705
+ case fundingTxPrepTypeGood :
706
+
707
+ case fundingTxPrepTypeInvalidOutput :
708
+
709
+ case fundingTxPrepTypeSpent :
710
+
711
+ case fundingTxPrepTypeNoTx :
712
+
713
+ case fundingTxPrepTypeNone :
714
+ }
715
+
668
716
a := & lnwire.ChannelAnnouncement1 {
669
717
ShortChannelID : lnwire.ShortChannelID {
670
718
BlockHeight : blockHeight ,
@@ -1015,7 +1063,9 @@ func TestPrematureAnnouncement(t *testing.T) {
1015
1063
// remote side, but block height of this announcement is greater than
1016
1064
// highest know to us, for that reason it should be ignored and not
1017
1065
// added to the router.
1018
- ca , err := ctx .createRemoteChannelAnnouncement (1 )
1066
+ ca , err := ctx .createRemoteChannelAnnouncement (
1067
+ 1 , withFundingTxPrep (fundingTxPrepTypeNone ),
1068
+ )
1019
1069
require .NoError (t , err , "can't create channel announcement" )
1020
1070
1021
1071
select {
@@ -1840,7 +1890,9 @@ func TestDeDuplicatedAnnouncements(t *testing.T) {
1840
1890
1841
1891
// Ensure that remote channel announcements are properly stored
1842
1892
// and de-duplicated.
1843
- ca , err := ctx .createRemoteChannelAnnouncement (0 )
1893
+ ca , err := ctx .createRemoteChannelAnnouncement (
1894
+ 0 , withFundingTxPrep (fundingTxPrepTypeNone ),
1895
+ )
1844
1896
require .NoError (t , err , "can't create remote channel announcement" )
1845
1897
1846
1898
nodePeer := & mockPeer {bitcoinKeyPub2 , nil , nil , atomic.Bool {}}
@@ -1856,7 +1908,9 @@ func TestDeDuplicatedAnnouncements(t *testing.T) {
1856
1908
// We'll create a second instance of the same announcement with the
1857
1909
// same channel ID. Adding this shouldn't cause an increase in the
1858
1910
// number of items as they should be de-duplicated.
1859
- ca2 , err := ctx .createRemoteChannelAnnouncement (0 )
1911
+ ca2 , err := ctx .createRemoteChannelAnnouncement (
1912
+ 0 , withFundingTxPrep (fundingTxPrepTypeNone ),
1913
+ )
1860
1914
require .NoError (t , err , "can't create remote channel announcement" )
1861
1915
announcements .AddMsgs (networkMsg {
1862
1916
msg : ca2 ,
@@ -3616,11 +3670,18 @@ func TestProcessChannelAnnouncementOptionalMsgFields(t *testing.T) {
3616
3670
ctx , err := createTestCtx (t , 0 , false )
3617
3671
require .NoError (t , err , "unable to create test context" )
3618
3672
3673
+ // We set AssumeValid to true for this test so that the full validation
3674
+ // of a funding transaction is not done and ie, we don't fetch the
3675
+ // channel capacity from the on-chain transaction.
3676
+ ctx .gossiper .cfg .AssumeChannelValid = true
3677
+
3619
3678
chanAnn1 := ctx .createAnnouncementWithoutProof (
3620
3679
100 , selfKeyDesc .PubKey , remoteKeyPub1 ,
3680
+ withFundingTxPrep (fundingTxPrepTypeNone ),
3621
3681
)
3622
3682
chanAnn2 := ctx .createAnnouncementWithoutProof (
3623
3683
101 , selfKeyDesc .PubKey , remoteKeyPub1 ,
3684
+ withFundingTxPrep (fundingTxPrepTypeNone ),
3624
3685
)
3625
3686
3626
3687
// assertOptionalMsgFields is a helper closure that ensures the optional
@@ -4251,8 +4312,11 @@ func TestChanAnnBanningNonChanPeer(t *testing.T) {
4251
4312
for i := 0 ; i < 100 ; i ++ {
4252
4313
// Craft a valid channel announcement for a channel we don't
4253
4314
// have. We will ensure that it fails validation by modifying
4254
- // the router.
4255
- ca , err := ctx .createRemoteChannelAnnouncement (uint32 (i ))
4315
+ // the tx script.
4316
+ ca , err := ctx .createRemoteChannelAnnouncement (
4317
+ uint32 (i ),
4318
+ withFundingTxPrep (fundingTxPrepTypeInvalidOutput ),
4319
+ )
4256
4320
require .NoError (t , err , "can't create channel announcement" )
4257
4321
4258
4322
select {
@@ -4272,7 +4336,11 @@ func TestChanAnnBanningNonChanPeer(t *testing.T) {
4272
4336
// Assert that nodePeer has been disconnected.
4273
4337
require .True (t , nodePeer1 .disconnected .Load ())
4274
4338
4275
- ca , err := ctx .createRemoteChannelAnnouncement (101 )
4339
+ // Mark the UTXO as spent so that we get the ErrChannelSpent error and
4340
+ // can thus tests that the gossiper ignores closed channels.
4341
+ ca , err := ctx .createRemoteChannelAnnouncement (
4342
+ 101 , withFundingTxPrep (fundingTxPrepTypeSpent ),
4343
+ )
4276
4344
require .NoError (t , err , "can't create channel announcement" )
4277
4345
4278
4346
// Set the error to ErrChannelSpent so that we can test that the
@@ -4332,7 +4400,10 @@ func TestChanAnnBanningChanPeer(t *testing.T) {
4332
4400
// Craft a valid channel announcement for a channel we don't
4333
4401
// have. We will ensure that it fails validation by modifying
4334
4402
// the router.
4335
- ca , err := ctx .createRemoteChannelAnnouncement (uint32 (i ))
4403
+ ca , err := ctx .createRemoteChannelAnnouncement (
4404
+ uint32 (i ),
4405
+ withFundingTxPrep (fundingTxPrepTypeInvalidOutput ),
4406
+ )
4336
4407
require .NoError (t , err , "can't create channel announcement" )
4337
4408
4338
4409
select {
0 commit comments