Skip to content

Commit 3761912

Browse files
committed
htlcswitch: avoid leaking peer interface from link
Here we notice that the only use of the Peer call on the link is to find out what the peer's pubkey is. To avoid leaking handles to IO actions outside the interface we reduce the surface area to just return the peer's public key.
1 parent be69b02 commit 3761912

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

htlcswitch/interfaces.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/lightningnetwork/lnd/channeldb"
88
"github.com/lightningnetwork/lnd/channeldb/models"
99
"github.com/lightningnetwork/lnd/invoices"
10-
"github.com/lightningnetwork/lnd/lnpeer"
1110
"github.com/lightningnetwork/lnd/lntypes"
1211
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
1312
"github.com/lightningnetwork/lnd/lnwire"
@@ -262,9 +261,9 @@ type ChannelLink interface {
262261
// total sent/received milli-satoshis.
263262
Stats() (uint64, lnwire.MilliSatoshi, lnwire.MilliSatoshi)
264263

265-
// Peer returns the representation of remote peer with which we have
266-
// the channel link opened.
267-
Peer() lnpeer.Peer
264+
// Peer returns the serialized public key of remote peer with which we
265+
// have the channel link opened.
266+
PeerPubKey() [33]byte
268267

269268
// AttachMailBox delivers an active MailBox to the link. The MailBox may
270269
// have buffered messages.

htlcswitch/link.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,8 +2552,8 @@ func (l *channelLink) updateCommitTx() error {
25522552
// channel link opened.
25532553
//
25542554
// NOTE: Part of the ChannelLink interface.
2555-
func (l *channelLink) Peer() lnpeer.Peer {
2556-
return l.cfg.Peer
2555+
func (l *channelLink) PeerPubKey() [33]byte {
2556+
return l.cfg.Peer.PubKey()
25572557
}
25582558

25592559
// ChannelPoint returns the channel outpoint for the channel link.

htlcswitch/mock.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -891,10 +891,22 @@ func (f *mockChannelLink) Start() error {
891891
return nil
892892
}
893893

894-
func (f *mockChannelLink) ChanID() lnwire.ChannelID { return f.chanID }
895-
func (f *mockChannelLink) ShortChanID() lnwire.ShortChannelID { return f.shortChanID }
896-
func (f *mockChannelLink) Bandwidth() lnwire.MilliSatoshi { return 99999999 }
897-
func (f *mockChannelLink) Peer() lnpeer.Peer { return f.peer }
894+
func (f *mockChannelLink) ChanID() lnwire.ChannelID {
895+
return f.chanID
896+
}
897+
898+
func (f *mockChannelLink) ShortChanID() lnwire.ShortChannelID {
899+
return f.shortChanID
900+
}
901+
902+
func (f *mockChannelLink) Bandwidth() lnwire.MilliSatoshi {
903+
return 99999999
904+
}
905+
906+
func (f *mockChannelLink) PeerPubKey() [33]byte {
907+
return f.peer.PubKey()
908+
}
909+
898910
func (f *mockChannelLink) ChannelPoint() *wire.OutPoint { return &wire.OutPoint{} }
899911
func (f *mockChannelLink) Stop() {}
900912
func (f *mockChannelLink) EligibleToForward() bool { return f.eligible }

htlcswitch/switch.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ func (s *Switch) handlePacketForward(packet *htlcPacket) error {
11471147

11481148
return s.failAddPacket(packet, linkError)
11491149
}
1150-
targetPeerKey := targetLink.Peer().PubKey()
1150+
targetPeerKey := targetLink.PeerPubKey()
11511151
interfaceLinks, _ := s.getLinks(targetPeerKey)
11521152
s.indexMtx.RUnlock()
11531153

@@ -1810,9 +1810,9 @@ out:
18101810
}
18111811
s.indexMtx.RUnlock()
18121812

1813-
peerPub := link.Peer().PubKey()
1813+
peerPub := link.PeerPubKey()
18141814
log.Debugf("Requesting local channel close: peer=%v, "+
1815-
"chan_id=%x", link.Peer(), chanID[:])
1815+
"chan_id=%x", link.PeerPubKey(), chanID[:])
18161816

18171817
go s.cfg.LocalChannelClose(peerPub[:], req)
18181818

@@ -2335,7 +2335,7 @@ func (s *Switch) addLiveLink(link ChannelLink) {
23352335

23362336
// Next we'll add the link to the interface index so we can
23372337
// quickly look up all the channels for a particular node.
2338-
peerPub := link.Peer().PubKey()
2338+
peerPub := link.PeerPubKey()
23392339
if _, ok := s.interfaceIndex[peerPub]; !ok {
23402340
s.interfaceIndex[peerPub] = make(map[lnwire.ChannelID]ChannelLink)
23412341
}
@@ -2610,7 +2610,7 @@ func (s *Switch) removeLink(chanID lnwire.ChannelID) ChannelLink {
26102610

26112611
// If the link has been added to the peer index, then we'll move to
26122612
// delete the entry within the index.
2613-
peerPub := link.Peer().PubKey()
2613+
peerPub := link.PeerPubKey()
26142614
if peerIndex, ok := s.interfaceIndex[peerPub]; ok {
26152615
delete(peerIndex, link.ChanID())
26162616

0 commit comments

Comments
 (0)