Skip to content

Commit 8f37699

Browse files
committed
discovery: prepare tests for shared chain state
Convert a bunch of the helper functions to instead be methods on the testCtx type. This is in preparation for adding a mockChain to the testCtx that these helpers can then use to add blocks and utxos to. See `notifications_test.go` for an idea of what we are trying to emulate here. Once the funding tx code has moved to the gossiper, then the logic in `notifications_test.go` will be removed.
1 parent b117daa commit 8f37699

File tree

1 file changed

+60
-41
lines changed

1 file changed

+60
-41
lines changed

discovery/gossiper_test.go

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -468,15 +468,23 @@ type annBatch struct {
468468
remoteProofAnn *lnwire.AnnounceSignatures1
469469
}
470470

471-
func createLocalAnnouncements(blockHeight uint32) (*annBatch, error) {
472-
return createAnnouncements(blockHeight, selfKeyPriv, remoteKeyPriv1)
471+
func (ctx *testCtx) createLocalAnnouncements(blockHeight uint32) (*annBatch,
472+
error) {
473+
474+
return ctx.createAnnouncements(blockHeight, selfKeyPriv, remoteKeyPriv1)
473475
}
474476

475-
func createRemoteAnnouncements(blockHeight uint32) (*annBatch, error) {
476-
return createAnnouncements(blockHeight, remoteKeyPriv1, remoteKeyPriv2)
477+
func (ctx *testCtx) createRemoteAnnouncements(blockHeight uint32) (*annBatch,
478+
error) {
479+
480+
return ctx.createAnnouncements(
481+
blockHeight, remoteKeyPriv1, remoteKeyPriv2,
482+
)
477483
}
478484

479-
func createAnnouncements(blockHeight uint32, key1, key2 *btcec.PrivateKey) (*annBatch, error) {
485+
func (ctx *testCtx) createAnnouncements(blockHeight uint32, key1,
486+
key2 *btcec.PrivateKey) (*annBatch, error) {
487+
480488
var err error
481489
var batch annBatch
482490
timestamp := testTimestamp
@@ -491,7 +499,9 @@ func createAnnouncements(blockHeight uint32, key1, key2 *btcec.PrivateKey) (*ann
491499
return nil, err
492500
}
493501

494-
batch.chanAnn, err = createChannelAnnouncement(blockHeight, key1, key2)
502+
batch.chanAnn, err = ctx.createChannelAnnouncement(
503+
blockHeight, key1, key2,
504+
)
495505
if err != nil {
496506
return nil, err
497507
}
@@ -616,7 +626,7 @@ func signUpdate(nodeKey *btcec.PrivateKey, a *lnwire.ChannelUpdate1) error {
616626
return nil
617627
}
618628

619-
func createAnnouncementWithoutProof(blockHeight uint32,
629+
func (ctx *testCtx) createAnnouncementWithoutProof(blockHeight uint32,
620630
key1, key2 *btcec.PublicKey,
621631
extraBytes ...[]byte) *lnwire.ChannelAnnouncement1 {
622632

@@ -639,16 +649,21 @@ func createAnnouncementWithoutProof(blockHeight uint32,
639649
return a
640650
}
641651

642-
func createRemoteChannelAnnouncement(blockHeight uint32,
652+
func (ctx *testCtx) createRemoteChannelAnnouncement(blockHeight uint32,
643653
extraBytes ...[]byte) (*lnwire.ChannelAnnouncement1, error) {
644654

645-
return createChannelAnnouncement(blockHeight, remoteKeyPriv1, remoteKeyPriv2, extraBytes...)
655+
return ctx.createChannelAnnouncement(
656+
blockHeight, remoteKeyPriv1, remoteKeyPriv2, extraBytes...,
657+
)
646658
}
647659

648-
func createChannelAnnouncement(blockHeight uint32, key1, key2 *btcec.PrivateKey,
660+
func (ctx *testCtx) createChannelAnnouncement(blockHeight uint32, key1,
661+
key2 *btcec.PrivateKey,
649662
extraBytes ...[]byte) (*lnwire.ChannelAnnouncement1, error) {
650663

651-
a := createAnnouncementWithoutProof(blockHeight, key1.PubKey(), key2.PubKey(), extraBytes...)
664+
a := ctx.createAnnouncementWithoutProof(
665+
blockHeight, key1.PubKey(), key2.PubKey(), extraBytes...,
666+
)
652667

653668
signer := mock.SingleSigner{Privkey: key1}
654669
sig, err := netann.SignAnnouncement(&signer, testKeyLoc, a)
@@ -846,7 +861,7 @@ func TestProcessAnnouncement(t *testing.T) {
846861

847862
// First, we'll craft a valid remote channel announcement and send it to
848863
// the gossiper so that it can be processed.
849-
ca, err := createRemoteChannelAnnouncement(0)
864+
ca, err := ctx.createRemoteChannelAnnouncement(0)
850865
require.NoError(t, err, "can't create channel announcement")
851866

852867
select {
@@ -958,7 +973,7 @@ func TestPrematureAnnouncement(t *testing.T) {
958973
// remote side, but block height of this announcement is greater than
959974
// highest know to us, for that reason it should be ignored and not
960975
// added to the router.
961-
ca, err := createRemoteChannelAnnouncement(1)
976+
ca, err := ctx.createRemoteChannelAnnouncement(1)
962977
require.NoError(t, err, "can't create channel announcement")
963978

964979
select {
@@ -996,7 +1011,7 @@ func TestSignatureAnnouncementLocalFirst(t *testing.T) {
9961011
}
9971012
}
9981013

999-
batch, err := createLocalAnnouncements(0)
1014+
batch, err := ctx.createLocalAnnouncements(0)
10001015
require.NoError(t, err, "can't generate announcements")
10011016

10021017
remoteKey, err := btcec.ParsePubKey(batch.nodeAnn2.NodeID[:])
@@ -1172,7 +1187,7 @@ func TestOrphanSignatureAnnouncement(t *testing.T) {
11721187
}
11731188
}
11741189

1175-
batch, err := createLocalAnnouncements(0)
1190+
batch, err := ctx.createLocalAnnouncements(0)
11761191
require.NoError(t, err, "can't generate announcements")
11771192

11781193
remoteKey, err := btcec.ParsePubKey(batch.nodeAnn2.NodeID[:])
@@ -1343,7 +1358,7 @@ func TestSignatureAnnouncementRetryAtStartup(t *testing.T) {
13431358
ctx, err := createTestCtx(t, proofMatureDelta, false)
13441359
require.NoError(t, err, "can't create context")
13451360

1346-
batch, err := createLocalAnnouncements(0)
1361+
batch, err := ctx.createLocalAnnouncements(0)
13471362
require.NoError(t, err, "can't generate announcements")
13481363

13491364
remoteKey, err := btcec.ParsePubKey(batch.nodeAnn2.NodeID[:])
@@ -1578,7 +1593,7 @@ func TestSignatureAnnouncementFullProofWhenRemoteProof(t *testing.T) {
15781593
ctx, err := createTestCtx(t, proofMatureDelta, false)
15791594
require.NoError(t, err, "can't create context")
15801595

1581-
batch, err := createLocalAnnouncements(0)
1596+
batch, err := ctx.createLocalAnnouncements(0)
15821597
require.NoError(t, err, "can't generate announcements")
15831598

15841599
remoteKey, err := btcec.ParsePubKey(batch.nodeAnn2.NodeID[:])
@@ -1766,6 +1781,8 @@ func TestDeDuplicatedAnnouncements(t *testing.T) {
17661781
timestamp := testTimestamp
17671782
announcements := deDupedAnnouncements{}
17681783
announcements.Reset()
1784+
ctx, err := createTestCtx(t, 0, false)
1785+
require.NoError(t, err)
17691786

17701787
// Ensure that after new deDupedAnnouncements struct is created and
17711788
// reset that storage of each announcement type is empty.
@@ -1781,7 +1798,7 @@ func TestDeDuplicatedAnnouncements(t *testing.T) {
17811798

17821799
// Ensure that remote channel announcements are properly stored
17831800
// and de-duplicated.
1784-
ca, err := createRemoteChannelAnnouncement(0)
1801+
ca, err := ctx.createRemoteChannelAnnouncement(0)
17851802
require.NoError(t, err, "can't create remote channel announcement")
17861803

17871804
nodePeer := &mockPeer{bitcoinKeyPub2, nil, nil, atomic.Bool{}}
@@ -1797,7 +1814,7 @@ func TestDeDuplicatedAnnouncements(t *testing.T) {
17971814
// We'll create a second instance of the same announcement with the
17981815
// same channel ID. Adding this shouldn't cause an increase in the
17991816
// number of items as they should be de-duplicated.
1800-
ca2, err := createRemoteChannelAnnouncement(0)
1817+
ca2, err := ctx.createRemoteChannelAnnouncement(0)
18011818
require.NoError(t, err, "can't create remote channel announcement")
18021819
announcements.AddMsgs(networkMsg{
18031820
msg: ca2,
@@ -2022,7 +2039,7 @@ func TestForwardPrivateNodeAnnouncement(t *testing.T) {
20222039
// We'll start off by processing a channel announcement without a proof
20232040
// (i.e., an unadvertised channel), followed by a node announcement for
20242041
// this same channel announcement.
2025-
chanAnn := createAnnouncementWithoutProof(
2042+
chanAnn := ctx.createAnnouncementWithoutProof(
20262043
startingHeight-2, selfKeyDesc.PubKey, remoteKeyPub1,
20272044
)
20282045
pubKey := remoteKeyPriv1.PubKey()
@@ -2068,7 +2085,9 @@ func TestForwardPrivateNodeAnnouncement(t *testing.T) {
20682085
// by opening a public channel on the network. We'll create a
20692086
// ChannelAnnouncement and hand it off to the gossiper in order to
20702087
// process it.
2071-
remoteChanAnn, err := createRemoteChannelAnnouncement(startingHeight - 1)
2088+
remoteChanAnn, err := ctx.createRemoteChannelAnnouncement(
2089+
startingHeight - 1,
2090+
)
20722091
require.NoError(t, err, "unable to create remote channel announcement")
20732092
peer := &mockPeer{pubKey, nil, nil, atomic.Bool{}}
20742093

@@ -2118,7 +2137,7 @@ func TestRejectZombieEdge(t *testing.T) {
21182137
ctx, err := createTestCtx(t, 0, false)
21192138
require.NoError(t, err, "unable to create test context")
21202139

2121-
batch, err := createRemoteAnnouncements(0)
2140+
batch, err := ctx.createRemoteAnnouncements(0)
21222141
require.NoError(t, err, "unable to create announcements")
21232142
remotePeer := &mockPeer{pk: remoteKeyPriv2.PubKey()}
21242143

@@ -2219,7 +2238,7 @@ func TestProcessZombieEdgeNowLive(t *testing.T) {
22192238
ctx, err := createTestCtx(t, 0, false)
22202239
require.NoError(t, err, "unable to create test context")
22212240

2222-
batch, err := createRemoteAnnouncements(0)
2241+
batch, err := ctx.createRemoteAnnouncements(0)
22232242
require.NoError(t, err, "unable to create announcements")
22242243

22252244
remotePeer := &mockPeer{pk: remoteKeyPriv1.PubKey()}
@@ -2374,7 +2393,7 @@ func TestReceiveRemoteChannelUpdateFirst(t *testing.T) {
23742393
ctx, err := createTestCtx(t, proofMatureDelta, false)
23752394
require.NoError(t, err, "can't create context")
23762395

2377-
batch, err := createLocalAnnouncements(0)
2396+
batch, err := ctx.createLocalAnnouncements(0)
23782397
require.NoError(t, err, "can't generate announcements")
23792398

23802399
remoteKey, err := btcec.ParsePubKey(batch.nodeAnn2.NodeID[:])
@@ -2581,7 +2600,7 @@ func TestExtraDataChannelAnnouncementValidation(t *testing.T) {
25812600
// that we don't know of ourselves, but should still include in the
25822601
// final signature check.
25832602
extraBytes := []byte("gotta validate this still!")
2584-
ca, err := createRemoteChannelAnnouncement(0, extraBytes)
2603+
ca, err := ctx.createRemoteChannelAnnouncement(0, extraBytes)
25852604
require.NoError(t, err, "can't create channel announcement")
25862605

25872606
// We'll now send the announcement to the main gossiper. We should be
@@ -2613,7 +2632,7 @@ func TestExtraDataChannelUpdateValidation(t *testing.T) {
26132632
// In this scenario, we'll create two announcements, one regular
26142633
// channel announcement, and another channel update announcement, that
26152634
// has additional data that we won't be interpreting.
2616-
chanAnn, err := createRemoteChannelAnnouncement(0)
2635+
chanAnn, err := ctx.createRemoteChannelAnnouncement(0)
26172636
require.NoError(t, err, "unable to create chan ann")
26182637
chanUpdAnn1, err := createUpdateAnnouncement(
26192638
0, 0, remoteKeyPriv1, timestamp,
@@ -2729,7 +2748,7 @@ func TestRetransmit(t *testing.T) {
27292748
ctx, err := createTestCtx(t, proofMatureDelta, false)
27302749
require.NoError(t, err, "can't create context")
27312750

2732-
batch, err := createLocalAnnouncements(0)
2751+
batch, err := ctx.createLocalAnnouncements(0)
27332752
require.NoError(t, err, "can't generate announcements")
27342753

27352754
remoteKey, err := btcec.ParsePubKey(batch.nodeAnn2.NodeID[:])
@@ -2835,7 +2854,7 @@ func TestNodeAnnouncementNoChannels(t *testing.T) {
28352854
ctx, err := createTestCtx(t, 0, false)
28362855
require.NoError(t, err, "can't create context")
28372856

2838-
batch, err := createRemoteAnnouncements(0)
2857+
batch, err := ctx.createRemoteAnnouncements(0)
28392858
require.NoError(t, err, "can't generate announcements")
28402859

28412860
remoteKey, err := btcec.ParsePubKey(batch.nodeAnn2.NodeID[:])
@@ -2928,7 +2947,7 @@ func TestOptionalFieldsChannelUpdateValidation(t *testing.T) {
29282947

29292948
// In this scenario, we'll test whether the message flags field in a
29302949
// channel update is properly handled.
2931-
chanAnn, err := createRemoteChannelAnnouncement(chanUpdateHeight)
2950+
chanAnn, err := ctx.createRemoteChannelAnnouncement(chanUpdateHeight)
29322951
require.NoError(t, err, "can't create channel announcement")
29332952

29342953
select {
@@ -3019,7 +3038,7 @@ func TestSendChannelUpdateReliably(t *testing.T) {
30193038
ctx, err := createTestCtx(t, proofMatureDelta, false)
30203039
require.NoError(t, err, "unable to create test context")
30213040

3022-
batch, err := createLocalAnnouncements(0)
3041+
batch, err := ctx.createLocalAnnouncements(0)
30233042
require.NoError(t, err, "can't generate announcements")
30243043

30253044
// We'll also create two keys, one for ourselves and another for the
@@ -3376,7 +3395,7 @@ func TestPropagateChanPolicyUpdate(t *testing.T) {
33763395
const numChannels = 3
33773396
channelsToAnnounce := make([]*annBatch, 0, numChannels)
33783397
for i := 0; i < numChannels; i++ {
3379-
newChan, err := createLocalAnnouncements(uint32(i + 1))
3398+
newChan, err := ctx.createLocalAnnouncements(uint32(i + 1))
33803399
if err != nil {
33813400
t.Fatalf("unable to make new channel ann: %v", err)
33823401
}
@@ -3553,10 +3572,10 @@ func TestProcessChannelAnnouncementOptionalMsgFields(t *testing.T) {
35533572
ctx, err := createTestCtx(t, 0, false)
35543573
require.NoError(t, err, "unable to create test context")
35553574

3556-
chanAnn1 := createAnnouncementWithoutProof(
3575+
chanAnn1 := ctx.createAnnouncementWithoutProof(
35573576
100, selfKeyDesc.PubKey, remoteKeyPub1,
35583577
)
3559-
chanAnn2 := createAnnouncementWithoutProof(
3578+
chanAnn2 := ctx.createAnnouncementWithoutProof(
35603579
101, selfKeyDesc.PubKey, remoteKeyPub1,
35613580
)
35623581

@@ -3772,7 +3791,7 @@ func TestBroadcastAnnsAfterGraphSynced(t *testing.T) {
37723791

37733792
// A remote channel announcement should not be broadcast since the graph
37743793
// has not yet been synced.
3775-
chanAnn1, err := createRemoteChannelAnnouncement(0)
3794+
chanAnn1, err := ctx.createRemoteChannelAnnouncement(0)
37763795
require.NoError(t, err, "unable to create channel announcement")
37773796
assertBroadcast(chanAnn1, true, false)
37783797

@@ -3786,7 +3805,7 @@ func TestBroadcastAnnsAfterGraphSynced(t *testing.T) {
37863805
// should to be broadcast.
37873806
ctx.gossiper.syncMgr.markGraphSynced()
37883807

3789-
chanAnn2, err := createRemoteChannelAnnouncement(1)
3808+
chanAnn2, err := ctx.createRemoteChannelAnnouncement(1)
37903809
require.NoError(t, err, "unable to create channel announcement")
37913810
assertBroadcast(chanAnn2, true, true)
37923811
}
@@ -3811,7 +3830,7 @@ func TestRateLimitChannelUpdates(t *testing.T) {
38113830
// We'll create a batch of signed announcements, including updates for
38123831
// both sides, for a channel and process them. They should all be
38133832
// forwarded as this is our first time learning about the channel.
3814-
batch, err := createRemoteAnnouncements(blockHeight)
3833+
batch, err := ctx.createRemoteAnnouncements(blockHeight)
38153834
require.NoError(t, err)
38163835

38173836
nodePeer1 := &mockPeer{
@@ -3951,7 +3970,7 @@ func TestIgnoreOwnAnnouncement(t *testing.T) {
39513970
ctx, err := createTestCtx(t, proofMatureDelta, false)
39523971
require.NoError(t, err, "can't create context")
39533972

3954-
batch, err := createLocalAnnouncements(0)
3973+
batch, err := ctx.createLocalAnnouncements(0)
39553974
require.NoError(t, err, "can't generate announcements")
39563975

39573976
remoteKey, err := btcec.ParsePubKey(batch.nodeAnn2.NodeID[:])
@@ -4097,7 +4116,7 @@ func TestRejectCacheChannelAnn(t *testing.T) {
40974116

40984117
// First, we create a channel announcement to send over to our test
40994118
// peer.
4100-
batch, err := createRemoteAnnouncements(0)
4119+
batch, err := ctx.createRemoteAnnouncements(0)
41014120
require.NoError(t, err, "can't generate announcements")
41024121

41034122
remoteKey, err := btcec.ParsePubKey(batch.nodeAnn2.NodeID[:])
@@ -4189,7 +4208,7 @@ func TestChanAnnBanningNonChanPeer(t *testing.T) {
41894208
// Craft a valid channel announcement for a channel we don't
41904209
// have. We will ensure that it fails validation by modifying
41914210
// the router.
4192-
ca, err := createRemoteChannelAnnouncement(uint32(i))
4211+
ca, err := ctx.createRemoteChannelAnnouncement(uint32(i))
41934212
require.NoError(t, err, "can't create channel announcement")
41944213

41954214
select {
@@ -4209,7 +4228,7 @@ func TestChanAnnBanningNonChanPeer(t *testing.T) {
42094228
// Assert that nodePeer has been disconnected.
42104229
require.True(t, nodePeer1.disconnected.Load())
42114230

4212-
ca, err := createRemoteChannelAnnouncement(101)
4231+
ca, err := ctx.createRemoteChannelAnnouncement(101)
42134232
require.NoError(t, err, "can't create channel announcement")
42144233

42154234
// Set the error to ErrChannelSpent so that we can test that the
@@ -4269,7 +4288,7 @@ func TestChanAnnBanningChanPeer(t *testing.T) {
42694288
// Craft a valid channel announcement for a channel we don't
42704289
// have. We will ensure that it fails validation by modifying
42714290
// the router.
4272-
ca, err := createRemoteChannelAnnouncement(uint32(i))
4291+
ca, err := ctx.createRemoteChannelAnnouncement(uint32(i))
42734292
require.NoError(t, err, "can't create channel announcement")
42744293

42754294
select {

0 commit comments

Comments
 (0)