Skip to content

Commit 3e318b4

Browse files
Crypt-iQRoasbeef
authored andcommitted
multi: extend lnpeer.Peer interface with Disconnect function
This will be used in the gossiper to disconnect from peers if their ban score passes the ban threshold.
1 parent b7c4a3c commit 3e318b4

File tree

8 files changed

+78
-30
lines changed

8 files changed

+78
-30
lines changed

discovery/gossiper_test.go

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ func createTestCtx(t *testing.T, startHeight uint32) (*testCtx, error) {
765765
peerChan chan<- lnpeer.Peer) {
766766

767767
pk, _ := btcec.ParsePubKey(target[:])
768-
peerChan <- &mockPeer{pk, nil, nil}
768+
peerChan <- &mockPeer{pk, nil, nil, atomic.Bool{}}
769769
},
770770
NotifyWhenOffline: func(_ [33]byte) <-chan struct{} {
771771
c := make(chan struct{})
@@ -843,7 +843,7 @@ func TestProcessAnnouncement(t *testing.T) {
843843
}
844844
}
845845

846-
nodePeer := &mockPeer{remoteKeyPriv1.PubKey(), nil, nil}
846+
nodePeer := &mockPeer{remoteKeyPriv1.PubKey(), nil, nil, atomic.Bool{}}
847847

848848
// First, we'll craft a valid remote channel announcement and send it to
849849
// the gossiper so that it can be processed.
@@ -953,7 +953,7 @@ func TestPrematureAnnouncement(t *testing.T) {
953953
_, err = createNodeAnnouncement(remoteKeyPriv1, timestamp)
954954
require.NoError(t, err, "can't create node announcement")
955955

956-
nodePeer := &mockPeer{remoteKeyPriv1.PubKey(), nil, nil}
956+
nodePeer := &mockPeer{remoteKeyPriv1.PubKey(), nil, nil, atomic.Bool{}}
957957

958958
// Pretending that we receive the valid channel announcement from
959959
// remote side, but block height of this announcement is greater than
@@ -990,7 +990,9 @@ func TestSignatureAnnouncementLocalFirst(t *testing.T) {
990990
pk, _ := btcec.ParsePubKey(target[:])
991991

992992
select {
993-
case peerChan <- &mockPeer{pk, sentMsgs, ctx.gossiper.quit}:
993+
case peerChan <- &mockPeer{
994+
pk, sentMsgs, ctx.gossiper.quit, atomic.Bool{},
995+
}:
994996
case <-ctx.gossiper.quit:
995997
}
996998
}
@@ -1000,7 +1002,9 @@ func TestSignatureAnnouncementLocalFirst(t *testing.T) {
10001002

10011003
remoteKey, err := btcec.ParsePubKey(batch.nodeAnn2.NodeID[:])
10021004
require.NoError(t, err, "unable to parse pubkey")
1003-
remotePeer := &mockPeer{remoteKey, sentMsgs, ctx.gossiper.quit}
1005+
remotePeer := &mockPeer{
1006+
remoteKey, sentMsgs, ctx.gossiper.quit, atomic.Bool{},
1007+
}
10041008

10051009
// Recreate lightning network topology. Initialize router with channel
10061010
// between two nodes.
@@ -1162,7 +1166,9 @@ func TestOrphanSignatureAnnouncement(t *testing.T) {
11621166
pk, _ := btcec.ParsePubKey(target[:])
11631167

11641168
select {
1165-
case peerChan <- &mockPeer{pk, sentMsgs, ctx.gossiper.quit}:
1169+
case peerChan <- &mockPeer{
1170+
pk, sentMsgs, ctx.gossiper.quit, atomic.Bool{},
1171+
}:
11661172
case <-ctx.gossiper.quit:
11671173
}
11681174
}
@@ -1172,7 +1178,9 @@ func TestOrphanSignatureAnnouncement(t *testing.T) {
11721178

11731179
remoteKey, err := btcec.ParsePubKey(batch.nodeAnn2.NodeID[:])
11741180
require.NoError(t, err, "unable to parse pubkey")
1175-
remotePeer := &mockPeer{remoteKey, sentMsgs, ctx.gossiper.quit}
1181+
remotePeer := &mockPeer{
1182+
remoteKey, sentMsgs, ctx.gossiper.quit, atomic.Bool{},
1183+
}
11761184

11771185
// Pretending that we receive local channel announcement from funding
11781186
// manager, thereby kick off the announcement exchange process, in
@@ -1344,7 +1352,9 @@ func TestSignatureAnnouncementRetryAtStartup(t *testing.T) {
13441352

13451353
// Set up a channel to intercept the messages sent to the remote peer.
13461354
sentToPeer := make(chan lnwire.Message, 1)
1347-
remotePeer := &mockPeer{remoteKey, sentToPeer, ctx.gossiper.quit}
1355+
remotePeer := &mockPeer{
1356+
remoteKey, sentToPeer, ctx.gossiper.quit, atomic.Bool{},
1357+
}
13481358

13491359
// Since the reliable send to the remote peer of the local channel proof
13501360
// requires a notification when the peer comes online, we'll capture the
@@ -1578,7 +1588,9 @@ func TestSignatureAnnouncementFullProofWhenRemoteProof(t *testing.T) {
15781588
// Set up a channel we can use to inspect messages sent by the
15791589
// gossiper to the remote peer.
15801590
sentToPeer := make(chan lnwire.Message, 1)
1581-
remotePeer := &mockPeer{remoteKey, sentToPeer, ctx.gossiper.quit}
1591+
remotePeer := &mockPeer{
1592+
remoteKey, sentToPeer, ctx.gossiper.quit, atomic.Bool{},
1593+
}
15821594

15831595
// Override NotifyWhenOnline to return the remote peer which we expect
15841596
// meesages to be sent to.
@@ -1772,7 +1784,7 @@ func TestDeDuplicatedAnnouncements(t *testing.T) {
17721784
ca, err := createRemoteChannelAnnouncement(0)
17731785
require.NoError(t, err, "can't create remote channel announcement")
17741786

1775-
nodePeer := &mockPeer{bitcoinKeyPub2, nil, nil}
1787+
nodePeer := &mockPeer{bitcoinKeyPub2, nil, nil, atomic.Bool{}}
17761788
announcements.AddMsgs(networkMsg{
17771789
msg: ca,
17781790
peer: nodePeer,
@@ -2058,7 +2070,7 @@ func TestForwardPrivateNodeAnnouncement(t *testing.T) {
20582070
// process it.
20592071
remoteChanAnn, err := createRemoteChannelAnnouncement(startingHeight - 1)
20602072
require.NoError(t, err, "unable to create remote channel announcement")
2061-
peer := &mockPeer{pubKey, nil, nil}
2073+
peer := &mockPeer{pubKey, nil, nil, atomic.Bool{}}
20622074

20632075
select {
20642076
case err := <-ctx.gossiper.ProcessRemoteAnnouncement(remoteChanAnn, peer):
@@ -2373,7 +2385,9 @@ func TestReceiveRemoteChannelUpdateFirst(t *testing.T) {
23732385
// Set up a channel that we can use to inspect the messages sent
23742386
// directly from the gossiper.
23752387
sentMsgs := make(chan lnwire.Message, 10)
2376-
remotePeer := &mockPeer{remoteKey, sentMsgs, ctx.gossiper.quit}
2388+
remotePeer := &mockPeer{
2389+
remoteKey, sentMsgs, ctx.gossiper.quit, atomic.Bool{},
2390+
}
23772391

23782392
// Override NotifyWhenOnline to return the remote peer which we expect
23792393
// messages to be sent to.
@@ -2561,7 +2575,9 @@ func TestExtraDataChannelAnnouncementValidation(t *testing.T) {
25612575
ctx, err := createTestCtx(t, 0)
25622576
require.NoError(t, err, "can't create context")
25632577

2564-
remotePeer := &mockPeer{remoteKeyPriv1.PubKey(), nil, nil}
2578+
remotePeer := &mockPeer{
2579+
remoteKeyPriv1.PubKey(), nil, nil, atomic.Bool{},
2580+
}
25652581

25662582
// We'll now create an announcement that contains an extra set of bytes
25672583
// that we don't know of ourselves, but should still include in the
@@ -2592,7 +2608,9 @@ func TestExtraDataChannelUpdateValidation(t *testing.T) {
25922608
ctx, err := createTestCtx(t, 0)
25932609
require.NoError(t, err, "can't create context")
25942610

2595-
remotePeer := &mockPeer{remoteKeyPriv1.PubKey(), nil, nil}
2611+
remotePeer := &mockPeer{
2612+
remoteKeyPriv1.PubKey(), nil, nil, atomic.Bool{},
2613+
}
25962614

25972615
// In this scenario, we'll create two announcements, one regular
25982616
// channel announcement, and another channel update announcement, that
@@ -2643,7 +2661,9 @@ func TestExtraDataNodeAnnouncementValidation(t *testing.T) {
26432661
ctx, err := createTestCtx(t, 0)
26442662
require.NoError(t, err, "can't create context")
26452663

2646-
remotePeer := &mockPeer{remoteKeyPriv1.PubKey(), nil, nil}
2664+
remotePeer := &mockPeer{
2665+
remoteKeyPriv1.PubKey(), nil, nil, atomic.Bool{},
2666+
}
26472667
timestamp := testTimestamp
26482668

26492669
// We'll create a node announcement that includes a set of opaque data
@@ -2716,7 +2736,7 @@ func TestRetransmit(t *testing.T) {
27162736

27172737
remoteKey, err := btcec.ParsePubKey(batch.nodeAnn2.NodeID[:])
27182738
require.NoError(t, err, "unable to parse pubkey")
2719-
remotePeer := &mockPeer{remoteKey, nil, nil}
2739+
remotePeer := &mockPeer{remoteKey, nil, nil, atomic.Bool{}}
27202740

27212741
// Process a local channel announcement, channel update and node
27222742
// announcement. No messages should be broadcasted yet, since no proof
@@ -2822,7 +2842,7 @@ func TestNodeAnnouncementNoChannels(t *testing.T) {
28222842

28232843
remoteKey, err := btcec.ParsePubKey(batch.nodeAnn2.NodeID[:])
28242844
require.NoError(t, err, "unable to parse pubkey")
2825-
remotePeer := &mockPeer{remoteKey, nil, nil}
2845+
remotePeer := &mockPeer{remoteKey, nil, nil, atomic.Bool{}}
28262846

28272847
// Process the remote node announcement.
28282848
select {
@@ -2906,7 +2926,7 @@ func TestOptionalFieldsChannelUpdateValidation(t *testing.T) {
29062926

29072927
chanUpdateHeight := uint32(0)
29082928
timestamp := uint32(123456)
2909-
nodePeer := &mockPeer{remoteKeyPriv1.PubKey(), nil, nil}
2929+
nodePeer := &mockPeer{remoteKeyPriv1.PubKey(), nil, nil, atomic.Bool{}}
29102930

29112931
// In this scenario, we'll test whether the message flags field in a
29122932
// channel update is properly handled.
@@ -3013,7 +3033,9 @@ func TestSendChannelUpdateReliably(t *testing.T) {
30133033
// Set up a channel we can use to inspect messages sent by the
30143034
// gossiper to the remote peer.
30153035
sentToPeer := make(chan lnwire.Message, 1)
3016-
remotePeer := &mockPeer{remoteKey, sentToPeer, ctx.gossiper.quit}
3036+
remotePeer := &mockPeer{
3037+
remoteKey, sentToPeer, ctx.gossiper.quit, atomic.Bool{},
3038+
}
30173039

30183040
// Since we first wait to be notified of the peer before attempting to
30193041
// send the message, we'll overwrite NotifyWhenOnline and
@@ -3367,7 +3389,9 @@ func TestPropagateChanPolicyUpdate(t *testing.T) {
33673389
remoteKey := remoteKeyPriv1.PubKey()
33683390

33693391
sentMsgs := make(chan lnwire.Message, 10)
3370-
remotePeer := &mockPeer{remoteKey, sentMsgs, ctx.gossiper.quit}
3392+
remotePeer := &mockPeer{
3393+
remoteKey, sentMsgs, ctx.gossiper.quit, atomic.Bool{},
3394+
}
33713395

33723396
// The forced code path for sending the private ChannelUpdate to the
33733397
// remote peer will be hit, forcing it to request a notification that
@@ -3715,7 +3739,9 @@ func TestBroadcastAnnsAfterGraphSynced(t *testing.T) {
37153739

37163740
t.Helper()
37173741

3718-
nodePeer := &mockPeer{remoteKeyPriv1.PubKey(), nil, nil}
3742+
nodePeer := &mockPeer{
3743+
remoteKeyPriv1.PubKey(), nil, nil, atomic.Bool{},
3744+
}
37193745
var errChan chan error
37203746
if isRemote {
37213747
errChan = ctx.gossiper.ProcessRemoteAnnouncement(
@@ -3791,7 +3817,9 @@ func TestRateLimitChannelUpdates(t *testing.T) {
37913817
batch, err := createRemoteAnnouncements(blockHeight)
37923818
require.NoError(t, err)
37933819

3794-
nodePeer1 := &mockPeer{remoteKeyPriv1.PubKey(), nil, nil}
3820+
nodePeer1 := &mockPeer{
3821+
remoteKeyPriv1.PubKey(), nil, nil, atomic.Bool{},
3822+
}
37953823
select {
37963824
case err := <-ctx.gossiper.ProcessRemoteAnnouncement(
37973825
batch.chanAnn, nodePeer1,
@@ -3810,7 +3838,9 @@ func TestRateLimitChannelUpdates(t *testing.T) {
38103838
t.Fatal("remote announcement not processed")
38113839
}
38123840

3813-
nodePeer2 := &mockPeer{remoteKeyPriv2.PubKey(), nil, nil}
3841+
nodePeer2 := &mockPeer{
3842+
remoteKeyPriv2.PubKey(), nil, nil, atomic.Bool{},
3843+
}
38143844
select {
38153845
case err := <-ctx.gossiper.ProcessRemoteAnnouncement(
38163846
batch.chanUpdAnn2, nodePeer2,
@@ -3929,7 +3959,7 @@ func TestIgnoreOwnAnnouncement(t *testing.T) {
39293959

39303960
remoteKey, err := btcec.ParsePubKey(batch.nodeAnn2.NodeID[:])
39313961
require.NoError(t, err, "unable to parse pubkey")
3932-
remotePeer := &mockPeer{remoteKey, nil, nil}
3962+
remotePeer := &mockPeer{remoteKey, nil, nil, atomic.Bool{}}
39333963

39343964
// Try to let the remote peer tell us about the channel we are part of.
39353965
select {
@@ -4075,7 +4105,7 @@ func TestRejectCacheChannelAnn(t *testing.T) {
40754105

40764106
remoteKey, err := btcec.ParsePubKey(batch.nodeAnn2.NodeID[:])
40774107
require.NoError(t, err, "unable to parse pubkey")
4078-
remotePeer := &mockPeer{remoteKey, nil, nil}
4108+
remotePeer := &mockPeer{remoteKey, nil, nil, atomic.Bool{}}
40794109

40804110
// Before sending over the announcement, we'll modify it such that we
40814111
// know it will always fail.

discovery/mock_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"net"
66
"sync"
7+
"sync/atomic"
78

89
"github.com/btcsuite/btcd/btcec/v2"
910
"github.com/btcsuite/btcd/wire"
@@ -14,9 +15,10 @@ import (
1415
// mockPeer implements the lnpeer.Peer interface and is used to test the
1516
// gossiper's interaction with peers.
1617
type mockPeer struct {
17-
pk *btcec.PublicKey
18-
sentMsgs chan lnwire.Message
19-
quit chan struct{}
18+
pk *btcec.PublicKey
19+
sentMsgs chan lnwire.Message
20+
quit chan struct{}
21+
disconnected atomic.Bool
2022
}
2123

2224
var _ lnpeer.Peer = (*mockPeer)(nil)
@@ -74,6 +76,10 @@ func (p *mockPeer) RemovePendingChannel(_ lnwire.ChannelID) error {
7476
return nil
7577
}
7678

79+
func (p *mockPeer) Disconnect(err error) {
80+
p.disconnected.Store(true)
81+
}
82+
7783
// mockMessageStore is an in-memory implementation of the MessageStore interface
7884
// used for the gossiper's unit tests.
7985
type mockMessageStore struct {

discovery/reliable_sender_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package discovery
22

33
import (
44
"fmt"
5+
"sync/atomic"
56
"testing"
67
"time"
78

@@ -74,7 +75,7 @@ func TestReliableSenderFlow(t *testing.T) {
7475
// Create a mock peer to send the messages to.
7576
pubKey := randPubKey(t)
7677
msgsSent := make(chan lnwire.Message)
77-
peer := &mockPeer{pubKey, msgsSent, reliableSender.quit}
78+
peer := &mockPeer{pubKey, msgsSent, reliableSender.quit, atomic.Bool{}}
7879

7980
// Override NotifyWhenOnline and NotifyWhenOffline to provide the
8081
// notification channels so that we can control when notifications get
@@ -193,7 +194,7 @@ func TestReliableSenderStaleMessages(t *testing.T) {
193194
// Create a mock peer to send the messages to.
194195
pubKey := randPubKey(t)
195196
msgsSent := make(chan lnwire.Message)
196-
peer := &mockPeer{pubKey, msgsSent, reliableSender.quit}
197+
peer := &mockPeer{pubKey, msgsSent, reliableSender.quit, atomic.Bool{}}
197198

198199
// Override NotifyWhenOnline to provide the notification channel so that
199200
// we can control when notifications get dispatched.

funding/manager_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ type testNode struct {
283283

284284
var _ lnpeer.Peer = (*testNode)(nil)
285285

286+
func (n *testNode) Disconnect(err error) {}
287+
286288
func (n *testNode) IdentityKey() *btcec.PublicKey {
287289
return n.addr.IdentityKey
288290
}

htlcswitch/link_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,6 +2072,8 @@ func (m *mockPeer) QuitSignal() <-chan struct{} {
20722072
return m.quit
20732073
}
20742074

2075+
func (m *mockPeer) Disconnect(err error) {}
2076+
20752077
var _ lnpeer.Peer = (*mockPeer)(nil)
20762078

20772079
func (m *mockPeer) SendMessage(sync bool, msgs ...lnwire.Message) error {

htlcswitch/mock.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,8 @@ func (s *mockServer) RemoteFeatures() *lnwire.FeatureVector {
684684
return nil
685685
}
686686

687+
func (s *mockServer) Disconnect(err error) {}
688+
687689
func (s *mockServer) Stop() error {
688690
if !atomic.CompareAndSwapInt32(&s.shutdown, 0, 1) {
689691
return nil

lnpeer/mock_peer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,5 @@ func (m *MockPeer) RemoteFeatures() *lnwire.FeatureVector {
7979
args := m.Called()
8080
return args.Get(0).(*lnwire.FeatureVector)
8181
}
82+
83+
func (m *MockPeer) Disconnect(err error) {}

lnpeer/peer.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,7 @@ type Peer interface {
7474
// by the remote peer. This allows sub-systems that use this interface
7575
// to gate their behavior off the set of negotiated feature bits.
7676
RemoteFeatures() *lnwire.FeatureVector
77+
78+
// Disconnect halts communication with the peer.
79+
Disconnect(error)
7780
}

0 commit comments

Comments
 (0)