Skip to content

Commit 8a07bb0

Browse files
committed
discovery: prepare tests for preparing the mock chain
Here, we add a new fundingTxOption modifier which will configure how we set-up expected calls to the mock Chain once we have moved funding tx logic to the gossiper. Note that in this commit, these modifiers don't yet do anything.
1 parent 22e391f commit 8a07bb0

File tree

1 file changed

+79
-8
lines changed

1 file changed

+79
-8
lines changed

discovery/gossiper_test.go

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,37 @@ func signUpdate(nodeKey *btcec.PrivateKey, a *lnwire.ChannelUpdate1) error {
644644
return nil
645645
}
646646

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+
647675
type fundingTxOpts struct {
648-
extraBytes []byte
676+
extraBytes []byte
677+
fundingTxPrep fundingTxPrepType
649678
}
650679

651680
type fundingTxOption func(*fundingTxOpts)
@@ -656,6 +685,12 @@ func withExtraBytes(extraBytes []byte) fundingTxOption {
656685
}
657686
}
658687

688+
func withFundingTxPrep(prep fundingTxPrepType) fundingTxOption {
689+
return func(opts *fundingTxOpts) {
690+
opts.fundingTxPrep = prep
691+
}
692+
}
693+
659694
func (ctx *testCtx) createAnnouncementWithoutProof(blockHeight uint32,
660695
key1, key2 *btcec.PublicKey,
661696
options ...fundingTxOption) *lnwire.ChannelAnnouncement1 {
@@ -665,6 +700,19 @@ func (ctx *testCtx) createAnnouncementWithoutProof(blockHeight uint32,
665700
opt(&opts)
666701
}
667702

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+
668716
a := &lnwire.ChannelAnnouncement1{
669717
ShortChannelID: lnwire.ShortChannelID{
670718
BlockHeight: blockHeight,
@@ -1015,7 +1063,9 @@ func TestPrematureAnnouncement(t *testing.T) {
10151063
// remote side, but block height of this announcement is greater than
10161064
// highest know to us, for that reason it should be ignored and not
10171065
// added to the router.
1018-
ca, err := ctx.createRemoteChannelAnnouncement(1)
1066+
ca, err := ctx.createRemoteChannelAnnouncement(
1067+
1, withFundingTxPrep(fundingTxPrepTypeNone),
1068+
)
10191069
require.NoError(t, err, "can't create channel announcement")
10201070

10211071
select {
@@ -1840,7 +1890,9 @@ func TestDeDuplicatedAnnouncements(t *testing.T) {
18401890

18411891
// Ensure that remote channel announcements are properly stored
18421892
// and de-duplicated.
1843-
ca, err := ctx.createRemoteChannelAnnouncement(0)
1893+
ca, err := ctx.createRemoteChannelAnnouncement(
1894+
0, withFundingTxPrep(fundingTxPrepTypeNone),
1895+
)
18441896
require.NoError(t, err, "can't create remote channel announcement")
18451897

18461898
nodePeer := &mockPeer{bitcoinKeyPub2, nil, nil, atomic.Bool{}}
@@ -1856,7 +1908,9 @@ func TestDeDuplicatedAnnouncements(t *testing.T) {
18561908
// We'll create a second instance of the same announcement with the
18571909
// same channel ID. Adding this shouldn't cause an increase in the
18581910
// 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+
)
18601914
require.NoError(t, err, "can't create remote channel announcement")
18611915
announcements.AddMsgs(networkMsg{
18621916
msg: ca2,
@@ -3616,11 +3670,18 @@ func TestProcessChannelAnnouncementOptionalMsgFields(t *testing.T) {
36163670
ctx, err := createTestCtx(t, 0, false)
36173671
require.NoError(t, err, "unable to create test context")
36183672

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+
36193678
chanAnn1 := ctx.createAnnouncementWithoutProof(
36203679
100, selfKeyDesc.PubKey, remoteKeyPub1,
3680+
withFundingTxPrep(fundingTxPrepTypeNone),
36213681
)
36223682
chanAnn2 := ctx.createAnnouncementWithoutProof(
36233683
101, selfKeyDesc.PubKey, remoteKeyPub1,
3684+
withFundingTxPrep(fundingTxPrepTypeNone),
36243685
)
36253686

36263687
// assertOptionalMsgFields is a helper closure that ensures the optional
@@ -4251,8 +4312,11 @@ func TestChanAnnBanningNonChanPeer(t *testing.T) {
42514312
for i := 0; i < 100; i++ {
42524313
// Craft a valid channel announcement for a channel we don't
42534314
// 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+
)
42564320
require.NoError(t, err, "can't create channel announcement")
42574321

42584322
select {
@@ -4272,7 +4336,11 @@ func TestChanAnnBanningNonChanPeer(t *testing.T) {
42724336
// Assert that nodePeer has been disconnected.
42734337
require.True(t, nodePeer1.disconnected.Load())
42744338

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+
)
42764344
require.NoError(t, err, "can't create channel announcement")
42774345

42784346
// Set the error to ErrChannelSpent so that we can test that the
@@ -4332,7 +4400,10 @@ func TestChanAnnBanningChanPeer(t *testing.T) {
43324400
// Craft a valid channel announcement for a channel we don't
43334401
// have. We will ensure that it fails validation by modifying
43344402
// the router.
4335-
ca, err := ctx.createRemoteChannelAnnouncement(uint32(i))
4403+
ca, err := ctx.createRemoteChannelAnnouncement(
4404+
uint32(i),
4405+
withFundingTxPrep(fundingTxPrepTypeInvalidOutput),
4406+
)
43364407
require.NoError(t, err, "can't create channel announcement")
43374408

43384409
select {

0 commit comments

Comments
 (0)